update credits
[LibreOffice.git] / qadevOOo / tests / java / ifc / accessibility / _XAccessibleSelection.java
bloba79282d895ea85a71bb9340c4fb6f2af9b23a7b4
1 /*
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;
27 import lib.Status;
28 import lib.StatusException;
31 /**
32 * Testing <code>com.sun.star.accessibility.XAccessibleSelection</code>
33 * interface methods :
34 * <ul>
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>
42 * </ul> <p>
44 * This test needs the following object relations :
45 * <ul>
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>
51 * </ul> <p>
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;
59 int childCount;
60 protected boolean multiSelection = true;
61 protected boolean OneAlwaysSelected = false;
63 // temporary while accessibility package is in com.sun.star
64 protected String getTestedClassName() {
65 return className;
68 /**
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);
78 if (xAC == null) {
79 throw new StatusException(Status.failed(
80 "Couldn't query XAccessibleContext. Test must be modified"));
83 Boolean b = (Boolean) tEnv.getObjRelation(
84 "XAccessibleSelection.multiSelection");
86 if (b != null) {
87 multiSelection = b.booleanValue();
90 Boolean b2 = (Boolean) tEnv.getObjRelation(
91 "XAccessibleSelection.OneAlwaysSelected");
93 if (b2 != null) {
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() {
108 boolean res = true;
110 try {
111 log.println("Try to select child with index " + childCount);
112 oObj.selectAccessibleChild(childCount);
113 log.println("Exception was expected");
114 res = false;
115 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
116 log.println("Expected exception");
117 res = true;
120 try {
121 log.println("Try to select child with index -1");
122 oObj.selectAccessibleChild(-1);
123 log.println("Exception was expected");
124 res = false;
125 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
126 log.println("Expected exception");
127 res &= true;
130 log.println("ChildCount: " + childCount);
132 int usedChildren = childCount;
134 if (childCount > 500) {
135 log.println("Restricting to 500");
136 usedChildren = 500;
139 if (usedChildren > 0) {
140 try {
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);
146 log.println("OK");
147 } else {
148 log.println("Child isn't selectable");
152 res &= true;
153 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
154 log.println("Unexpected exception");
155 e.printStackTrace(log);
156 res = false;
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()");
171 boolean res = true;
172 boolean isSelected = false;
174 try {
175 log.print("isAccessibleChildSelected(-1)? ");
176 isSelected = oObj.isAccessibleChildSelected(-1);
177 log.println(res);
178 log.println("Exception was expected");
179 res = false;
180 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
181 log.println("Expected exception");
182 res = true;
185 try {
186 log.print("isAccessibleChildSelected(" + childCount + ")? ");
187 isSelected = oObj.isAccessibleChildSelected(childCount);
188 log.println(isSelected);
189 log.println("Exception was expected");
190 res = false;
191 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
192 log.println("Expected exception");
193 res &= true;
196 int SelectableChildCount = chkSelectable(tEnv.getTestObject());
198 if (SelectableChildCount > 500) {
199 SelectableChildCount = 500;
202 log.println("SelectableChildCount: " + SelectableChildCount);
204 if (SelectableChildCount > 0) {
205 try {
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);
213 shortWait();
214 isSelected = oObj.isAccessibleChildSelected(k);
215 log.println("isAccessibleChildSelected - " +
216 isSelected);
217 res &= isSelected;
218 } else {
219 log.println("Child isn't selectable");
222 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
223 log.println("Unexpected exception");
224 e.printStackTrace(log);
225 res = false;
229 tRes.tested("isAccessibleChildSelected()", res);
233 * Calls the method.
234 * Has OK status if the method <code>isAccessibleChildSelected()</code>
235 * returned <code>false</code>.
237 public void _clearAccessibleSelection() {
238 executeMethod("isAccessibleChildSelected()");
240 boolean res = true;
242 log.println("clearAccessibleSelection");
243 oObj.clearAccessibleSelection();
246 // clearAccessibleSelection() call is oneway so we need
247 // some waiting
248 shortWait();
250 if ((childCount > 0) && !OneAlwaysSelected) {
251 try {
252 log.print("isAccessibleChildSelected(" + (childCount - 1) +
253 ")? ");
255 boolean isSel = oObj.isAccessibleChildSelected(childCount - 1);
256 log.println(isSel);
257 res = !isSel;
258 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
259 log.println("Unexpected exception");
260 e.printStackTrace(log);
261 res = false;
263 } else if (OneAlwaysSelected) {
264 log.println("Can't clear selection, one child is always selected");
267 tRes.tested("clearAccessibleSelection()", res);
271 * Calls the method.
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
284 // some waiting
285 shortWait();
287 boolean res = true;
288 boolean isSelected = true;
290 int SelectableChildCount = chkSelectable(tEnv.getTestObject());
292 if ((SelectableChildCount > 0) && multiSelection) {
293 try {
294 log.print("isAccessibleChildSelected(1)? ");
295 isSelected = oObj.isAccessibleChildSelected(1);
296 log.println(isSelected);
297 res = isSelected;
299 log.print("isAccessibleChildSelected(" + (childCount - 1) +
300 ")? ");
301 isSelected = oObj.isAccessibleChildSelected(childCount - 1);
302 log.println(isSelected);
303 res &= isSelected;
304 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
305 log.println("Unexpected exception");
306 e.printStackTrace(log);
307 res = false;
311 tRes.tested("selectAllAccessibleChildren()", res);
315 * Calls the method. Clears accessible selection and calls the method again.
316 * <p>
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();
327 } else {
328 int usedChildren = childCount;
330 if (childCount > 500) {
331 log.println("Restricting to 500");
332 usedChildren = 500;
335 if (usedChildren > 0) {
336 try {
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();
349 log.println("OK");
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 +
364 " are selected");
366 boolean res = true;
368 if (multiSelection) {
369 res &= (selectedCount == sCount);
370 } else {
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);
382 } else {
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()");
398 boolean res = true;
399 int selectedCount = oObj.getSelectedAccessibleChildCount();
400 log.println("getSelectedAccessibleChildCount: " + selectedCount);
402 try {
403 log.println("getSelectedAccessibleChild(-1)");
404 oObj.getSelectedAccessibleChild(-1);
405 log.println("Exception was expected");
406 res = false;
407 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
408 log.println("Expected exception");
409 res = true;
412 try {
413 log.println("getSelectedAccessibleChild(" + selectedCount + ")");
414 oObj.getSelectedAccessibleChild(selectedCount);
415 log.println("Exception was expected");
416 res &= false;
417 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
418 log.println("Expected exception");
419 res &= true;
422 int SelectableChildCount = chkSelectable(tEnv.getTestObject());
424 if (SelectableChildCount > 500) {
425 SelectableChildCount = 500;
428 if (SelectableChildCount > 0) {
429 int k = 0;
430 try {
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);
436 shortWait();
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));
442 } else {
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);
451 res = false;
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()");
468 boolean res = true;
469 int selCount = oObj.getSelectedAccessibleChildCount();
470 log.println("getSelectedAccessibleChildCount():" + selCount);
472 try {
473 log.println("deselectAccessibleChild(-1)");
474 oObj.deselectAccessibleChild(-1);
475 log.println("Exception was expected");
476 res = false;
477 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
478 log.println("Expected exception");
479 res &= true;
482 try {
483 log.println("deselectAccessibleChild(" + (childCount + 1) + ")");
484 oObj.deselectAccessibleChild(childCount + 1);
485 log.println("Exception was expected");
486 res = false;
487 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
488 log.println("Expected exception");
489 res &= true;
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)) {
499 try {
500 int maxCount = chkSelectable(tEnv.getTestObject());
502 if (childCount > 100) {
503 maxCount = 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():" +
516 newSelCount);
518 if (OneAlwaysSelected && (selCount == 1)) {
519 log.println("One Child is always selected");
520 res &= true;
521 } else {
522 res &= (selCount > newSelCount);
524 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
525 log.println("Unexpected exception");
526 e.printStackTrace(log);
527 res = false;
531 tRes.tested("deselectAccessibleChild()", res);
534 protected static int chkSelectable(Object Testcase) {
535 int ret = 0;
536 XAccessibleContext accCon = UnoRuntime.queryInterface(
537 XAccessibleContext.class, Testcase);
538 int cc = accCon.getAccessibleChildCount();
540 if (cc > 500) {
541 return cc;
544 for (int i = 0; i < cc; i++) {
545 try {
546 if (accCon.getAccessibleChild(i).getAccessibleContext()
547 .getAccessibleStateSet()
548 .contains(com.sun.star.accessibility.AccessibleStateType.SELECTABLE)) {
549 ret = ret + 1;
550 System.out.println("Child " + i + " is selectable");
552 } catch (com.sun.star.lang.IndexOutOfBoundsException iab) {
556 return ret;
559 protected static boolean isSelectable(Object Testcase, int index) {
560 XAccessibleContext accCon = UnoRuntime.queryInterface(
561 XAccessibleContext.class, Testcase);
562 boolean res = false;
564 try {
565 if (accCon.getAccessibleChild(index).getAccessibleContext()
566 .getAccessibleStateSet()
567 .contains(com.sun.star.accessibility.AccessibleStateType.SELECTABLE)) {
568 res = true;
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)) {
576 res = false;
578 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
579 System.out.println("Exception while checking for selectability");
582 return res;
585 private void shortWait() {
586 try {
587 Thread.sleep(500);
588 } catch (InterruptedException ex) {