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 java.util.ArrayList;
22 import java.util.List;
23 import java.util.Random;
24 import java.util.Set;
25 
26 import lib.MultiMethodTest;
27 import lib.StatusException;
28 import util.ValueChanger;
29 
30 import com.sun.star.beans.Property;
31 import com.sun.star.beans.PropertyAttribute;
32 import com.sun.star.beans.XFastPropertySet;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.beans.XPropertySetInfo;
35 import com.sun.star.uno.UnoRuntime;
36 
37 /**
38 * Testing <code>com.sun.star.beans.XFastPropertySet</code>
39 * interface methods :
40 * <ul>
41 *  <li><code> setFastPropertyValue()</code></li>
42 *  <li><code> getFastPropertyValue()</code></li>
43 * </ul> <p>
44 * Required relations :
45 * <ul>
46 *  <li> <code>'XFastPropertySet.ExcludeProps'</code>
47 *    <b>(optional) </b> : java.util.Set.
48 *    Has property names which must be skipped from testing in
49 *    some reasons (for example property accepts restricted set
50 *    of values).
51 *  </li>
52 * <ul> <p>
53 * @see com.sun.star.beans.XFastPropertySet
54 */
55 public class _XFastPropertySet extends MultiMethodTest {
56     private static final class Prop {
57         public final int handle;
58         public final String name;
59 
Prop(int handle, String name)60         public Prop(int handle, String name) {
61             this.handle = handle;
62             this.name = name;
63         }
64     }
65 
66     public XFastPropertySet oObj = null;
67     private final List<Prop> props = new ArrayList<Prop>();
68     private Prop prop;
69     private Set<String> exclude = null ;
70 
71     /**
72      * Retrieves relation.
73      */
74      @Override
before()75     protected void before() {
76         exclude = (Set<String>) tEnv.getObjRelation("XFastPropertySet.ExcludeProps") ;
77         if (exclude == null) {
78             exclude = new java.util.HashSet<String>() ;
79         }
80      }
81 
82     /**
83     * Test selects random property which can not be VOID and
84     * is writable, then change property value using <code>
85     * get/set</code> methods, and checks if value properly changed.
86     * Has <b> OK </b> status if value after change is not equal to value
87     * before and no exceptions were thrown. <p>
88     */
_setFastPropertyValue()89     public void _setFastPropertyValue() {
90         XPropertySet PS = UnoRuntime.queryInterface
91             (XPropertySet.class, oObj);
92         XPropertySetInfo propertySetInfo = PS.getPropertySetInfo();
93 
94         if (propertySetInfo == null) {
95             log.println("getPropertySetInfo() method returned null");
96             tRes.tested("setFastPropertyValue()", false) ;
97         }
98         getPropsToTest(propertySetInfo);
99 
100         Object gValue = null;
101         Object sValue = null;
102 
103         if ( prop.handle == -1) {
104             log.println("*** No changeable properties found ***");
105             tRes.tested("setFastPropertyValue()", false) ;
106         } else {
107             try {
108                 gValue = oObj.getFastPropertyValue(prop.handle);
109                 sValue = ValueChanger.changePValue(gValue, prop.name);
110                 oObj.setFastPropertyValue(prop.handle, sValue);
111                 sValue = oObj.getFastPropertyValue(prop.handle);
112             } catch (com.sun.star.beans.UnknownPropertyException e) {
113                 log.println("Exception occurred while trying to change property with handle = " + prop.handle);
114                 e.printStackTrace(log);
115             } catch (com.sun.star.lang.WrappedTargetException e) {
116                 log.println("Exception occurred while trying to change property with handle = " + prop.handle);
117                 e.printStackTrace(log);
118             } catch (com.sun.star.beans.PropertyVetoException e) {
119                 log.println("Exception occurred while trying to change property with handle = " + prop.handle);
120                 e.printStackTrace(log);
121             }  catch (com.sun.star.lang.IllegalArgumentException e) {
122                 log.println("Exception occurred while trying to change property with handle = " + prop.handle);
123                 e.printStackTrace(log);
124             }
125 
126             tRes.tested("setFastPropertyValue()",(!gValue.equals(sValue)));
127         }
128     }
129 
130     /**
131     * Test selects random property which can not be VOID and
132     * is writable, then calls the method and  checks that
133     * no exceptions were thrown. <p>
134     * Has <b> OK </b> status if exceptions were thrown. <p>
135     */
_getFastPropertyValue()136     public void _getFastPropertyValue() {
137         XPropertySet PS = UnoRuntime.queryInterface
138             (XPropertySet.class, oObj);
139         XPropertySetInfo propertySetInfo = PS.getPropertySetInfo();
140 
141         if (propertySetInfo == null) {
142             log.println("getPropertySetInfo() method returned null");
143             tRes.tested("getFastPropertyValue()", false) ;
144         }
145 
146         getPropsToTest(propertySetInfo);
147 
148         try {
149             oObj.getFastPropertyValue(prop.handle);
150             tRes.tested("getFastPropertyValue()",true);
151         } catch (com.sun.star.beans.UnknownPropertyException e) {
152             log.println("Exception occurred while trying to get property '"
153                 + prop.handle +"'");
154             e.printStackTrace(log);
155             tRes.tested("getFastPropertyValue()",false);
156         } catch (com.sun.star.lang.WrappedTargetException e) {
157             log.println("Exception occurred while trying to get property '"
158                 + prop.handle +"'");
159             e.printStackTrace(log);
160             tRes.tested("getFastPropertyValue()",false);
161         }
162     }
163 
164 
165     //Get the properties being tested
getPropsToTest(XPropertySetInfo xPSI)166     private void getPropsToTest(XPropertySetInfo xPSI) {
167 
168         Property[] properties = xPSI.getProperties();
169 
170         for (int i = 0; i < properties.length; i++) {
171             if (exclude.contains(properties[i].Name)) continue ;
172             Property property = properties[i];
173             String name = property.Name;
174             int handle = property.Handle;
175             log.println("Checking '" + name + "' with handle = " + handle);
176             boolean isWritable =
177                 ((property.Attributes & PropertyAttribute.READONLY) == 0);
178             boolean isNotNull =
179                 ((property.Attributes & PropertyAttribute.MAYBEVOID) == 0);
180             boolean canChange = false;
181             if ( isWritable && isNotNull )
182                 canChange = isChangeable(handle, name);
183             if ( isWritable && isNotNull && canChange)
184                 props.add(new Prop(handle, name));
185         } // endfor
186 
187         Random rnd = new Random();
188         int nr = rnd.nextInt(props.size());
189         prop = props.get(nr);
190     }
191 
isChangeable(int handle, String name)192     private boolean isChangeable(int handle, String name) {
193         boolean hasChanged = false;
194         try {
195             Object getProp = oObj.getFastPropertyValue(handle);
196             Object setValue = null;
197 
198             if (getProp != null)
199                 setValue = ValueChanger.changePValue(getProp, name);
200             else
201                 log.println("Property with handle = " + handle
202                     + " is null but 'MAYBEVOID' isn't set");
203             if (setValue != null) {
204                 oObj.setFastPropertyValue(handle, setValue);
205                 hasChanged =
206                     (!getProp.equals(oObj.getFastPropertyValue(handle)));
207             }
208             else
209                 log.println("Couldn't change Property with handle " + handle);
210         } catch(com.sun.star.lang.WrappedTargetException e) {
211             log.println("Property with handle " + handle + " throws exception");
212             e.printStackTrace(log);
213             throw new StatusException("Property with handle " + handle
214                 + " throws exception", e);
215         } catch(com.sun.star.lang.IllegalArgumentException e) {
216             log.println("Property with handle " + handle + " throws exception");
217             e.printStackTrace(log);
218             throw new StatusException("Property with handle " + handle
219                 + " throws exception", e);
220         } catch(com.sun.star.beans.PropertyVetoException e) {
221             log.println("Property with handle " + handle + " throws exception");
222             e.printStackTrace(log);
223             throw new StatusException("Property with handle " + handle
224                 + " throws exception", e);
225         } catch(com.sun.star.beans.UnknownPropertyException e) {
226             log.println("Property with handle " + handle + " throws exception");
227             e.printStackTrace(log);
228             throw new StatusException("Property with handle " + handle
229                 + " throws exception", e);
230         }
231 
232         return hasChanged;
233     }
234 }
235 
236 
237