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 .
21 import java
.util
.ArrayList
;
23 import lib
.MultiMethodTest
;
24 import util
.ValueChanger
;
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
;
37 * Testing <code>com.sun.star.beans.XPropertySet</code>
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>
48 * @see com.sun.star.beans.XPropertySet
50 public class _XPropertySet
extends MultiMethodTest
{
52 public XPropertySet oObj
= null;
55 * Flag that indicates change listener was called.
57 private boolean propertyChanged
= false;
60 * Listener that must be called on bound property changing.
62 public class MyChangeListener
implements XPropertyChangeListener
{
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();
75 * Flag that indicates veto listener was called.
77 private boolean vetoableChanged
= false;
80 * Listener that must be called on constrained property changing.
82 public class MyVetoListener
implements XVetoableChangeListener
{
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();
95 * Structure that collects the properties of different types to test :
96 * Constrained, Bound and Normal.
98 private static 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");
128 PTT
.constrained
.clear();
129 PTT
.normal
.add( ptt
[0] );
130 PTT
.bound
.add( ptt
[1] );
131 PTT
.constrained
.add( ptt
[2] );
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 :
144 * <li> <code>getPropertySetInfo</code> : in this method test
145 * one of bound properties is retrieved. </li>
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) ;
159 boolean error
= false;
160 for (int i
= 0; i
< count
; i
++) {
161 String propertyName
= PTT
.bound
.get(i
);
162 propertyChanged
= false;
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 '"+
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 :
202 * <li> <code>getPropertySetInfo</code> : in this method test
203 * one of constrained properties is retrieved. </li>
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) ;
217 boolean error
= false;
218 for (int i
= 0; i
< count
; i
++) {
219 String propertyName
= PTT
.constrained
.get(i
);
220 vetoableChanged
= false;
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 '"+
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
258 * Method tests to be successfully completed before :
260 * <li> <code>getPropertySetInfo</code> : in this method test
261 * one of normal properties is retrieved. </li>
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) ;
278 boolean error
= false;
279 for (int i
= 0; i
< count
; i
++) {
280 String propertyName
= PTT
.normal
.get(i
);
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");
314 tRes
.tested("setPropertyValue()",!error
);
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;
326 oObj
.getPropertyValue(propertyName
);
328 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
329 log
.println("Exception occurred while trying to get property '"+
331 e
.printStackTrace(log
);
332 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
333 log
.println("Exception occurred while trying to get property '"+
335 e
.printStackTrace(log
);
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 :
345 * <li> <code>getPropertySetInfo</code> : in this method test
346 * one of normal properties is retrieved. </li>
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
) );
365 log
.println("*** No properties found ***");
366 tRes
.tested("getPropertyValue()", true) ;
369 boolean error
= false;
370 for (int i
= 0; i
< count
; i
++) {
371 String propertyName
= PTT
.normal
.get(i
);
372 boolean runOk
= getSinglePropertyValue( propertyName
);
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 :
389 * <li> <code>addPropertyChangeListener</code> : here listener
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) ;
405 //remove all listeners first
406 for (int i
= 0; i
< count
; i
++) {
407 String propertyName
= PTT
.bound
.get(i
);
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
);
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 :
462 * <li> <code>addPropertyChangeListener</code> : here vetoable listener
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) ;
478 //remove all listeners first
479 for (int i
= 0; i
< count
; i
++) {
480 String propertyName
= PTT
.constrained
.get(i
);
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;
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
+ "'");
552 boolean isWritable
= ((property
.Attributes
&
553 PropertyAttribute
.READONLY
) == 0);
554 boolean isNotNull
= ((property
.Attributes
&
555 PropertyAttribute
.MAYBEVOID
) == 0);
556 boolean isBound
= ((property
.Attributes
&
557 PropertyAttribute
.BOUND
) != 0);
558 boolean isConstr
= ((property
.Attributes
&
559 PropertyAttribute
.CONSTRAINED
) != 0);
560 boolean canChange
= false;
562 if ( !isWritable
) log
.println("Property '"+name
+"' is READONLY");
564 if (name
.endsWith("URL")) isWritable
= false;
565 if (name
.startsWith("Fill")) isWritable
= false;
566 if (name
.startsWith("Font")) isWritable
= false;
567 if (name
.startsWith("IsNumbering")) isWritable
= false;
568 if (name
.startsWith("LayerName")) isWritable
= false;
569 if (name
.startsWith("Line")) isWritable
= false;
570 if (name
.startsWith("TextWriting")) isWritable
= false;
572 if ( isWritable
&& isNotNull
) canChange
= isChangeable(name
);
574 if ( isWritable
&& isNotNull
&& isBound
&& canChange
) {
578 if ( isWritable
&& isNotNull
&& isConstr
&& canChange
) {
579 PTT
.constrained
.add(name
);
582 if ( isWritable
&& isNotNull
&& canChange
) {
583 PTT
.normal
.add(name
);
590 public boolean isChangeable(String name
) {
591 boolean hasChanged
= false;
593 Object getProp
= oObj
.getPropertyValue(name
);
594 log
.println("Getting: "+getProp
);
595 if (name
.equals("xinterfaceA")) {
596 System
.out
.println("drin");
599 Object setValue
= null;
600 if (getProp
!= null) {
601 if (!utils
.isVoid(getProp
))
602 setValue
= ValueChanger
.changePValue(getProp
);
603 else log
.println("Property '"+name
+
604 "' is void but MAYBEVOID isn't set");
605 } else log
.println("Property '"+name
+"'is null and can't be changed");
606 if (name
.equals("LineStyle")) setValue
= null;
607 if (setValue
!= null) {
608 log
.println("Setting to :"+setValue
);
609 oObj
.setPropertyValue(name
, setValue
);
610 hasChanged
= (! getProp
.equals(oObj
.getPropertyValue(name
)));
611 } else log
.println("Couldn't change Property '"+name
+"'");
612 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
613 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
614 e
.printStackTrace(log
);
615 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
616 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
617 e
.printStackTrace(log
);
618 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
619 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
620 e
.printStackTrace(log
);
621 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
622 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
623 e
.printStackTrace(log
);
624 } catch (com
.sun
.star
.uno
.RuntimeException e
) {
625 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
626 e
.printStackTrace(log
);
627 } catch (java
.lang
.ArrayIndexOutOfBoundsException e
) {
628 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
629 e
.printStackTrace(log
);
636 * Forces environment recreation.
639 protected void after() {
640 disposeEnvironment();
644 } // finish class _XPropertySet