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