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 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
);
139 } // end of getPropertySetInfo()
142 * Tests change listener which added for bound properties.
143 * Adds listener to bound property (if it exists), then changes
144 * its value and check if listener was called. <p>
145 * Method tests to be successfully completed before :
147 * <li> <code>getPropertySetInfo</code> : in this method test
148 * one of bound properties is retrieved. </li>
150 * Has OK status if NO bound properties exist or if listener
151 * was successfully called.
153 public void _addPropertyChangeListener() {
155 requiredMethod("getPropertySetInfo()");
157 int count
= PTT
.bound
.size();
158 if ( count
==0 || PTT
.bound
.get(0).equals("none") ) {
159 log
.println("*** No bound properties found ***");
160 tRes
.tested("addPropertyChangeListener()", true) ;
162 boolean error
= false;
163 for (int i
= 0; i
< count
; i
++) {
164 String propertyName
= PTT
.bound
.get(i
);
165 propertyChanged
= false;
167 oObj
.addPropertyChangeListener(propertyName
,PClistener
);
168 Object gValue
= oObj
.getPropertyValue(propertyName
);
169 log
.println("Check bound property: " + propertyName
);
170 oObj
.setPropertyValue(propertyName
,
171 ValueChanger
.changePValue(gValue
));
172 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
173 log
.println("Exception occurred while trying to change "+
174 "property '"+ propertyName
+"'");
175 e
.printStackTrace(log
);
176 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
177 log
.println("Exception occurred while trying to change "+
178 "property '"+ propertyName
+"'");
179 e
.printStackTrace(log
);
180 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
181 log
.println("Exception occurred while trying to change "+
182 "property '"+ propertyName
+"'");
183 e
.printStackTrace(log
);
184 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
185 log
.println("Exception occurred while trying to change "+
186 "property '"+ propertyName
+"'");
187 e
.printStackTrace(log
);
188 } // end of try-catch
189 error
= error
|| !propertyChanged
;
190 if (!propertyChanged
) {
191 log
.println("propertyChangeListener wasn't called for '"+
195 tRes
.tested("addPropertyChangeListener()", !error
);
200 } // end of addPropertyChangeListener()
203 * Tests vetoable listener which added for constrained properties.
204 * Adds listener to constrained property (if it exists), then changes
205 * its value and check if listener was called. <p>
206 * Method tests to be successfully completed before :
208 * <li> <code>getPropertySetInfo</code> : in this method test
209 * one of constrained properties is retrieved. </li>
211 * Has OK status if NO constrained properties exist or if listener
212 * was successfully called.
214 public void _addVetoableChangeListener() {
216 requiredMethod("getPropertySetInfo()");
218 int count
= PTT
.constrained
.size();
219 if ( count
==0 || PTT
.constrained
.get(0).equals("none") ) {
220 log
.println("*** No constrained properties found ***");
221 tRes
.tested("addVetoableChangeListener()", true) ;
223 boolean error
= false;
224 for (int i
= 0; i
< count
; i
++) {
225 String propertyName
= PTT
.constrained
.get(i
);
226 vetoableChanged
= false;
228 oObj
.addVetoableChangeListener(propertyName
,VClistener
);
229 Object gValue
= oObj
.getPropertyValue(propertyName
);
230 oObj
.setPropertyValue(propertyName
,
231 ValueChanger
.changePValue(gValue
));
232 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
233 log
.println("Exception occurred while trying to change "+
234 "property '"+ propertyName
+"'");
235 e
.printStackTrace(log
);
236 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
237 log
.println("Exception occurred while trying to change "+
238 "property '"+ propertyName
+"'");
239 e
.printStackTrace(log
);
240 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
241 log
.println("Exception occurred while trying to change "+
242 "property '"+ propertyName
+"'");
243 e
.printStackTrace(log
);
244 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
245 log
.println("Exception occurred while trying to change "+
246 "property '"+ propertyName
+"'");
247 e
.printStackTrace(log
);
248 } // end of try-catch
249 error
= error
|| !vetoableChanged
;
250 if (!vetoableChanged
) {
251 log
.println("vetoableChangeListener wasn't called for '"+
255 tRes
.tested("addVetoableChangeListener()",!error
);
260 } // end of addVetoableChangeListener()
264 * Tests <code>setPropertyValue</code> method.
265 * Stores value before call, and compares it with value after
267 * Method tests to be successfully completed before :
269 * <li> <code>getPropertySetInfo</code> : in this method test
270 * one of normal properties is retrieved. </li>
272 * Has OK status if NO normal properties exist or if value before
273 * method call is not equal to value after.
275 public void _setPropertyValue() {
277 requiredMethod("getPropertySetInfo()");
279 Object gValue
= null;
280 Object sValue
= null;
282 int count
= PTT
.normal
.size();
283 if ( count
==0 || PTT
.normal
.get(0).equals("none") ) {
284 log
.println("*** No changeable properties found ***");
285 tRes
.tested("setPropertyValue()", true) ;
287 boolean error
= false;
288 for (int i
= 0; i
< count
; i
++) {
289 String propertyName
= PTT
.normal
.get(i
);
291 log
.println("try to change value of property '" + propertyName
+ "'" );
292 gValue
= oObj
.getPropertyValue(propertyName
);
293 sValue
= ValueChanger
.changePValue(gValue
);
294 oObj
.setPropertyValue(propertyName
, sValue
);
295 sValue
= oObj
.getPropertyValue(propertyName
);
296 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
297 log
.println("Exception occurred while trying to change "+
298 "property '"+ propertyName
+"'");
299 e
.printStackTrace(log
);
300 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
301 log
.println("Exception occurred while trying to change "+
302 "property '"+ propertyName
+"'");
303 e
.printStackTrace(log
);
304 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
305 log
.println("Exception occurred while trying to change "+
306 "property '"+ propertyName
+"'");
307 e
.printStackTrace(log
);
308 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
309 log
.println("Exception occurred while trying to change "+
310 "property '"+ propertyName
+"'");
311 e
.printStackTrace(log
);
312 } // end of try-catch
313 /* this is stupid: we can't set properties whose semantics we
314 * don't know to random values in an arbitrary order and
315 * expect that to actually work.
316 if( gValue.equals(sValue) )
318 log.println("setting property '"+ propertyName+"' failed");
323 tRes
.tested("setPropertyValue()",!error
);
328 } // end of setPropertyValue()
331 * Tests <code>getPropertyValue</code> method for the given property.
332 * Returns true if no exceptions occurred
334 private boolean getSinglePropertyValue( String propertyName
)
336 boolean runOk
= false;
338 oObj
.getPropertyValue(propertyName
);
340 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
341 log
.println("Exception occurred while trying to get property '"+
343 e
.printStackTrace(log
);
344 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
345 log
.println("Exception occurred while trying to get property '"+
347 e
.printStackTrace(log
);
353 * Tests <code>getPropertyValue</code> method.
354 * Just call this method and checks for no exceptions <p>
355 * Method tests to be successfully completed before :
357 * <li> <code>getPropertySetInfo</code> : in this method test
358 * one of normal properties is retrieved. </li>
360 * Has OK status if NO normal properties exist or if no
361 * exceptions were thrown.
363 public void _getPropertyValue() {
365 requiredMethod("getPropertySetInfo()");
367 int count
= PTT
.normal
.size();
368 if ( count
==0 || PTT
.normal
.get(0).equals("none") ) {
369 Property
[] properties
= oObj
.getPropertySetInfo().getProperties();
370 if( properties
.length
> 0 ) {
371 String propertyName
= properties
[0].Name
;
372 log
.println("All properties are Read Only");
373 log
.println("Using: "+propertyName
);
374 tRes
.tested("getPropertyValue()", getSinglePropertyValue( propertyName
) );
377 log
.println("*** No properties found ***");
378 tRes
.tested("getPropertyValue()", true) ;
381 boolean error
= false;
382 for (int i
= 0; i
< count
; i
++) {
383 String propertyName
= PTT
.normal
.get(i
);
384 boolean runOk
= getSinglePropertyValue( propertyName
);
388 log
.println("getPropertyValue() failed for property '"+propertyName
+"'");
391 tRes
.tested("getPropertyValue()", !error
) ;
398 * Tests <code>removePropertyChangeListener</code> method.
399 * Removes change listener, then changes bound property value
400 * and checks if the listener was NOT called.
401 * Method tests to be successfully completed before :
403 * <li> <code>addPropertyChangeListener</code> : here listener
406 * Has OK status if NO bound properties exist or if listener
407 * was not called and no exceptions arose.
409 public void _removePropertyChangeListener() {
411 requiredMethod("addPropertyChangeListener()");
413 int count
= PTT
.bound
.size();
414 if ( count
==0 || PTT
.bound
.get(0).equals("none") ) {
415 log
.println("*** No bound properties found ***");
416 tRes
.tested("removePropertyChangeListener()", true) ;
419 //remove all listeners first
420 for (int i
= 0; i
< count
; i
++) {
421 String propertyName
= PTT
.bound
.get(i
);
423 oObj
.removePropertyChangeListener(propertyName
,PClistener
);
424 } catch (Exception e
) {
425 log
.println("Exception occurred while removing change listener from"+
426 "property '"+ propertyName
+"'");
427 e
.printStackTrace(log
);
431 boolean error
= false;
432 for (int i
= 0; i
< count
; i
++) {
433 String propertyName
= PTT
.bound
.get(i
);
435 propertyChanged
= false;
436 oObj
.addPropertyChangeListener(propertyName
,PClistener
);
437 oObj
.removePropertyChangeListener(propertyName
,PClistener
);
438 Object gValue
= oObj
.getPropertyValue(propertyName
);
439 oObj
.setPropertyValue(propertyName
,
440 ValueChanger
.changePValue(gValue
));
441 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
442 log
.println("Exception occurred while trying to change "+
443 "property '"+ propertyName
+"'");
444 e
.printStackTrace(log
);
445 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
446 log
.println("Exception occurred while trying to change "+
447 "property '"+ propertyName
+"'");
448 e
.printStackTrace(log
);
449 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
450 log
.println("Exception occurred while trying to change "+
451 "property '"+ propertyName
+"'");
452 e
.printStackTrace(log
);
453 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
454 log
.println("Exception occurred while trying to change "+
455 "property '"+ propertyName
+"'");
456 e
.printStackTrace(log
);
457 } // end of try-catch
459 error
= error
|| propertyChanged
;
460 if (propertyChanged
) {
461 log
.println("propertyChangeListener was called after removing"+
462 " for '"+propertyName
+"'");
465 tRes
.tested("removePropertyChangeListener()",!error
);
470 } // end of removePropertyChangeListener()
474 * Tests <code>removeVetoableChangeListener</code> method.
475 * Removes vetoable listener, then changes constrained property value
476 * and checks if the listener was NOT called.
477 * Method tests to be successfully completed before :
479 * <li> <code>addPropertyChangeListener</code> : here vetoable listener
482 * Has OK status if NO constrained properties exist or if listener
483 * was NOT called and no exceptions arose.
485 public void _removeVetoableChangeListener() {
487 requiredMethod("addVetoableChangeListener()");
489 int count
= PTT
.constrained
.size();
490 if ( count
==0 || PTT
.constrained
.get(0).equals("none") ) {
491 log
.println("*** No constrained properties found ***");
492 tRes
.tested("removeVetoableChangeListener()", true) ;
495 //remove all listeners first
496 for (int i
= 0; i
< count
; i
++) {
497 String propertyName
= PTT
.constrained
.get(i
);
499 oObj
.removeVetoableChangeListener(propertyName
,VClistener
);
500 } catch (Exception e
) {
501 log
.println("Exception occurred while removing veto listener from"+
502 "property '"+ propertyName
+"'");
503 e
.printStackTrace(log
);
507 boolean error
= false;
508 for (int i
= 0; i
< count
; i
++) {
509 String propertyName
= PTT
.constrained
.get(i
);
510 vetoableChanged
= false;
512 oObj
.addVetoableChangeListener(propertyName
,VClistener
);
513 oObj
.removeVetoableChangeListener(propertyName
,VClistener
);
514 Object gValue
= oObj
.getPropertyValue(propertyName
);
515 oObj
.setPropertyValue(propertyName
,
516 ValueChanger
.changePValue(gValue
));
517 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
518 log
.println("Exception occurred while trying to change "+
519 "property '"+ propertyName
+"'");
520 e
.printStackTrace(log
);
521 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
522 log
.println("Exception occurred while trying to change "+
523 "property '"+ propertyName
+"'");
524 e
.printStackTrace(log
);
525 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
526 log
.println("Exception occurred while trying to change "+
527 "property '"+ propertyName
+"'");
528 e
.printStackTrace(log
);
529 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
530 log
.println("Exception occurred while trying to change "+
531 "property '"+ propertyName
+"'");
532 e
.printStackTrace(log
);
533 } // end of try-catch
534 error
= error
|| vetoableChanged
;
535 if (vetoableChanged
) {
536 log
.println("vetoableChangeListener was called after "+
537 "removing for '"+propertyName
+"'");
540 tRes
.tested("removeVetoableChangeListener()",!error
);
545 } // end of removeVetoableChangeListener()
548 * Gets the properties being tested. Searches and stores by one
549 * property of each kind (Bound, Vetoable, Normal).
551 public void getPropsToTest(XPropertySetInfo xPSI
) {
553 Property
[] properties
= xPSI
.getProperties();
554 // some properties should not be changed in a unspecific way
555 String
[] skip
= {"PrinterName", "CharRelief", "IsLayerMode"};
557 for (int i
= 0; i
< properties
.length
; i
++) {
559 Property property
= properties
[i
];
560 String name
= property
.Name
;
562 boolean cont
= false;
563 for (int j
= 0; j
< skip
.length
; j
++) {
564 if (name
.equals(skip
[j
])){
565 log
.println("skipping '" + name
+ "'");
572 if (name
.equals(oObj
))
573 log
.println("Checking '"+name
+"'");
574 boolean isWritable
= ((property
.Attributes
&
575 PropertyAttribute
.READONLY
) == 0);
576 boolean isNotNull
= ((property
.Attributes
&
577 PropertyAttribute
.MAYBEVOID
) == 0);
578 boolean isBound
= ((property
.Attributes
&
579 PropertyAttribute
.BOUND
) != 0);
580 boolean isConstr
= ((property
.Attributes
&
581 PropertyAttribute
.CONSTRAINED
) != 0);
582 boolean canChange
= false;
584 if ( !isWritable
) log
.println("Property '"+name
+"' is READONLY");
586 if (name
.endsWith("URL")) isWritable
= false;
587 if (name
.startsWith("Fill")) isWritable
= false;
588 if (name
.startsWith("Font")) isWritable
= false;
589 if (name
.startsWith("IsNumbering")) isWritable
= false;
590 if (name
.startsWith("LayerName")) isWritable
= false;
591 if (name
.startsWith("Line")) isWritable
= false;
592 if (name
.startsWith("TextWriting")) isWritable
= false;
594 //if (name.equals("xinterfaceA") || name.equals("xtypeproviderA")
595 //|| name.equals("arAnyA")) isWritable=false;
597 if ( isWritable
&& isNotNull
) canChange
= isChangeable(name
);
599 if ( isWritable
&& isNotNull
&& isBound
&& canChange
) {
603 if ( isWritable
&& isNotNull
&& isConstr
&& canChange
) {
604 PTT
.constrained
.add(name
);
607 if ( isWritable
&& isNotNull
&& canChange
) {
608 PTT
.normal
.add(name
);
615 public boolean isChangeable(String name
) {
616 boolean hasChanged
= false;
618 Object getProp
= oObj
.getPropertyValue(name
);
619 log
.println("Getting: "+getProp
);
620 if (name
.equals("xinterfaceA")) {
621 System
.out
.println("drin");
624 Object setValue
= null;
625 if (getProp
!= null) {
626 if (!utils
.isVoid(getProp
))
627 setValue
= ValueChanger
.changePValue(getProp
);
628 else log
.println("Property '"+name
+
629 "' is void but MAYBEVOID isn't set");
630 } else log
.println("Property '"+name
+"'is null and can't be changed");
631 if (name
.equals("LineStyle")) setValue
= null;
632 if (setValue
!= null) {
633 log
.println("Setting to :"+setValue
);
634 oObj
.setPropertyValue(name
, setValue
);
635 hasChanged
= (! getProp
.equals(oObj
.getPropertyValue(name
)));
636 } else log
.println("Couldn't change Property '"+name
+"'");
637 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
638 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
639 e
.printStackTrace(log
);
640 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
641 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
642 e
.printStackTrace(log
);
643 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
644 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
645 e
.printStackTrace(log
);
646 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
647 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
648 e
.printStackTrace(log
);
649 } catch (com
.sun
.star
.uno
.RuntimeException e
) {
650 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
651 e
.printStackTrace(log
);
652 } catch (java
.lang
.ArrayIndexOutOfBoundsException e
) {
653 log
.println("'" + name
+ "' throws exception '" + e
+ "'");
654 e
.printStackTrace(log
);
661 * Forces environment recreation.
663 protected void after() {
664 disposeEnvironment();
668 } // finish class _XPropertySet