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.accessibility;
25 
26 
27 import lib.MultiMethodTest;
28 import lib.Status;
29 import lib.StatusException;
30 
31 import com.sun.star.accessibility.XAccessibleValue;
32 
33 /**
34  * Testing <code>com.sun.star.accessibility.XAccessibleValue</code>
35  * interface methods :
36  * <ul>
37  *  <li><code> getCurrentValue()</code></li>
38  *  <li><code> setCurrentValue()</code></li>
39  *  <li><code> getMaximumValue()</code></li>
40  *  <li><code> getMinimumValue()</code></li>
41  * </ul> <p>
42  *
43  * This test needs the following object relations :
44  * <ul>
45  *  <li> <code>'XAccessibleValue.anotherFromGroup'</code>
46  *  (of type <code>XAccessibleValue</code>) <b> optional </b>:
47  *   another component from the group(e.g. radio button group)</li>
48  *  </ul><p>
49  * @see com.sun.star.accessibility.XAccessibleValue
50  */
51 public class _XAccessibleValue extends MultiMethodTest {
52 
53     public XAccessibleValue oObj = null;
54 
55     private double minVal = 0;
56     private double maxVal = 0;
57     private double curVal = 0;
58     private Object val = null;
59     XAccessibleValue anotherFromGroup = null;
60 
before()61     protected void before() {
62         anotherFromGroup = (XAccessibleValue)tEnv.getObjRelation(
63             "XAccessibleValue.anotherFromGroup");
64     }
65 
66     /**
67      * Gets current value and stores it as double. <p>
68      *
69      * Has <b> OK </b> status if the current value is between Min and Max
70      * values. <p>
71      *
72      * The following method tests are to be executed before :
73      * <ul>
74      *  <li> <code> getMaximumValue </code> </li>
75      *  <li> <code> getMinimumValue </code> </li>
76      * </ul>
77      */
_getCurrentValue()78     public void _getCurrentValue() {
79         executeMethod("getMaximumValue()");
80         executeMethod("getMinimumValue()");
81 
82         boolean result = true;
83 
84         double curVal ;
85         val = oObj.getCurrentValue() ;
86         if (util.utils.isVoid(val)) {
87             val = new Integer(0);
88             curVal = 0;
89         } else  {
90             curVal = getDoubleValue(val);
91         }
92 
93         if (curVal < minVal || maxVal < curVal) {
94             log.println("" + (curVal - minVal) + "," + (maxVal - curVal));
95             log.println("Current value " + curVal + " is not in range ["
96                 + minVal + "," + maxVal + "]");
97             result = false;
98         }
99 
100         tRes.tested("getCurrentValue()", result) ;
101     }
102 
103     /**
104      * Performs testing for following cases :
105      * <ul>
106      *  <li> Creates new value in valid range and sets it. </li>
107      *  <li> Sets maximum and minimum values. </li>
108      *  <li> Sets Min - 1, and Max + 1 values </li>
109      * </ul> <p>
110      *
111      * Has <b> OK </b> status if for the first case the value returned
112      * is the same as was set and the method <code>setCurrentValue</code>
113      * returns <code>true</code>.
114      *
115      * In the second if Max and Min values are set and method
116      * <code>setCurrentValue</code> returns <code>true</code>.
117      *
118      * In the third case invalid values are truncated to Min and Max
119      * values accordingly. <p>
120      *
121      * The following method tests are to be executed before :
122      * <ul>
123      *  <li> <code> getCurrentValue() </code> </li>
124      * </ul>
125      */
_setCurrentValue()126     public void _setCurrentValue() {
127         executeMethod("getCurrentValue()");
128 
129         boolean result = true ;
130 		boolean partResult=true;
131 		String noMax = "com.sun.star.comp.toolkit.AccessibleScrollBar";
132 		String implName = util.utils.getImplName(oObj);
133 
134         if (tEnv.getObjRelation("ValueNotPersitent")!=null) {
135             log.println("Excluded since it works like AccessibleAction");
136             tRes.tested("setCurrentValue()",Status.skipped(true));
137             return;
138         }
139 
140         if (anotherFromGroup == null) {
141             double newVal = curVal + 1;
142             if (newVal > maxVal) newVal -= 2;
143             if (newVal < minVal) newVal += 1;
144 
145             log.println("New value is " + newVal);
146 
147             Object setVal = getObjectValue(newVal, val.getClass());
148 
149             result &= oObj.setCurrentValue(setVal);
150 
151             if (!result) {
152                 log.println("The value can't be set");
153                 throw new StatusException(Status.skipped(true));
154             }
155 
156             double resVal = getDoubleValue(oObj.getCurrentValue());
157             log.println("Res value is " + resVal);
158 
159             result &= Math.abs(newVal - resVal) < 0.00001;
160 
161             log.println("Checking min/max values");
162             result &= oObj.setCurrentValue(getObjectValue(minVal, val.getClass()));
163             log.println("Setting to "+ getObjectValue(minVal, val.getClass()));
164             resVal = getDoubleValue(oObj.getCurrentValue());
165             log.println("Result min value is " + resVal);
166             result &= Math.abs(minVal - resVal) < 0.00001;
167 			log.println("\t works: "+(Math.abs(minVal - resVal) < 0.00001));
168 
169             result &= oObj.setCurrentValue(getObjectValue(maxVal, val.getClass()));
170             log.println("Setting to "+ getObjectValue(maxVal, val.getClass()));
171             resVal = getDoubleValue(oObj.getCurrentValue());
172             log.println("Result max value is " + resVal);
173             partResult = Math.abs(maxVal - resVal) < 0.00001;
174 
175 			if (implName.equals(noMax)) {
176 				log.println("If one sets the maximum value of a scroll bar with XScrollBar::setMaximum(),"+
177 				"then XScrollBar::getValue() returns the maximum value minus the visible size of"+
178 				"the thumb");
179 				//using arbitrary Value, since we can't determine the resulting value
180 				partResult = resVal > 10;
181 			}
182 
183 			result &=partResult;
184 			log.println("\t works: "+partResult);
185 
186             log.println("Checking truncating of min/max values");
187             oObj.setCurrentValue(getObjectValue(minVal - 1, val.getClass()));
188             log.println("Setting to "+ getObjectValue(minVal -1 , val.getClass()));
189             resVal = getDoubleValue(oObj.getCurrentValue());
190             log.println("Result min value is " + resVal);
191             result &= Math.abs(minVal - resVal) < 0.00001;
192 			log.println("\t works: "+(Math.abs(minVal - resVal) < 0.00001));
193 
194             oObj.setCurrentValue(getObjectValue(maxVal + 1, val.getClass()));
195             log.println("Setting to "+ getObjectValue(maxVal +1 , val.getClass()));
196             resVal = getDoubleValue(oObj.getCurrentValue());
197             log.println("Result max value is " + resVal);
198             partResult = Math.abs(maxVal - resVal) < 0.00001;
199 			if (implName.equals(noMax)) {
200 				log.println("If one sets the maximum value of a scroll bar with XScrollBar::setMaximum(),"+
201 				"then XScrollBar::getValue() returns the maximum value minus the visible size of"+
202 				"the thumb");
203 				//using arbitrary Value, since we can't determine the resulting value
204 				partResult = resVal > 10;
205 			}
206 
207 			result &=partResult;
208 			log.println("\t works: "+partResult);
209         } else {
210             int curValBase = getIntegerValue(val);
211             Object valAnotherFromGroup = anotherFromGroup.getCurrentValue();
212             int curValAnother = getIntegerValue(valAnotherFromGroup);
213             log.println("Current value of base component: " + curValBase);
214             log.println("Current value of another component from group: " +
215                 curValAnother);
216             log.println("Set value of base component to " + curValAnother);
217             if (tEnv.getTestCase().getObjectName().equals("AccessibleRadioButton")) {
218                 anotherFromGroup.setCurrentValue(new Integer(curValBase));
219             } else {
220                 oObj.setCurrentValue(valAnotherFromGroup);
221             }
222             log.println("Checking of values...");
223             int newValBase = getIntegerValue(oObj.getCurrentValue());
224             int newValAnother = getIntegerValue(
225                 anotherFromGroup.getCurrentValue());
226             log.println("New value of base component: " + newValBase);
227             log.println("Expected value of base component: " + curValAnother);
228             log.println("New value of another component from group: " +
229                 newValAnother);
230             log.println("Expected value of another component from group: " +
231                 curValBase);
232 
233             result = (newValBase == curValAnother) &&
234                 (newValAnother == curValBase);
235         }
236 
237         tRes.tested("setCurrentValue()", result);
238     }
239 
240     /**
241      * Gets and stores maximal value. <p>
242      *
243      * Has <b> OK </b> status if non empty value returned and
244      * Max value is greater than Min value.
245      *
246      * The following method tests are to be completed successfully before :
247      * <ul>
248      *  <li> <code> getMinimumValue() </code> : to compare with </li>
249      * </ul>
250      */
251     public void _getMaximumValue() {
252         requiredMethod("getMinimumValue()");
253 
254         boolean result = true ;
255 
256         Object val = oObj.getMaximumValue();
257         if (util.utils.isVoid(val)) {
258             maxVal = Double.MAX_VALUE ;
259             result = false;
260         } else {
261             maxVal = getDoubleValue(val);
262         }
263         log.println("Max is " + val.getClass()+ " = " + maxVal);
264 
265         result &= maxVal >= minVal;
266 
267         tRes.tested("getMaximumValue()", result) ;
268     }
269 
270     /**
271      * Gets and stores minimal value. <p>
272      *
273      * Has <b> OK </b> status if non empty value returned. <p>
274      *
275      */
276     public void _getMinimumValue() {
277         boolean result = true ;
278 
279         Object val = oObj.getMinimumValue() ;
280         if (util.utils.isVoid(val)) {
281             minVal = - Double.MAX_VALUE ;
282             result = false;
283         } else {
284             minVal = getDoubleValue(val);
285         }
286         log.println("Min is " + val.getClass()+ " = " + minVal);
287 
288         tRes.tested("getMinimumValue()", result) ;
289     }
290 
291     private int getIntegerValue(Object val) {
292         if (val instanceof Integer) {
293             return ((Integer) val).intValue();
294         } else {
295             throw new StatusException
296                 (Status.failed("Unexpected value type: " + val.getClass()));
297         }
298     }
299 
300     private double getDoubleValue(Object val) {
301         if (val instanceof Integer) {
302             return ((Integer) val).doubleValue();
303         }
304         else if (val instanceof Short) {
305             return ((Short) val).doubleValue();
306         }
307         else if (val instanceof Float) {
308             return ((Float) val).doubleValue();
309         }
310         else if (val instanceof Double) {
311             return ((Double) val).doubleValue();
312         }
313         else if (util.utils.isVoid(val)) {
314             return Double.NaN;
315         }
316         else {
317             throw new StatusException
318                 (Status.failed("Undetected value type: " + val.getClass()));
319         }
320     }
321 
322     private Object getObjectValue(double val, Class clazz) {
323         if (clazz.equals(Integer.class)) {
324             return new Integer((int)val);
325         }
326         else if (clazz.equals(Short.class)) {
327             return new Short((short)val);
328         }
329         else if (clazz.equals(Float.class)) {
330             return new Float((float)val);
331         }
332         else if (clazz.equals(Double.class)) {
333             return new Double(val);
334         }
335         else {
336             throw new StatusException
337                 (Status.failed("Unexpected class: " + clazz));
338         }
339     }
340 
341     /**
342      * Disposes test environment.
343      */
344     protected void after() {
345         disposeEnvironment();
346     }
347 }
348