bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / accessibility / _XAccessibleSelection.java
blobfdb7d2fb8c89cbdee41038ba2f226dcac66dbe06
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 @Override
65 protected String getTestedClassName() {
66 return className;
69 /**
70 * Retrieves the interface <code>XAccessibleContext</code>
71 * and object relation.
72 * @see com.sun.star.accessibility.XAccessibleContext
73 * @see ifc.accessibility._XAccessibleContext
75 @Override
76 protected void before() {
77 xAC = UnoRuntime.queryInterface(
78 XAccessibleContext.class, oObj);
80 if (xAC == null) {
81 throw new StatusException(Status.failed(
82 "Couldn't query XAccessibleContext. Test must be modified"));
85 Boolean b = (Boolean) tEnv.getObjRelation(
86 "XAccessibleSelection.multiSelection");
88 if (b != null) {
89 multiSelection = b.booleanValue();
92 Boolean b2 = (Boolean) tEnv.getObjRelation(
93 "XAccessibleSelection.OneAlwaysSelected");
95 if (b2 != null) {
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() {
110 boolean res = true;
112 try {
113 log.println("Try to select child with index " + childCount);
114 oObj.selectAccessibleChild(childCount);
115 log.println("Exception was expected");
116 res = false;
117 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
118 log.println("Expected exception");
119 res = true;
122 try {
123 log.println("Try to select child with index -1");
124 oObj.selectAccessibleChild(-1);
125 log.println("Exception was expected");
126 res = false;
127 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
128 log.println("Expected exception");
129 res &= true;
132 log.println("ChildCount: " + childCount);
134 int usedChildren = childCount;
136 if (childCount > 500) {
137 log.println("Restricting to 500");
138 usedChildren = 500;
141 if (usedChildren > 0) {
142 try {
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);
148 log.println("OK");
149 } else {
150 log.println("Child isn't selectable");
154 res &= true;
155 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
156 log.println("Unexpected exception");
157 e.printStackTrace(log);
158 res = false;
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()");
173 boolean res = true;
174 boolean isSelected = false;
176 try {
177 log.print("isAccessibleChildSelected(-1)? ");
178 isSelected = oObj.isAccessibleChildSelected(-1);
179 log.println(res);
180 log.println("Exception was expected");
181 res = false;
182 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
183 log.println("Expected exception");
184 res = true;
187 try {
188 log.print("isAccessibleChildSelected(" + childCount + ")? ");
189 isSelected = oObj.isAccessibleChildSelected(childCount);
190 log.println(isSelected);
191 log.println("Exception was expected");
192 res = false;
193 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
194 log.println("Expected exception");
195 res &= true;
198 int SelectableChildCount = chkSelectable(tEnv.getTestObject());
200 if (SelectableChildCount > 500) {
201 SelectableChildCount = 500;
204 log.println("SelectableChildCount: " + SelectableChildCount);
206 if (SelectableChildCount > 0) {
207 try {
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 - " +
218 isSelected);
219 res &= isSelected;
220 } else {
221 log.println("Child isn't selectable");
224 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
225 log.println("Unexpected exception");
226 e.printStackTrace(log);
227 res = false;
231 tRes.tested("isAccessibleChildSelected()", res);
235 * Calls the method.
236 * Has OK status if the method <code>isAccessibleChildSelected()</code>
237 * returned <code>false</code>.
239 public void _clearAccessibleSelection() {
240 executeMethod("isAccessibleChildSelected()");
242 boolean res = true;
244 log.println("clearAccessibleSelection");
245 oObj.clearAccessibleSelection();
248 // clearAccessibleSelection() call is oneway so we need
249 // some waiting
250 util.utils.shortWait();
252 if ((childCount > 0) && !OneAlwaysSelected) {
253 try {
254 log.print("isAccessibleChildSelected(" + (childCount - 1) +
255 ")? ");
257 boolean isSel = oObj.isAccessibleChildSelected(childCount - 1);
258 log.println(isSel);
259 res = !isSel;
260 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
261 log.println("Unexpected exception");
262 e.printStackTrace(log);
263 res = false;
265 } else if (OneAlwaysSelected) {
266 log.println("Can't clear selection, one child is always selected");
269 tRes.tested("clearAccessibleSelection()", res);
273 * Calls the method.
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
286 // some waiting
287 util.utils.shortWait();
289 boolean res = true;
290 boolean isSelected = true;
292 int SelectableChildCount = chkSelectable(tEnv.getTestObject());
294 if ((SelectableChildCount > 0) && multiSelection) {
295 try {
296 log.print("isAccessibleChildSelected(1)? ");
297 isSelected = oObj.isAccessibleChildSelected(1);
298 log.println(isSelected);
299 res = isSelected;
301 log.print("isAccessibleChildSelected(" + (childCount - 1) +
302 ")? ");
303 isSelected = oObj.isAccessibleChildSelected(childCount - 1);
304 log.println(isSelected);
305 res &= isSelected;
306 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
307 log.println("Unexpected exception");
308 e.printStackTrace(log);
309 res = false;
313 tRes.tested("selectAllAccessibleChildren()", res);
317 * Calls the method. Clears accessible selection and calls the method again.
318 * <p>
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();
329 } else {
330 int usedChildren = childCount;
332 if (childCount > 500) {
333 log.println("Restricting to 500");
334 usedChildren = 500;
337 if (usedChildren > 0) {
338 try {
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();
351 log.println("OK");
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 +
366 " are selected");
368 boolean res = true;
370 if (multiSelection) {
371 res &= (selectedCount == sCount);
372 } else {
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);
384 } else {
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()");
400 boolean res = true;
401 int selectedCount = oObj.getSelectedAccessibleChildCount();
402 log.println("getSelectedAccessibleChildCount: " + selectedCount);
404 try {
405 log.println("getSelectedAccessibleChild(-1)");
406 oObj.getSelectedAccessibleChild(-1);
407 log.println("Exception was expected");
408 res = false;
409 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
410 log.println("Expected exception");
411 res = true;
414 try {
415 log.println("getSelectedAccessibleChild(" + selectedCount + ")");
416 oObj.getSelectedAccessibleChild(selectedCount);
417 log.println("Exception was expected");
418 res &= false;
419 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
420 log.println("Expected exception");
421 res &= true;
424 int SelectableChildCount = chkSelectable(tEnv.getTestObject());
426 if (SelectableChildCount > 500) {
427 SelectableChildCount = 500;
430 if (SelectableChildCount > 0) {
431 int k = 0;
432 try {
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));
444 } else {
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);
453 res = false;
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()");
470 boolean res = true;
471 int selCount = oObj.getSelectedAccessibleChildCount();
472 log.println("getSelectedAccessibleChildCount():" + selCount);
474 try {
475 log.println("deselectAccessibleChild(-1)");
476 oObj.deselectAccessibleChild(-1);
477 log.println("Exception was expected");
478 res = false;
479 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
480 log.println("Expected exception");
481 res &= true;
484 try {
485 log.println("deselectAccessibleChild(" + (childCount + 1) + ")");
486 oObj.deselectAccessibleChild(childCount + 1);
487 log.println("Exception was expected");
488 res = false;
489 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
490 log.println("Expected exception");
491 res &= true;
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)) {
501 try {
502 int maxCount = chkSelectable(tEnv.getTestObject());
504 if (childCount > 100) {
505 maxCount = 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():" +
518 newSelCount);
520 if (OneAlwaysSelected && (selCount == 1)) {
521 log.println("One Child is always selected");
522 res &= true;
523 } else {
524 res &= (selCount > newSelCount);
526 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
527 log.println("Unexpected exception");
528 e.printStackTrace(log);
529 res = false;
533 tRes.tested("deselectAccessibleChild()", res);
536 protected static int chkSelectable(Object Testcase) {
537 int ret = 0;
538 XAccessibleContext accCon = UnoRuntime.queryInterface(
539 XAccessibleContext.class, Testcase);
540 int cc = accCon.getAccessibleChildCount();
542 if (cc > 500) {
543 return cc;
546 for (int i = 0; i < cc; i++) {
547 try {
548 if (accCon.getAccessibleChild(i).getAccessibleContext()
549 .getAccessibleStateSet()
550 .contains(com.sun.star.accessibility.AccessibleStateType.SELECTABLE)) {
551 ret = ret + 1;
552 System.out.println("Child " + i + " is selectable");
554 } catch (com.sun.star.lang.IndexOutOfBoundsException iab) {
558 return ret;
561 protected static boolean isSelectable(Object Testcase, int index) {
562 XAccessibleContext accCon = UnoRuntime.queryInterface(
563 XAccessibleContext.class, Testcase);
564 boolean res = false;
566 try {
567 if (accCon.getAccessibleChild(index).getAccessibleContext()
568 .getAccessibleStateSet()
569 .contains(com.sun.star.accessibility.AccessibleStateType.SELECTABLE)) {
570 res = true;
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)) {
578 res = false;
580 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
581 System.out.println("Exception while checking for selectability");
584 return res;