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