1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XAccessibleSelection.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package ifc
.accessibility
;
32 import com
.sun
.star
.accessibility
.AccessibleRole
;
33 import com
.sun
.star
.accessibility
.XAccessible
;
34 import com
.sun
.star
.accessibility
.XAccessibleContext
;
35 import com
.sun
.star
.accessibility
.XAccessibleSelection
;
36 import com
.sun
.star
.uno
.UnoRuntime
;
38 import lib
.MultiMethodTest
;
40 import lib
.StatusException
;
44 * Testing <code>com.sun.star.accessibility.XAccessibleSelection</code>
47 * <li><code>selectAccessibleChild()</code></li>
48 * <li><code>isAccessibleChildSelected()</code></li>
49 * <li><code>clearAccessibleSelection()</code></li>
50 * <li><code>selectAllAccessibleChildren()</code></li>
51 * <li><code>getSelectedAccessibleChildCount()</code></li>
52 * <li><code>getSelectedAccessibleChild()</code></li>
53 * <li><code>deselectAccessibleChild()</code></li>
56 * This test needs the following object relations :
58 * <li> <code>'XAccessibleSelection.multiSelection'</code>
59 * (of type <code>Boolean</code>) <b> optional </b>:
60 * Indicates whether or not mutiply children could be selected.
61 * If the relation is <code>false</code> then more than 1 child
62 * couldn't be selected. </li>
65 * @see com.sun.star.accessibility.XAccessibleSelection
67 public class _XAccessibleSelection
extends MultiMethodTest
{
68 private static final String className
= "com.sun.star.accessibility.XAccessibleSelection";
69 public XAccessibleSelection oObj
= null;
70 XAccessibleContext xAC
= null;
72 protected boolean multiSelection
= true;
73 protected boolean OneAlwaysSelected
= false;
75 // temporary while accessibility package is in com.sun.star
76 protected String
getTestedClassName() {
81 * Retrieves the interface <code>XAccessibleContext</code>
82 * and object relation.
83 * @see com.sun.star.accessibility.XAccessibleContext
84 * @see ifc.accessibility.XAccessibleContext
86 protected void before() {
87 xAC
= (XAccessibleContext
) UnoRuntime
.queryInterface(
88 XAccessibleContext
.class, oObj
);
91 throw new StatusException(Status
.failed(
92 "Couldn't query XAccessibleContext. Test must be modified"));
95 Boolean b
= (Boolean
) tEnv
.getObjRelation(
96 "XAccessibleSelection.multiSelection");
99 multiSelection
= b
.booleanValue();
102 Boolean b2
= (Boolean
) tEnv
.getObjRelation(
103 "XAccessibleSelection.OneAlwaysSelected");
106 OneAlwaysSelected
= b2
.booleanValue();
109 childCount
= xAC
.getAccessibleChildCount();
110 log
.println("Child count: " + childCount
);
114 * Selects accessible child with index some wrong indexes
115 * and with legal index.
116 * Has OK status if exception was thrown for wrong indexes
117 * and if exception wasn't thrown for correct index.
119 public void _selectAccessibleChild() {
123 log
.println("Try to select child with index " + childCount
);
124 oObj
.selectAccessibleChild(childCount
);
125 log
.println("Exception was expected");
127 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
128 log
.println("Expected exception");
133 log
.println("Try to select child with index -1");
134 oObj
.selectAccessibleChild(-1);
135 log
.println("Exception was expected");
137 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
138 log
.println("Expected exception");
142 log
.println("ChildCount: " + childCount
);
144 int usedChilds
= childCount
;
146 if (childCount
> 500) {
147 log
.println("Restricting to 500");
151 if (usedChilds
> 0) {
153 for (int i
= 0; i
< usedChilds
; i
++) {
154 log
.print("Trying to select child with index " + i
+ ": ");
156 if (isSelectable(tEnv
.getTestObject(), i
)) {
157 oObj
.selectAccessibleChild(i
);
160 log
.println("Child isn't selectable");
165 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
166 log
.println("Unexpected exception");
167 e
.printStackTrace(log
);
172 tRes
.tested("selectAccessibleChild()", res
);
176 * Calls the method with the wrong index and with the correct index.
177 * Has OK status if exception was thrown for wrong index and
178 * if exception wasn't thrown for the correct index.
180 public void _isAccessibleChildSelected() {
181 executeMethod("selectAccessibleChild()");
184 boolean isSelected
= false;
187 log
.print("isAccessibleChildSelected(-1)? ");
188 isSelected
= oObj
.isAccessibleChildSelected(-1);
190 log
.println("Exception was expected");
192 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
193 log
.println("Expected exception");
198 log
.print("isAccessibleChildSelected(" + childCount
+ ")? ");
199 isSelected
= oObj
.isAccessibleChildSelected(childCount
);
200 log
.println(isSelected
);
201 log
.println("Exception was expected");
203 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
204 log
.println("Expected exception");
208 int SelectableChildCount
= chkSelectable(tEnv
.getTestObject());
210 if (SelectableChildCount
> 500) {
211 SelectableChildCount
= 500;
214 log
.println("SelectableChildCount: " + SelectableChildCount
);
216 if (SelectableChildCount
> 0) {
218 oObj
.selectAllAccessibleChildren();
220 for (int k
= 0; k
< SelectableChildCount
; k
++) {
221 log
.println("Trying to select child with index " + k
);
223 if (isSelectable(tEnv
.getTestObject(), k
)) {
224 oObj
.selectAccessibleChild(k
);
226 isSelected
= oObj
.isAccessibleChildSelected(k
);
227 log
.println("isAccessibleChildSelected - " +
231 log
.println("Child isn't selectable");
234 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
235 log
.println("Unexpected exception");
236 e
.printStackTrace(log
);
241 tRes
.tested("isAccessibleChildSelected()", res
);
246 * Has OK status if the method <code>isAccessibleChildSelected()</code>
247 * returned <code>false</code>.
249 public void _clearAccessibleSelection() {
250 executeMethod("isAccessibleChildSelected()");
254 log
.println("clearAccessibleSelection");
255 oObj
.clearAccessibleSelection();
258 // clearAccessibleSelection() call is oneway so we need
262 if ((childCount
> 0) && !OneAlwaysSelected
) {
264 log
.print("isAccessibleChildSelected(" + (childCount
- 1) +
267 boolean isSel
= oObj
.isAccessibleChildSelected(childCount
- 1);
270 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
271 log
.println("Unexpected exception");
272 e
.printStackTrace(log
);
275 } else if (OneAlwaysSelected
) {
276 log
.println("Can't clear selection, one child is always selected");
279 tRes
.tested("clearAccessibleSelection()", res
);
284 * Has OK status if the method <code>isAccessibleChildSelected()</code>
285 * returns <code>true</code> for first and for last accessible child
286 * or if multiselection is not allowed.
288 public void _selectAllAccessibleChildren() {
289 executeMethod("clearAccessibleSelection()");
291 log
.println("selectAllAccessibleChildren...");
292 oObj
.selectAllAccessibleChildren();
295 // selectAllAccessibleChildren() call is oneway so we need
300 boolean isSelected
= true;
302 int SelectableChildCount
= chkSelectable(tEnv
.getTestObject());
304 if ((SelectableChildCount
> 0) && multiSelection
) {
306 log
.print("isAccessibleChildSelected(1)? ");
307 isSelected
= oObj
.isAccessibleChildSelected(1);
308 log
.println(isSelected
);
311 log
.print("isAccessibleChildSelected(" + (childCount
- 1) +
313 isSelected
= oObj
.isAccessibleChildSelected(childCount
- 1);
314 log
.println(isSelected
);
316 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
317 log
.println("Unexpected exception");
318 e
.printStackTrace(log
);
323 tRes
.tested("selectAllAccessibleChildren()", res
);
327 * Calls the method. Clears accessible selection and calls the method again.
329 * Has OK status if the method returns number equal to number of accessible
330 * child count after first call if multiselection allowed, or
331 * 1 returned if multiselection not allowed.
332 * And if the method returns a zero after clearing selection.
334 public void _getSelectedAccessibleChildCount() {
335 log
.println("getSelectedAccessibleChildCount():");
337 if (multiSelection
) {
338 oObj
.selectAllAccessibleChildren();
340 int usedChilds
= childCount
;
342 if (childCount
> 500) {
343 log
.println("Restricting to 500");
347 if (usedChilds
> 0) {
349 for (int i
= 0; i
< usedChilds
; i
++) {
351 if (isSelectable(tEnv
.getTestObject(), i
)) {
352 log
.print("Trying to select child with index "+i
+": ");
353 oObj
.selectAccessibleChild(i
);
354 long curtime
= System
.currentTimeMillis();
355 long checktime
= System
.currentTimeMillis();
357 while (!oObj
.isAccessibleChildSelected(i
) && (checktime
-curtime
<5000)) {
358 checktime
= System
.currentTimeMillis();
364 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
365 log
.println("Unexpected exception");
366 e
.printStackTrace(log
);
371 int sCount
= chkSelectable(tEnv
.getTestObject());
372 log
.println("Found " + sCount
+ " selectable Childs");
374 int selectedCount
= oObj
.getSelectedAccessibleChildCount();
375 log
.println("After selecting all accessible " + selectedCount
+
380 if (multiSelection
) {
381 res
&= (selectedCount
== sCount
);
383 res
&= (selectedCount
== 1);
386 log
.println("clearAccessibleSelection...");
387 oObj
.clearAccessibleSelection();
388 log
.print("getSelectedAccessibleChildCount: ");
389 selectedCount
= oObj
.getSelectedAccessibleChildCount();
390 log
.println(selectedCount
);
392 if (OneAlwaysSelected
) {
393 res
&= (selectedCount
== 1);
395 res
&= (selectedCount
== 0);
398 tRes
.tested("getSelectedAccessibleChildCount()", res
);
402 * Calls the method with wrong and correct indexes.
403 * Has OK status if exception was thrown for the wrong indexes,
404 * if exception wasn't thrown for the correct index and
405 * if the method have returned a not null for the correct index.
407 public void _getSelectedAccessibleChild() {
408 executeMethod("getSelectedAccessibleChildCount()");
411 int selectedCount
= oObj
.getSelectedAccessibleChildCount();
412 log
.println("getSelectedAccessibleChildCount: " + selectedCount
);
415 log
.println("getSelectedAccessibleChild(-1)");
416 oObj
.getSelectedAccessibleChild(-1);
417 log
.println("Exception was expected");
419 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
420 log
.println("Expected exception");
425 log
.println("getSelectedAccessibleChild(" + selectedCount
+ ")");
426 oObj
.getSelectedAccessibleChild(selectedCount
);
427 log
.println("Exception was expected");
429 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
430 log
.println("Expected exception");
434 int SelectableChildCount
= chkSelectable(tEnv
.getTestObject());
436 if (SelectableChildCount
> 500) {
437 SelectableChildCount
= 500;
440 if (SelectableChildCount
> 0) {
443 for (k
= 0; k
< SelectableChildCount
; k
++) {
444 log
.println("Trying to select child with index " + k
);
446 if (isSelectable(tEnv
.getTestObject(), k
)) {
447 oObj
.selectAccessibleChild(k
);
449 log
.println("selected child count: " +
450 oObj
.getSelectedAccessibleChildCount());
451 XAccessible selChild
= oObj
.getSelectedAccessibleChild(0);
452 res
&= (selChild
!= null);
453 log
.println("valid child - " + (selChild
!= null));
455 log
.println("Child isn't selectable");
458 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
459 log
.println("Unexpected exception: Last relevant calls:\n " +
460 "\toObj.selectAccessibleChild("+k
+")\n" +
461 "\toObj.getSelectedAccessibleChild(0)");
462 e
.printStackTrace(log
);
467 tRes
.tested("getSelectedAccessibleChild()", res
);
471 * Calls the method with wrong and with correct indexes.
472 * Has OK status if exceptions were thrown for the calls with
473 * the wrong indexes, if exception wasn't thrown for the call
474 * with correct index and if number of selected child was
475 * decreased after the correct call.
477 public void _deselectAccessibleChild() {
478 executeMethod("getSelectedAccessibleChild()");
481 int selCount
= oObj
.getSelectedAccessibleChildCount();
482 log
.println("getSelectedAccessibleChildCount():" + selCount
);
485 log
.println("deselectAccessibleChild(-1)");
486 oObj
.deselectAccessibleChild(-1);
487 log
.println("Exception was expected");
489 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
490 log
.println("Expected exception");
495 log
.println("deselectAccessibleChild(" + (childCount
+ 1) + ")");
496 oObj
.deselectAccessibleChild(childCount
+ 1);
497 log
.println("Exception was expected");
499 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
500 log
.println("Expected exception");
504 log
.println("#################");
505 log
.println("Selecting all accessible");
506 oObj
.selectAllAccessibleChildren();
507 selCount
= oObj
.getSelectedAccessibleChildCount();
508 log
.println("getSelectedAccessibleChildCount():" + selCount
);
510 if ((childCount
> 0) && (selCount
> 0)) {
512 int maxCount
= chkSelectable(tEnv
.getTestObject());
514 if (childCount
> 100) {
518 for (int k
= 0; k
< maxCount
; k
++) {
519 log
.println("deselectAccessibleChild(" + k
+ ")");
521 if (oObj
.isAccessibleChildSelected(k
)) {
522 oObj
.deselectAccessibleChild(k
);
526 int newSelCount
= oObj
.getSelectedAccessibleChildCount();
527 log
.println("getSelectedAccessibleChildCount():" +
530 if (OneAlwaysSelected
&& (selCount
== 1)) {
531 log
.println("One Child is always selected");
534 res
&= (selCount
> newSelCount
);
536 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
537 log
.println("Unexpected exception");
538 e
.printStackTrace(log
);
543 tRes
.tested("deselectAccessibleChild()", res
);
546 protected static int chkSelectable(Object Testcase
) {
548 XAccessibleContext accCon
= (XAccessibleContext
) UnoRuntime
.queryInterface(
549 XAccessibleContext
.class, Testcase
);
550 int cc
= accCon
.getAccessibleChildCount();
556 for (int i
= 0; i
< cc
; i
++) {
558 if (accCon
.getAccessibleChild(i
).getAccessibleContext()
559 .getAccessibleStateSet()
560 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SELECTABLE
)) {
562 System
.out
.println("Child " + i
+ " is selectable");
564 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException iab
) {
571 protected static boolean isSelectable(Object Testcase
, int index
) {
572 XAccessibleContext accCon
= (XAccessibleContext
) UnoRuntime
.queryInterface(
573 XAccessibleContext
.class, Testcase
);
577 if (accCon
.getAccessibleChild(index
).getAccessibleContext()
578 .getAccessibleStateSet()
579 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SELECTABLE
)) {
583 //selecting menuitems or the separator will lead to closing the menu
584 if ((accCon
.getAccessibleChild(index
).getAccessibleContext()
585 .getAccessibleRole() == AccessibleRole
.MENU_ITEM
) ||
586 (accCon
.getAccessibleChild(index
).getAccessibleContext()
587 .getAccessibleRole() == AccessibleRole
.SEPARATOR
)) {
590 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
591 System
.out
.println("Exception while checking for selectability");
597 private void shortWait() {
600 } catch (InterruptedException ex
) {