xref: /AOO41X/main/qadevOOo/tests/java/ifc/accessibility/_XAccessibleContext.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3) !
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 package ifc.accessibility;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import com.sun.star.accessibility.IllegalAccessibleComponentStateException;
30*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
31*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
32*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleRelationSet;
33*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleStateSet;
34*cdf0e10cSrcweir import com.sun.star.lang.Locale;
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir import lib.MultiMethodTest;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir import util.AccessibilityTools;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir /**
42*cdf0e10cSrcweir  * Testing <code>com.sun.star.accessibility.XAccessibleContext</code>
43*cdf0e10cSrcweir  * interface methods :
44*cdf0e10cSrcweir  * <ul>
45*cdf0e10cSrcweir  *  <li><code> getAccessibleChildCount()</code></li>
46*cdf0e10cSrcweir  *  <li><code> getAccessibleChild()</code></li>
47*cdf0e10cSrcweir  *  <li><code> getAccessibleParent()</code></li>
48*cdf0e10cSrcweir  *  <li><code> getAccessibleIndexInParent()</code></li>
49*cdf0e10cSrcweir  *  <li><code> getAccessibleRole()</code></li>
50*cdf0e10cSrcweir  *  <li><code> getAccessibleDescription()</code></li>
51*cdf0e10cSrcweir  *  <li><code> getAccessibleName()</code></li>
52*cdf0e10cSrcweir  *  <li><code> getAccessibleRelationSet()</code></li>
53*cdf0e10cSrcweir  *  <li><code> getAccessibleStateSet()</code></li>
54*cdf0e10cSrcweir  *  <li><code> getLocale()</code></li>
55*cdf0e10cSrcweir  * </ul> <p>
56*cdf0e10cSrcweir  *
57*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleContext
58*cdf0e10cSrcweir  */
59*cdf0e10cSrcweir public class _XAccessibleContext extends MultiMethodTest {
60*cdf0e10cSrcweir     private static final String className = "com.sun.star.accessibility.XAccessibleContext";
61*cdf0e10cSrcweir     public XAccessibleContext oObj = null;
62*cdf0e10cSrcweir     private int childCount = 0;
63*cdf0e10cSrcweir     private XAccessible parent = null;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     // temporary while accessibility package is in com.sun.star
66*cdf0e10cSrcweir     protected String getTestedClassName() {
67*cdf0e10cSrcweir         return className;
68*cdf0e10cSrcweir     }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     /**
71*cdf0e10cSrcweir      * Calls the method and stores the number of children. <p>
72*cdf0e10cSrcweir      * Has <b> OK </b> status if non-negative number rutrned.
73*cdf0e10cSrcweir      */
74*cdf0e10cSrcweir     public void _getAccessibleChildCount() {
75*cdf0e10cSrcweir         childCount = oObj.getAccessibleChildCount();
76*cdf0e10cSrcweir         log.println("" + childCount + " children found.");
77*cdf0e10cSrcweir         tRes.tested("getAccessibleChildCount()", childCount > -1);
78*cdf0e10cSrcweir     }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     /**
81*cdf0e10cSrcweir      * Tries to get every child and checks its parent. <p>
82*cdf0e10cSrcweir      *
83*cdf0e10cSrcweir      * Has <b> OK </b> status if parent of every child
84*cdf0e10cSrcweir      * and the tested component are the same objects.
85*cdf0e10cSrcweir      *
86*cdf0e10cSrcweir      * The following method tests are to be completed successfully before :
87*cdf0e10cSrcweir      * <ul>
88*cdf0e10cSrcweir      *  <li> <code> getAccessibleChildCount() </code> : to have a number of
89*cdf0e10cSrcweir      *     children </li>
90*cdf0e10cSrcweir      * </ul>
91*cdf0e10cSrcweir      */
92*cdf0e10cSrcweir     public void _getAccessibleChild() {
93*cdf0e10cSrcweir         requiredMethod("getAccessibleChildCount()");
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         log.println("testing 'getAccessibleChild()'...");
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         boolean bOK = true;
98*cdf0e10cSrcweir         int counter = childCount;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir         if (childCount > 500) {
101*cdf0e10cSrcweir             counter = 500;
102*cdf0e10cSrcweir         }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         for (int i = 0; i < counter; i++) {
105*cdf0e10cSrcweir             try {
106*cdf0e10cSrcweir                 XAccessible ch = oObj.getAccessibleChild(i);
107*cdf0e10cSrcweir                 XAccessibleContext chAC = ch.getAccessibleContext();
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir                 log.println("## Child " + i + ": " +
110*cdf0e10cSrcweir                             chAC.getAccessibleDescription());
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir                 if (!AccessibilityTools.equals(chAC.getAccessibleParent()
113*cdf0e10cSrcweir                                                    .getAccessibleContext(),
114*cdf0e10cSrcweir                                                oObj)) {
115*cdf0e10cSrcweir                     log.println("The parent of child and component " +
116*cdf0e10cSrcweir                                 "itself differ.");
117*cdf0e10cSrcweir                     log.println("\tRole:");
118*cdf0e10cSrcweir                     log.println("Getting:  " +
119*cdf0e10cSrcweir                                 chAC.getAccessibleParent()
120*cdf0e10cSrcweir                                     .getAccessibleContext()
121*cdf0e10cSrcweir                                     .getAccessibleRole());
122*cdf0e10cSrcweir                     log.println("Expected: " + oObj.getAccessibleRole());
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir                     log.println("\tImplementationName:");
125*cdf0e10cSrcweir                     log.println("Getting:  " +
126*cdf0e10cSrcweir                                 util.utils.getImplName(
127*cdf0e10cSrcweir                                         chAC.getAccessibleParent()
128*cdf0e10cSrcweir                                             .getAccessibleContext()));
129*cdf0e10cSrcweir                     log.println("Expected: " + util.utils.getImplName(oObj));
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir                     log.println("\tAccessibleDescription:");
132*cdf0e10cSrcweir                     log.println("Getting(Description):  " +
133*cdf0e10cSrcweir                                 chAC.getAccessibleParent()
134*cdf0e10cSrcweir                                     .getAccessibleContext()
135*cdf0e10cSrcweir                                     .getAccessibleDescription());
136*cdf0e10cSrcweir                     log.println("Expected(Description): " +
137*cdf0e10cSrcweir                                 oObj.getAccessibleDescription());
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir                     log.println("\tAccessibleName:");
140*cdf0e10cSrcweir                     log.println("Getting(Name):  " +
141*cdf0e10cSrcweir                                 chAC.getAccessibleParent()
142*cdf0e10cSrcweir                                     .getAccessibleContext()
143*cdf0e10cSrcweir                                     .getAccessibleName());
144*cdf0e10cSrcweir                     log.println("Expected(Name): " +
145*cdf0e10cSrcweir                                 oObj.getAccessibleName());
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir                     log.println("\tChildCount:");
148*cdf0e10cSrcweir                     log.println("Getting:  " +
149*cdf0e10cSrcweir                                 chAC.getAccessibleParent()
150*cdf0e10cSrcweir                                     .getAccessibleContext()
151*cdf0e10cSrcweir                                     .getAccessibleChildCount());
152*cdf0e10cSrcweir                     log.println("Expected: " +
153*cdf0e10cSrcweir                                 oObj.getAccessibleChildCount());
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir                     log.println("\tParentName:");
156*cdf0e10cSrcweir                     log.println("Getting (Name):  " +
157*cdf0e10cSrcweir                                 chAC.getAccessibleParent()
158*cdf0e10cSrcweir                                     .getAccessibleContext()
159*cdf0e10cSrcweir                                     .getAccessibleParent()
160*cdf0e10cSrcweir                                     .getAccessibleContext()
161*cdf0e10cSrcweir                                     .getAccessibleName());
162*cdf0e10cSrcweir                     log.println("Expected(Name): " +
163*cdf0e10cSrcweir                                 oObj.getAccessibleParent()
164*cdf0e10cSrcweir                                     .getAccessibleContext()
165*cdf0e10cSrcweir                                     .getAccessibleName());
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir                     log.println("##");
168*cdf0e10cSrcweir                     bOK = false;
169*cdf0e10cSrcweir                 } else {
170*cdf0e10cSrcweir                     log.println("Role: " + chAC.getAccessibleRole());
171*cdf0e10cSrcweir                     log.println("Name: " + chAC.getAccessibleName());
172*cdf0e10cSrcweir                     log.println("IndexInParent: " +
173*cdf0e10cSrcweir                                 chAC.getAccessibleIndexInParent());
174*cdf0e10cSrcweir                     log.println("ImplementationName: " +
175*cdf0e10cSrcweir                                 util.utils.getImplName(chAC));
176*cdf0e10cSrcweir                 }
177*cdf0e10cSrcweir             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
178*cdf0e10cSrcweir                 e.printStackTrace(log);
179*cdf0e10cSrcweir                 bOK = false;
180*cdf0e10cSrcweir             }
181*cdf0e10cSrcweir         }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir         tRes.tested("getAccessibleChild()", bOK);
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     /**
187*cdf0e10cSrcweir      * Just gets the parent. <p>
188*cdf0e10cSrcweir      *
189*cdf0e10cSrcweir      * Has <b> OK </b> status if parent is not null.
190*cdf0e10cSrcweir      */
191*cdf0e10cSrcweir     public void _getAccessibleParent() {
192*cdf0e10cSrcweir         // assume that the component is not ROOT
193*cdf0e10cSrcweir         parent = oObj.getAccessibleParent();
194*cdf0e10cSrcweir         tRes.tested("getAccessibleParent()", parent != null);
195*cdf0e10cSrcweir     }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     /**
198*cdf0e10cSrcweir      * Retrieves the index of tested component in its parent.
199*cdf0e10cSrcweir      * Then gets the parent's child by this index and compares
200*cdf0e10cSrcweir      * it with tested component.<p>
201*cdf0e10cSrcweir      *
202*cdf0e10cSrcweir      * Has <b> OK </b> status if the parent's child and the tested
203*cdf0e10cSrcweir      * component are the same objects.
204*cdf0e10cSrcweir      *
205*cdf0e10cSrcweir      * The following method tests are to be completed successfully before :
206*cdf0e10cSrcweir      * <ul>
207*cdf0e10cSrcweir      *  <li> <code> getAccessibleParent() </code> : to have a parent </li>
208*cdf0e10cSrcweir      * </ul>
209*cdf0e10cSrcweir      */
210*cdf0e10cSrcweir     public void _getAccessibleIndexInParent() {
211*cdf0e10cSrcweir         requiredMethod("getAccessibleParent()");
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir         boolean bOK = true;
214*cdf0e10cSrcweir         int idx = oObj.getAccessibleIndexInParent();
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         XAccessibleContext parentAC = parent.getAccessibleContext();
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir         try {
219*cdf0e10cSrcweir             if (parentAC.getAccessibleChild(idx) == null) {
220*cdf0e10cSrcweir                 log.println("Parent has no child with this index");
221*cdf0e10cSrcweir                 bOK &= false;
222*cdf0e10cSrcweir             } else {
223*cdf0e10cSrcweir                 bOK &= AccessibilityTools.equals(parentAC.getAccessibleChild(
224*cdf0e10cSrcweir                                                          idx)
225*cdf0e10cSrcweir                                                          .getAccessibleContext(),
226*cdf0e10cSrcweir                                                  oObj);
227*cdf0e10cSrcweir             }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir             if (!bOK) {
230*cdf0e10cSrcweir                 log.println("Expected: " + util.utils.getImplName(oObj));
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir                 if (parentAC.getAccessibleChild(idx) != null) {
233*cdf0e10cSrcweir                     log.println("Getting: " +
234*cdf0e10cSrcweir                                 util.utils.getImplName(
235*cdf0e10cSrcweir                                         parentAC.getAccessibleChild(idx)
236*cdf0e10cSrcweir                                                 .getAccessibleContext()));
237*cdf0e10cSrcweir                 }
238*cdf0e10cSrcweir             }
239*cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
240*cdf0e10cSrcweir             e.printStackTrace(log);
241*cdf0e10cSrcweir             bOK = false;
242*cdf0e10cSrcweir         }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir         tRes.tested("getAccessibleIndexInParent()", bOK);
245*cdf0e10cSrcweir     }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir     /**
248*cdf0e10cSrcweir      * Get the accessible role of component. <p>
249*cdf0e10cSrcweir      *
250*cdf0e10cSrcweir      * Has <b> OK </b> status if non-negative number rutrned.
251*cdf0e10cSrcweir      */
252*cdf0e10cSrcweir     public void _getAccessibleRole() {
253*cdf0e10cSrcweir         short role = oObj.getAccessibleRole();
254*cdf0e10cSrcweir         log.println("The role is " + role);
255*cdf0e10cSrcweir         tRes.tested("getAccessibleRole()", role > -1);
256*cdf0e10cSrcweir     }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir     /**
259*cdf0e10cSrcweir      * Get the accessible name of the component. <p>
260*cdf0e10cSrcweir      *
261*cdf0e10cSrcweir      * Has <b> OK </b> status if the name has non-zero length.
262*cdf0e10cSrcweir      */
263*cdf0e10cSrcweir     public void _getAccessibleName() {
264*cdf0e10cSrcweir         String name = oObj.getAccessibleName();
265*cdf0e10cSrcweir         log.println("The name is '" + name + "'");
266*cdf0e10cSrcweir         tRes.tested("getAccessibleName()", name != null);
267*cdf0e10cSrcweir     }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir     /**
270*cdf0e10cSrcweir      * Get the accessible description of the component. <p>
271*cdf0e10cSrcweir      *
272*cdf0e10cSrcweir      * Has <b> OK </b> status if the description has non-zero length.
273*cdf0e10cSrcweir      */
274*cdf0e10cSrcweir     public void _getAccessibleDescription() {
275*cdf0e10cSrcweir         String descr = oObj.getAccessibleDescription();
276*cdf0e10cSrcweir         log.println("The description is '" + descr + "'");
277*cdf0e10cSrcweir         tRes.tested("getAccessibleDescription()", descr != null);
278*cdf0e10cSrcweir     }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir     /**
281*cdf0e10cSrcweir      * Just gets the set. <p>
282*cdf0e10cSrcweir      *
283*cdf0e10cSrcweir      * Has <b> OK </b> status if the set is not null.
284*cdf0e10cSrcweir      */
285*cdf0e10cSrcweir     public void _getAccessibleRelationSet() {
286*cdf0e10cSrcweir         XAccessibleRelationSet set = oObj.getAccessibleRelationSet();
287*cdf0e10cSrcweir         tRes.tested("getAccessibleRelationSet()", true);
288*cdf0e10cSrcweir     }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir     /**
291*cdf0e10cSrcweir      * Just gets the set. <p>
292*cdf0e10cSrcweir      *
293*cdf0e10cSrcweir      * Has <b> OK </b> status if the set is not null.
294*cdf0e10cSrcweir      */
295*cdf0e10cSrcweir     public void _getAccessibleStateSet() {
296*cdf0e10cSrcweir         XAccessibleStateSet set = oObj.getAccessibleStateSet();
297*cdf0e10cSrcweir         boolean res = true;
298*cdf0e10cSrcweir         String[] expectedStateNames = (String[]) tEnv.getObjRelation(
299*cdf0e10cSrcweir                                                 "expectedStateNames");
300*cdf0e10cSrcweir         short[] expectedStates = (short[]) tEnv.getObjRelation(
301*cdf0e10cSrcweir                                            "expectedStates");
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir         if ((expectedStateNames != null) && (expectedStates != null)) {
304*cdf0e10cSrcweir             res = checkStates(expectedStateNames, expectedStates, set);
305*cdf0e10cSrcweir         } else {
306*cdf0e10cSrcweir             res = set != null;
307*cdf0e10cSrcweir         }
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir         tRes.tested("getAccessibleStateSet()", res);
310*cdf0e10cSrcweir     }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir     /**
313*cdf0e10cSrcweir      * Gets the locale. <p>
314*cdf0e10cSrcweir      *
315*cdf0e10cSrcweir      * Has <b> OK </b> status if <code>Country</code> and
316*cdf0e10cSrcweir      * <code>Language</code> fields of locale structure
317*cdf0e10cSrcweir      * are not empty.
318*cdf0e10cSrcweir      */
319*cdf0e10cSrcweir     public void _getLocale() {
320*cdf0e10cSrcweir         Locale loc = null;
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir         try {
323*cdf0e10cSrcweir             loc = oObj.getLocale();
324*cdf0e10cSrcweir             log.println("The locale is " + loc.Language + "," + loc.Country);
325*cdf0e10cSrcweir         } catch (IllegalAccessibleComponentStateException e) {
326*cdf0e10cSrcweir             e.printStackTrace(log);
327*cdf0e10cSrcweir         }
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir         tRes.tested("getLocale()",
330*cdf0e10cSrcweir                     (loc != null) && (loc.Language.length() > 0));
331*cdf0e10cSrcweir     }
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir     protected boolean checkStates(String[] expectedStateNames,
334*cdf0e10cSrcweir                                   short[] expectedStates,
335*cdf0e10cSrcweir                                   XAccessibleStateSet set) {
336*cdf0e10cSrcweir         boolean works = true;
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir         for (int k = 0; k < expectedStateNames.length; k++) {
339*cdf0e10cSrcweir             boolean contains = set.contains(expectedStates[k]);
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir             if (contains) {
342*cdf0e10cSrcweir                 log.println("Set contains " + expectedStateNames[k] +
343*cdf0e10cSrcweir                             " ... OK");
344*cdf0e10cSrcweir                 works &= true;
345*cdf0e10cSrcweir             } else {
346*cdf0e10cSrcweir                 log.println("Set doesn't contain " + expectedStateNames[k] +
347*cdf0e10cSrcweir                             " ... FAILED");
348*cdf0e10cSrcweir                 works &= false;
349*cdf0e10cSrcweir             }
350*cdf0e10cSrcweir         }
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir         return works;
353*cdf0e10cSrcweir     }
354*cdf0e10cSrcweir }
355