Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / beans / _XPropertySet.java
blob76aac038c6eda45b10caa5323a4c87758faae4c7
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.ArrayList;
23 import lib.MultiMethodTest;
24 import util.ValueChanger;
25 import util.utils;
27 import com.sun.star.beans.Property;
28 import com.sun.star.beans.PropertyAttribute;
29 import com.sun.star.beans.PropertyChangeEvent;
30 import com.sun.star.beans.XPropertyChangeListener;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.beans.XPropertySetInfo;
33 import com.sun.star.beans.XVetoableChangeListener;
34 import com.sun.star.lang.EventObject;
36 /**
37 * Testing <code>com.sun.star.beans.XPropertySet</code>
38 * interface methods :
39 * <ul>
40 * <li><code>getPropertySetInfo()</code></li>
41 * <li><code>setPropertyValue()</code></li>
42 * <li><code>getPropertyValue()</code></li>
43 * <li><code>addPropertyChangeListener()</code></li>
44 * <li><code>removePropertyChangeListener()</code></li>
45 * <li><code>addVetoableChangeListener()</code></li>
46 * <li><code>removeVetoableChangeListener()</code></li>
47 * </ul>
48 * @see com.sun.star.beans.XPropertySet
50 public class _XPropertySet extends MultiMethodTest {
52 public XPropertySet oObj = null;
54 /**
55 * Flag that indicates change listener was called.
57 private boolean propertyChanged = false;
59 /**
60 * Listener that must be called on bound property changing.
62 public class MyChangeListener implements XPropertyChangeListener {
63 /**
64 * Just set <code>propertyChanged</code> flag to true.
66 public void propertyChange(PropertyChangeEvent e) {
67 propertyChanged = true;
69 public void disposing (EventObject obj) {}
72 private final XPropertyChangeListener PClistener = new MyChangeListener();
74 /**
75 * Flag that indicates veto listener was called.
77 private boolean vetoableChanged = false;
79 /**
80 * Listener that must be called on constrained property changing.
82 public class MyVetoListener implements XVetoableChangeListener {
83 /**
84 * Just set <code>vetoableChanged</code> flag to true.
86 public void vetoableChange(PropertyChangeEvent e) {
87 vetoableChanged = true;
89 public void disposing (EventObject obj) {}
92 private final XVetoableChangeListener VClistener = new MyVetoListener();
94 /**
95 * Structure that collects the properties of different types to test :
96 * Constrained, Bound and Normal.
98 private final class PropsToTest {
99 ArrayList< String > constrained = new ArrayList< String >();
100 ArrayList< String > bound = new ArrayList< String >();
101 ArrayList< String > normal = new ArrayList< String >();
104 private final PropsToTest PTT = new PropsToTest();
107 * Tests method <code>getPropertySetInfo</code>. After test completed
108 * call {@link #getPropsToTest} method to retrieve different kinds
109 * of properties to test then. <p>
110 * Has OK status if not null <code>XPropertySetInfo</code>
111 * object returned.<p>
112 * Since <code>getPropertySetInfo</code> is optional, it may return null,
113 * if it is not implemented. This method uses then an object relation
114 * <code>PTT</code> (Properties To Test) to determine available properties.
115 * All tests for services without <code>getPropertySetInfo</code> must
116 * provide this object relation.
118 public void _getPropertySetInfo() {
120 XPropertySetInfo propertySetInfo = oObj.getPropertySetInfo();
122 if (propertySetInfo == null) {
123 log.println("getPropertySetInfo() method returned null");
124 tRes.tested("getPropertySetInfo()", true) ;
125 String[] ptt = (String[]) tEnv.getObjRelation("PTT");
126 PTT.normal.clear();
127 PTT.bound.clear();
128 PTT.constrained.clear();
129 PTT.normal.add( ptt[0] );
130 PTT.bound.add( ptt[1] );
131 PTT.constrained.add( ptt[2] );
132 } else {
133 tRes.tested("getPropertySetInfo()", true );
134 getPropsToTest(propertySetInfo);
136 } // end of getPropertySetInfo()
139 * Tests change listener which added for bound properties.
140 * Adds listener to bound property (if it exists), then changes
141 * its value and check if listener was called. <p>
142 * Method tests to be successfully completed before :
143 * <ul>
144 * <li> <code>getPropertySetInfo</code> : in this method test
145 * one of bound properties is retrieved. </li>
146 * </ul> <p>
147 * Has OK status if NO bound properties exist or if listener
148 * was successfully called.
150 public void _addPropertyChangeListener() {
152 requiredMethod("getPropertySetInfo()");
154 int count = PTT.bound.size();
155 if ( count==0 || PTT.bound.get(0).equals("none") ) {
156 log.println("*** No bound properties found ***");
157 tRes.tested("addPropertyChangeListener()", true) ;
158 } else {
159 boolean error = false;
160 for (int i = 0; i < count; i++) {
161 String propertyName = PTT.bound.get(i);
162 propertyChanged = false;
163 try {
164 oObj.addPropertyChangeListener(propertyName,PClistener);
165 Object gValue = oObj.getPropertyValue(propertyName);
166 log.println("Check bound property: " + propertyName );
167 oObj.setPropertyValue(propertyName,
168 ValueChanger.changePValue(gValue));
169 } catch (com.sun.star.beans.PropertyVetoException e) {
170 log.println("Exception occurred while trying to change "+
171 "property '"+ propertyName+"'");
172 e.printStackTrace(log);
173 } catch (com.sun.star.lang.IllegalArgumentException e) {
174 log.println("Exception occurred while trying to change "+
175 "property '"+ propertyName+"'");
176 e.printStackTrace(log);
177 } catch (com.sun.star.beans.UnknownPropertyException e) {
178 log.println("Exception occurred while trying to change "+
179 "property '"+ propertyName+"'");
180 e.printStackTrace(log);
181 } catch (com.sun.star.lang.WrappedTargetException e) {
182 log.println("Exception occurred while trying to change "+
183 "property '"+ propertyName+"'");
184 e.printStackTrace(log);
185 } // end of try-catch
186 error = error || !propertyChanged;
187 if (!propertyChanged) {
188 log.println("propertyChangeListener wasn't called for '"+
189 propertyName+"'");
192 tRes.tested("addPropertyChangeListener()", !error);
194 } // end of addPropertyChangeListener()
197 * Tests vetoable listener which added for constrained properties.
198 * Adds listener to constrained property (if it exists), then changes
199 * its value and check if listener was called. <p>
200 * Method tests to be successfully completed before :
201 * <ul>
202 * <li> <code>getPropertySetInfo</code> : in this method test
203 * one of constrained properties is retrieved. </li>
204 * </ul> <p>
205 * Has OK status if NO constrained properties exist or if listener
206 * was successfully called.
208 public void _addVetoableChangeListener() {
210 requiredMethod("getPropertySetInfo()");
212 int count = PTT.constrained.size();
213 if ( count==0 || PTT.constrained.get(0).equals("none") ) {
214 log.println("*** No constrained properties found ***");
215 tRes.tested("addVetoableChangeListener()", true) ;
216 } else {
217 boolean error = false;
218 for (int i = 0; i < count; i++) {
219 String propertyName = PTT.constrained.get(i);
220 vetoableChanged = false;
221 try {
222 oObj.addVetoableChangeListener(propertyName,VClistener);
223 Object gValue = oObj.getPropertyValue(propertyName);
224 oObj.setPropertyValue(propertyName,
225 ValueChanger.changePValue(gValue));
226 } catch (com.sun.star.beans.PropertyVetoException e) {
227 log.println("Exception occurred while trying to change "+
228 "property '"+ propertyName+"'");
229 e.printStackTrace(log);
230 } catch (com.sun.star.lang.IllegalArgumentException e) {
231 log.println("Exception occurred while trying to change "+
232 "property '"+ propertyName+"'");
233 e.printStackTrace(log);
234 } catch (com.sun.star.beans.UnknownPropertyException e) {
235 log.println("Exception occurred while trying to change "+
236 "property '"+ propertyName+"'");
237 e.printStackTrace(log);
238 } catch (com.sun.star.lang.WrappedTargetException e) {
239 log.println("Exception occurred while trying to change "+
240 "property '"+ propertyName+"'");
241 e.printStackTrace(log);
242 } // end of try-catch
243 error = error || !vetoableChanged;
244 if (!vetoableChanged) {
245 log.println("vetoableChangeListener wasn't called for '"+
246 propertyName+"'");
249 tRes.tested("addVetoableChangeListener()",!error);
251 } // end of addVetoableChangeListener()
255 * Tests <code>setPropertyValue</code> method.
256 * Stores value before call, and compares it with value after
257 * call. <p>
258 * Method tests to be successfully completed before :
259 * <ul>
260 * <li> <code>getPropertySetInfo</code> : in this method test
261 * one of normal properties is retrieved. </li>
262 * </ul> <p>
263 * Has OK status if NO normal properties exist or if value before
264 * method call is not equal to value after.
266 public void _setPropertyValue() {
268 requiredMethod("getPropertySetInfo()");
270 Object gValue = null;
271 Object sValue = null;
273 int count = PTT.normal.size();
274 if ( count==0 || PTT.normal.get(0).equals("none") ) {
275 log.println("*** No changeable properties found ***");
276 tRes.tested("setPropertyValue()", true) ;
277 } else {
278 boolean error = false;
279 for (int i = 0; i < count; i++) {
280 String propertyName = PTT.normal.get(i);
281 try {
282 log.println("try to change value of property '" + propertyName + "'" );
283 gValue = oObj.getPropertyValue(propertyName);
284 sValue = ValueChanger.changePValue(gValue, propertyName);
285 oObj.setPropertyValue(propertyName, sValue);
286 sValue = oObj.getPropertyValue(propertyName);
287 } catch (com.sun.star.beans.PropertyVetoException e) {
288 log.println("Exception occurred while trying to change "+
289 "property '"+ propertyName+"'");
290 e.printStackTrace(log);
291 } catch (com.sun.star.lang.IllegalArgumentException e) {
292 log.println("Exception occurred while trying to change "+
293 "property '"+ propertyName+"'");
294 e.printStackTrace(log);
295 } catch (com.sun.star.beans.UnknownPropertyException e) {
296 log.println("Exception occurred while trying to change "+
297 "property '"+ propertyName+"'");
298 e.printStackTrace(log);
299 } catch (com.sun.star.lang.WrappedTargetException e) {
300 log.println("Exception occurred while trying to change "+
301 "property '"+ propertyName+"'");
302 e.printStackTrace(log);
303 } // end of try-catch
304 /* this is stupid: we can't set properties whose semantics we
305 * don't know to random values in an arbitrary order and
306 * expect that to actually work.
307 if( gValue.equals(sValue) )
309 log.println("setting property '"+ propertyName+"' failed");
310 error = true;
314 tRes.tested("setPropertyValue()",!error);
315 } //endif
316 } // end of setPropertyValue()
319 * Tests <code>getPropertyValue</code> method for the given property.
320 * Returns true if no exceptions occurred
322 private boolean getSinglePropertyValue( String propertyName )
324 boolean runOk = false;
325 try {
326 oObj.getPropertyValue(propertyName);
327 runOk = true;
328 } catch (com.sun.star.beans.UnknownPropertyException e) {
329 log.println("Exception occurred while trying to get property '"+
330 propertyName+"'");
331 e.printStackTrace(log);
332 } catch (com.sun.star.lang.WrappedTargetException e) {
333 log.println("Exception occurred while trying to get property '"+
334 propertyName+"'");
335 e.printStackTrace(log);
337 return runOk;
341 * Tests <code>getPropertyValue</code> method.
342 * Just call this method and checks for no exceptions <p>
343 * Method tests to be successfully completed before :
344 * <ul>
345 * <li> <code>getPropertySetInfo</code> : in this method test
346 * one of normal properties is retrieved. </li>
347 * </ul> <p>
348 * Has OK status if NO normal properties exist or if no
349 * exceptions were thrown.
351 public void _getPropertyValue() {
353 requiredMethod("getPropertySetInfo()");
355 int count = PTT.normal.size();
356 if ( count==0 || PTT.normal.get(0).equals("none") ) {
357 Property[] properties = oObj.getPropertySetInfo().getProperties();
358 if( properties.length > 0 ) {
359 String propertyName = properties[0].Name;
360 log.println("All properties are Read Only");
361 log.println("Using: "+propertyName);
362 tRes.tested("getPropertyValue()", getSinglePropertyValue( propertyName ) );
364 else {
365 log.println("*** No properties found ***");
366 tRes.tested("getPropertyValue()", true) ;
368 } else {
369 boolean error = false;
370 for (int i = 0; i < count; i++) {
371 String propertyName = PTT.normal.get(i);
372 boolean runOk = getSinglePropertyValue( propertyName );
373 if( !runOk )
375 error = true;
376 log.println("getPropertyValue() failed for property '"+propertyName+"'");
379 tRes.tested("getPropertyValue()", !error) ;
384 * Tests <code>removePropertyChangeListener</code> method.
385 * Removes change listener, then changes bound property value
386 * and checks if the listener was NOT called.
387 * Method tests to be successfully completed before :
388 * <ul>
389 * <li> <code>addPropertyChangeListener</code> : here listener
390 * was added. </li>
391 * </ul> <p>
392 * Has OK status if NO bound properties exist or if listener
393 * was not called and no exceptions arose.
395 public void _removePropertyChangeListener() {
397 requiredMethod("addPropertyChangeListener()");
399 int count = PTT.bound.size();
400 if ( count==0 || PTT.bound.get(0).equals("none") ) {
401 log.println("*** No bound properties found ***");
402 tRes.tested("removePropertyChangeListener()", true) ;
403 } else {
405 //remove all listeners first
406 for (int i = 0; i < count; i++) {
407 String propertyName = PTT.bound.get(i);
408 try {
409 oObj.removePropertyChangeListener(propertyName,PClistener);
410 } catch (Exception e) {
411 log.println("Exception occurred while removing change listener from"+
412 "property '"+ propertyName+"'");
413 e.printStackTrace(log);
417 boolean error = false;
418 for (int i = 0; i < count; i++) {
419 String propertyName = PTT.bound.get(i);
420 try {
421 propertyChanged = false;
422 oObj.addPropertyChangeListener(propertyName,PClistener);
423 oObj.removePropertyChangeListener(propertyName,PClistener);
424 Object gValue = oObj.getPropertyValue(propertyName);
425 oObj.setPropertyValue(propertyName,
426 ValueChanger.changePValue(gValue));
427 } catch (com.sun.star.beans.PropertyVetoException e) {
428 log.println("Exception occurred while trying to change "+
429 "property '"+ propertyName+"'");
430 e.printStackTrace(log);
431 } catch (com.sun.star.lang.IllegalArgumentException e) {
432 log.println("Exception occurred while trying to change "+
433 "property '"+ propertyName+"'");
434 e.printStackTrace(log);
435 } catch (com.sun.star.beans.UnknownPropertyException e) {
436 log.println("Exception occurred while trying to change "+
437 "property '"+ propertyName+"'");
438 e.printStackTrace(log);
439 } catch (com.sun.star.lang.WrappedTargetException e) {
440 log.println("Exception occurred while trying to change "+
441 "property '"+ propertyName+"'");
442 e.printStackTrace(log);
443 } // end of try-catch
445 error = error || propertyChanged;
446 if (propertyChanged) {
447 log.println("propertyChangeListener was called after removing"+
448 " for '"+propertyName+"'");
451 tRes.tested("removePropertyChangeListener()",!error);
453 } // end of removePropertyChangeListener()
457 * Tests <code>removeVetoableChangeListener</code> method.
458 * Removes vetoable listener, then changes constrained property value
459 * and checks if the listener was NOT called.
460 * Method tests to be successfully completed before :
461 * <ul>
462 * <li> <code>addPropertyChangeListener</code> : here vetoable listener
463 * was added. </li>
464 * </ul> <p>
465 * Has OK status if NO constrained properties exist or if listener
466 * was NOT called and no exceptions arose.
468 public void _removeVetoableChangeListener() {
470 requiredMethod("addVetoableChangeListener()");
472 int count = PTT.constrained.size();
473 if ( count==0 || PTT.constrained.get(0).equals("none") ) {
474 log.println("*** No constrained properties found ***");
475 tRes.tested("removeVetoableChangeListener()", true) ;
476 } else {
478 //remove all listeners first
479 for (int i = 0; i < count; i++) {
480 String propertyName = PTT.constrained.get(i);
481 try {
482 oObj.removeVetoableChangeListener(propertyName,VClistener);
483 } catch (Exception e) {
484 log.println("Exception occurred while removing veto listener from"+
485 "property '"+ propertyName+"'");
486 e.printStackTrace(log);
490 boolean error = false;
491 for (int i = 0; i < count; i++) {
492 String propertyName = PTT.constrained.get(i);
493 vetoableChanged = false;
494 try {
495 oObj.addVetoableChangeListener(propertyName,VClistener);
496 oObj.removeVetoableChangeListener(propertyName,VClistener);
497 Object gValue = oObj.getPropertyValue(propertyName);
498 oObj.setPropertyValue(propertyName,
499 ValueChanger.changePValue(gValue));
500 } catch (com.sun.star.beans.PropertyVetoException e) {
501 log.println("Exception occurred while trying to change "+
502 "property '"+ propertyName+"'");
503 e.printStackTrace(log);
504 } catch (com.sun.star.lang.IllegalArgumentException e) {
505 log.println("Exception occurred while trying to change "+
506 "property '"+ propertyName+"'");
507 e.printStackTrace(log);
508 } catch (com.sun.star.beans.UnknownPropertyException e) {
509 log.println("Exception occurred while trying to change "+
510 "property '"+ propertyName+"'");
511 e.printStackTrace(log);
512 } catch (com.sun.star.lang.WrappedTargetException e) {
513 log.println("Exception occurred while trying to change "+
514 "property '"+ propertyName+"'");
515 e.printStackTrace(log);
516 } // end of try-catch
517 error = error || vetoableChanged;
518 if (vetoableChanged) {
519 log.println("vetoableChangeListener was called after "+
520 "removing for '"+propertyName+"'");
523 tRes.tested("removeVetoableChangeListener()",!error);
525 } // end of removeVetoableChangeListener()
528 * Gets the properties being tested. Searches and stores by one
529 * property of each kind (Bound, Vetoable, Normal).
531 public void getPropsToTest(XPropertySetInfo xPSI) {
533 Property[] properties = xPSI.getProperties();
534 // some properties should not be changed in a unspecific way
535 String[] skip = {"PrinterName", "CharRelief", "IsLayerMode"};
537 for (int i = 0; i < properties.length; i++) {
539 Property property = properties[i];
540 String name = property.Name;
542 boolean cont = false;
543 for (int j = 0; j < skip.length; j++) {
544 if (name.equals(skip[j])){
545 log.println("skipping '" + name + "'");
546 cont = true;
550 if (cont) continue;
552 if (name.equals(oObj))
553 log.println("Checking '"+name+"'");
554 boolean isWritable = ((property.Attributes &
555 PropertyAttribute.READONLY) == 0);
556 boolean isNotNull = ((property.Attributes &
557 PropertyAttribute.MAYBEVOID) == 0);
558 boolean isBound = ((property.Attributes &
559 PropertyAttribute.BOUND) != 0);
560 boolean isConstr = ((property.Attributes &
561 PropertyAttribute.CONSTRAINED) != 0);
562 boolean canChange = false;
564 if ( !isWritable ) log.println("Property '"+name+"' is READONLY");
566 if (name.endsWith("URL")) isWritable = false;
567 if (name.startsWith("Fill")) isWritable = false;
568 if (name.startsWith("Font")) isWritable = false;
569 if (name.startsWith("IsNumbering")) isWritable = false;
570 if (name.startsWith("LayerName")) isWritable = false;
571 if (name.startsWith("Line")) isWritable = false;
572 if (name.startsWith("TextWriting")) isWritable = false;
574 if ( isWritable && isNotNull ) canChange = isChangeable(name);
576 if ( isWritable && isNotNull && isBound && canChange) {
577 PTT.bound.add(name);
580 if ( isWritable && isNotNull && isConstr && canChange) {
581 PTT.constrained.add(name);
584 if ( isWritable && isNotNull && canChange) {
585 PTT.normal.add(name);
589 } // endfor
592 public boolean isChangeable(String name) {
593 boolean hasChanged = false;
594 try {
595 Object getProp = oObj.getPropertyValue(name);
596 log.println("Getting: "+getProp);
597 if (name.equals("xinterfaceA")) {
598 System.out.println("drin");
601 Object setValue = null;
602 if (getProp != null) {
603 if (!utils.isVoid(getProp))
604 setValue = ValueChanger.changePValue(getProp);
605 else log.println("Property '"+name+
606 "' is void but MAYBEVOID isn't set");
607 } else log.println("Property '"+name+"'is null and can't be changed");
608 if (name.equals("LineStyle")) setValue = null;
609 if (setValue != null) {
610 log.println("Setting to :"+setValue);
611 oObj.setPropertyValue(name, setValue);
612 hasChanged = (! getProp.equals(oObj.getPropertyValue(name)));
613 } else log.println("Couldn't change Property '"+name+"'");
614 } catch (com.sun.star.beans.PropertyVetoException e) {
615 log.println("'" + name + "' throws exception '" + e + "'");
616 e.printStackTrace(log);
617 } catch (com.sun.star.lang.IllegalArgumentException e) {
618 log.println("'" + name + "' throws exception '" + e + "'");
619 e.printStackTrace(log);
620 } catch (com.sun.star.beans.UnknownPropertyException e) {
621 log.println("'" + name + "' throws exception '" + e + "'");
622 e.printStackTrace(log);
623 } catch (com.sun.star.lang.WrappedTargetException e) {
624 log.println("'" + name + "' throws exception '" + e + "'");
625 e.printStackTrace(log);
626 } catch (com.sun.star.uno.RuntimeException e) {
627 log.println("'" + name + "' throws exception '" + e + "'");
628 e.printStackTrace(log);
629 } catch (java.lang.ArrayIndexOutOfBoundsException e) {
630 log.println("'" + name + "' throws exception '" + e + "'");
631 e.printStackTrace(log);
634 return hasChanged;
638 * Forces environment recreation.
640 @Override
641 protected void after() {
642 disposeEnvironment();
646 } // finish class _XPropertySet