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