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 lib
.MultiMethodTest
;
23 import lib
.StatusException
;
25 import com
.sun
.star
.beans
.Property
;
26 import com
.sun
.star
.beans
.PropertyAttribute
;
27 import com
.sun
.star
.beans
.PropertyState
;
28 import com
.sun
.star
.beans
.XPropertySet
;
29 import com
.sun
.star
.beans
.XPropertySetInfo
;
30 import com
.sun
.star
.beans
.XPropertyState
;
31 import com
.sun
.star
.uno
.Any
;
32 import com
.sun
.star
.uno
.UnoRuntime
;
36 * Testing <code>com.sun.star.beans.XPropertyState</code>
39 * <li><code> getPropertyState()</code></li>
40 * <li><code> getPropertyStates()</code></li>
41 * <li><code> setPropertyToDefault()</code></li>
42 * <li><code> getPropertyDefault()</code></li>
44 * Test is <b> NOT </b> multithread compilant. <p>
45 * After test completion object environment has to be recreated. <p>
46 * <b>Note:</b> object tested must also implement
47 * <code>com.sun.star.beans.XPropertySet</code> interface.
48 * @see com.sun.star.beans.XPropertyState
50 public class _XPropertyState
extends MultiMethodTest
{
52 public XPropertyState oObj
= null;
54 private XPropertySet oPS
= null ;
55 private XPropertySetInfo propertySetInfo
= null;
56 private Property
[] properties
= null ;
57 private String pName
= null ;
58 private Object propDef
= null ;
61 * Queries object for <code>XPropertySet</code> interface and
62 * initializes some fields used by all methods. <p>
64 * Searches property which is not READONLY and MAYBEDEFAULT, if
65 * such property is not found, then uses property with only
66 * READONLY attribute. This property name is stored and is used
69 * @throws StatusException If <code>XPropertySet</code> is not
70 * implemented by object.
72 public void before() throws StatusException
{
73 oPS
= UnoRuntime
.queryInterface( XPropertySet
.class, oObj
);
75 throw new StatusException
76 ("XPropertySet interface isn't implemented.",
77 new NullPointerException
78 ("XPropertySet interface isn't implemented.")) ;
80 propertySetInfo
= oPS
.getPropertySetInfo();
81 properties
= propertySetInfo
.getProperties();
83 for (int i
=0;i
<properties
.length
;i
++) {
85 prop
= propertySetInfo
.getPropertyByName
87 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
88 log
.println("Unknown Property "+prop
.Name
);
90 boolean readOnly
= (prop
.Attributes
&
91 PropertyAttribute
.READONLY
) != 0;
92 boolean maybeDefault
= (prop
.Attributes
&
93 PropertyAttribute
.MAYBEDEFAULT
) != 0;
94 if (!readOnly
&& maybeDefault
) {
95 pName
= properties
[i
].Name
;
96 log
.println("Property '" + pName
+ "' has attributes "+
101 pName
= properties
[i
].Name
;
102 log
.println("Property '" + pName
+
103 "' is not readonly, may be used ...");
105 log
.println("Skipping property '" + properties
[i
].Name
+
106 "' Readonly: " + readOnly
+ ", MaybeDefault: " +
114 * Test calls the method and checks that no exceptions were thrown. <p>
115 * Has <b> OK </b> status if no exceptions were thrown. <p>
117 public void _getPropertyDefault(){
118 boolean result
= true ;
119 String localName
= pName
;
120 if (localName
== null) {
121 localName
= (propertySetInfo
.getProperties()[0]).Name
;
124 propDef
= oObj
.getPropertyDefault(localName
);
125 log
.println("Default property value is : '" + propDef
+ "'");
126 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
127 log
.println("Exception " + e
+
128 "occurred while getting Property default");
130 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
131 log
.println("Exception " + e
+
132 "occurred while getting Property default");
135 tRes
.tested("getPropertyDefault()", result
);
139 * Test calls the method and checks return value and that
140 * no exceptions were thrown. <p>
141 * Has <b> OK </b> status if the method returns not null value
142 * and no exceptions were thrown. <p>
144 public void _getPropertyState(){
145 boolean result
= true ;
147 String localName
= pName
;
148 if (localName
== null) {
149 localName
= (propertySetInfo
.getProperties()[0]).Name
;
153 PropertyState ps
= oObj
.getPropertyState(localName
);
155 log
.println("!!! Returned value == null") ;
158 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
159 log
.println("Exception " + e
+
160 "occurred while getting Property state");
163 tRes
.tested("getPropertyState()", result
);
167 * Test calls the method with array of one property name
168 * and checks return value and that no exceptions were thrown. <p>
169 * Has <b> OK </b> status if the method returns array with one
170 * PropertyState and no exceptions were thrown. <p>
172 public void _getPropertyStates(){
173 boolean result
= true ;
175 String localName
= pName
;
176 if (localName
== null) {
177 localName
= (propertySetInfo
.getProperties()[0]).Name
;
181 PropertyState
[] ps
= oObj
.getPropertyStates
182 (new String
[] {localName
});
184 log
.println("!!! Returned value == null") ;
187 if (ps
.length
!= 1) {
188 log
.println("!!! Array lebgth returned is invalid - " +
193 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
194 log
.println("Exception " + e
+
195 "occurred while getting Property state");
199 tRes
.tested("getPropertyStates()", result
);
204 * Sets the property to default, then compares the current property
205 * value to value received by method <code>getPropertyDefault</code>.
206 * Has <b> OK </b> status if the current proeprty value equals to
207 * default property. <p>
208 * The following method tests are to be completed successfully before :
210 * <li> <code>getPropertyDefault</code>: we have to know what is
211 * default value</li></ul>
213 public void _setPropertyToDefault(){
214 requiredMethod("getPropertyDefault()") ;
217 log
.println("all found properties are read only");
218 tRes
.tested("setPropertyToDefault()",Status
.skipped(true));
222 boolean result
= true ;
225 oObj
.setPropertyToDefault(pName
);
227 catch(RuntimeException e
) {
228 System
.out
.println("Ignoring RuntimeException: " + e
.getMessage());
230 if ((properties
[0].Attributes
&
231 PropertyAttribute
.MAYBEDEFAULT
) != 0) {
232 Object actualDef
= propDef
;
233 if (propDef
instanceof Any
) {
234 actualDef
= ((Any
)propDef
).getObject() ;
236 Object actualVal
= oPS
.getPropertyValue(pName
) ;
237 if (actualVal
instanceof Any
) {
238 actualVal
= ((Any
)actualVal
).getObject() ;
240 result
= util
.ValueComparer
.equalValue
241 (actualDef
,actualVal
) ;
242 log
.println("Default value = '" + actualDef
+
243 "', returned value = '"
244 + actualVal
+ "' for property " + pName
) ;
246 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
247 log
.println("Exception " + e
+
248 "occurred while setting Property to default");
250 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
251 log
.println("Exception " + e
+
252 "occurred while testing property value");
256 tRes
.tested("setPropertyToDefault()", result
);
259 public void after() {
260 disposeEnvironment() ;
263 }// EOF _XPropertyState