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