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
65 protected String
getTestedClassName() {
70 * Retrieves the interface <code>XAccessibleContext</code>
71 * and object relation.
72 * @see com.sun.star.accessibility.XAccessibleContext
73 * @see ifc.accessibility._XAccessibleContext
76 protected void before() {
77 xAC
= UnoRuntime
.queryInterface(
78 XAccessibleContext
.class, oObj
);
81 throw new StatusException(Status
.failed(
82 "Couldn't query XAccessibleContext. Test must be modified"));
85 Boolean b
= (Boolean
) tEnv
.getObjRelation(
86 "XAccessibleSelection.multiSelection");
89 multiSelection
= b
.booleanValue();
92 Boolean b2
= (Boolean
) tEnv
.getObjRelation(
93 "XAccessibleSelection.OneAlwaysSelected");
96 OneAlwaysSelected
= b2
.booleanValue();
99 childCount
= xAC
.getAccessibleChildCount();
100 log
.println("Child count: " + childCount
);
104 * Selects accessible child with index some wrong indexes
105 * and with legal index.
106 * Has OK status if exception was thrown for wrong indexes
107 * and if exception wasn't thrown for correct index.
109 public void _selectAccessibleChild() {
113 log
.println("Try to select child with index " + childCount
);
114 oObj
.selectAccessibleChild(childCount
);
115 log
.println("Exception was expected");
117 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
118 log
.println("Expected exception");
123 log
.println("Try to select child with index -1");
124 oObj
.selectAccessibleChild(-1);
125 log
.println("Exception was expected");
127 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
128 log
.println("Expected exception");
132 log
.println("ChildCount: " + childCount
);
134 int usedChildren
= childCount
;
136 if (childCount
> 500) {
137 log
.println("Restricting to 500");
141 if (usedChildren
> 0) {
143 for (int i
= 0; i
< usedChildren
; i
++) {
144 log
.print("Trying to select child with index " + i
+ ": ");
146 if (isSelectable(tEnv
.getTestObject(), i
)) {
147 oObj
.selectAccessibleChild(i
);
150 log
.println("Child isn't selectable");
155 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
156 log
.println("Unexpected exception");
157 e
.printStackTrace(log
);
162 tRes
.tested("selectAccessibleChild()", res
);
166 * Calls the method with the wrong index and with the correct index.
167 * Has OK status if exception was thrown for wrong index and
168 * if exception wasn't thrown for the correct index.
170 public void _isAccessibleChildSelected() {
171 executeMethod("selectAccessibleChild()");
174 boolean isSelected
= false;
177 log
.print("isAccessibleChildSelected(-1)? ");
178 isSelected
= oObj
.isAccessibleChildSelected(-1);
180 log
.println("Exception was expected");
182 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
183 log
.println("Expected exception");
188 log
.print("isAccessibleChildSelected(" + childCount
+ ")? ");
189 isSelected
= oObj
.isAccessibleChildSelected(childCount
);
190 log
.println(isSelected
);
191 log
.println("Exception was expected");
193 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
194 log
.println("Expected exception");
198 int SelectableChildCount
= chkSelectable(tEnv
.getTestObject());
200 if (SelectableChildCount
> 500) {
201 SelectableChildCount
= 500;
204 log
.println("SelectableChildCount: " + SelectableChildCount
);
206 if (SelectableChildCount
> 0) {
208 oObj
.selectAllAccessibleChildren();
210 for (int k
= 0; k
< SelectableChildCount
; k
++) {
211 log
.println("Trying to select child with index " + k
);
213 if (isSelectable(tEnv
.getTestObject(), k
)) {
214 oObj
.selectAccessibleChild(k
);
215 util
.utils
.shortWait();
216 isSelected
= oObj
.isAccessibleChildSelected(k
);
217 log
.println("isAccessibleChildSelected - " +
221 log
.println("Child isn't selectable");
224 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
225 log
.println("Unexpected exception");
226 e
.printStackTrace(log
);
231 tRes
.tested("isAccessibleChildSelected()", res
);
236 * Has OK status if the method <code>isAccessibleChildSelected()</code>
237 * returned <code>false</code>.
239 public void _clearAccessibleSelection() {
240 executeMethod("isAccessibleChildSelected()");
244 log
.println("clearAccessibleSelection");
245 oObj
.clearAccessibleSelection();
248 // clearAccessibleSelection() call is oneway so we need
250 util
.utils
.shortWait();
252 if ((childCount
> 0) && !OneAlwaysSelected
) {
254 log
.print("isAccessibleChildSelected(" + (childCount
- 1) +
257 boolean isSel
= oObj
.isAccessibleChildSelected(childCount
- 1);
260 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
261 log
.println("Unexpected exception");
262 e
.printStackTrace(log
);
265 } else if (OneAlwaysSelected
) {
266 log
.println("Can't clear selection, one child is always selected");
269 tRes
.tested("clearAccessibleSelection()", res
);
274 * Has OK status if the method <code>isAccessibleChildSelected()</code>
275 * returns <code>true</code> for first and for last accessible child
276 * or if multiselection is not allowed.
278 public void _selectAllAccessibleChildren() {
279 executeMethod("clearAccessibleSelection()");
281 log
.println("selectAllAccessibleChildren...");
282 oObj
.selectAllAccessibleChildren();
285 // selectAllAccessibleChildren() call is oneway so we need
287 util
.utils
.shortWait();
290 boolean isSelected
= true;
292 int SelectableChildCount
= chkSelectable(tEnv
.getTestObject());
294 if ((SelectableChildCount
> 0) && multiSelection
) {
296 log
.print("isAccessibleChildSelected(1)? ");
297 isSelected
= oObj
.isAccessibleChildSelected(1);
298 log
.println(isSelected
);
301 log
.print("isAccessibleChildSelected(" + (childCount
- 1) +
303 isSelected
= oObj
.isAccessibleChildSelected(childCount
- 1);
304 log
.println(isSelected
);
306 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
307 log
.println("Unexpected exception");
308 e
.printStackTrace(log
);
313 tRes
.tested("selectAllAccessibleChildren()", res
);
317 * Calls the method. Clears accessible selection and calls the method again.
319 * Has OK status if the method returns number equal to number of accessible
320 * child count after first call if multiselection allowed, or
321 * 1 returned if multiselection not allowed.
322 * And if the method returns a zero after clearing selection.
324 public void _getSelectedAccessibleChildCount() {
325 log
.println("getSelectedAccessibleChildCount():");
327 if (multiSelection
) {
328 oObj
.selectAllAccessibleChildren();
330 int usedChildren
= childCount
;
332 if (childCount
> 500) {
333 log
.println("Restricting to 500");
337 if (usedChildren
> 0) {
339 for (int i
= 0; i
< usedChildren
; i
++) {
341 if (isSelectable(tEnv
.getTestObject(), i
)) {
342 log
.print("Trying to select child with index "+i
+": ");
343 oObj
.selectAccessibleChild(i
);
344 long curtime
= System
.currentTimeMillis();
345 long checktime
= System
.currentTimeMillis();
347 while (!oObj
.isAccessibleChildSelected(i
) && (checktime
-curtime
<5000)) {
348 checktime
= System
.currentTimeMillis();
354 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
355 log
.println("Unexpected exception");
356 e
.printStackTrace(log
);
361 int sCount
= chkSelectable(tEnv
.getTestObject());
362 log
.println("Found " + sCount
+ " selectable Children");
364 int selectedCount
= oObj
.getSelectedAccessibleChildCount();
365 log
.println("After selecting all accessible " + selectedCount
+
370 if (multiSelection
) {
371 res
&= (selectedCount
== sCount
);
373 res
&= (selectedCount
== 1);
376 log
.println("clearAccessibleSelection...");
377 oObj
.clearAccessibleSelection();
378 log
.print("getSelectedAccessibleChildCount: ");
379 selectedCount
= oObj
.getSelectedAccessibleChildCount();
380 log
.println(selectedCount
);
382 if (OneAlwaysSelected
) {
383 res
&= (selectedCount
== 1);
385 res
&= (selectedCount
== 0);
388 tRes
.tested("getSelectedAccessibleChildCount()", res
);
392 * Calls the method with wrong and correct indexes.
393 * Has OK status if exception was thrown for the wrong indexes,
394 * if exception wasn't thrown for the correct index and
395 * if the method have returned a not null for the correct index.
397 public void _getSelectedAccessibleChild() {
398 executeMethod("getSelectedAccessibleChildCount()");
401 int selectedCount
= oObj
.getSelectedAccessibleChildCount();
402 log
.println("getSelectedAccessibleChildCount: " + selectedCount
);
405 log
.println("getSelectedAccessibleChild(-1)");
406 oObj
.getSelectedAccessibleChild(-1);
407 log
.println("Exception was expected");
409 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
410 log
.println("Expected exception");
415 log
.println("getSelectedAccessibleChild(" + selectedCount
+ ")");
416 oObj
.getSelectedAccessibleChild(selectedCount
);
417 log
.println("Exception was expected");
419 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
420 log
.println("Expected exception");
424 int SelectableChildCount
= chkSelectable(tEnv
.getTestObject());
426 if (SelectableChildCount
> 500) {
427 SelectableChildCount
= 500;
430 if (SelectableChildCount
> 0) {
433 for (k
= 0; k
< SelectableChildCount
; k
++) {
434 log
.println("Trying to select child with index " + k
);
436 if (isSelectable(tEnv
.getTestObject(), k
)) {
437 oObj
.selectAccessibleChild(k
);
438 util
.utils
.shortWait();
439 log
.println("selected child count: " +
440 oObj
.getSelectedAccessibleChildCount());
441 XAccessible selChild
= oObj
.getSelectedAccessibleChild(0);
442 res
&= (selChild
!= null);
443 log
.println("valid child - " + (selChild
!= null));
445 log
.println("Child isn't selectable");
448 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
449 log
.println("Unexpected exception: Last relevant calls:\n " +
450 "\toObj.selectAccessibleChild("+k
+")\n" +
451 "\toObj.getSelectedAccessibleChild(0)");
452 e
.printStackTrace(log
);
457 tRes
.tested("getSelectedAccessibleChild()", res
);
461 * Calls the method with wrong and with correct indexes.
462 * Has OK status if exceptions were thrown for the calls with
463 * the wrong indexes, if exception wasn't thrown for the call
464 * with correct index and if number of selected child was
465 * decreased after the correct call.
467 public void _deselectAccessibleChild() {
468 executeMethod("getSelectedAccessibleChild()");
471 int selCount
= oObj
.getSelectedAccessibleChildCount();
472 log
.println("getSelectedAccessibleChildCount():" + selCount
);
475 log
.println("deselectAccessibleChild(-1)");
476 oObj
.deselectAccessibleChild(-1);
477 log
.println("Exception was expected");
479 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
480 log
.println("Expected exception");
485 log
.println("deselectAccessibleChild(" + (childCount
+ 1) + ")");
486 oObj
.deselectAccessibleChild(childCount
+ 1);
487 log
.println("Exception was expected");
489 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
490 log
.println("Expected exception");
494 log
.println("#################");
495 log
.println("Selecting all accessible");
496 oObj
.selectAllAccessibleChildren();
497 selCount
= oObj
.getSelectedAccessibleChildCount();
498 log
.println("getSelectedAccessibleChildCount():" + selCount
);
500 if ((childCount
> 0) && (selCount
> 0)) {
502 int maxCount
= chkSelectable(tEnv
.getTestObject());
504 if (childCount
> 100) {
508 for (int k
= 0; k
< maxCount
; k
++) {
509 log
.println("deselectAccessibleChild(" + k
+ ")");
511 if (oObj
.isAccessibleChildSelected(k
)) {
512 oObj
.deselectAccessibleChild(k
);
516 int newSelCount
= oObj
.getSelectedAccessibleChildCount();
517 log
.println("getSelectedAccessibleChildCount():" +
520 if (OneAlwaysSelected
&& (selCount
== 1)) {
521 log
.println("One Child is always selected");
524 res
&= (selCount
> newSelCount
);
526 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
527 log
.println("Unexpected exception");
528 e
.printStackTrace(log
);
533 tRes
.tested("deselectAccessibleChild()", res
);
536 protected static int chkSelectable(Object Testcase
) {
538 XAccessibleContext accCon
= UnoRuntime
.queryInterface(
539 XAccessibleContext
.class, Testcase
);
540 int cc
= accCon
.getAccessibleChildCount();
546 for (int i
= 0; i
< cc
; i
++) {
548 if (accCon
.getAccessibleChild(i
).getAccessibleContext()
549 .getAccessibleStateSet()
550 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SELECTABLE
)) {
552 System
.out
.println("Child " + i
+ " is selectable");
554 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException iab
) {
561 protected static boolean isSelectable(Object Testcase
, int index
) {
562 XAccessibleContext accCon
= UnoRuntime
.queryInterface(
563 XAccessibleContext
.class, Testcase
);
567 if (accCon
.getAccessibleChild(index
).getAccessibleContext()
568 .getAccessibleStateSet()
569 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SELECTABLE
)) {
573 //selecting menuitems or the separator will lead to closing the menu
574 if ((accCon
.getAccessibleChild(index
).getAccessibleContext()
575 .getAccessibleRole() == AccessibleRole
.MENU_ITEM
) ||
576 (accCon
.getAccessibleChild(index
).getAccessibleContext()
577 .getAccessibleRole() == AccessibleRole
.SEPARATOR
)) {
580 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
581 System
.out
.println("Exception while checking for selectability");