1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package complex.forms;
28 
29 import com.sun.star.beans.Property;
30 import com.sun.star.beans.PropertyAttribute;
31 import com.sun.star.beans.PropertyChangeEvent;
32 import com.sun.star.beans.XMultiPropertySet;
33 import com.sun.star.beans.XPropertiesChangeListener;
34 import com.sun.star.lang.EventObject;
35 import com.sun.star.drawing.XControlShape;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.uno.UnoRuntime;
39 // import complexlib.ComplexTestCase;
40 import com.sun.star.util.CloseVetoException;
41 import com.sun.star.util.XCloseable;
42 import java.util.Vector;
43 import java.util.logging.Level;
44 import java.util.logging.Logger;
45 import util.FormTools;
46 import util.SOfficeFactory;
47 import util.ValueChanger;
48 
49 import org.junit.After;
50 import org.junit.AfterClass;
51 import org.junit.Before;
52 import org.junit.BeforeClass;
53 import org.junit.Test;
54 import org.openoffice.test.OfficeConnection;
55 import static org.junit.Assert.*;
56 
57 /**
58  */
59 public class CheckOGroupBoxModel
60 {
61 
62     private XMultiPropertySet m_xPropSet;
63     private XComponent m_xDrawDoc;
64 
65 //    public String[] getTestMethodNames() {
66 //        return new String[] {"setPropertyValues"};
67 //    }
68     @Before public void before()
69     {
70         // XComponent xDrawDoc = null;
71         SOfficeFactory SOF = SOfficeFactory.getFactory(getMSF());
72 
73         try
74         {
75             System.out.println("creating a draw document");
76             m_xDrawDoc = SOF.createDrawDoc(null);
77         }
78         catch (com.sun.star.uno.Exception e)
79         {
80             fail("Couldn't create document.");
81         }
82 
83         String objName = "GroupBox";
84         XControlShape shape = FormTools.insertControlShape(m_xDrawDoc, 5000, 7000, 2000, 2000, objName);
85         m_xPropSet = UnoRuntime.queryInterface(XMultiPropertySet.class, shape.getControl());
86     }
87 
88     @After public void after()
89     {
90         XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, m_xDrawDoc);
91         if (xClose != null)
92         {
93             try
94             {
95                 xClose.close(true);
96             }
97             catch (CloseVetoException ex)
98             {
99                 fail("Can't close document. Exception caught: " + ex.getMessage());
100                 /* ignore! */
101             }
102         }
103     }
104     @Test public void setPropertyValues()
105     {
106         String[] boundPropsToTest = getBoundPropsToTest();
107 
108         MyChangeListener ml = new MyChangeListener();
109         m_xPropSet.addPropertiesChangeListener(boundPropsToTest, ml);
110 
111         Object[] gValues = m_xPropSet.getPropertyValues(boundPropsToTest);
112         Object[] newValue = new Object[gValues.length];
113         System.out.println("Trying to change all properties.");
114         for (int i = 0; i < boundPropsToTest.length; i++)
115         {
116             newValue[i] = ValueChanger.changePValue(gValues[i]);
117         }
118         try
119         {
120             m_xPropSet.setPropertyValues(boundPropsToTest, newValue);
121         }
122         catch (com.sun.star.beans.PropertyVetoException e)
123         {
124             fail("Exception occured while trying to change the properties.");
125         }
126         catch (com.sun.star.lang.IllegalArgumentException e)
127         {
128             fail("Exception occured while trying to change the properties.");
129         }
130         catch (com.sun.star.lang.WrappedTargetException e)
131         {
132             fail("Exception occured while trying to change the properties.");
133         } // end of try-catch
134 
135         assertTrue("Listener was not called.", ml.wasListenerCalled());
136         m_xPropSet.removePropertiesChangeListener(ml);
137     }
138 
139     private String[] getBoundPropsToTest()
140     {
141         Property[] properties = m_xPropSet.getPropertySetInfo().getProperties();
142         String[] testPropsNames = null;
143 
144         Vector<String> tNames = new Vector<String>();
145 
146         for (int i = 0; i < properties.length; i++)
147         {
148 
149             Property property = properties[i];
150             String name = property.Name;
151             boolean isWritable = ((property.Attributes
152                     & PropertyAttribute.READONLY) == 0);
153             boolean isNotNull = ((property.Attributes
154                     & PropertyAttribute.MAYBEVOID) == 0);
155             boolean isBound = ((property.Attributes
156                     & PropertyAttribute.BOUND) != 0);
157 
158             if (isWritable && isNotNull && isBound)
159             {
160                 tNames.add(name);
161             }
162 
163         } // endfor
164 
165         //get a array of bound properties
166         testPropsNames = new String[tNames.size()];
167         testPropsNames = tNames.toArray(testPropsNames);
168         return testPropsNames;
169     }
170 
171     /**
172      * Listener implementation which sets a flag when
173      * listener was called.
174      */
175     public class MyChangeListener implements XPropertiesChangeListener
176     {
177 
178         boolean propertiesChanged = false;
179 
180         public void propertiesChange(PropertyChangeEvent[] e)
181         {
182             propertiesChanged = true;
183         }
184 
185         public void disposing(EventObject obj)
186         {
187         }
188 
189         public boolean wasListenerCalled()
190         {
191             return propertiesChanged;
192         }
193 
194         public void reset()
195         {
196             propertiesChanged = false;
197         }
198     };
199 
200     private XMultiServiceFactory getMSF()
201     {
202         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
203         return xMSF1;
204     }
205 
206     // setup and close connections
207     @BeforeClass
208     public static void setUpConnection() throws Exception
209     {
210         System.out.println("setUpConnection()");
211         connection.setUp();
212     }
213 
214     @AfterClass
215     public static void tearDownConnection()
216             throws InterruptedException, com.sun.star.uno.Exception
217     {
218         System.out.println("tearDownConnection()");
219         connection.tearDown();
220     }
221     private static final OfficeConnection connection = new OfficeConnection();
222 }
223