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