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