xref: /AOO41X/main/forms/qa/complex/forms/CheckOGroupBoxModel.java (revision 67e470dafe1997e73f56ff7ff4878983707e3e07)
1*b9b79128SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b9b79128SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b9b79128SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b9b79128SAndrew Rist  * distributed with this work for additional information
6*b9b79128SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b9b79128SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b9b79128SAndrew Rist  * "License"); you may not use this file except in compliance
9*b9b79128SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*b9b79128SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*b9b79128SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b9b79128SAndrew Rist  * software distributed under the License is distributed on an
15*b9b79128SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b9b79128SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b9b79128SAndrew Rist  * specific language governing permissions and limitations
18*b9b79128SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*b9b79128SAndrew Rist  *************************************************************/
21*b9b79128SAndrew Rist 
22*b9b79128SAndrew Rist 
23cdf0e10cSrcweir package complex.forms;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.Property;
26cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
27cdf0e10cSrcweir import com.sun.star.beans.PropertyChangeEvent;
28cdf0e10cSrcweir import com.sun.star.beans.XMultiPropertySet;
29cdf0e10cSrcweir import com.sun.star.beans.XPropertiesChangeListener;
30cdf0e10cSrcweir import com.sun.star.lang.EventObject;
31cdf0e10cSrcweir import com.sun.star.drawing.XControlShape;
32cdf0e10cSrcweir import com.sun.star.lang.XComponent;
33cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
35cdf0e10cSrcweir // import complexlib.ComplexTestCase;
36cdf0e10cSrcweir import com.sun.star.util.CloseVetoException;
37cdf0e10cSrcweir import com.sun.star.util.XCloseable;
38cdf0e10cSrcweir import java.util.Vector;
39cdf0e10cSrcweir import java.util.logging.Level;
40cdf0e10cSrcweir import java.util.logging.Logger;
41cdf0e10cSrcweir import util.FormTools;
42cdf0e10cSrcweir import util.SOfficeFactory;
43cdf0e10cSrcweir import util.ValueChanger;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir import org.junit.After;
46cdf0e10cSrcweir import org.junit.AfterClass;
47cdf0e10cSrcweir import org.junit.Before;
48cdf0e10cSrcweir import org.junit.BeforeClass;
49cdf0e10cSrcweir import org.junit.Test;
50cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
51cdf0e10cSrcweir import static org.junit.Assert.*;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir /**
54cdf0e10cSrcweir  */
55cdf0e10cSrcweir public class CheckOGroupBoxModel
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     private XMultiPropertySet m_xPropSet;
59cdf0e10cSrcweir     private XComponent m_xDrawDoc;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir //    public String[] getTestMethodNames() {
62cdf0e10cSrcweir //        return new String[] {"setPropertyValues"};
63cdf0e10cSrcweir //    }
before()64cdf0e10cSrcweir     @Before public void before()
65cdf0e10cSrcweir     {
66cdf0e10cSrcweir         // XComponent xDrawDoc = null;
67cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(getMSF());
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         try
70cdf0e10cSrcweir         {
71cdf0e10cSrcweir             System.out.println("creating a draw document");
72cdf0e10cSrcweir             m_xDrawDoc = SOF.createDrawDoc(null);
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e)
75cdf0e10cSrcweir         {
76cdf0e10cSrcweir             fail("Couldn't create document.");
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         String objName = "GroupBox";
80cdf0e10cSrcweir         XControlShape shape = FormTools.insertControlShape(m_xDrawDoc, 5000, 7000, 2000, 2000, objName);
81cdf0e10cSrcweir         m_xPropSet = UnoRuntime.queryInterface(XMultiPropertySet.class, shape.getControl());
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
after()84cdf0e10cSrcweir     @After public void after()
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, m_xDrawDoc);
87cdf0e10cSrcweir         if (xClose != null)
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir             try
90cdf0e10cSrcweir             {
91cdf0e10cSrcweir                 xClose.close(true);
92cdf0e10cSrcweir             }
93cdf0e10cSrcweir             catch (CloseVetoException ex)
94cdf0e10cSrcweir             {
95cdf0e10cSrcweir                 fail("Can't close document. Exception caught: " + ex.getMessage());
96cdf0e10cSrcweir                 /* ignore! */
97cdf0e10cSrcweir             }
98cdf0e10cSrcweir         }
99cdf0e10cSrcweir     }
setPropertyValues()100cdf0e10cSrcweir     @Test public void setPropertyValues()
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         String[] boundPropsToTest = getBoundPropsToTest();
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         MyChangeListener ml = new MyChangeListener();
105cdf0e10cSrcweir         m_xPropSet.addPropertiesChangeListener(boundPropsToTest, ml);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         Object[] gValues = m_xPropSet.getPropertyValues(boundPropsToTest);
108cdf0e10cSrcweir         Object[] newValue = new Object[gValues.length];
109cdf0e10cSrcweir         System.out.println("Trying to change all properties.");
110cdf0e10cSrcweir         for (int i = 0; i < boundPropsToTest.length; i++)
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             newValue[i] = ValueChanger.changePValue(gValues[i]);
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         try
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             m_xPropSet.setPropertyValues(boundPropsToTest, newValue);
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir         catch (com.sun.star.beans.PropertyVetoException e)
119cdf0e10cSrcweir         {
120cdf0e10cSrcweir             fail("Exception occured while trying to change the properties.");
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir         catch (com.sun.star.lang.IllegalArgumentException e)
123cdf0e10cSrcweir         {
124cdf0e10cSrcweir             fail("Exception occured while trying to change the properties.");
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir         catch (com.sun.star.lang.WrappedTargetException e)
127cdf0e10cSrcweir         {
128cdf0e10cSrcweir             fail("Exception occured while trying to change the properties.");
129cdf0e10cSrcweir         } // end of try-catch
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         assertTrue("Listener was not called.", ml.wasListenerCalled());
132cdf0e10cSrcweir         m_xPropSet.removePropertiesChangeListener(ml);
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir 
getBoundPropsToTest()135cdf0e10cSrcweir     private String[] getBoundPropsToTest()
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         Property[] properties = m_xPropSet.getPropertySetInfo().getProperties();
138cdf0e10cSrcweir         String[] testPropsNames = null;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         Vector<String> tNames = new Vector<String>();
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         for (int i = 0; i < properties.length; i++)
143cdf0e10cSrcweir         {
144cdf0e10cSrcweir 
145cdf0e10cSrcweir             Property property = properties[i];
146cdf0e10cSrcweir             String name = property.Name;
147cdf0e10cSrcweir             boolean isWritable = ((property.Attributes
148cdf0e10cSrcweir                     & PropertyAttribute.READONLY) == 0);
149cdf0e10cSrcweir             boolean isNotNull = ((property.Attributes
150cdf0e10cSrcweir                     & PropertyAttribute.MAYBEVOID) == 0);
151cdf0e10cSrcweir             boolean isBound = ((property.Attributes
152cdf0e10cSrcweir                     & PropertyAttribute.BOUND) != 0);
153cdf0e10cSrcweir 
154cdf0e10cSrcweir             if (isWritable && isNotNull && isBound)
155cdf0e10cSrcweir             {
156cdf0e10cSrcweir                 tNames.add(name);
157cdf0e10cSrcweir             }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         } // endfor
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         //get a array of bound properties
162cdf0e10cSrcweir         testPropsNames = new String[tNames.size()];
163cdf0e10cSrcweir         testPropsNames = tNames.toArray(testPropsNames);
164cdf0e10cSrcweir         return testPropsNames;
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     /**
168cdf0e10cSrcweir      * Listener implementation which sets a flag when
169cdf0e10cSrcweir      * listener was called.
170cdf0e10cSrcweir      */
171cdf0e10cSrcweir     public class MyChangeListener implements XPropertiesChangeListener
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         boolean propertiesChanged = false;
175cdf0e10cSrcweir 
propertiesChange(PropertyChangeEvent[] e)176cdf0e10cSrcweir         public void propertiesChange(PropertyChangeEvent[] e)
177cdf0e10cSrcweir         {
178cdf0e10cSrcweir             propertiesChanged = true;
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir 
disposing(EventObject obj)181cdf0e10cSrcweir         public void disposing(EventObject obj)
182cdf0e10cSrcweir         {
183cdf0e10cSrcweir         }
184cdf0e10cSrcweir 
wasListenerCalled()185cdf0e10cSrcweir         public boolean wasListenerCalled()
186cdf0e10cSrcweir         {
187cdf0e10cSrcweir             return propertiesChanged;
188cdf0e10cSrcweir         }
189cdf0e10cSrcweir 
reset()190cdf0e10cSrcweir         public void reset()
191cdf0e10cSrcweir         {
192cdf0e10cSrcweir             propertiesChanged = false;
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir     };
195cdf0e10cSrcweir 
getMSF()196cdf0e10cSrcweir     private XMultiServiceFactory getMSF()
197cdf0e10cSrcweir     {
198cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
199cdf0e10cSrcweir         return xMSF1;
200cdf0e10cSrcweir     }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     // setup and close connections
203cdf0e10cSrcweir     @BeforeClass
setUpConnection()204cdf0e10cSrcweir     public static void setUpConnection() throws Exception
205cdf0e10cSrcweir     {
206cdf0e10cSrcweir         System.out.println("setUpConnection()");
207cdf0e10cSrcweir         connection.setUp();
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     @AfterClass
tearDownConnection()211cdf0e10cSrcweir     public static void tearDownConnection()
212cdf0e10cSrcweir             throws InterruptedException, com.sun.star.uno.Exception
213cdf0e10cSrcweir     {
214cdf0e10cSrcweir         System.out.println("tearDownConnection()");
215cdf0e10cSrcweir         connection.tearDown();
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
218cdf0e10cSrcweir }
219