merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / accessibility / _XAccessibleSelection.java
blob6422cb52b1532c1f1ca05008bc297e478604f99c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XAccessibleSelection.java,v $
10 * $Revision: 1.11 $
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;
39 import lib.Status;
40 import lib.StatusException;
43 /**
44 * Testing <code>com.sun.star.accessibility.XAccessibleSelection</code>
45 * interface methods :
46 * <ul>
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>
54 * </ul> <p>
56 * This test needs the following object relations :
57 * <ul>
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>
63 * </ul> <p>
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;
71 int childCount;
72 protected boolean multiSelection = true;
73 protected boolean OneAlwaysSelected = false;
75 // temporary while accessibility package is in com.sun.star
76 protected String getTestedClassName() {
77 return className;
80 /**
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);
90 if (xAC == null) {
91 throw new StatusException(Status.failed(
92 "Couldn't query XAccessibleContext. Test must be modified"));
95 Boolean b = (Boolean) tEnv.getObjRelation(
96 "XAccessibleSelection.multiSelection");
98 if (b != null) {
99 multiSelection = b.booleanValue();
102 Boolean b2 = (Boolean) tEnv.getObjRelation(
103 "XAccessibleSelection.OneAlwaysSelected");
105 if (b2 != null) {
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() {
120 boolean res = true;
122 try {
123 log.println("Try to select child with index " + childCount);
124 oObj.selectAccessibleChild(childCount);
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 try {
133 log.println("Try to select child with index -1");
134 oObj.selectAccessibleChild(-1);
135 log.println("Exception was expected");
136 res = false;
137 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
138 log.println("Expected exception");
139 res &= true;
142 log.println("ChildCount: " + childCount);
144 int usedChilds = childCount;
146 if (childCount > 500) {
147 log.println("Restricting to 500");
148 usedChilds = 500;
151 if (usedChilds > 0) {
152 try {
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);
158 log.println("OK");
159 } else {
160 log.println("Child isn't selectable");
164 res &= true;
165 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
166 log.println("Unexpected exception");
167 e.printStackTrace(log);
168 res = false;
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()");
183 boolean res = true;
184 boolean isSelected = false;
186 try {
187 log.print("isAccessibleChildSelected(-1)? ");
188 isSelected = oObj.isAccessibleChildSelected(-1);
189 log.println(res);
190 log.println("Exception was expected");
191 res = false;
192 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
193 log.println("Expected exception");
194 res = true;
197 try {
198 log.print("isAccessibleChildSelected(" + childCount + ")? ");
199 isSelected = oObj.isAccessibleChildSelected(childCount);
200 log.println(isSelected);
201 log.println("Exception was expected");
202 res = false;
203 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
204 log.println("Expected exception");
205 res &= true;
208 int SelectableChildCount = chkSelectable(tEnv.getTestObject());
210 if (SelectableChildCount > 500) {
211 SelectableChildCount = 500;
214 log.println("SelectableChildCount: " + SelectableChildCount);
216 if (SelectableChildCount > 0) {
217 try {
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);
225 shortWait();
226 isSelected = oObj.isAccessibleChildSelected(k);
227 log.println("isAccessibleChildSelected - " +
228 isSelected);
229 res &= isSelected;
230 } else {
231 log.println("Child isn't selectable");
234 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
235 log.println("Unexpected exception");
236 e.printStackTrace(log);
237 res = false;
241 tRes.tested("isAccessibleChildSelected()", res);
245 * Calls the method.
246 * Has OK status if the method <code>isAccessibleChildSelected()</code>
247 * returned <code>false</code>.
249 public void _clearAccessibleSelection() {
250 executeMethod("isAccessibleChildSelected()");
252 boolean res = true;
254 log.println("clearAccessibleSelection");
255 oObj.clearAccessibleSelection();
258 // clearAccessibleSelection() call is oneway so we need
259 // some waiting
260 shortWait();
262 if ((childCount > 0) && !OneAlwaysSelected) {
263 try {
264 log.print("isAccessibleChildSelected(" + (childCount - 1) +
265 ")? ");
267 boolean isSel = oObj.isAccessibleChildSelected(childCount - 1);
268 log.println(isSel);
269 res = !isSel;
270 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
271 log.println("Unexpected exception");
272 e.printStackTrace(log);
273 res = false;
275 } else if (OneAlwaysSelected) {
276 log.println("Can't clear selection, one child is always selected");
279 tRes.tested("clearAccessibleSelection()", res);
283 * Calls the method.
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
296 // some waiting
297 shortWait();
299 boolean res = true;
300 boolean isSelected = true;
302 int SelectableChildCount = chkSelectable(tEnv.getTestObject());
304 if ((SelectableChildCount > 0) && multiSelection) {
305 try {
306 log.print("isAccessibleChildSelected(1)? ");
307 isSelected = oObj.isAccessibleChildSelected(1);
308 log.println(isSelected);
309 res = isSelected;
311 log.print("isAccessibleChildSelected(" + (childCount - 1) +
312 ")? ");
313 isSelected = oObj.isAccessibleChildSelected(childCount - 1);
314 log.println(isSelected);
315 res &= isSelected;
316 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
317 log.println("Unexpected exception");
318 e.printStackTrace(log);
319 res = false;
323 tRes.tested("selectAllAccessibleChildren()", res);
327 * Calls the method. Clears accessible selection and calls the method again.
328 * <p>
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();
339 } else {
340 int usedChilds = childCount;
342 if (childCount > 500) {
343 log.println("Restricting to 500");
344 usedChilds = 500;
347 if (usedChilds > 0) {
348 try {
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();
361 log.println("OK");
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 +
376 " are selected");
378 boolean res = true;
380 if (multiSelection) {
381 res &= (selectedCount == sCount);
382 } else {
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);
394 } else {
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()");
410 boolean res = true;
411 int selectedCount = oObj.getSelectedAccessibleChildCount();
412 log.println("getSelectedAccessibleChildCount: " + selectedCount);
414 try {
415 log.println("getSelectedAccessibleChild(-1)");
416 oObj.getSelectedAccessibleChild(-1);
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 try {
425 log.println("getSelectedAccessibleChild(" + selectedCount + ")");
426 oObj.getSelectedAccessibleChild(selectedCount);
427 log.println("Exception was expected");
428 res &= false;
429 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
430 log.println("Expected exception");
431 res &= true;
434 int SelectableChildCount = chkSelectable(tEnv.getTestObject());
436 if (SelectableChildCount > 500) {
437 SelectableChildCount = 500;
440 if (SelectableChildCount > 0) {
441 int k = 0;
442 try {
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);
448 shortWait();
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));
454 } else {
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);
463 res = false;
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()");
480 boolean res = true;
481 int selCount = oObj.getSelectedAccessibleChildCount();
482 log.println("getSelectedAccessibleChildCount():" + selCount);
484 try {
485 log.println("deselectAccessibleChild(-1)");
486 oObj.deselectAccessibleChild(-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 try {
495 log.println("deselectAccessibleChild(" + (childCount + 1) + ")");
496 oObj.deselectAccessibleChild(childCount + 1);
497 log.println("Exception was expected");
498 res = false;
499 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
500 log.println("Expected exception");
501 res &= true;
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)) {
511 try {
512 int maxCount = chkSelectable(tEnv.getTestObject());
514 if (childCount > 100) {
515 maxCount = 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():" +
528 newSelCount);
530 if (OneAlwaysSelected && (selCount == 1)) {
531 log.println("One Child is always selected");
532 res &= true;
533 } else {
534 res &= (selCount > newSelCount);
536 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
537 log.println("Unexpected exception");
538 e.printStackTrace(log);
539 res = false;
543 tRes.tested("deselectAccessibleChild()", res);
546 protected static int chkSelectable(Object Testcase) {
547 int ret = 0;
548 XAccessibleContext accCon = (XAccessibleContext) UnoRuntime.queryInterface(
549 XAccessibleContext.class, Testcase);
550 int cc = accCon.getAccessibleChildCount();
552 if (cc > 500) {
553 return cc;
556 for (int i = 0; i < cc; i++) {
557 try {
558 if (accCon.getAccessibleChild(i).getAccessibleContext()
559 .getAccessibleStateSet()
560 .contains(com.sun.star.accessibility.AccessibleStateType.SELECTABLE)) {
561 ret = ret + 1;
562 System.out.println("Child " + i + " is selectable");
564 } catch (com.sun.star.lang.IndexOutOfBoundsException iab) {
568 return ret;
571 protected static boolean isSelectable(Object Testcase, int index) {
572 XAccessibleContext accCon = (XAccessibleContext) UnoRuntime.queryInterface(
573 XAccessibleContext.class, Testcase);
574 boolean res = false;
576 try {
577 if (accCon.getAccessibleChild(index).getAccessibleContext()
578 .getAccessibleStateSet()
579 .contains(com.sun.star.accessibility.AccessibleStateType.SELECTABLE)) {
580 res = true;
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)) {
588 res = false;
590 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
591 System.out.println("Exception while checking for selectability");
594 return res;
597 private void shortWait() {
598 try {
599 Thread.sleep(500);
600 } catch (InterruptedException ex) {