Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / accessibility / _XAccessibleComponent.java
blobf09b1e3f00234e1a16bf88d0b3a67817a82b5b87
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 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;
32 /**
33 * Testing <code>com.sun.star.accessibility.XAccessibleComponent</code>
34 * interface methods :
35 * <ul>
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>
44 * </ul> <p>
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>();
55 /**
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 :
65 * <ul>
66 * <li> <code> getBounds() </code> : to have size of a component.</li>
67 * </ul>
69 public void _containsPoint() {
70 requiredMethod("getBounds()");
72 boolean result = true;
74 int curX = 0;
76 while (!oObj.containsPoint(new Point(curX, 0)) &&
77 (curX < bounds.Width)) {
78 curX++;
81 if (curX < bounds.Width) {
82 log.println("Upper bound of box containsPoint point (" + curX +
83 ",0) - OK");
84 } else {
85 log.println(
86 "Upper bound of box containsPoint no component points - FAILED");
87 result = false;
90 curX = 0;
92 while (!oObj.containsPoint(new Point(curX, bounds.Height - 1)) &&
93 (curX < bounds.Width)) {
94 log.println("containsPoint returns false for (" + curX + "," +
95 bounds.Height + ")");
96 curX++;
99 if (curX < bounds.Width) {
100 log.println("Lower bound of box containsPoint point (" + curX +
101 "," + (bounds.Height - 1) + ") - OK");
102 } else {
103 log.println(
104 "Lower bound of box containsPoint no component points - FAILED");
105 result = false;
108 int curY = 0;
110 while (!oObj.containsPoint(new Point(0, curY)) &&
111 (curY < bounds.Height)) {
112 curY++;
115 if (curY < bounds.Height) {
116 log.println("Left bound of box containsPoint point (0," + curY +
117 ") - OK");
118 } else {
119 log.println(
120 "Left bound of box containsPoint no component points - FAILED");
121 result = false;
124 curY = 0;
126 while (!oObj.containsPoint(new Point(bounds.Width - 1, curY)) &&
127 (curY < bounds.Height)) {
128 curY++;
131 if (curY < bounds.Height) {
132 log.println("Right bound of box containsPoint point (" +
133 (bounds.Width - 1) + "," + curY + ") - OK");
134 } else {
135 log.println(
136 "Right bound of box containsPoint no component points - FAILED");
137 result = false;
140 boolean locRes = true;
142 for (int x = -1; x <= bounds.Width; x++) {
143 if (oObj.containsPoint(new Point(x, -1))) {
144 log.println(
145 "Outer upper and lower bounds CONTAIN some component point"
146 + " (" + x + ", -1) - FAILED");
147 locRes = false;
148 break;
150 if (oObj.containsPoint(new Point(x, bounds.Height + bounds.Y))) {
151 log.println(
152 "Outer upper and lower bounds CONTAIN some component point"
153 + " (" + x + ", " + (bounds.Height + bounds.Y)
154 + ") - FAILED");
155 locRes = false;
156 break;
160 if (locRes) {
161 log.println("Outer upper and lower bounds contain no component " +
162 "points - OK");
163 } else {
164 result = false;
167 locRes = true;
169 for (int y = -1; y <= bounds.Height; y++) {
170 if (oObj.containsPoint(new Point(-1, y))) {
171 log.println(
172 "Outer left and right bounds CONTAIN some component point"
173 + " (-1, " + y + ") - FAILED");
174 locRes = false;
175 break;
177 if (oObj.containsPoint(new Point(bounds.X + bounds.Width, y))) {
178 log.println(
179 "Outer left and right bounds CONTAIN some component point"
180 + " (" + (bounds.X + bounds.Width) + ", " + y + ") - FAILED");
181 locRes = false;
182 break;
186 if (locRes) {
187 log.println("Outer left and right bounds contain no component " +
188 "points - OK");
189 } else {
190 result = false;
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
202 * box. <p>
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();
216 if (chBnd.X == -1) {
217 continue;
220 log.println("Checking child with bounds " + "(" + chBnd.X +
221 "," + chBnd.Y + "),(" + chBnd.Width + "," +
222 chBnd.Height + "): " +
223 util.AccessibilityTools.accessibleToString(
224 children[i]));
226 XAccessibleContext xAc = UnoRuntime.queryInterface(
227 XAccessibleContext.class,
228 children[i]);
230 boolean MightBeCovered = false;
231 boolean isShowing = xAc.getAccessibleStateSet()
232 .contains(com.sun.star.accessibility.AccessibleStateType.SHOWING);
233 log.println("\tStateType containsPoint SHOWING: " +
234 isShowing);
236 if (!isShowing) {
237 log.println("Child is invisible - OK");
239 continue;
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)) {
249 curX--;
250 curY--;
253 if ((curX == chBnd.Width) && isShowing) {
254 log.println("Couldn't find a point with containsPoint");
256 continue;
259 // trying the point laying on child
260 XAccessible xAcc = oObj.getAccessibleAtPoint(
261 new Point(chBnd.X + curX,
262 chBnd.Y + curY));
265 Point p = new Point(chBnd.X + curX,chBnd.X + curX);
267 if (isCovered(p) && isShowing) {
268 log.println(
269 "Child might be covered by another and can't be reached");
270 MightBeCovered = true;
273 KnownBounds.add(chBnd);
275 if (xAcc == null) {
276 log.println("The child not found at point (" +
277 (chBnd.X + curX) + "," + (chBnd.Y + curY) +
278 ") - FAILED");
280 if (isShowing) {
281 result = false;
282 } else {
283 result &= true;
285 } else {
286 XAccessible xAccCh = UnoRuntime.queryInterface(
287 XAccessible.class,
288 children[i]);
289 XAccessibleContext xAccC = UnoRuntime.queryInterface(
290 XAccessibleContext.class,
291 children[i]);
292 log.println("Child found at point (" + (chBnd.X + curX) +
293 "," + (chBnd.Y + curY) + ") - OK");
295 boolean res = false;
296 int expIndex;
297 String expName;
298 String expDesc;
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();
308 } else {
309 res = xAccC.getAccessibleName()
310 .equals(xAcc.getAccessibleContext()
311 .getAccessibleName());
312 expIndex = xAccC.getAccessibleIndexInParent();
313 expName = xAccC.getAccessibleName();
314 expDesc = xAccC.getAccessibleDescription();
317 if (!res) {
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");
327 } else {
328 log.println(
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");
340 } else {
341 log.println("... FAILED");
342 result = false;
350 // trying the point NOT laying on child
351 xAcc = oObj.getAccessibleAtPoint(
352 new Point(chBnd.X - 1, chBnd.Y - 1));
354 if (xAcc == null) {
355 log.println("No children found at point (" +
356 (chBnd.X - 1) + "," + (chBnd.Y - 1) +
357 ") - OK");
358 result &= true;
359 } else {
360 XAccessible xAccCh = UnoRuntime.queryInterface(
361 XAccessible.class,
362 children[i]);
363 boolean res = util.AccessibilityTools.equals(xAccCh, xAcc);
365 if (res) {
366 log.println("The same child found outside " +
367 "its bounds at (" + (chBnd.X - 1) + "," +
368 (chBnd.Y - 1) + ") - FAILED");
369 result = false;
373 } else {
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 = " +
394 ((bounds != null)
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 :
408 * <ul>
409 * <li> <code> getBounds() </code> : to have bounds </li>
410 * </ul>
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 :
432 * <ul>
433 * <li> <code> getBounds() </code> : to have location of the component
434 * relative to its parent</li>
435 * </ul>
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 +
449 ")");
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 :
464 * <ul>
465 * <li> <code> getBounds() </code> </li>
466 * </ul>
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++)
476 result = true;
477 result &= (size.Width == bounds.Width);
478 result &= (size.Height == bounds.Height);
479 if (!result) {
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;
498 oObj.grabFocus();
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);
514 if (xAcc == null) {
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
524 if (cnt > 50) {
525 cnt = 50;
528 ArrayList<XAccessibleComponent> childComp = new ArrayList<XAccessibleComponent>();
530 for (int i = 0; i < cnt; i++) {
531 try {
532 XAccessible child = xAccCon.getAccessibleChild(i);
533 XAccessibleContext xAccConCh = child.getAccessibleContext();
534 XAccessibleComponent xChAccComp = UnoRuntime.queryInterface(
535 XAccessibleComponent.class,
536 xAccConCh);
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);
560 if (xAcc == null) {
561 log.println("Component doesn't support XAccessible.");
563 return null;
566 XAccessibleContext xAccCon = xAcc.getAccessibleContext();
567 XAccessible xAccPar = xAccCon.getAccessibleParent();
569 if (xAccPar == null) {
570 log.println("Component has no accessible parent.");
572 return null;
575 XAccessibleContext xAccConPar = xAccPar.getAccessibleContext();
576 XAccessibleComponent parent = UnoRuntime.queryInterface(
577 XAccessibleComponent.class,
578 xAccConPar);
580 if (parent == null) {
581 log.println(
582 "Accessible parent doesn't support XAccessibleComponent");
584 return null;
587 return parent;
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.
611 @Override
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);
628 if (Covered) {
629 break;
632 return Covered;