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 java
.util
.ArrayList
;
21 import lib
.MultiMethodTest
;
23 import com
.sun
.star
.accessibility
.XAccessible
;
24 import com
.sun
.star
.accessibility
.XAccessibleComponent
;
25 import com
.sun
.star
.accessibility
.XAccessibleContext
;
26 import com
.sun
.star
.awt
.Point
;
27 import com
.sun
.star
.awt
.Rectangle
;
28 import com
.sun
.star
.awt
.Size
;
29 import com
.sun
.star
.uno
.UnoRuntime
;
33 * Testing <code>com.sun.star.accessibility.XAccessibleComponent</code>
36 * <li><code> containsPoint()</code></li>
37 * <li><code> getAccessibleAtPoint()</code></li>
38 * <li><code> getBounds()</code></li>
39 * <li><code> getLocation()</code></li>
40 * <li><code> getLocationOnScreen()</code></li>
41 * <li><code> getSize()</code></li>
42 * <li><code> grabFocus()</code></li>
43 * <li><code> getAccessibleKeyBinding()</code></li>
46 * @see com.sun.star.accessibility.XAccessibleComponent
48 public class _XAccessibleComponent
extends MultiMethodTest
{
50 public XAccessibleComponent oObj
= null;
51 private Rectangle bounds
= null;
52 private final ArrayList
<Rectangle
> KnownBounds
= new ArrayList
<Rectangle
>();
56 * First checks 4 inner bounds (upper, lower, left and right)
57 * of component bounding box to contain
58 * at least one point of the component. Second 4 outer bounds
59 * are checked to not contain any component points.<p>
61 * Has <b> OK </b> status if inner bounds contain component points
62 * and outer bounds don't contain any component points. <p>
64 * The following method tests are to be completed successfully before :
66 * <li> <code> getBounds() </code> : to have size of a component.</li>
69 public void _containsPoint() {
70 requiredMethod("getBounds()");
72 boolean result
= true;
76 while (!oObj
.containsPoint(new Point(curX
, 0)) &&
77 (curX
< bounds
.Width
)) {
81 if (curX
< bounds
.Width
) {
82 log
.println("Upper bound of box containsPoint point (" + curX
+
86 "Upper bound of box containsPoint no component points - FAILED");
92 while (!oObj
.containsPoint(new Point(curX
, bounds
.Height
- 1)) &&
93 (curX
< bounds
.Width
)) {
94 log
.println("containsPoint returns false for (" + curX
+ "," +
99 if (curX
< bounds
.Width
) {
100 log
.println("Lower bound of box containsPoint point (" + curX
+
101 "," + (bounds
.Height
- 1) + ") - OK");
104 "Lower bound of box containsPoint no component points - FAILED");
110 while (!oObj
.containsPoint(new Point(0, curY
)) &&
111 (curY
< bounds
.Height
)) {
115 if (curY
< bounds
.Height
) {
116 log
.println("Left bound of box containsPoint point (0," + curY
+
120 "Left bound of box containsPoint no component points - FAILED");
126 while (!oObj
.containsPoint(new Point(bounds
.Width
- 1, curY
)) &&
127 (curY
< bounds
.Height
)) {
131 if (curY
< bounds
.Height
) {
132 log
.println("Right bound of box containsPoint point (" +
133 (bounds
.Width
- 1) + "," + curY
+ ") - OK");
136 "Right bound of box containsPoint no component points - FAILED");
140 boolean locRes
= true;
142 for (int x
= -1; x
<= bounds
.Width
; x
++) {
143 if (oObj
.containsPoint(new Point(x
, -1))) {
145 "Outer upper and lower bounds CONTAIN some component point"
146 + " (" + x
+ ", -1) - FAILED");
150 if (oObj
.containsPoint(new Point(x
, bounds
.Height
+ bounds
.Y
))) {
152 "Outer upper and lower bounds CONTAIN some component point"
153 + " (" + x
+ ", " + (bounds
.Height
+ bounds
.Y
)
161 log
.println("Outer upper and lower bounds contain no component " +
169 for (int y
= -1; y
<= bounds
.Height
; y
++) {
170 if (oObj
.containsPoint(new Point(-1, y
))) {
172 "Outer left and right bounds CONTAIN some component point"
173 + " (-1, " + y
+ ") - FAILED");
177 if (oObj
.containsPoint(new Point(bounds
.X
+ bounds
.Width
, y
))) {
179 "Outer left and right bounds CONTAIN some component point"
180 + " (" + (bounds
.X
+ bounds
.Width
) + ", " + y
+ ") - FAILED");
187 log
.println("Outer left and right bounds contain no component " +
193 tRes
.tested("containsPoint()", result
);
197 * Iterates through all children which implement
198 * <code>XAccessibleComponent</code> (if they exist) determines their
199 * boundaries and tries to get each child by <code>getAccessibleAtPoint</code>
200 * passing point which belongs to the child.
201 * Also the point is checked which doesn't belong to child boundary
204 * Has <b> OK </b> status if in the first cases the right children
205 * are returned, and in the second <code>null</code> or
206 * another child is returned.
208 public void _getAccessibleAtPoint() {
209 boolean result
= true;
210 XAccessibleComponent
[] children
= getChildrenComponents();
212 if (children
.length
> 0) {
213 for (int i
= 0; i
< children
.length
; i
++) {
214 Rectangle chBnd
= children
[i
].getBounds();
220 log
.println("Checking child with bounds " + "(" + chBnd
.X
+
221 "," + chBnd
.Y
+ "),(" + chBnd
.Width
+ "," +
222 chBnd
.Height
+ "): " +
223 util
.AccessibilityTools
.accessibleToString(
226 XAccessibleContext xAc
= UnoRuntime
.queryInterface(
227 XAccessibleContext
.class,
230 boolean MightBeCovered
= false;
231 boolean isShowing
= xAc
.getAccessibleStateSet()
232 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
233 log
.println("\tStateType containsPoint SHOWING: " +
237 log
.println("Child is invisible - OK");
242 log
.println("finding the point which lies on the component");
244 int curX
= chBnd
.Width
/ 2;
245 int curY
= chBnd
.Height
/ 2;
247 while (!children
[i
].containsPoint(new Point(curX
, curY
)) &&
248 (curX
> 0) && (curY
> 0)) {
253 if ((curX
== chBnd
.Width
) && isShowing
) {
254 log
.println("Couldn't find a point with containsPoint");
259 // trying the point laying on child
260 XAccessible xAcc
= oObj
.getAccessibleAtPoint(
261 new Point(chBnd
.X
+ curX
,
265 Point p
= new Point(chBnd
.X
+ curX
,chBnd
.X
+ curX
);
267 if (isCovered(p
) && isShowing
) {
269 "Child might be covered by another and can't be reached");
270 MightBeCovered
= true;
273 KnownBounds
.add(chBnd
);
276 log
.println("The child not found at point (" +
277 (chBnd
.X
+ curX
) + "," + (chBnd
.Y
+ curY
) +
286 XAccessible xAccCh
= UnoRuntime
.queryInterface(
289 XAccessibleContext xAccC
= UnoRuntime
.queryInterface(
290 XAccessibleContext
.class,
292 log
.println("Child found at point (" + (chBnd
.X
+ curX
) +
293 "," + (chBnd
.Y
+ curY
) + ") - OK");
300 if (xAccCh
!= null) {
301 res
= util
.AccessibilityTools
.equals(xAccCh
, xAcc
);
302 expIndex
= xAccCh
.getAccessibleContext()
303 .getAccessibleIndexInParent();
304 expName
= xAccCh
.getAccessibleContext()
305 .getAccessibleName();
306 expDesc
= xAccCh
.getAccessibleContext()
307 .getAccessibleDescription();
309 res
= xAccC
.getAccessibleName()
310 .equals(xAcc
.getAccessibleContext()
311 .getAccessibleName());
312 expIndex
= xAccC
.getAccessibleIndexInParent();
313 expName
= xAccC
.getAccessibleName();
314 expDesc
= xAccC
.getAccessibleDescription();
318 int gotIndex
= xAcc
.getAccessibleContext()
319 .getAccessibleIndexInParent();
321 if (expIndex
< gotIndex
) {
322 log
.println("The children found is not the same");
323 log
.println("The expected child " + expName
);
324 log
.print("is hidden behind the found Child ");
325 log
.println(xAcc
.getAccessibleContext()
326 .getAccessibleName() + " - OK");
329 "The children found is not the same");
330 log
.println("Expected: " + expName
);
331 log
.println("Description: " + expDesc
);
332 log
.println("Found: " +
333 xAcc
.getAccessibleContext()
334 .getAccessibleName());
335 log
.println("Description: " +
336 xAcc
.getAccessibleContext()
337 .getAccessibleDescription());
338 if (MightBeCovered
) {
339 log
.println("... Child is covered by another - OK");
341 log
.println("... FAILED");
350 // trying the point NOT laying on child
351 xAcc
= oObj
.getAccessibleAtPoint(
352 new Point(chBnd
.X
- 1, chBnd
.Y
- 1));
355 log
.println("No children found at point (" +
356 (chBnd
.X
- 1) + "," + (chBnd
.Y
- 1) +
360 XAccessible xAccCh
= UnoRuntime
.queryInterface(
363 boolean res
= util
.AccessibilityTools
.equals(xAccCh
, xAcc
);
366 log
.println("The same child found outside " +
367 "its bounds at (" + (chBnd
.X
- 1) + "," +
368 (chBnd
.Y
- 1) + ") - FAILED");
374 log
.println("There are no children supporting " +
375 "XAccessibleComponent");
378 tRes
.tested("getAccessibleAtPoint()", result
);
382 * Retrieves the component bounds and stores it. <p>
384 * Has <b> OK </b> status if boundary position (x,y) is not negative
385 * and size (Width, Height) is greater than 0.
387 public void _getBounds() {
388 boolean result
= true;
390 bounds
= oObj
.getBounds();
391 result
&= ((bounds
!= null) && (bounds
.X
>= 0) && (bounds
.Y
>= 0) && (bounds
.Width
> 0) && (bounds
.Height
> 0));
393 log
.println("Bounds = " +
395 ?
("(" + bounds
.X
+ "," + bounds
.Y
+ "),(" +
396 bounds
.Width
+ "," + bounds
.Height
+ ")") : "null"));
398 tRes
.tested("getBounds()", result
);
402 * Gets the location. <p>
404 * Has <b> OK </b> status if the location is the same as location
405 * of boundary obtained by <code>getBounds()</code> method.
407 * The following method tests are to be completed successfully before :
409 * <li> <code> getBounds() </code> : to have bounds </li>
412 public void _getLocation() {
413 requiredMethod("getBounds()");
415 boolean result
= true;
416 Point loc
= oObj
.getLocation();
418 result
&= ((loc
.X
== bounds
.X
) && (loc
.Y
== bounds
.Y
));
420 tRes
.tested("getLocation()", result
);
424 * Get the screen location of the component and its parent
425 * (if it exists and supports <code>XAccessibleComponent</code>). <p>
427 * Has <b> OK </b> status if component screen location equals
428 * to screen location of its parent plus location of the component
429 * relative to the parent. <p>
431 * The following method tests are to be completed successfully before :
433 * <li> <code> getBounds() </code> : to have location of the component
434 * relative to its parent</li>
437 public void _getLocationOnScreen() {
438 requiredMethod("getBounds()");
440 XAccessibleComponent parent
= getParentComponent();
442 boolean result
= true;
443 Point loc
= oObj
.getLocationOnScreen();
444 log
.println("Location is (" + loc
.X
+ "," + loc
.Y
+ ")");
446 if (parent
!= null) {
447 Point parLoc
= parent
.getLocationOnScreen();
448 log
.println("Parent location is (" + parLoc
.X
+ "," + parLoc
.Y
+
451 result
&= ((parLoc
.X
+ bounds
.X
) == loc
.X
);
452 result
&= ((parLoc
.Y
+ bounds
.Y
) == loc
.Y
);
455 tRes
.tested("getLocationOnScreen()", result
);
459 * Obtains the size of the component. <p>
461 * Has <b> OK </b> status if the size is the same as in bounds. <p>
463 * The following method tests are to be completed successfully before :
465 * <li> <code> getBounds() </code> </li>
468 public void _getSize() {
469 requiredMethod("getBounds()");
471 boolean result
= false;
472 Size size
= oObj
.getSize();
474 for (int i
= 0; i
< 2 && !result
; i
++)
477 result
&= (size
.Width
== bounds
.Width
);
478 result
&= (size
.Height
== bounds
.Height
);
480 log
.println( "potential race bounds " + bounds
.Width
+ "x" + bounds
.Height
+
481 " vs. size " + size
.Width
+ "x" + size
.Height
);
482 // Possibily we hit a race condition and it re-sized (?) ...
483 bounds
= oObj
.getBounds();
484 size
= oObj
.getSize();
488 tRes
.tested("getSize()", result
);
492 * Just calls the method. <p>
494 * Has <b> OK </b> status if no runtime exceptions occurred.
496 public void _grabFocus() {
497 boolean result
= true;
500 tRes
.tested("grabFocus()", result
);
504 * Retrieves all children (not more than 50) of the current
505 * component which support <code>XAccessibleComponent</code>.
507 * @return The array of children. Empty array returned if
508 * such children were not found or some error occurred.
510 private XAccessibleComponent
[] getChildrenComponents() {
511 XAccessible xAcc
= UnoRuntime
.queryInterface(
512 XAccessible
.class, oObj
);
515 log
.println("Component doesn't support XAccessible.");
517 return new XAccessibleComponent
[0];
520 XAccessibleContext xAccCon
= xAcc
.getAccessibleContext();
521 int cnt
= xAccCon
.getAccessibleChildCount();
523 // for cases when too many children exist checking only first 50
528 ArrayList
<XAccessibleComponent
> childComp
= new ArrayList
<XAccessibleComponent
>();
530 for (int i
= 0; i
< cnt
; i
++) {
532 XAccessible child
= xAccCon
.getAccessibleChild(i
);
533 XAccessibleContext xAccConCh
= child
.getAccessibleContext();
534 XAccessibleComponent xChAccComp
= UnoRuntime
.queryInterface(
535 XAccessibleComponent
.class,
538 if (xChAccComp
!= null) {
539 childComp
.add(xChAccComp
);
541 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
545 return childComp
.toArray(
546 new XAccessibleComponent
[childComp
.size()]);
550 * Gets the parent of the current component which support
551 * <code>XAccessibleComponent</code>.
553 * @return The parent or <code>null</code> if the component
554 * has no parent or some errors occurred.
556 private XAccessibleComponent
getParentComponent() {
557 XAccessible xAcc
= UnoRuntime
.queryInterface(
558 XAccessible
.class, oObj
);
561 log
.println("Component doesn't support XAccessible.");
566 XAccessibleContext xAccCon
= xAcc
.getAccessibleContext();
567 XAccessible xAccPar
= xAccCon
.getAccessibleParent();
569 if (xAccPar
== null) {
570 log
.println("Component has no accessible parent.");
575 XAccessibleContext xAccConPar
= xAccPar
.getAccessibleContext();
576 XAccessibleComponent parent
= UnoRuntime
.queryInterface(
577 XAccessibleComponent
.class,
580 if (parent
== null) {
582 "Accessible parent doesn't support XAccessibleComponent");
591 * Just calls the method.
593 public void _getForeground() {
594 int forColor
= oObj
.getForeground();
595 log
.println("getForeground(): " + forColor
);
596 tRes
.tested("getForeground()", true);
600 * Just calls the method.
602 public void _getBackground() {
603 int backColor
= oObj
.getBackground();
604 log
.println("getBackground(): " + backColor
);
605 tRes
.tested("getBackground()", true);
609 * Restores initial component text.
612 protected void after() {
613 if (tEnv
.getObjRelation("Destroy") != null) {
614 disposeEnvironment();
618 private boolean isCovered(Point p
) {
619 int elements
= KnownBounds
.size();
620 boolean Covered
= false;
621 for (int k
=0;k
<elements
;k
++) {
622 Rectangle known
= KnownBounds
.get(k
);
623 Covered
= (known
.X
< p
.X
);
624 Covered
&= (known
.Y
< p
.Y
);
625 Covered
&= (p
.Y
< known
.Y
+known
.Height
);
626 Covered
&= (p
.X
< known
.X
+known
.Width
);