Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / tests / java / ifc / beans / _XPropertySet.java
blob09e933fb6a65d459a1704b6c46e9349a6742f25a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package ifc.beans;
30 import java.util.Vector;
32 import lib.MultiMethodTest;
33 import util.ValueChanger;
34 import util.utils;
36 import com.sun.star.beans.Property;
37 import com.sun.star.beans.PropertyAttribute;
38 import com.sun.star.beans.PropertyChangeEvent;
39 import com.sun.star.beans.XPropertyChangeListener;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.beans.XPropertySetInfo;
42 import com.sun.star.beans.XVetoableChangeListener;
43 import com.sun.star.lang.EventObject;
45 /**
46 * Testing <code>com.sun.star.beans.XPropertySet</code>
47 * interface methods :
48 * <ul>
49 * <li><code>getPropertySetInfo()</code></li>
50 * <li><code>setPropertyValue()</code></li>
51 * <li><code>getPropertyValue()</code></li>
52 * <li><code>addPropertyChangeListener()</code></li>
53 * <li><code>removePropertyChangeListener()</code></li>
54 * <li><code>addVetoableChangeListener()</code></li>
55 * <li><code>removeVetoableChangeListener()</code></li>
56 * </ul>
57 * @see com.sun.star.beans.XPropertySet
59 public class _XPropertySet extends MultiMethodTest {
61 public XPropertySet oObj = null;
63 /**
64 * Flag that indicates change listener was called.
66 private boolean propertyChanged = false;
68 /**
69 * Listener that must be called on bound property changing.
71 public class MyChangeListener implements XPropertyChangeListener {
72 /**
73 * Just set <code>propertyChanged</code> flag to true.
75 public void propertyChange(PropertyChangeEvent e) {
76 propertyChanged = true;
78 public void disposing (EventObject obj) {}
81 private final XPropertyChangeListener PClistener = new MyChangeListener();
83 /**
84 * Flag that indicates veto listener was called.
86 private boolean vetoableChanged = false;
88 /**
89 * Listener that must be called on constrained property changing.
91 public class MyVetoListener implements XVetoableChangeListener {
92 /**
93 * Just set <code>vetoableChanged</code> flag to true.
95 public void vetoableChange(PropertyChangeEvent e) {
96 vetoableChanged = true;
98 public void disposing (EventObject obj) {}
101 private final XVetoableChangeListener VClistener = new MyVetoListener();
104 * Structure that collects the properties of different types to test :
105 * Constrained, Bound and Normal.
107 private final class PropsToTest {
108 Vector< String > constrained = new Vector< String >();
109 Vector< String > bound = new Vector< String >();
110 Vector< String > normal = new Vector< String >();
113 private final PropsToTest PTT = new PropsToTest();
116 * Tests method <code>getPropertySetInfo</code>. After test completed
117 * call {@link #getPropsToTest} method to retrieve different kinds
118 * of properties to test then. <p>
119 * Has OK status if not null <code>XPropertySetInfo</code>
120 * object returned.<p>
121 * Since <code>getPropertySetInfo</code> is optional, it may return null,
122 * if it is not implemented. This method uses then an object relation
123 * <code>PTT</code> (Properties To Test) to determine available properties.
124 * All tests for services without <code>getPropertySetInfo</code> must
125 * provide this object relation.
127 public void _getPropertySetInfo() {
129 XPropertySetInfo propertySetInfo = oObj.getPropertySetInfo();
131 if (propertySetInfo == null) {
132 log.println("getPropertySetInfo() method returned null");
133 tRes.tested("getPropertySetInfo()", true) ;
134 String[] ptt = (String[]) tEnv.getObjRelation("PTT");
135 PTT.normal.clear();
136 PTT.bound.clear();
137 PTT.constrained.clear();
138 PTT.normal.add( ptt[0] );
139 PTT.bound.add( ptt[1] );
140 PTT.constrained.add( ptt[2] );
141 } else {
142 tRes.tested("getPropertySetInfo()", true );
143 getPropsToTest(propertySetInfo);
146 return;
148 } // end of getPropertySetInfo()
151 * Tests change listener which added for bound properties.
152 * Adds listener to bound property (if it exists), then changes
153 * its value and check if listener was called. <p>
154 * Method tests to be successfully completed before :
155 * <ul>
156 * <li> <code>getPropertySetInfo</code> : in this method test
157 * one of bound properties is retrieved. </li>
158 * </ul> <p>
159 * Has OK status if NO bound properties exist or if listener
160 * was successfully called.
162 public void _addPropertyChangeListener() {
164 requiredMethod("getPropertySetInfo()");
166 int count = PTT.bound.size();
167 if ( count==0 || PTT.bound.get(0).equals("none") ) {
168 log.println("*** No bound properties found ***");
169 tRes.tested("addPropertyChangeListener()", true) ;
170 } else {
171 boolean error = false;
172 for (int i = 0; i < count; i++) {
173 String propertyName = PTT.bound.get(i);
174 propertyChanged = false;
175 try {
176 oObj.addPropertyChangeListener(propertyName,PClistener);
177 Object gValue = oObj.getPropertyValue(propertyName);
178 log.println("Check bound property: " + propertyName );
179 oObj.setPropertyValue(propertyName,
180 ValueChanger.changePValue(gValue));
181 } catch (com.sun.star.beans.PropertyVetoException e) {
182 log.println("Exception occurred while trying to change "+
183 "property '"+ propertyName+"'");
184 e.printStackTrace(log);
185 } catch (com.sun.star.lang.IllegalArgumentException e) {
186 log.println("Exception occurred while trying to change "+
187 "property '"+ propertyName+"'");
188 e.printStackTrace(log);
189 } catch (com.sun.star.beans.UnknownPropertyException e) {
190 log.println("Exception occurred while trying to change "+
191 "property '"+ propertyName+"'");
192 e.printStackTrace(log);
193 } catch (com.sun.star.lang.WrappedTargetException e) {
194 log.println("Exception occurred while trying to change "+
195 "property '"+ propertyName+"'");
196 e.printStackTrace(log);
197 } // end of try-catch
198 error = error || !propertyChanged;
199 if (!propertyChanged) {
200 log.println("propertyChangeListener wasn't called for '"+
201 propertyName+"'");
204 tRes.tested("addPropertyChangeListener()", !error);
207 return;
209 } // end of addPropertyChangeListener()
212 * Tests vetoable listener which added for constrained properties.
213 * Adds listener to constrained property (if it exists), then changes
214 * its value and check if listener was called. <p>
215 * Method tests to be successfully completed before :
216 * <ul>
217 * <li> <code>getPropertySetInfo</code> : in this method test
218 * one of constrained properties is retrieved. </li>
219 * </ul> <p>
220 * Has OK status if NO constrained properties exist or if listener
221 * was successfully called.
223 public void _addVetoableChangeListener() {
225 requiredMethod("getPropertySetInfo()");
227 int count = PTT.constrained.size();
228 if ( count==0 || PTT.constrained.get(0).equals("none") ) {
229 log.println("*** No constrained properties found ***");
230 tRes.tested("addVetoableChangeListener()", true) ;
231 } else {
232 boolean error = false;
233 for (int i = 0; i < count; i++) {
234 String propertyName = PTT.constrained.get(i);
235 vetoableChanged = false;
236 try {
237 oObj.addVetoableChangeListener(propertyName,VClistener);
238 Object gValue = oObj.getPropertyValue(propertyName);
239 oObj.setPropertyValue(propertyName,
240 ValueChanger.changePValue(gValue));
241 } catch (com.sun.star.beans.PropertyVetoException e) {
242 log.println("Exception occurred while trying to change "+
243 "property '"+ propertyName+"'");
244 e.printStackTrace(log);
245 } catch (com.sun.star.lang.IllegalArgumentException e) {
246 log.println("Exception occurred while trying to change "+
247 "property '"+ propertyName+"'");
248 e.printStackTrace(log);
249 } catch (com.sun.star.beans.UnknownPropertyException e) {
250 log.println("Exception occurred while trying to change "+
251 "property '"+ propertyName+"'");
252 e.printStackTrace(log);
253 } catch (com.sun.star.lang.WrappedTargetException e) {
254 log.println("Exception occurred while trying to change "+
255 "property '"+ propertyName+"'");
256 e.printStackTrace(log);
257 } // end of try-catch
258 error = error || !vetoableChanged;
259 if (!vetoableChanged) {
260 log.println("vetoableChangeListener wasn't called for '"+
261 propertyName+"'");
264 tRes.tested("addVetoableChangeListener()",!error);
267 return;
269 } // end of addVetoableChangeListener()
273 * Tests <code>setPropertyValue</code> method.
274 * Stores value before call, and compares it with value after
275 * call. <p>
276 * Method tests to be successfully completed before :
277 * <ul>
278 * <li> <code>getPropertySetInfo</code> : in this method test
279 * one of normal properties is retrieved. </li>
280 * </ul> <p>
281 * Has OK status if NO normal properties exist or if value before
282 * method call is not equal to value after.
284 public void _setPropertyValue() {
286 requiredMethod("getPropertySetInfo()");
288 Object gValue = null;
289 Object sValue = null;
291 int count = PTT.normal.size();
292 if ( count==0 || PTT.normal.get(0).equals("none") ) {
293 log.println("*** No changeable properties found ***");
294 tRes.tested("setPropertyValue()", true) ;
295 } else {
296 boolean error = false;
297 for (int i = 0; i < count; i++) {
298 String propertyName = PTT.normal.get(i);
299 try {
300 log.println("try to change value of property '" + propertyName + "'" );
301 gValue = oObj.getPropertyValue(propertyName);
302 sValue = ValueChanger.changePValue(gValue);
303 oObj.setPropertyValue(propertyName, sValue);
304 sValue = oObj.getPropertyValue(propertyName);
305 } catch (com.sun.star.beans.PropertyVetoException e) {
306 log.println("Exception occurred while trying to change "+
307 "property '"+ propertyName+"'");
308 e.printStackTrace(log);
309 } catch (com.sun.star.lang.IllegalArgumentException e) {
310 log.println("Exception occurred while trying to change "+
311 "property '"+ propertyName+"'");
312 e.printStackTrace(log);
313 } catch (com.sun.star.beans.UnknownPropertyException e) {
314 log.println("Exception occurred while trying to change "+
315 "property '"+ propertyName+"'");
316 e.printStackTrace(log);
317 } catch (com.sun.star.lang.WrappedTargetException e) {
318 log.println("Exception occurred while trying to change "+
319 "property '"+ propertyName+"'");
320 e.printStackTrace(log);
321 } // end of try-catch
322 /* this is stupid: we can't set properties whose semantics we
323 * don't know to random values in an arbitrary order and
324 * expect that to actually work.
325 if( gValue.equals(sValue) )
327 log.println("setting property '"+ propertyName+"' failed");
328 error = true;
332 tRes.tested("setPropertyValue()",!error);
333 } //endif
335 return;
337 } // end of setPropertyValue()
340 * Tests <code>getPropertyValue</code> method for the given property.
341 * Returns true if no exceptions occurred
343 private boolean getSinglePropertyValue( String propertyName )
345 boolean runOk = false;
346 try {
347 oObj.getPropertyValue(propertyName);
348 runOk = true;
349 } catch (com.sun.star.beans.UnknownPropertyException e) {
350 log.println("Exception occurred while trying to get property '"+
351 propertyName+"'");
352 e.printStackTrace(log);
353 } catch (com.sun.star.lang.WrappedTargetException e) {
354 log.println("Exception occurred while trying to get property '"+
355 propertyName+"'");
356 e.printStackTrace(log);
358 return runOk;
362 * Tests <code>getPropertyValue</code> method.
363 * Just call this method and checks for no exceptions <p>
364 * Method tests to be successfully completed before :
365 * <ul>
366 * <li> <code>getPropertySetInfo</code> : in this method test
367 * one of normal properties is retrieved. </li>
368 * </ul> <p>
369 * Has OK status if NO normal properties exist or if no
370 * exceptions were thrown.
372 public void _getPropertyValue() {
374 requiredMethod("getPropertySetInfo()");
376 int count = PTT.normal.size();
377 if ( count==0 || PTT.normal.get(0).equals("none") ) {
378 Property[] properties = oObj.getPropertySetInfo().getProperties();
379 if( properties.length > 0 ) {
380 String propertyName = properties[0].Name;
381 log.println("All properties are Read Only");
382 log.println("Using: "+propertyName);
383 tRes.tested("getPropertyValue()", getSinglePropertyValue( propertyName ) );
385 else {
386 log.println("*** No properties found ***");
387 tRes.tested("getPropertyValue()", true) ;
389 } else {
390 boolean error = false;
391 for (int i = 0; i < count; i++) {
392 String propertyName = PTT.normal.get(i);
393 boolean runOk = getSinglePropertyValue( propertyName );
394 if( !runOk )
396 error = true;
397 log.println("getPropertyValue() failed for property '"+propertyName+"'");
400 tRes.tested("getPropertyValue()", !error) ;
403 return;
407 * Tests <code>removePropertyChangeListener</code> method.
408 * Removes change listener, then changes bound property value
409 * and checks if the listener was NOT called.
410 * Method tests to be successfully completed before :
411 * <ul>
412 * <li> <code>addPropertyChangeListener</code> : here listener
413 * was added. </li>
414 * </ul> <p>
415 * Has OK status if NO bound properties exist or if listener
416 * was not called and no exceptions arose.
418 public void _removePropertyChangeListener() {
420 requiredMethod("addPropertyChangeListener()");
422 int count = PTT.bound.size();
423 if ( count==0 || PTT.bound.get(0).equals("none") ) {
424 log.println("*** No bound properties found ***");
425 tRes.tested("removePropertyChangeListener()", true) ;
426 } else {
428 //remove all listeners first
429 for (int i = 0; i < count; i++) {
430 String propertyName = PTT.bound.get(i);
431 try {
432 oObj.removePropertyChangeListener(propertyName,PClistener);
433 } catch (Exception e) {
434 log.println("Exception occurred while removing change listener from"+
435 "property '"+ propertyName+"'");
436 e.printStackTrace(log);
440 boolean error = false;
441 for (int i = 0; i < count; i++) {
442 String propertyName = PTT.bound.get(i);
443 try {
444 propertyChanged = false;
445 oObj.addPropertyChangeListener(propertyName,PClistener);
446 oObj.removePropertyChangeListener(propertyName,PClistener);
447 Object gValue = oObj.getPropertyValue(propertyName);
448 oObj.setPropertyValue(propertyName,
449 ValueChanger.changePValue(gValue));
450 } catch (com.sun.star.beans.PropertyVetoException e) {
451 log.println("Exception occurred while trying to change "+
452 "property '"+ propertyName+"'");
453 e.printStackTrace(log);
454 } catch (com.sun.star.lang.IllegalArgumentException e) {
455 log.println("Exception occurred while trying to change "+
456 "property '"+ propertyName+"'");
457 e.printStackTrace(log);
458 } catch (com.sun.star.beans.UnknownPropertyException e) {
459 log.println("Exception occurred while trying to change "+
460 "property '"+ propertyName+"'");
461 e.printStackTrace(log);
462 } catch (com.sun.star.lang.WrappedTargetException e) {
463 log.println("Exception occurred while trying to change "+
464 "property '"+ propertyName+"'");
465 e.printStackTrace(log);
466 } // end of try-catch
468 error = error || propertyChanged;
469 if (propertyChanged) {
470 log.println("propertyChangeListener was called after removing"+
471 " for '"+propertyName+"'");
474 tRes.tested("removePropertyChangeListener()",!error);
477 return;
479 } // end of removePropertyChangeListener()
483 * Tests <code>removeVetoableChangeListener</code> method.
484 * Removes vetoable listener, then changes constrained property value
485 * and checks if the listener was NOT called.
486 * Method tests to be successfully completed before :
487 * <ul>
488 * <li> <code>addPropertyChangeListener</code> : here vetoable listener
489 * was added. </li>
490 * </ul> <p>
491 * Has OK status if NO constrained properties exist or if listener
492 * was NOT called and no exceptions arose.
494 public void _removeVetoableChangeListener() {
496 requiredMethod("addVetoableChangeListener()");
498 int count = PTT.constrained.size();
499 if ( count==0 || PTT.constrained.get(0).equals("none") ) {
500 log.println("*** No constrained properties found ***");
501 tRes.tested("removeVetoableChangeListener()", true) ;
502 } else {
504 //remove all listeners first
505 for (int i = 0; i < count; i++) {
506 String propertyName = PTT.constrained.get(i);
507 try {
508 oObj.removeVetoableChangeListener(propertyName,VClistener);
509 } catch (Exception e) {
510 log.println("Exception occurred while removing veto listener from"+
511 "property '"+ propertyName+"'");
512 e.printStackTrace(log);
516 boolean error = false;
517 for (int i = 0; i < count; i++) {
518 String propertyName = PTT.constrained.get(i);
519 vetoableChanged = false;
520 try {
521 oObj.addVetoableChangeListener(propertyName,VClistener);
522 oObj.removeVetoableChangeListener(propertyName,VClistener);
523 Object gValue = oObj.getPropertyValue(propertyName);
524 oObj.setPropertyValue(propertyName,
525 ValueChanger.changePValue(gValue));
526 } catch (com.sun.star.beans.PropertyVetoException e) {
527 log.println("Exception occurred while trying to change "+
528 "property '"+ propertyName+"'");
529 e.printStackTrace(log);
530 } catch (com.sun.star.lang.IllegalArgumentException e) {
531 log.println("Exception occurred while trying to change "+
532 "property '"+ propertyName+"'");
533 e.printStackTrace(log);
534 } catch (com.sun.star.beans.UnknownPropertyException e) {
535 log.println("Exception occurred while trying to change "+
536 "property '"+ propertyName+"'");
537 e.printStackTrace(log);
538 } catch (com.sun.star.lang.WrappedTargetException e) {
539 log.println("Exception occurred while trying to change "+
540 "property '"+ propertyName+"'");
541 e.printStackTrace(log);
542 } // end of try-catch
543 error = error || vetoableChanged;
544 if (vetoableChanged) {
545 log.println("vetoableChangeListener was called after "+
546 "removing for '"+propertyName+"'");
549 tRes.tested("removeVetoableChangeListener()",!error);
552 return;
554 } // end of removeVetoableChangeListener()
557 * Gets the properties being tested. Searches and stores by one
558 * property of each kind (Bound, Vetoable, Normal).
560 public void getPropsToTest(XPropertySetInfo xPSI) {
562 Property[] properties = xPSI.getProperties();
563 // some properties should not be changed in a unspecific way
564 String[] skip = {"PrinterName", "CharRelief", "IsLayerMode"};
566 for (int i = 0; i < properties.length; i++) {
568 Property property = properties[i];
569 String name = property.Name;
571 boolean cont = false;
572 for (int j = 0; j < skip.length; j++) {
573 if (name.equals(skip[j])){
574 log.println("skipping '" + name + "'");
575 cont = true;
579 if (cont) continue;
581 if (name.equals(oObj))
582 log.println("Checking '"+name+"'");
583 boolean isWritable = ((property.Attributes &
584 PropertyAttribute.READONLY) == 0);
585 boolean isNotNull = ((property.Attributes &
586 PropertyAttribute.MAYBEVOID) == 0);
587 boolean isBound = ((property.Attributes &
588 PropertyAttribute.BOUND) != 0);
589 boolean isConstr = ((property.Attributes &
590 PropertyAttribute.CONSTRAINED) != 0);
591 boolean canChange = false;
593 if ( !isWritable ) log.println("Property '"+name+"' is READONLY");
595 if (name.endsWith("URL")) isWritable = false;
596 if (name.startsWith("Fill")) isWritable = false;
597 if (name.startsWith("Font")) isWritable = false;
598 if (name.startsWith("IsNumbering")) isWritable = false;
599 if (name.startsWith("LayerName")) isWritable = false;
600 if (name.startsWith("Line")) isWritable = false;
601 if (name.startsWith("TextWriting")) isWritable = false;
603 //if (name.equals("xinterfaceA") || name.equals("xtypeproviderA")
604 //|| name.equals("arAnyA")) isWritable=false;
606 if ( isWritable && isNotNull ) canChange = isChangeable(name);
608 if ( isWritable && isNotNull && isBound && canChange) {
609 PTT.bound.add(name);
612 if ( isWritable && isNotNull && isConstr && canChange) {
613 PTT.constrained.add(name);
616 if ( isWritable && isNotNull && canChange) {
617 PTT.normal.add(name);
621 } // endfor
624 public boolean isChangeable(String name) {
625 boolean hasChanged = false;
626 try {
627 Object getProp = oObj.getPropertyValue(name);
628 log.println("Getting: "+getProp);
629 if (name.equals("xinterfaceA")) {
630 System.out.println("drin");
633 Object setValue = null;
634 if (getProp != null) {
635 if (!utils.isVoid(getProp))
636 setValue = ValueChanger.changePValue(getProp);
637 else log.println("Property '"+name+
638 "' is void but MAYBEVOID isn't set");
639 } else log.println("Property '"+name+"'is null and can't be changed");
640 if (name.equals("LineStyle")) setValue = null;
641 if (setValue != null) {
642 log.println("Setting to :"+setValue);
643 oObj.setPropertyValue(name, setValue);
644 hasChanged = (! getProp.equals(oObj.getPropertyValue(name)));
645 } else log.println("Couldn't change Property '"+name+"'");
646 } catch (com.sun.star.beans.PropertyVetoException e) {
647 log.println("'" + name + "' throws exception '" + e + "'");
648 e.printStackTrace(log);
649 } catch (com.sun.star.lang.IllegalArgumentException e) {
650 log.println("'" + name + "' throws exception '" + e + "'");
651 e.printStackTrace(log);
652 } catch (com.sun.star.beans.UnknownPropertyException e) {
653 log.println("'" + name + "' throws exception '" + e + "'");
654 e.printStackTrace(log);
655 } catch (com.sun.star.lang.WrappedTargetException e) {
656 log.println("'" + name + "' throws exception '" + e + "'");
657 e.printStackTrace(log);
658 } catch (com.sun.star.uno.RuntimeException e) {
659 log.println("'" + name + "' throws exception '" + e + "'");
660 e.printStackTrace(log);
661 } catch (java.lang.ArrayIndexOutOfBoundsException e) {
662 log.println("'" + name + "' throws exception '" + e + "'");
663 e.printStackTrace(log);
666 return hasChanged;
670 * Forces environment recreation.
672 protected void after() {
673 disposeEnvironment();
677 } // finish class _XPropertySet