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