Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / beans / _XMultiPropertySet.java
blob50508ce8c9b6cc9b4426e61ccdbc69ed2e82346f
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.beans;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.StringTokenizer;
25 import lib.MultiMethodTest;
26 import lib.Status;
27 import util.ValueChanger;
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.beans.XPropertySetInfo;
35 import com.sun.star.lang.EventObject;
38 /**
39 * Testing <code>com.sun.star.beans.XMultiPropertySet</code>
40 * interface methods :
41 * <ul>
42 * <li><code> getPropertySetInfo()</code></li>
43 * <li><code> setPropertyValues()</code></li>
44 * <li><code> getPropertyValues()</code></li>
45 * <li><code> addPropertiesChangeListener()</code></li>
46 * <li><code> removePropertiesChangeListener()</code></li>
47 * <li><code> firePropertiesChangeEvent()</code></li>
48 * </ul> <p>
50 * Required relations :
51 * <ul>
52 * <li> <code>'XMultiPropertySet.ExcludeProps'</code>
53 * <b>(optional) </b> : java.util.Set.
54 * Has property names which must be skipped from testing in
55 * some reasons (for example property accepts restricted set
56 * of values).
57 * </li>
58 * <ul> <p>
60 * Test is <b> NOT </b> multithread compliant. <p>
61 * After test completion object environment has to be recreated.
62 * @see com.sun.star.beans.XMultiPropertySet
64 public class _XMultiPropertySet extends MultiMethodTest {
66 public XMultiPropertySet oObj = null;
68 private boolean propertiesChanged = false;
69 private XPropertySetInfo propertySetInfo = null;
70 private String [] testPropsNames = null;
71 private int testPropsAmount = 0;
72 private Object[] values = null;
74 private Set<String> exclProps = null;
76 /**
77 * Initializes some fields.
79 @Override
80 public void before() {
81 exclProps = (Set<String>) tEnv.getObjRelation("XMultiPropertySet.ExcludeProps");
82 if (exclProps == null) exclProps = new HashSet<String>(0);
85 /**
86 * Listener implementation which sets a flag when
87 * listener was called.
89 public class MyChangeListener implements XPropertiesChangeListener {
90 public void propertiesChange(PropertyChangeEvent[] e) {
91 propertiesChanged = true;
93 public void disposing (EventObject obj) {}
96 private final XPropertiesChangeListener PClistener =
97 new MyChangeListener();
99 /**
100 * Test calls the method and checks return value.
101 * <code>PropertySetInfo</code> object is stored<p>
102 * Has <b> OK </b> status if the method returns not null value
103 * and no exceptions were thrown. <p>
105 public void _getPropertySetInfo() {
106 boolean bResult = true;
107 propertySetInfo = oObj.getPropertySetInfo();
109 if (propertySetInfo == null) {
110 log.println("getPropertySetInfo() method returned null");
111 bResult = false;
114 tRes.tested("getPropertySetInfo()", bResult) ;
119 * Test collects all property names and retrieves their values,
120 * then checks the value returned. Finally it also collects
121 * bound properties for other methods tests.<p>
122 * Has <b> OK </b> status if the method returns non null value
123 * and no exceptions were thrown. <p>
124 * The following method tests are to be completed successfully before :
125 * <ul>
126 * <li> <code> getPropertySetInfo() </code> : to have a list
127 * of properties.</li>
128 * </ul>
130 public void _getPropertyValues() {
131 requiredMethod("getPropertySetInfo()");
132 boolean bResult = true;
134 Property[] properties = propertySetInfo.getProperties();
135 String[] allnames = new String[properties.length];
136 for (int i = 0; i < properties.length; i++) {
137 allnames[i] = properties[i].Name;
140 values = oObj.getPropertyValues(allnames);
142 bResult &= values!=null;
143 tRes.tested("getPropertyValues()", bResult) ;
145 getPropsToTest(properties);
149 * Test adds listener for all bound properties then each property
150 * is changed and listener call . <p>
151 * Has <b> OK </b> status if on each property change the listener was
152 * called and no exceptions were thrown. <p>
153 * The following method tests are to be completed successfully before :
154 * <ul>
155 * <li> <code> getPropertyValues() </code> : to collect bound
156 * properties.</li>
157 * </ul>
159 public void _addPropertiesChangeListener() {
161 requiredMethod("getPropertyValues()");
163 // Creating listener
164 oObj.addPropertiesChangeListener(testPropsNames, PClistener);
166 if ((testPropsAmount==1) && (testPropsNames[0].equals("none"))) {
167 testPropsAmount = 0;
170 if (testPropsAmount == 0) {
171 log.println("all properties are read only");
172 tRes.tested("addPropertiesChangeListener()", Status.skipped(true));
173 return;
176 // Change one of the property to be sure, that this event was cauched.
177 for (int i=0; i<testPropsAmount;i++) {
178 log.print("Trying to change property " + testPropsNames[i]);
179 try {
180 Object[] gValues = oObj.getPropertyValues(testPropsNames);
181 Object newValue = ValueChanger.changePValue(gValues[i]);
182 gValues[i] = newValue;
183 propertiesChanged = false;
184 oObj.setPropertyValues(testPropsNames, gValues);
185 log.println(" ... done");
186 } catch (com.sun.star.beans.PropertyVetoException e) {
187 log.println("Exception occurred while trying to change "+
188 "property '"+testPropsNames[i] + "' :" + e);
189 e.printStackTrace(log);
190 } catch (com.sun.star.lang.IllegalArgumentException e) {
191 log.println("Exception occurred while trying to change "+
192 "property '"+testPropsNames[i] + "' :" + e);
193 e.printStackTrace(log);
194 } catch (com.sun.star.lang.WrappedTargetException e) {
195 log.println("Exception occurred while trying to change "+
196 "property '"+testPropsNames[i] + "' :" + e);
197 e.printStackTrace(log);
198 } // end of try-catch
200 util.utils.pause(200);
201 tRes.tested("addPropertiesChangeListener()", propertiesChanged);
205 * Calls method and check if listener was called. <p>
206 * Has <b> OK </b> status if the listener was
207 * called and no exceptions were thrown. <p>
208 * The following method tests are to be completed successfully before :
209 * <ul>
210 * <li> <code> addPropertiesChangeListener() </code> : listener to
211 * be added.</li>
212 * </ul>
214 public void _firePropertiesChangeEvent() {
215 requiredMethod("addPropertiesChangeListener()");
216 propertiesChanged = false ;
218 oObj.firePropertiesChangeEvent(testPropsNames, PClistener);
219 util.utils.pause(200);
221 tRes.tested("firePropertiesChangeEvent()", propertiesChanged);
226 * Removes listener added before. <p>
227 * Has <b> OK </b> status no exceptions were thrown. <p>
228 * The following method tests are to be completed successfully before :
229 * <ul>
230 * <li> <code> addPropertiesChangeListener() </code> : listener to
231 * be added.</li>
232 * </ul>
234 public void _removePropertiesChangeListener() {
235 requiredMethod("firePropertiesChangeEvent()");
236 boolean bResult = true;
238 oObj.removePropertiesChangeListener(PClistener);
240 tRes.tested("removePropertiesChangeListener()", bResult);
245 * Changes all properties, then set them to new values, get them
246 * and checks if their values were changed properly. <p>
247 * Has <b> OK </b> status if all properties properly changed
248 * and no exceptions were thrown. <p>
249 * The following method tests are to be completed successfully before :
250 * <ul>
251 * <li> <code> getPropertyValues() </code> : to collect bound
252 * properties.</li>
253 * </ul>
255 public void _setPropertyValues() {
256 requiredMethod("getPropertyValues()");
257 boolean bResult = true;
259 if ((testPropsNames.length==1)&&(testPropsNames[0].equals("none"))) {
260 log.println("all properties are readOnly");
261 tRes.tested("setPropertyValues()",Status.skipped(true));
262 return;
265 log.println("Changing all properties");
266 Object[] gValues = oObj.getPropertyValues(testPropsNames);
267 for (int i=0; i<testPropsAmount;i++) {
268 Object oldValue = gValues[i];
269 Object newValue = ValueChanger.changePValue(oldValue);
270 gValues[i] = newValue;
273 try {
274 oObj.setPropertyValues(testPropsNames, gValues);
275 Object[] newValues = oObj.getPropertyValues(testPropsNames);
276 for (int i=0; i<testPropsAmount;i++) {
277 if (newValues[i].equals(gValues[i])) {
278 bResult = true;
281 } catch (com.sun.star.beans.PropertyVetoException e) {
282 log.println("Exception occurred while setting properties");
283 e.printStackTrace(log);
284 bResult = false;
285 } catch (com.sun.star.lang.IllegalArgumentException e) {
286 log.println("Exception occurred while setting properties");
287 e.printStackTrace(log);
288 bResult = false;
289 } catch (com.sun.star.lang.WrappedTargetException e) {
290 log.println("Exception occurred while setting properties");
291 e.printStackTrace(log);
292 bResult = false;
293 } // end of try-catch
295 tRes.tested("setPropertyValues()", bResult);
298 //Get the properties being tested
299 private void getPropsToTest(Property[] properties) {
301 String bound = "";
303 for (int i = 0; i < properties.length; i++) {
305 Property property = properties[i];
306 String name = property.Name;
307 boolean isWritable = ((property.Attributes &
308 PropertyAttribute.READONLY) == 0);
309 boolean isNotNull = ((property.Attributes &
310 PropertyAttribute.MAYBEVOID) == 0);
311 boolean isBound = ((property.Attributes &
312 PropertyAttribute.BOUND) != 0);
313 boolean isExcluded = exclProps.contains(name);
315 //exclude UserDefined, because we can't change XNameContainer
316 if (name.indexOf("UserDefined")>0 || name.indexOf("Device")>0) {
317 isWritable=false;
320 values = oObj.getPropertyValues(new String[]{property.Name});
322 boolean isVoid = util.utils.isVoid(values[0]);
324 if ( isWritable && isNotNull && isBound && !isExcluded && !isVoid) {
325 bound+=name+";";
328 } // endfor
330 //get a array of bound properties
331 if (bound.equals("")) bound = "none";
332 StringTokenizer ST=new StringTokenizer(bound,";");
333 int nr = ST.countTokens();
334 testPropsNames = new String[nr];
335 for (int i=0; i<nr; i++) testPropsNames[i] = ST.nextToken();
336 testPropsAmount = nr;
340 * Does nothing.
342 @Override
343 protected void after() {
344 disposeEnvironment();