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