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