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