1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew 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.XMultiPropertyStates;
34cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
35cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
36cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /**
39cdf0e10cSrcweir * Testing <code>com.sun.star.beans.XMultiPropertyStates</code>
40cdf0e10cSrcweir * interface methods :
41cdf0e10cSrcweir * <ul>
42cdf0e10cSrcweir *  <li><code> getPropertyStates()</code></li>
43cdf0e10cSrcweir *  <li><code> setAllPropertiesToDefault()</code></li>
44cdf0e10cSrcweir *  <li><code> getPropertyValues()</code></li>
45cdf0e10cSrcweir *  <li><code> setPropertiesToDefault()</code></li>
46cdf0e10cSrcweir *  <li><code> getPropertyDefaults()</code></li>
47cdf0e10cSrcweir * </ul>
48cdf0e10cSrcweir * @see com.sun.star.beans.XMultiPropertyStates
49cdf0e10cSrcweir */
50cdf0e10cSrcweir public class _XMultiPropertyStates extends MultiMethodTest {
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     public XMultiPropertyStates oObj = null;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     private PropertyState[] states = null;
55cdf0e10cSrcweir     private String[] names = null;
56cdf0e10cSrcweir 
before()57cdf0e10cSrcweir     public void before() {
58cdf0e10cSrcweir         names = (String[]) tEnv.getObjRelation("PropertyNames");
59cdf0e10cSrcweir         if (names == null) {
60cdf0e10cSrcweir             throw new StatusException(Status.failed("No PropertyNames given"));
61cdf0e10cSrcweir         }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir         log.println("Totally " + names.length + " properties encountered:");
64cdf0e10cSrcweir         log.print("{");
65cdf0e10cSrcweir         for (int i = 0; i < names.length; i++)
66cdf0e10cSrcweir             log.print(names[i] + " ");
67cdf0e10cSrcweir         log.print("}");
68cdf0e10cSrcweir         log.println("");
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     /**
73cdf0e10cSrcweir     * Test calls the method and checks return value.
74cdf0e10cSrcweir     * <code>PropertyDefaults</code> are stored<p>
75cdf0e10cSrcweir     * Has <b> OK </b> status if the method returns not null value
76cdf0e10cSrcweir     * and no exceptions were thrown. <p>
77cdf0e10cSrcweir     */
_getPropertyDefaults()78cdf0e10cSrcweir     public void _getPropertyDefaults() {
79cdf0e10cSrcweir         boolean result = false;
80cdf0e10cSrcweir         try {
81cdf0e10cSrcweir             Object[] defaults = oObj.getPropertyDefaults(names);
82cdf0e10cSrcweir             result = (defaults != null) && defaults.length == names.length;
83cdf0e10cSrcweir             log.println("Number of default values: " + defaults.length);
84cdf0e10cSrcweir         } catch (com.sun.star.beans.UnknownPropertyException e) {
85cdf0e10cSrcweir             log.println("some properties seem to be unknown: " + e.toString());
86cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
87cdf0e10cSrcweir             log.println("Wrapped target Exception was thrown: " + e.toString());
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir         tRes.tested("getPropertyDefaults()", result) ;
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     /**
93cdf0e10cSrcweir     * Test calls the method and checks return value.
94cdf0e10cSrcweir     * Has <b> OK </b> status if the method returns not null value
95cdf0e10cSrcweir     * and no exceptions were thrown. <p>
96cdf0e10cSrcweir     */
_getPropertyStates()97cdf0e10cSrcweir     public void _getPropertyStates() {
98cdf0e10cSrcweir         boolean result = false;
99cdf0e10cSrcweir         try {
100cdf0e10cSrcweir             states = oObj.getPropertyStates(names);
101cdf0e10cSrcweir             result = (states != null) && (states.length == names.length);
102cdf0e10cSrcweir             log.println("Number of states: " + states.length);
103cdf0e10cSrcweir         } catch (com.sun.star.beans.UnknownPropertyException e) {
104cdf0e10cSrcweir             log.println("some properties seem to be unknown: " + e.toString());
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir         tRes.tested("getPropertyStates()", result) ;
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     /**
110cdf0e10cSrcweir     * Test calls the method and checks return value.
111cdf0e10cSrcweir     * Has <b> OK </b> status if the Property
112cdf0e10cSrcweir     * has default state afterwards. <p>
113cdf0e10cSrcweir     */
_setPropertiesToDefault()114cdf0e10cSrcweir     public void _setPropertiesToDefault() {
115cdf0e10cSrcweir         requiredMethod("getPropertyStates()");
116cdf0e10cSrcweir         // searching for property which currently don't have default value
117cdf0e10cSrcweir         // and preferable has MAYBEDEFAULT attr
118cdf0e10cSrcweir         // if no such properties are found then the first one is selected
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         String ro = (String) tEnv.getObjRelation("allReadOnly");
121cdf0e10cSrcweir         if (ro != null) {
122cdf0e10cSrcweir             log.println(ro);
123cdf0e10cSrcweir             tRes.tested("setPropertiesToDefault()",Status.skipped(true));
124cdf0e10cSrcweir             return;
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         boolean mayBeDef = false;
128cdf0e10cSrcweir         String propName = names[0];
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         for(int i = 0; i < names.length; i++) {
131cdf0e10cSrcweir             if (!mayBeDef && states[i] != PropertyState.DEFAULT_VALUE ) {
132cdf0e10cSrcweir                 propName = names[i];
133cdf0e10cSrcweir                 XPropertySet xPropSet = (XPropertySet)
134cdf0e10cSrcweir                     UnoRuntime.queryInterface(XPropertySet.class, oObj);
135cdf0e10cSrcweir                 XPropertySetInfo xPropSetInfo = xPropSet.getPropertySetInfo();
136cdf0e10cSrcweir                 Property prop = null;
137cdf0e10cSrcweir                 try {
138cdf0e10cSrcweir                     prop = xPropSetInfo.getPropertyByName(names[i]);
139cdf0e10cSrcweir                 }
140cdf0e10cSrcweir                 catch(com.sun.star.beans.UnknownPropertyException e) {
141cdf0e10cSrcweir                     log.println("couldn't get property info: " + e.toString());
142cdf0e10cSrcweir                     throw new StatusException(Status.failed
143cdf0e10cSrcweir                         ("couldn't get property info"));
144cdf0e10cSrcweir                 }
145cdf0e10cSrcweir                 if ( (prop.Attributes & PropertyAttribute.MAYBEDEFAULT) != 0){
146cdf0e10cSrcweir                     log.println("Property " + names[i] +
147cdf0e10cSrcweir                         " 'may be default' and doesn't have default value");
148cdf0e10cSrcweir                     mayBeDef = true;
149cdf0e10cSrcweir                 }
150cdf0e10cSrcweir             }
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir         log.println("The property " + propName + " selected");
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         boolean result = false;
155cdf0e10cSrcweir         try {
156cdf0e10cSrcweir             String[] the_first = new String[1];
157cdf0e10cSrcweir             the_first[0] = propName;
158cdf0e10cSrcweir             log.println("Setting " + propName + " to default");
159cdf0e10cSrcweir             oObj.setPropertiesToDefault(the_first);
160cdf0e10cSrcweir             result = (oObj.getPropertyStates(the_first)[0].equals
161cdf0e10cSrcweir                 (PropertyState.DEFAULT_VALUE));
162cdf0e10cSrcweir         } catch (com.sun.star.beans.UnknownPropertyException e) {
163cdf0e10cSrcweir             log.println("some properties seem to be unknown: " + e.toString());
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         if (!result) {
167cdf0e10cSrcweir             log.println("The property didn't change its state to default ...");
168cdf0e10cSrcweir             if (mayBeDef) {
169cdf0e10cSrcweir                 log.println("   ... and it may be default - FAILED");
170cdf0e10cSrcweir             } else {
171cdf0e10cSrcweir                 log.println("   ... but it may not be default - OK");
172cdf0e10cSrcweir                 result = true;
173cdf0e10cSrcweir             }
174cdf0e10cSrcweir         }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         tRes.tested("setPropertiesToDefault()", result) ;
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     /**
180cdf0e10cSrcweir     * Test calls the method and checks return value.
181cdf0e10cSrcweir     * Has <b> OK </b> status if the all Properties
182cdf0e10cSrcweir     * have default state afterwards. <p>
183cdf0e10cSrcweir     */
_setAllPropertiesToDefault()184cdf0e10cSrcweir     public void _setAllPropertiesToDefault() {
185cdf0e10cSrcweir         requiredMethod("setPropertiesToDefault()");
186cdf0e10cSrcweir         boolean result = true;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir        try {
189cdf0e10cSrcweir             oObj.setAllPropertiesToDefault();
190cdf0e10cSrcweir        } catch(RuntimeException e) {
191cdf0e10cSrcweir            log.println("Ignore Runtime Exception: " + e.getMessage());
192cdf0e10cSrcweir        }
193cdf0e10cSrcweir         log.println("Checking that all properties are now in DEFAULT state" +
194cdf0e10cSrcweir             " excepting may be those which 'cann't be default'");
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         try {
197cdf0e10cSrcweir             states = oObj.getPropertyStates(names);
198cdf0e10cSrcweir             for (int i = 0; i < states.length; i++) {
199cdf0e10cSrcweir                 boolean part_result = states[i].equals
200cdf0e10cSrcweir                     (PropertyState.DEFAULT_VALUE);
201cdf0e10cSrcweir                 if (!part_result) {
202cdf0e10cSrcweir                     log.println("Property '" + names[i] +
203cdf0e10cSrcweir                         "' wasn't set to default");
204cdf0e10cSrcweir                     XPropertySet xPropSet = (XPropertySet)
205cdf0e10cSrcweir                         UnoRuntime.queryInterface(XPropertySet.class, oObj);
206cdf0e10cSrcweir                     XPropertySetInfo xPropSetInfo =
207cdf0e10cSrcweir                         xPropSet.getPropertySetInfo();
208cdf0e10cSrcweir                     Property prop = xPropSetInfo.getPropertyByName(names[i]);
209cdf0e10cSrcweir                     if ( (prop.Attributes &
210cdf0e10cSrcweir                             PropertyAttribute.MAYBEDEFAULT) != 0 ) {
211cdf0e10cSrcweir                         log.println("   ... and it has MAYBEDEFAULT "+
212cdf0e10cSrcweir                             "attribute - FAILED");
213cdf0e10cSrcweir                     } else {
214cdf0e10cSrcweir                         log.println("   ... but it has no MAYBEDEFAULT "+
215cdf0e10cSrcweir                             "attribute - OK");
216cdf0e10cSrcweir                         part_result = true;
217cdf0e10cSrcweir                     }
218cdf0e10cSrcweir                 }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir                 result &= part_result;
221cdf0e10cSrcweir             }
222cdf0e10cSrcweir         } catch (com.sun.star.beans.UnknownPropertyException e) {
223cdf0e10cSrcweir             log.println("some properties seem to be unknown: " + e.toString());
224cdf0e10cSrcweir             result=false;
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir         tRes.tested("setAllPropertiesToDefault()", result) ;
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232