2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package ifc
.accessibility
;
20 import com
.sun
.star
.accessibility
.AccessibleRole
;
21 import com
.sun
.star
.accessibility
.XAccessible
;
22 import com
.sun
.star
.accessibility
.XAccessibleContext
;
23 import com
.sun
.star
.accessibility
.XAccessibleSelection
;
24 import com
.sun
.star
.uno
.UnoRuntime
;
26 import lib
.MultiMethodTest
;
28 import lib
.StatusException
;
32 * Testing <code>com.sun.star.accessibility.XAccessibleSelection</code>
35 * <li><code>selectAccessibleChild()</code></li>
36 * <li><code>isAccessibleChildSelected()</code></li>
37 * <li><code>clearAccessibleSelection()</code></li>
38 * <li><code>selectAllAccessibleChildren()</code></li>
39 * <li><code>getSelectedAccessibleChildCount()</code></li>
40 * <li><code>getSelectedAccessibleChild()</code></li>
41 * <li><code>deselectAccessibleChild()</code></li>
44 * This test needs the following object relations :
46 * <li> <code>'XAccessibleSelection.multiSelection'</code>
47 * (of type <code>Boolean</code>) <b> optional </b>:
48 * Indicates whether or not mutiply children could be selected.
49 * If the relation is <code>false</code> then more than 1 child
50 * couldn't be selected. </li>
53 * @see com.sun.star.accessibility.XAccessibleSelection
55 public class _XAccessibleSelection
extends MultiMethodTest
{
56 private static final String className
= "com.sun.star.accessibility.XAccessibleSelection";
57 public XAccessibleSelection oObj
= null;
58 XAccessibleContext xAC
= null;
60 protected boolean multiSelection
= true;
61 protected boolean OneAlwaysSelected
= false;
63 // temporary while accessibility package is in com.sun.star
64 protected String
getTestedClassName() {
69 * Retrieves the interface <code>XAccessibleContext</code>
70 * and object relation.
71 * @see com.sun.star.accessibility.XAccessibleContext
72 * @see ifc.accessibility._XAccessibleContext
74 protected void before() {
75 xAC
= UnoRuntime
.queryInterface(
76 XAccessibleContext
.class, oObj
);
79 throw new StatusException(Status
.failed(
80 "Couldn't query XAccessibleContext. Test must be modified"));
83 Boolean b
= (Boolean
) tEnv
.getObjRelation(
84 "XAccessibleSelection.multiSelection");
87 multiSelection
= b
.booleanValue();
90 Boolean b2
= (Boolean
) tEnv
.getObjRelation(
91 "XAccessibleSelection.OneAlwaysSelected");
94 OneAlwaysSelected
= b2
.booleanValue();
97 childCount
= xAC
.getAccessibleChildCount();
98 log
.println("Child count: " + childCount
);
102 * Selects accessible child with index some wrong indexes
103 * and with legal index.
104 * Has OK status if exception was thrown for wrong indexes
105 * and if exception wasn't thrown for correct index.
107 public void _selectAccessibleChild() {
111 log
.println("Try to select child with index " + childCount
);
112 oObj
.selectAccessibleChild(childCount
);
113 log
.println("Exception was expected");
115 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
116 log
.println("Expected exception");
121 log
.println("Try to select child with index -1");
122 oObj
.selectAccessibleChild(-1);
123 log
.println("Exception was expected");
125 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
126 log
.println("Expected exception");
130 log
.println("ChildCount: " + childCount
);
132 int usedChildren
= childCount
;
134 if (childCount
> 500) {
135 log
.println("Restricting to 500");
139 if (usedChildren
> 0) {
141 for (int i
= 0; i
< usedChildren
; i
++) {
142 log
.print("Trying to select child with index " + i
+ ": ");
144 if (isSelectable(tEnv
.getTestObject(), i
)) {
145 oObj
.selectAccessibleChild(i
);
148 log
.println("Child isn't selectable");
153 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
154 log
.println("Unexpected exception");
155 e
.printStackTrace(log
);
160 tRes
.tested("selectAccessibleChild()", res
);
164 * Calls the method with the wrong index and with the correct index.
165 * Has OK status if exception was thrown for wrong index and
166 * if exception wasn't thrown for the correct index.
168 public void _isAccessibleChildSelected() {
169 executeMethod("selectAccessibleChild()");
172 boolean isSelected
= false;
175 log
.print("isAccessibleChildSelected(-1)? ");
176 isSelected
= oObj
.isAccessibleChildSelected(-1);
178 log
.println("Exception was expected");
180 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
181 log
.println("Expected exception");
186 log
.print("isAccessibleChildSelected(" + childCount
+ ")? ");
187 isSelected
= oObj
.isAccessibleChildSelected(childCount
);
188 log
.println(isSelected
);
189 log
.println("Exception was expected");
191 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
192 log
.println("Expected exception");
196 int SelectableChildCount
= chkSelectable(tEnv
.getTestObject());
198 if (SelectableChildCount
> 500) {
199 SelectableChildCount
= 500;
202 log
.println("SelectableChildCount: " + SelectableChildCount
);
204 if (SelectableChildCount
> 0) {
206 oObj
.selectAllAccessibleChildren();
208 for (int k
= 0; k
< SelectableChildCount
; k
++) {
209 log
.println("Trying to select child with index " + k
);
211 if (isSelectable(tEnv
.getTestObject(), k
)) {
212 oObj
.selectAccessibleChild(k
);
214 isSelected
= oObj
.isAccessibleChildSelected(k
);
215 log
.println("isAccessibleChildSelected - " +
219 log
.println("Child isn't selectable");
222 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
223 log
.println("Unexpected exception");
224 e
.printStackTrace(log
);
229 tRes
.tested("isAccessibleChildSelected()", res
);
234 * Has OK status if the method <code>isAccessibleChildSelected()</code>
235 * returned <code>false</code>.
237 public void _clearAccessibleSelection() {
238 executeMethod("isAccessibleChildSelected()");
242 log
.println("clearAccessibleSelection");
243 oObj
.clearAccessibleSelection();
246 // clearAccessibleSelection() call is oneway so we need
250 if ((childCount
> 0) && !OneAlwaysSelected
) {
252 log
.print("isAccessibleChildSelected(" + (childCount
- 1) +
255 boolean isSel
= oObj
.isAccessibleChildSelected(childCount
- 1);
258 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
259 log
.println("Unexpected exception");
260 e
.printStackTrace(log
);
263 } else if (OneAlwaysSelected
) {
264 log
.println("Can't clear selection, one child is always selected");
267 tRes
.tested("clearAccessibleSelection()", res
);
272 * Has OK status if the method <code>isAccessibleChildSelected()</code>
273 * returns <code>true</code> for first and for last accessible child
274 * or if multiselection is not allowed.
276 public void _selectAllAccessibleChildren() {
277 executeMethod("clearAccessibleSelection()");
279 log
.println("selectAllAccessibleChildren...");
280 oObj
.selectAllAccessibleChildren();
283 // selectAllAccessibleChildren() call is oneway so we need
288 boolean isSelected
= true;
290 int SelectableChildCount
= chkSelectable(tEnv
.getTestObject());
292 if ((SelectableChildCount
> 0) && multiSelection
) {
294 log
.print("isAccessibleChildSelected(1)? ");
295 isSelected
= oObj
.isAccessibleChildSelected(1);
296 log
.println(isSelected
);
299 log
.print("isAccessibleChildSelected(" + (childCount
- 1) +
301 isSelected
= oObj
.isAccessibleChildSelected(childCount
- 1);
302 log
.println(isSelected
);
304 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
305 log
.println("Unexpected exception");
306 e
.printStackTrace(log
);
311 tRes
.tested("selectAllAccessibleChildren()", res
);
315 * Calls the method. Clears accessible selection and calls the method again.
317 * Has OK status if the method returns number equal to number of accessible
318 * child count after first call if multiselection allowed, or
319 * 1 returned if multiselection not allowed.
320 * And if the method returns a zero after clearing selection.
322 public void _getSelectedAccessibleChildCount() {
323 log
.println("getSelectedAccessibleChildCount():");
325 if (multiSelection
) {
326 oObj
.selectAllAccessibleChildren();
328 int usedChildren
= childCount
;
330 if (childCount
> 500) {
331 log
.println("Restricting to 500");
335 if (usedChildren
> 0) {
337 for (int i
= 0; i
< usedChildren
; i
++) {
339 if (isSelectable(tEnv
.getTestObject(), i
)) {
340 log
.print("Trying to select child with index "+i
+": ");
341 oObj
.selectAccessibleChild(i
);
342 long curtime
= System
.currentTimeMillis();
343 long checktime
= System
.currentTimeMillis();
345 while (!oObj
.isAccessibleChildSelected(i
) && (checktime
-curtime
<5000)) {
346 checktime
= System
.currentTimeMillis();
352 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
353 log
.println("Unexpected exception");
354 e
.printStackTrace(log
);
359 int sCount
= chkSelectable(tEnv
.getTestObject());
360 log
.println("Found " + sCount
+ " selectable Children");
362 int selectedCount
= oObj
.getSelectedAccessibleChildCount();
363 log
.println("After selecting all accessible " + selectedCount
+
368 if (multiSelection
) {
369 res
&= (selectedCount
== sCount
);
371 res
&= (selectedCount
== 1);
374 log
.println("clearAccessibleSelection...");
375 oObj
.clearAccessibleSelection();
376 log
.print("getSelectedAccessibleChildCount: ");
377 selectedCount
= oObj
.getSelectedAccessibleChildCount();
378 log
.println(selectedCount
);
380 if (OneAlwaysSelected
) {
381 res
&= (selectedCount
== 1);
383 res
&= (selectedCount
== 0);
386 tRes
.tested("getSelectedAccessibleChildCount()", res
);
390 * Calls the method with wrong and correct indexes.
391 * Has OK status if exception was thrown for the wrong indexes,
392 * if exception wasn't thrown for the correct index and
393 * if the method have returned a not null for the correct index.
395 public void _getSelectedAccessibleChild() {
396 executeMethod("getSelectedAccessibleChildCount()");
399 int selectedCount
= oObj
.getSelectedAccessibleChildCount();
400 log
.println("getSelectedAccessibleChildCount: " + selectedCount
);
403 log
.println("getSelectedAccessibleChild(-1)");
404 oObj
.getSelectedAccessibleChild(-1);
405 log
.println("Exception was expected");
407 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
408 log
.println("Expected exception");
413 log
.println("getSelectedAccessibleChild(" + selectedCount
+ ")");
414 oObj
.getSelectedAccessibleChild(selectedCount
);
415 log
.println("Exception was expected");
417 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
418 log
.println("Expected exception");
422 int SelectableChildCount
= chkSelectable(tEnv
.getTestObject());
424 if (SelectableChildCount
> 500) {
425 SelectableChildCount
= 500;
428 if (SelectableChildCount
> 0) {
431 for (k
= 0; k
< SelectableChildCount
; k
++) {
432 log
.println("Trying to select child with index " + k
);
434 if (isSelectable(tEnv
.getTestObject(), k
)) {
435 oObj
.selectAccessibleChild(k
);
437 log
.println("selected child count: " +
438 oObj
.getSelectedAccessibleChildCount());
439 XAccessible selChild
= oObj
.getSelectedAccessibleChild(0);
440 res
&= (selChild
!= null);
441 log
.println("valid child - " + (selChild
!= null));
443 log
.println("Child isn't selectable");
446 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
447 log
.println("Unexpected exception: Last relevant calls:\n " +
448 "\toObj.selectAccessibleChild("+k
+")\n" +
449 "\toObj.getSelectedAccessibleChild(0)");
450 e
.printStackTrace(log
);
455 tRes
.tested("getSelectedAccessibleChild()", res
);
459 * Calls the method with wrong and with correct indexes.
460 * Has OK status if exceptions were thrown for the calls with
461 * the wrong indexes, if exception wasn't thrown for the call
462 * with correct index and if number of selected child was
463 * decreased after the correct call.
465 public void _deselectAccessibleChild() {
466 executeMethod("getSelectedAccessibleChild()");
469 int selCount
= oObj
.getSelectedAccessibleChildCount();
470 log
.println("getSelectedAccessibleChildCount():" + selCount
);
473 log
.println("deselectAccessibleChild(-1)");
474 oObj
.deselectAccessibleChild(-1);
475 log
.println("Exception was expected");
477 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
478 log
.println("Expected exception");
483 log
.println("deselectAccessibleChild(" + (childCount
+ 1) + ")");
484 oObj
.deselectAccessibleChild(childCount
+ 1);
485 log
.println("Exception was expected");
487 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
488 log
.println("Expected exception");
492 log
.println("#################");
493 log
.println("Selecting all accessible");
494 oObj
.selectAllAccessibleChildren();
495 selCount
= oObj
.getSelectedAccessibleChildCount();
496 log
.println("getSelectedAccessibleChildCount():" + selCount
);
498 if ((childCount
> 0) && (selCount
> 0)) {
500 int maxCount
= chkSelectable(tEnv
.getTestObject());
502 if (childCount
> 100) {
506 for (int k
= 0; k
< maxCount
; k
++) {
507 log
.println("deselectAccessibleChild(" + k
+ ")");
509 if (oObj
.isAccessibleChildSelected(k
)) {
510 oObj
.deselectAccessibleChild(k
);
514 int newSelCount
= oObj
.getSelectedAccessibleChildCount();
515 log
.println("getSelectedAccessibleChildCount():" +
518 if (OneAlwaysSelected
&& (selCount
== 1)) {
519 log
.println("One Child is always selected");
522 res
&= (selCount
> newSelCount
);
524 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
525 log
.println("Unexpected exception");
526 e
.printStackTrace(log
);
531 tRes
.tested("deselectAccessibleChild()", res
);
534 protected static int chkSelectable(Object Testcase
) {
536 XAccessibleContext accCon
= UnoRuntime
.queryInterface(
537 XAccessibleContext
.class, Testcase
);
538 int cc
= accCon
.getAccessibleChildCount();
544 for (int i
= 0; i
< cc
; i
++) {
546 if (accCon
.getAccessibleChild(i
).getAccessibleContext()
547 .getAccessibleStateSet()
548 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SELECTABLE
)) {
550 System
.out
.println("Child " + i
+ " is selectable");
552 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException iab
) {
559 protected static boolean isSelectable(Object Testcase
, int index
) {
560 XAccessibleContext accCon
= UnoRuntime
.queryInterface(
561 XAccessibleContext
.class, Testcase
);
565 if (accCon
.getAccessibleChild(index
).getAccessibleContext()
566 .getAccessibleStateSet()
567 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SELECTABLE
)) {
571 //selecting menuitems or the separator will lead to closing the menu
572 if ((accCon
.getAccessibleChild(index
).getAccessibleContext()
573 .getAccessibleRole() == AccessibleRole
.MENU_ITEM
) ||
574 (accCon
.getAccessibleChild(index
).getAccessibleContext()
575 .getAccessibleRole() == AccessibleRole
.SEPARATOR
)) {
578 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
579 System
.out
.println("Exception while checking for selectability");
585 private void shortWait() {
588 } catch (InterruptedException ex
) {