1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.beans;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir import java.util.HashSet;
28cdf0e10cSrcweir import java.util.Set;
29cdf0e10cSrcweir import java.util.StringTokenizer;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir import lib.MultiMethodTest;
32cdf0e10cSrcweir import lib.Status;
33cdf0e10cSrcweir import util.ValueChanger;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.beans.Property;
36cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
37cdf0e10cSrcweir import com.sun.star.beans.PropertyChangeEvent;
38cdf0e10cSrcweir import com.sun.star.beans.XMultiPropertySet;
39cdf0e10cSrcweir import com.sun.star.beans.XPropertiesChangeListener;
40cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
41cdf0e10cSrcweir import com.sun.star.lang.EventObject;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 
44cdf0e10cSrcweir /**
45cdf0e10cSrcweir * Testing <code>com.sun.star.beans.XMultiPropertySet</code>
46cdf0e10cSrcweir * interface methods :
47cdf0e10cSrcweir * <ul>
48cdf0e10cSrcweir *  <li><code> getPropertySetInfo()</code></li>
49cdf0e10cSrcweir *  <li><code> setPropertyValues()</code></li>
50cdf0e10cSrcweir *  <li><code> getPropertyValues()</code></li>
51cdf0e10cSrcweir *  <li><code> addPropertiesChangeListener()</code></li>
52cdf0e10cSrcweir *  <li><code> removePropertiesChangeListener()</code></li>
53cdf0e10cSrcweir *  <li><code> firePropertiesChangeEvent()</code></li>
54cdf0e10cSrcweir * </ul> <p>
55cdf0e10cSrcweir *
56cdf0e10cSrcweir * Required relations :
57cdf0e10cSrcweir * <ul>
58cdf0e10cSrcweir *  <li> <code>'XMultiPropertySet.ExcludeProps'</code>
59cdf0e10cSrcweir *    <b>(optional) </b> : java.util.Set.
60cdf0e10cSrcweir *    Has property names which must be skipped from testing in
61cdf0e10cSrcweir *    some reasons (for example property accepts restricted set
62cdf0e10cSrcweir *    of values).
63cdf0e10cSrcweir *  </li>
64cdf0e10cSrcweir * <ul> <p>
65cdf0e10cSrcweir *
66cdf0e10cSrcweir * Test is <b> NOT </b> multithread compilant. <p>
67cdf0e10cSrcweir * After test completion object environment has to be recreated.
68cdf0e10cSrcweir * @see com.sun.star.beans.XMultiPropertySet
69cdf0e10cSrcweir */
70cdf0e10cSrcweir public class _XMultiPropertySet extends MultiMethodTest {
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     public XMultiPropertySet oObj = null;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     private boolean propertiesChanged = false;
75cdf0e10cSrcweir     private XPropertySetInfo propertySetInfo = null;
76cdf0e10cSrcweir     private String [] testPropsNames = null;
77cdf0e10cSrcweir     private int testPropsAmount = 0;
78cdf0e10cSrcweir     private PrintWriter _log = null;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     private Object[] values = null;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     private Set exclProps = null;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     /**
85cdf0e10cSrcweir     * Initializes some fields.
86cdf0e10cSrcweir     */
87cdf0e10cSrcweir     public void before() {
88cdf0e10cSrcweir         _log = log;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         exclProps = (Set) tEnv.getObjRelation("XMultiPropertySet.ExcludeProps");
91cdf0e10cSrcweir         if (exclProps == null) exclProps = new HashSet(0);
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     /**
95cdf0e10cSrcweir     * Listener implementation which sets a flag when
96cdf0e10cSrcweir     * listener was called.
97cdf0e10cSrcweir     */
98cdf0e10cSrcweir     public class MyChangeListener implements XPropertiesChangeListener {
99cdf0e10cSrcweir          public void propertiesChange(PropertyChangeEvent[] e) {
100cdf0e10cSrcweir              //_log.println("Listener was called");
101cdf0e10cSrcweir              propertiesChanged = true;
102cdf0e10cSrcweir          }
103cdf0e10cSrcweir          public void disposing (EventObject obj) {}
104*170fb961SPedro Giffuni     }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     private XPropertiesChangeListener PClistener =
107cdf0e10cSrcweir         new MyChangeListener();
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     /**
110cdf0e10cSrcweir     * Test calls the method and checks return value.
111cdf0e10cSrcweir     * <code>PropertySetInfo</code> object is stored<p>
112cdf0e10cSrcweir     * Has <b> OK </b> status if the method returns not null value
113cdf0e10cSrcweir     * and no exceptions were thrown. <p>
114cdf0e10cSrcweir     */
115cdf0e10cSrcweir     public void _getPropertySetInfo() {
116cdf0e10cSrcweir         boolean bResult = true;
117cdf0e10cSrcweir         propertySetInfo = oObj.getPropertySetInfo();
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         if (propertySetInfo == null) {
120cdf0e10cSrcweir             log.println("getPropertySetInfo() method returned null");
121cdf0e10cSrcweir             bResult = false;
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         tRes.tested("getPropertySetInfo()", bResult) ;
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     /**
129cdf0e10cSrcweir     * Test collects all property names and retrieves their values,
130cdf0e10cSrcweir     * then checks the value returned. Finally it also collects
131cdf0e10cSrcweir     * bound properties for other methods tests.<p>
132cdf0e10cSrcweir     * Has <b> OK </b> status if the method  returns non null value
133cdf0e10cSrcweir     * and no exceptions were thrown. <p>
134cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
135cdf0e10cSrcweir     * <ul>
136cdf0e10cSrcweir     *  <li> <code> getPropertySetInfo() </code> : to have a list
137cdf0e10cSrcweir     *   of properties.</li>
138cdf0e10cSrcweir     * </ul>
139cdf0e10cSrcweir     */
140cdf0e10cSrcweir     public void _getPropertyValues() {
141cdf0e10cSrcweir         requiredMethod("getPropertySetInfo()");
142cdf0e10cSrcweir         boolean bResult = true;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         Property[] properties = propertySetInfo.getProperties();
145cdf0e10cSrcweir         String[] allnames = new String[properties.length];
146cdf0e10cSrcweir         for (int i = 0; i < properties.length; i++) {
147cdf0e10cSrcweir             allnames[i] = properties[i].Name;
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         values = oObj.getPropertyValues(allnames);
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         bResult &= values!=null;
153cdf0e10cSrcweir         tRes.tested("getPropertyValues()", bResult) ;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         getPropsToTest(properties);
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     /**
159cdf0e10cSrcweir     * Test adds listener for all bound properties then each property
160cdf0e10cSrcweir     * is changed and listener call . <p>
161cdf0e10cSrcweir     * Has <b> OK </b> status if on each property change the listener was
162cdf0e10cSrcweir     * called and no exceptions were thrown. <p>
163cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
164cdf0e10cSrcweir     * <ul>
165cdf0e10cSrcweir     *  <li> <code> getPropertyValues() </code> : to collect bound
166cdf0e10cSrcweir     *   properties.</li>
167cdf0e10cSrcweir     * </ul>
168cdf0e10cSrcweir     */
169cdf0e10cSrcweir     public void _addPropertiesChangeListener() {
170cdf0e10cSrcweir 
171cdf0e10cSrcweir         requiredMethod("getPropertyValues()");
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         boolean result  = true ;
174cdf0e10cSrcweir         // Creating listener
175cdf0e10cSrcweir         oObj.addPropertiesChangeListener(testPropsNames, PClistener);
176cdf0e10cSrcweir 
177cdf0e10cSrcweir         if ((testPropsAmount==1) && (testPropsNames[0].equals("none"))) {
178cdf0e10cSrcweir             testPropsAmount = 0;
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         // Change one of the property to be sure, that this event was cauched.
183cdf0e10cSrcweir         //Random rnd = new Random();
184cdf0e10cSrcweir         //int idx = rnd.nextInt(testPropsAmount);
185cdf0e10cSrcweir         for (int i=0; i<testPropsAmount;i++) {
186cdf0e10cSrcweir             log.print("Trying to change property " + testPropsNames[i]);
187cdf0e10cSrcweir             try {
188cdf0e10cSrcweir                 Object[] gValues = oObj.getPropertyValues(testPropsNames);
189cdf0e10cSrcweir                 Object newValue = ValueChanger.changePValue(gValues[i]);
190cdf0e10cSrcweir                 gValues[i] = newValue;
191cdf0e10cSrcweir                 propertiesChanged = false;
192cdf0e10cSrcweir                 oObj.setPropertyValues(testPropsNames, gValues);
193cdf0e10cSrcweir                 waitAMoment() ;
194cdf0e10cSrcweir                 result &= propertiesChanged ;
195cdf0e10cSrcweir                 log.println(" ... done");
196cdf0e10cSrcweir             } catch (com.sun.star.beans.PropertyVetoException e) {
197cdf0e10cSrcweir                 log.println("Exception occured while trying to change "+
198cdf0e10cSrcweir                     "property '"+testPropsNames[i] + "' :" + e);
199cdf0e10cSrcweir                 e.printStackTrace(log);
200cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException e) {
201cdf0e10cSrcweir                 log.println("Exception occured while trying to change "+
202cdf0e10cSrcweir                     "property '"+testPropsNames[i] + "' :" + e);
203cdf0e10cSrcweir                 e.printStackTrace(log);
204cdf0e10cSrcweir             } catch (com.sun.star.lang.WrappedTargetException e) {
205cdf0e10cSrcweir                 log.println("Exception occured while trying to change "+
206cdf0e10cSrcweir                     "property '"+testPropsNames[i] + "' :" + e);
207cdf0e10cSrcweir                 e.printStackTrace(log);
208cdf0e10cSrcweir             } // end of try-catch
209cdf0e10cSrcweir         }
210cdf0e10cSrcweir         if (testPropsAmount == 0) {
211cdf0e10cSrcweir             log.println("all properties are read only");
212cdf0e10cSrcweir             tRes.tested("addPropertiesChangeListener()", Status.skipped(true));
213cdf0e10cSrcweir         } else {
214cdf0e10cSrcweir             tRes.tested("addPropertiesChangeListener()", propertiesChanged);
215cdf0e10cSrcweir         }
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     /**
219cdf0e10cSrcweir     * Calls method and check if listener was called. <p>
220cdf0e10cSrcweir     * Has <b> OK </b> status if the listener was
221cdf0e10cSrcweir     * called and no exceptions were thrown. <p>
222cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
223cdf0e10cSrcweir     * <ul>
224cdf0e10cSrcweir     *  <li> <code> addPropertiesChangeListener() </code> : listener to
225cdf0e10cSrcweir     *    be added.</li>
226cdf0e10cSrcweir     * </ul>
227cdf0e10cSrcweir     */
228cdf0e10cSrcweir     public void _firePropertiesChangeEvent() {
229cdf0e10cSrcweir         requiredMethod("addPropertiesChangeListener()");
230cdf0e10cSrcweir         propertiesChanged = false ;
231cdf0e10cSrcweir 
232cdf0e10cSrcweir         oObj.firePropertiesChangeEvent(testPropsNames, PClistener);
233cdf0e10cSrcweir         waitAMoment() ;
234cdf0e10cSrcweir 
235cdf0e10cSrcweir         tRes.tested("firePropertiesChangeEvent()", propertiesChanged);
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     /**
240cdf0e10cSrcweir     * Removes listener added before. <p>
241cdf0e10cSrcweir     * Has <b> OK </b> status no exceptions were thrown. <p>
242cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
243cdf0e10cSrcweir     * <ul>
244cdf0e10cSrcweir     *  <li> <code> addPropertiesChangeListener() </code> : listener to
245cdf0e10cSrcweir     *    be added.</li>
246cdf0e10cSrcweir     * </ul>
247cdf0e10cSrcweir     */
248cdf0e10cSrcweir     public void _removePropertiesChangeListener() {
249cdf0e10cSrcweir         requiredMethod("firePropertiesChangeEvent()");
250cdf0e10cSrcweir         boolean bResult = true;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         oObj.removePropertiesChangeListener(PClistener);
253cdf0e10cSrcweir 
254cdf0e10cSrcweir         tRes.tested("removePropertiesChangeListener()", bResult);
255cdf0e10cSrcweir     }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 
258cdf0e10cSrcweir     /**
259cdf0e10cSrcweir     * Changes all properties, then set them to new values, get them
260cdf0e10cSrcweir     * and checks if their values were changed properly. <p>
261cdf0e10cSrcweir     * Has <b> OK </b> status if all properties properly changed
262cdf0e10cSrcweir     * and no exceptions were thrown. <p>
263cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
264cdf0e10cSrcweir     * <ul>
265cdf0e10cSrcweir     *  <li> <code> getPropertyValues() </code> : to collect bound
266cdf0e10cSrcweir     *   properties.</li>
267cdf0e10cSrcweir     * </ul>
268cdf0e10cSrcweir     */
269cdf0e10cSrcweir     public void _setPropertyValues() {
270cdf0e10cSrcweir         requiredMethod("getPropertyValues()");
271cdf0e10cSrcweir         boolean bResult = true;
272cdf0e10cSrcweir 
273cdf0e10cSrcweir         if ((testPropsNames.length==1)&&(testPropsNames[0].equals("none"))) {
274cdf0e10cSrcweir             log.println("all properties are readOnly");
275cdf0e10cSrcweir             tRes.tested("setPropertyValues()",Status.skipped(true));
276cdf0e10cSrcweir             return;
277cdf0e10cSrcweir         }
278cdf0e10cSrcweir 
279cdf0e10cSrcweir         log.println("Changing all properties");
280cdf0e10cSrcweir         Object[] gValues = oObj.getPropertyValues(testPropsNames);
281cdf0e10cSrcweir         for (int i=0; i<testPropsAmount;i++) {
282cdf0e10cSrcweir             Object oldValue = gValues[i];
283cdf0e10cSrcweir             Object newValue = ValueChanger.changePValue(oldValue);
284cdf0e10cSrcweir             gValues[i] = newValue;
285cdf0e10cSrcweir         }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir         try {
288cdf0e10cSrcweir             oObj.setPropertyValues(testPropsNames, gValues);
289cdf0e10cSrcweir             Object[] newValues = oObj.getPropertyValues(testPropsNames);
290cdf0e10cSrcweir             for (int i=0; i<testPropsAmount;i++) {
291cdf0e10cSrcweir                 if (newValues[i].equals(gValues[i])) {
292cdf0e10cSrcweir                     bResult = true;
293cdf0e10cSrcweir                 }
294cdf0e10cSrcweir             }
295cdf0e10cSrcweir         } catch (com.sun.star.beans.PropertyVetoException e) {
296cdf0e10cSrcweir             log.println("Exception occured while setting properties");
297cdf0e10cSrcweir             e.printStackTrace(log);
298cdf0e10cSrcweir             bResult = false;
299cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
300cdf0e10cSrcweir             log.println("Exception occured while setting properties");
301cdf0e10cSrcweir             e.printStackTrace(log);
302cdf0e10cSrcweir             bResult = false;
303cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
304cdf0e10cSrcweir             log.println("Exception occured while setting properties");
305cdf0e10cSrcweir             e.printStackTrace(log);
306cdf0e10cSrcweir             bResult = false;
307cdf0e10cSrcweir         } // end of try-catch
308cdf0e10cSrcweir 
309cdf0e10cSrcweir         tRes.tested("setPropertyValues()", bResult);
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir     //Get the properties being tested
313cdf0e10cSrcweir     private void getPropsToTest(Property[] properties) {
314cdf0e10cSrcweir 
315cdf0e10cSrcweir         String bound = "";
316cdf0e10cSrcweir 
317cdf0e10cSrcweir         for (int i = 0; i < properties.length; i++) {
318cdf0e10cSrcweir 
319cdf0e10cSrcweir             Property property = properties[i];
320cdf0e10cSrcweir             String name = property.Name;
321cdf0e10cSrcweir             boolean isWritable = ((property.Attributes &
322cdf0e10cSrcweir                 PropertyAttribute.READONLY) == 0);
323cdf0e10cSrcweir             boolean isNotNull = ((property.Attributes &
324cdf0e10cSrcweir                 PropertyAttribute.MAYBEVOID) == 0);
325cdf0e10cSrcweir             boolean isBound = ((property.Attributes &
326cdf0e10cSrcweir                 PropertyAttribute.BOUND) != 0);
327cdf0e10cSrcweir             boolean isExcluded = exclProps.contains(name);
328cdf0e10cSrcweir 
329cdf0e10cSrcweir             //exclude UserDefined, because we can't change XNameContainer
330cdf0e10cSrcweir             if (name.indexOf("UserDefined")>0 || name.indexOf("Device")>0) {
331cdf0e10cSrcweir                 isWritable=false;
332cdf0e10cSrcweir             }
333cdf0e10cSrcweir 
334cdf0e10cSrcweir             values = oObj.getPropertyValues(new String[]{property.Name});
335cdf0e10cSrcweir 
336cdf0e10cSrcweir             boolean isVoid = util.utils.isVoid(values[0]);
337cdf0e10cSrcweir 
338cdf0e10cSrcweir             if ( isWritable && isNotNull && isBound && !isExcluded && !isVoid) {
339cdf0e10cSrcweir                 bound+=name+";";
340cdf0e10cSrcweir             }
341cdf0e10cSrcweir 
342cdf0e10cSrcweir         } // endfor
343cdf0e10cSrcweir 
344cdf0e10cSrcweir         //get a array of bound properties
345cdf0e10cSrcweir         if (bound.equals("")) bound = "none";
346cdf0e10cSrcweir         StringTokenizer ST=new StringTokenizer(bound,";");
347cdf0e10cSrcweir         int nr = ST.countTokens();
348cdf0e10cSrcweir         testPropsNames = new String[nr];
349cdf0e10cSrcweir         for (int i=0; i<nr; i++) testPropsNames[i] = ST.nextToken();
350cdf0e10cSrcweir         testPropsAmount = nr;
351cdf0e10cSrcweir         return;
352cdf0e10cSrcweir 
353cdf0e10cSrcweir     }
354cdf0e10cSrcweir 
355cdf0e10cSrcweir     /**
356cdf0e10cSrcweir     * Waits some time for listener to be called.
357cdf0e10cSrcweir     */
358cdf0e10cSrcweir     private void waitAMoment() {
359cdf0e10cSrcweir         try {
360cdf0e10cSrcweir             Thread.sleep(200) ;
361cdf0e10cSrcweir         } catch (java.lang.InterruptedException e) {
362cdf0e10cSrcweir             log.println("!!! Exception while waiting !!!") ;
363cdf0e10cSrcweir         }
364cdf0e10cSrcweir     }
365cdf0e10cSrcweir 
366cdf0e10cSrcweir     /*
367cdf0e10cSrcweir     * Does nothing.
368cdf0e10cSrcweir     */
369cdf0e10cSrcweir     protected void after() {
370cdf0e10cSrcweir         disposeEnvironment();
371cdf0e10cSrcweir     }
372cdf0e10cSrcweir }
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 
375