1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.beans;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import lib.MultiMethodTest;
31*cdf0e10cSrcweir import lib.Status;
32*cdf0e10cSrcweir import lib.StatusException;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir import com.sun.star.beans.Property;
35*cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
36*cdf0e10cSrcweir import com.sun.star.beans.PropertyState;
37*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
38*cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
39*cdf0e10cSrcweir import com.sun.star.beans.XPropertyState;
40*cdf0e10cSrcweir import com.sun.star.uno.Any;
41*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir /**
45*cdf0e10cSrcweir * Testing <code>com.sun.star.beans.XPropertyState</code>
46*cdf0e10cSrcweir * interface methods :
47*cdf0e10cSrcweir * <ul>
48*cdf0e10cSrcweir *  <li><code> getPropertyState()</code></li>
49*cdf0e10cSrcweir *  <li><code> getPropertyStates()</code></li>
50*cdf0e10cSrcweir *  <li><code> setPropertyToDefault()</code></li>
51*cdf0e10cSrcweir *  <li><code> getPropertyDefault()</code></li>
52*cdf0e10cSrcweir * </ul>
53*cdf0e10cSrcweir * Test is <b> NOT </b> multithread compilant. <p>
54*cdf0e10cSrcweir * After test completion object environment has to be recreated. <p>
55*cdf0e10cSrcweir * <b>Note:</b> object tested must also implement
56*cdf0e10cSrcweir * <code>com.sun.star.beans.XPropertySet</code> interface.
57*cdf0e10cSrcweir * @see com.sun.star.beans.XPropertyState
58*cdf0e10cSrcweir */
59*cdf0e10cSrcweir public class _XPropertyState extends MultiMethodTest {
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir         public XPropertyState oObj = null;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir         private XPropertySet oPS = null ;
64*cdf0e10cSrcweir         private XPropertySetInfo propertySetInfo = null;
65*cdf0e10cSrcweir         private Property[] properties = null ;
66*cdf0e10cSrcweir         private String pName = null ;
67*cdf0e10cSrcweir         private Object propDef = null ;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir         /**
70*cdf0e10cSrcweir         * Queries object for <code>XPropertySet</code> interface and
71*cdf0e10cSrcweir         * initializes some fields used by all methods. <p>
72*cdf0e10cSrcweir         *
73*cdf0e10cSrcweir         * Searches property which is not READONLY and MAYBEDEFAULT, if
74*cdf0e10cSrcweir         * such property is not found, then uses property with only
75*cdf0e10cSrcweir         * READONLY attribute. This property name is stored and is used
76*cdf0e10cSrcweir         * by all tests.
77*cdf0e10cSrcweir         *
78*cdf0e10cSrcweir         * @throws StatusException If <code>XPropertySet</code> is not
79*cdf0e10cSrcweir         * implemented by object.
80*cdf0e10cSrcweir         */
81*cdf0e10cSrcweir         public void before() throws StatusException {
82*cdf0e10cSrcweir             oPS = (XPropertySet)
83*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XPropertySet.class, oObj );
84*cdf0e10cSrcweir             if (oPS == null)
85*cdf0e10cSrcweir                 throw new StatusException
86*cdf0e10cSrcweir                     ("XPropertySet interface isn't implemented.",
87*cdf0e10cSrcweir                         new NullPointerException
88*cdf0e10cSrcweir                             ("XPropertySet interface isn't implemented.")) ;
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir             propertySetInfo = oPS.getPropertySetInfo();
91*cdf0e10cSrcweir             properties = propertySetInfo.getProperties();
92*cdf0e10cSrcweir             Property prop = null;
93*cdf0e10cSrcweir             for (int i=0;i<properties.length;i++) {
94*cdf0e10cSrcweir                 try {
95*cdf0e10cSrcweir                     prop = propertySetInfo.getPropertyByName
96*cdf0e10cSrcweir                         (properties[i].Name);
97*cdf0e10cSrcweir                 } catch (com.sun.star.beans.UnknownPropertyException e) {
98*cdf0e10cSrcweir                     log.println("Unknown Property "+prop.Name);
99*cdf0e10cSrcweir                 }
100*cdf0e10cSrcweir                 boolean readOnly = (prop.Attributes &
101*cdf0e10cSrcweir                     PropertyAttribute.READONLY) != 0;
102*cdf0e10cSrcweir                 boolean maybeDefault = (prop.Attributes &
103*cdf0e10cSrcweir                     PropertyAttribute.MAYBEDEFAULT) != 0;
104*cdf0e10cSrcweir                 if (!readOnly && maybeDefault) {
105*cdf0e10cSrcweir                     pName = properties[i].Name;
106*cdf0e10cSrcweir                     log.println("Property '" + pName + "' has attributes "+
107*cdf0e10cSrcweir                          prop.Attributes);
108*cdf0e10cSrcweir                     break ;
109*cdf0e10cSrcweir                 } else
110*cdf0e10cSrcweir                 if (!readOnly) {
111*cdf0e10cSrcweir                     pName = properties[i].Name;
112*cdf0e10cSrcweir                     log.println("Property '" + pName +
113*cdf0e10cSrcweir                          "' is not readonly, may be used ...");
114*cdf0e10cSrcweir                 } else {
115*cdf0e10cSrcweir                     log.println("Skipping property '" + properties[i].Name +
116*cdf0e10cSrcweir                         "' Readonly: " + readOnly + ", MaybeDefault: " +
117*cdf0e10cSrcweir                              maybeDefault);
118*cdf0e10cSrcweir                 }
119*cdf0e10cSrcweir             }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         /**
124*cdf0e10cSrcweir         * Test calls the method and checks that no exceptions were thrown. <p>
125*cdf0e10cSrcweir         * Has <b> OK </b> status if no exceptions were thrown. <p>
126*cdf0e10cSrcweir         */
127*cdf0e10cSrcweir         public void _getPropertyDefault(){
128*cdf0e10cSrcweir             boolean result = true ;
129*cdf0e10cSrcweir             String localName = pName;
130*cdf0e10cSrcweir             if (localName == null) {
131*cdf0e10cSrcweir                 localName = (propertySetInfo.getProperties()[0]).Name;
132*cdf0e10cSrcweir             }
133*cdf0e10cSrcweir             try {
134*cdf0e10cSrcweir                 propDef = oObj.getPropertyDefault(localName);
135*cdf0e10cSrcweir                 log.println("Default property value is : '" + propDef + "'");
136*cdf0e10cSrcweir             } catch (com.sun.star.beans.UnknownPropertyException e) {
137*cdf0e10cSrcweir                 log.println("Exception " + e +
138*cdf0e10cSrcweir                     "occured while getting Property default");
139*cdf0e10cSrcweir                 result=false;
140*cdf0e10cSrcweir             } catch (com.sun.star.lang.WrappedTargetException e) {
141*cdf0e10cSrcweir                 log.println("Exception " + e +
142*cdf0e10cSrcweir                     "occured while getting Property default");
143*cdf0e10cSrcweir                 result=false;
144*cdf0e10cSrcweir             }
145*cdf0e10cSrcweir             tRes.tested("getPropertyDefault()", result);
146*cdf0e10cSrcweir         }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir         /**
149*cdf0e10cSrcweir         * Test calls the method and checks return value and that
150*cdf0e10cSrcweir         * no exceptions were thrown. <p>
151*cdf0e10cSrcweir         * Has <b> OK </b> status if the method returns not null value
152*cdf0e10cSrcweir         * and no exceptions were thrown. <p>
153*cdf0e10cSrcweir         */
154*cdf0e10cSrcweir         public void _getPropertyState(){
155*cdf0e10cSrcweir             boolean result = true ;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir             String localName = pName;
158*cdf0e10cSrcweir             if (localName == null) {
159*cdf0e10cSrcweir                 localName = (propertySetInfo.getProperties()[0]).Name;
160*cdf0e10cSrcweir             }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir             try {
163*cdf0e10cSrcweir                 PropertyState ps = oObj.getPropertyState(localName);
164*cdf0e10cSrcweir                 if (ps == null) {
165*cdf0e10cSrcweir                     log.println("!!! Returned value == null") ;
166*cdf0e10cSrcweir                     result = false ;
167*cdf0e10cSrcweir                 }
168*cdf0e10cSrcweir             } catch (com.sun.star.beans.UnknownPropertyException e) {
169*cdf0e10cSrcweir                 log.println("Exception " + e +
170*cdf0e10cSrcweir                     "occured while getting Property state");
171*cdf0e10cSrcweir                 result = false;
172*cdf0e10cSrcweir             }
173*cdf0e10cSrcweir             tRes.tested("getPropertyState()", result);
174*cdf0e10cSrcweir         }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         /**
177*cdf0e10cSrcweir         * Test calls the method with array of one property name
178*cdf0e10cSrcweir         * and checks return value and that no exceptions were thrown. <p>
179*cdf0e10cSrcweir         * Has <b> OK </b> status if the method returns array with one
180*cdf0e10cSrcweir         * PropertyState and no exceptions were thrown. <p>
181*cdf0e10cSrcweir         */
182*cdf0e10cSrcweir         public void _getPropertyStates(){
183*cdf0e10cSrcweir             boolean result = true ;
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir             String localName = pName;
186*cdf0e10cSrcweir             if (localName == null) {
187*cdf0e10cSrcweir                 localName = (propertySetInfo.getProperties()[0]).Name;
188*cdf0e10cSrcweir             }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir             try {
191*cdf0e10cSrcweir                 PropertyState[] ps = oObj.getPropertyStates
192*cdf0e10cSrcweir                     (new String[] {localName});
193*cdf0e10cSrcweir                 if (ps == null) {
194*cdf0e10cSrcweir                     log.println("!!! Returned value == null") ;
195*cdf0e10cSrcweir                     result = false ;
196*cdf0e10cSrcweir                 } else {
197*cdf0e10cSrcweir                     if (ps.length != 1) {
198*cdf0e10cSrcweir                         log.println("!!! Array lebgth returned is invalid - " +
199*cdf0e10cSrcweir                              ps.length) ;
200*cdf0e10cSrcweir                         result = false ;
201*cdf0e10cSrcweir                     }
202*cdf0e10cSrcweir                 }
203*cdf0e10cSrcweir             } catch (com.sun.star.beans.UnknownPropertyException e) {
204*cdf0e10cSrcweir                 log.println("Exception " + e +
205*cdf0e10cSrcweir                     "occured while getting Property state");
206*cdf0e10cSrcweir                 result = false;
207*cdf0e10cSrcweir             }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir             tRes.tested("getPropertyStates()", result);
210*cdf0e10cSrcweir         }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir         /**
214*cdf0e10cSrcweir         * Sets the property to default, then compares the current property
215*cdf0e10cSrcweir         * value to value received by method <code>getPropertyDefault</code>.
216*cdf0e10cSrcweir         * Has <b> OK </b> status if the current proeprty value equals to
217*cdf0e10cSrcweir         * default property. <p>
218*cdf0e10cSrcweir         * The following method tests are to be completed successfully before :
219*cdf0e10cSrcweir         * <ul>
220*cdf0e10cSrcweir         *  <li> <code>getPropertyDefault</code>: we have to know what is
221*cdf0e10cSrcweir         * default value</li></ul>
222*cdf0e10cSrcweir         */
223*cdf0e10cSrcweir         public void _setPropertyToDefault(){
224*cdf0e10cSrcweir             requiredMethod("getPropertyDefault()") ;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir             if (pName == null) {
227*cdf0e10cSrcweir                 log.println("all found properties are read only");
228*cdf0e10cSrcweir                 tRes.tested("setPropertyToDefault()",Status.skipped(true));
229*cdf0e10cSrcweir                 return;
230*cdf0e10cSrcweir             }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir             boolean result = true ;
233*cdf0e10cSrcweir             try {
234*cdf0e10cSrcweir                 try {
235*cdf0e10cSrcweir                     oObj.setPropertyToDefault(pName);
236*cdf0e10cSrcweir                 }
237*cdf0e10cSrcweir                 catch(RuntimeException e) {
238*cdf0e10cSrcweir                     System.out.println("Ignoring RuntimeException: " + e.getMessage());
239*cdf0e10cSrcweir                 }
240*cdf0e10cSrcweir                 if ((properties[0].Attributes &
241*cdf0e10cSrcweir                         PropertyAttribute.MAYBEDEFAULT) != 0) {
242*cdf0e10cSrcweir                     Object actualDef = propDef ;
243*cdf0e10cSrcweir                     if (propDef instanceof Any) {
244*cdf0e10cSrcweir                         actualDef = ((Any)propDef).getObject() ;
245*cdf0e10cSrcweir                     }
246*cdf0e10cSrcweir                     Object actualVal = oPS.getPropertyValue(pName) ;
247*cdf0e10cSrcweir                     if (actualVal instanceof Any) {
248*cdf0e10cSrcweir                         actualVal = ((Any)actualVal).getObject() ;
249*cdf0e10cSrcweir                     }
250*cdf0e10cSrcweir                     result = util.ValueComparer.equalValue
251*cdf0e10cSrcweir                         (actualDef,actualVal) ;
252*cdf0e10cSrcweir                     log.println("Default value = '" + actualDef +
253*cdf0e10cSrcweir                          "', returned value = '"
254*cdf0e10cSrcweir                         + actualVal + "' for property " + pName) ;
255*cdf0e10cSrcweir                 }
256*cdf0e10cSrcweir             } catch (com.sun.star.beans.UnknownPropertyException e) {
257*cdf0e10cSrcweir                 log.println("Exception " + e +
258*cdf0e10cSrcweir                     "occured while setting Property to default");
259*cdf0e10cSrcweir                 result=false;
260*cdf0e10cSrcweir             } catch (com.sun.star.lang.WrappedTargetException e) {
261*cdf0e10cSrcweir                 log.println("Exception " + e +
262*cdf0e10cSrcweir                     "occured while testing property value");
263*cdf0e10cSrcweir                 result=false;
264*cdf0e10cSrcweir             }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir             tRes.tested("setPropertyToDefault()", result);
267*cdf0e10cSrcweir         }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir         public void after() {
270*cdf0e10cSrcweir             disposeEnvironment() ;
271*cdf0e10cSrcweir         }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir  }// EOF _XPropertyState
274*cdf0e10cSrcweir 
275