update credits
[LibreOffice.git] / qadevOOo / tests / java / ifc / accessibility / _XAccessibleComponent.java
blobfd8833b1b89e1149c3bc7b803e5df0b35da38f71
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 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, bounds.Y)) && curX < bounds.Width+bounds.X) {
77 while (!oObj.containsPoint(new Point(curX, 0)) &&
78 (curX < bounds.Width)) {
79 curX++;
82 //if ((bounds.X <= curX) && (curX < bounds.Width+bounds.X)) {
83 if (curX < bounds.Width) {
84 log.println("Upper bound of box containsPoint point (" + curX +
85 ",0) - OK");
86 } else {
87 log.println(
88 "Upper bound of box containsPoint no component points - FAILED");
89 result = false;
92 curX = 0;
94 //while (!oObj.containsPoint(new Point(curX, bounds.Y+bounds.Height - 1))
95 while (!oObj.containsPoint(new Point(curX, bounds.Height - 1)) &&
96 (curX < bounds.Width)) {
97 log.println("containsPoint returns false for (" + curX + "," +
98 bounds.Height + ")");
99 curX++;
102 //if ((bounds.X <= curX) && (curX < bounds.Width+bounds.X)) {
103 if (curX < bounds.Width) {
104 log.println("Lower bound of box containsPoint point (" + curX +
105 "," + (bounds.Height - 1) + ") - OK");
106 } else {
107 log.println(
108 "Lower bound of box containsPoint no component points - FAILED");
109 result = false;
112 int curY = 0;
114 //while (!oObj.containsPoint(new Point(bounds.X, curY)) && curY < bounds.Height+bounds.Y) {
115 while (!oObj.containsPoint(new Point(0, curY)) &&
116 (curY < bounds.Height)) {
117 curY++;
120 //if ((bounds.Y <= curY) && (curY < bounds.Height+bounds.Y)) {
121 if (curY < bounds.Height) {
122 log.println("Left bound of box containsPoint point (0," + curY +
123 ") - OK");
124 } else {
125 log.println(
126 "Left bound of box containsPoint no component points - FAILED");
127 result = false;
130 curY = 0;
132 //while (!oObj.containsPoint(new Point(bounds.X+bounds.Width - 1, curY))
133 // && curY < bounds.Height+bounds.Y) {
134 while (!oObj.containsPoint(new Point(bounds.Width - 1, curY)) &&
135 (curY < bounds.Height)) {
136 curY++;
139 //if ((bounds.Y <= curY) && (curY < bounds.Height + bounds.Y)) {
140 if (curY < bounds.Height) {
141 log.println("Right bound of box containsPoint point (" +
142 (bounds.Width - 1) + "," + curY + ") - OK");
143 } else {
144 log.println(
145 "Right bound of box containsPoint no component points - FAILED");
146 result = false;
149 boolean locRes = true;
151 for (int x = -1; x <= bounds.Width; x++) {
152 if (oObj.containsPoint(new Point(x, -1))) {
153 log.println(
154 "Outer upper and lower bounds CONTAIN some component point"
155 + " (" + x + ", -1) - FAILED");
156 locRes = false;
157 break;
159 if (oObj.containsPoint(new Point(x, bounds.Height + bounds.Y))) {
160 log.println(
161 "Outer upper and lower bounds CONTAIN some component point"
162 + " (" + x + ", " + bounds.Height + bounds.Y
163 + ") - FAILED");
164 locRes = false;
165 break;
169 if (locRes) {
170 log.println("Outer upper and lower bounds contain no component " +
171 "points - OK");
172 } else {
173 result = false;
176 locRes = true;
178 for (int y = -1; y <= bounds.Height; y++) {
179 if (oObj.containsPoint(new Point(-1, y))) {
180 log.println(
181 "Outer left and right bounds CONTAIN some component point"
182 + " (-1, " + y + ") - FAILED");
183 locRes = false;
184 break;
186 if (oObj.containsPoint(new Point(bounds.X + bounds.Width, y))) {
187 log.println(
188 "Outer left and right bounds CONTAIN some component point"
189 + " (" + bounds.X + bounds.Width + ", " + y + ") - FAILED");
190 locRes = false;
191 break;
195 if (locRes) {
196 log.println("Outer left and right bounds contain no component " +
197 "points - OK");
198 } else {
199 result = false;
202 tRes.tested("containsPoint()", result);
206 * Iterates through all children which implement
207 * <code>XAccessibleComponent</code> (if they exist) determines their
208 * boundaries and tries to get each child by <code>getAccessibleAtPoint</code>
209 * passing point which belongs to the child.
210 * Also the point is checked which doesn't belong to child boundary
211 * box. <p>
213 * Has <b> OK </b> status if in the first cases the right children
214 * are returned, and in the second <code>null</code> or
215 * another child is returned.
217 public void _getAccessibleAtPoint() {
218 boolean result = true;
219 XAccessibleComponent[] children = getChildrenComponents();
221 if (children.length > 0) {
222 for (int i = 0; i < children.length; i++) {
223 Rectangle chBnd = children[i].getBounds();
225 if (chBnd.X == -1) {
226 continue;
229 log.println("Checking child with bounds " + "(" + chBnd.X +
230 "," + chBnd.Y + "),(" + chBnd.Width + "," +
231 chBnd.Height + "): " +
232 util.AccessibilityTools.accessibleToString(
233 children[i]));
235 XAccessibleContext xAc = UnoRuntime.queryInterface(
236 XAccessibleContext.class,
237 children[i]);
239 boolean MightBeCovered = false;
240 boolean isShowing = xAc.getAccessibleStateSet()
241 .contains(com.sun.star.accessibility.AccessibleStateType.SHOWING);
242 log.println("\tStateType containsPoint SHOWING: " +
243 isShowing);
245 if (!isShowing) {
246 log.println("Child is invisible - OK");
248 continue;
251 log.println("finding the point which lies on the component");
253 int curX = chBnd.Width / 2;
254 int curY = chBnd.Height / 2;
256 while (!children[i].containsPoint(new Point(curX, curY)) &&
257 (curX > 0) && (curY > 0)) {
258 curX--;
259 curY--;
262 if ((curX == chBnd.Width) && isShowing) {
263 log.println("Couldn't find a point with containsPoint");
265 continue;
268 // trying the point laying on child
269 XAccessible xAcc = oObj.getAccessibleAtPoint(
270 new Point(chBnd.X + curX,
271 chBnd.Y + curY));
274 Point p = new Point(chBnd.X + curX,chBnd.X + curX);
276 if (isCovered(p) && isShowing) {
277 log.println(
278 "Child might be covered by another and can't be reached");
279 MightBeCovered = true;
282 KnownBounds.add(chBnd);
284 if (xAcc == null) {
285 log.println("The child not found at point (" +
286 (chBnd.X + curX) + "," + (chBnd.Y + curY) +
287 ") - FAILED");
289 if (isShowing) {
290 result = false;
291 } else {
292 result &= true;
294 } else {
295 XAccessible xAccCh = UnoRuntime.queryInterface(
296 XAccessible.class,
297 children[i]);
298 XAccessibleContext xAccC = UnoRuntime.queryInterface(
299 XAccessibleContext.class,
300 children[i]);
301 log.println("Child found at point (" + (chBnd.X + curX) +
302 "," + (chBnd.Y + curY) + ") - OK");
304 boolean res = false;
305 int expIndex;
306 String expName;
307 String expDesc;
309 if (xAccCh != null) {
310 res = util.AccessibilityTools.equals(xAccCh, xAcc);
311 expIndex = xAccCh.getAccessibleContext()
312 .getAccessibleIndexInParent();
313 expName = xAccCh.getAccessibleContext()
314 .getAccessibleName();
315 expDesc = xAccCh.getAccessibleContext()
316 .getAccessibleDescription();
317 } else {
318 res = xAccC.getAccessibleName()
319 .equals(xAcc.getAccessibleContext()
320 .getAccessibleName());
321 expIndex = xAccC.getAccessibleIndexInParent();
322 expName = xAccC.getAccessibleName();
323 expDesc = xAccC.getAccessibleDescription();
326 if (!res) {
327 int gotIndex = xAcc.getAccessibleContext()
328 .getAccessibleIndexInParent();
330 if (expIndex < gotIndex) {
331 log.println("The children found is not the same");
332 log.println("The expected child " + expName);
333 log.print("is hidden behind the found Child ");
334 log.println(xAcc.getAccessibleContext()
335 .getAccessibleName() + " - OK");
336 } else {
337 log.println(
338 "The children found is not the same");
339 log.println("Expected: " + expName);
340 log.println("Description: " + expDesc);
341 log.println("Found: " +
342 xAcc.getAccessibleContext()
343 .getAccessibleName());
344 log.println("Description: " +
345 xAcc.getAccessibleContext()
346 .getAccessibleDescription());
347 if (MightBeCovered) {
348 log.println("... Child is covered by another - OK");
349 } else {
350 log.println("... FAILED");
351 result = false;
359 // trying the point NOT laying on child
360 xAcc = oObj.getAccessibleAtPoint(
361 new Point(chBnd.X - 1, chBnd.Y - 1));
363 if (xAcc == null) {
364 log.println("No children found at point (" +
365 (chBnd.X - 1) + "," + (chBnd.Y - 1) +
366 ") - OK");
367 result &= true;
368 } else {
369 XAccessible xAccCh = UnoRuntime.queryInterface(
370 XAccessible.class,
371 children[i]);
372 boolean res = util.AccessibilityTools.equals(xAccCh, xAcc);
374 if (res) {
375 log.println("The same child found outside " +
376 "its bounds at (" + (chBnd.X - 1) + "," +
377 (chBnd.Y - 1) + ") - FAILED");
378 result = false;
382 } else {
383 log.println("There are no children supporting " +
384 "XAccessibleComponent");
387 tRes.tested("getAccessibleAtPoint()", result);
391 * Retrieves the component bounds and stores it. <p>
393 * Has <b> OK </b> status if boundary position (x,y) is not negative
394 * and size (Width, Height) is greater than 0.
396 public void _getBounds() {
397 boolean result = true;
399 bounds = oObj.getBounds();
400 result &= ((bounds != null) && (bounds.X >= 0) && (bounds.Y >= 0) && (bounds.Width > 0) && (bounds.Height > 0));
402 log.println("Bounds = " +
403 ((bounds != null)
404 ? ("(" + bounds.X + "," + bounds.Y + "),(" +
405 bounds.Width + "," + bounds.Height + ")") : "null"));
407 tRes.tested("getBounds()", result);
411 * Gets the location. <p>
413 * Has <b> OK </b> status if the location is the same as location
414 * of boundary obtained by <code>getBounds()</code> method.
416 * The following method tests are to be completed successfully before :
417 * <ul>
418 * <li> <code> getBounds() </code> : to have bounds </li>
419 * </ul>
421 public void _getLocation() {
422 requiredMethod("getBounds()");
424 boolean result = true;
425 Point loc = oObj.getLocation();
427 result &= ((loc.X == bounds.X) && (loc.Y == bounds.Y));
429 tRes.tested("getLocation()", result);
433 * Get the screen location of the component and its parent
434 * (if it exists and supports <code>XAccessibleComponent</code>). <p>
436 * Has <b> OK </b> status if component screen location equals
437 * to screen location of its parent plus location of the component
438 * relative to the parent. <p>
440 * The following method tests are to be completed successfully before :
441 * <ul>
442 * <li> <code> getBounds() </code> : to have location of the component
443 * relative to its parent</li>
444 * </ul>
446 public void _getLocationOnScreen() {
447 requiredMethod("getBounds()");
449 XAccessibleComponent parent = getParentComponent();
451 boolean result = true;
452 Point loc = oObj.getLocationOnScreen();
453 log.println("Location is (" + loc.X + "," + loc.Y + ")");
455 if (parent != null) {
456 Point parLoc = parent.getLocationOnScreen();
457 log.println("Parent location is (" + parLoc.X + "," + parLoc.Y +
458 ")");
460 result &= ((parLoc.X + bounds.X) == loc.X);
461 result &= ((parLoc.Y + bounds.Y) == loc.Y);
464 tRes.tested("getLocationOnScreen()", result);
468 * Obtains the size of the component. <p>
470 * Has <b> OK </b> status if the size is the same as in bounds. <p>
472 * The following method tests are to be completed successfully before :
473 * <ul>
474 * <li> <code> getBounds() </code> </li>
475 * </ul>
477 public void _getSize() {
478 requiredMethod("getBounds()");
480 boolean result = true;
481 Size size = oObj.getSize();
483 result &= (size.Width == bounds.Width);
484 result &= (size.Height == bounds.Height);
485 if (!result) {
486 log.println(
487 "bounds " + bounds.Width + "x" + bounds.Height + " vs. size "
488 + size.Width + "x" + size.Height);
491 tRes.tested("getSize()", result);
495 * Just calls the method. <p>
497 * Has <b> OK </b> status if no runtime exceptions occurred.
499 public void _grabFocus() {
500 boolean result = true;
501 oObj.grabFocus();
503 tRes.tested("grabFocus()", result);
507 * Retrieves all children (not more than 50) of the current
508 * component which support <code>XAccessibleComponent</code>.
510 * @return The array of children. Empty array returned if
511 * such children were not found or some error occurred.
513 private XAccessibleComponent[] getChildrenComponents() {
514 XAccessible xAcc = UnoRuntime.queryInterface(
515 XAccessible.class, oObj);
517 if (xAcc == null) {
518 log.println("Component doesn't support XAccessible.");
520 return new XAccessibleComponent[0];
523 XAccessibleContext xAccCon = xAcc.getAccessibleContext();
524 int cnt = xAccCon.getAccessibleChildCount();
526 // for cases when too many children exist checking only first 50
527 if (cnt > 50) {
528 cnt = 50;
531 ArrayList<XAccessibleComponent> childComp = new ArrayList<XAccessibleComponent>();
533 for (int i = 0; i < cnt; i++) {
534 try {
535 XAccessible child = xAccCon.getAccessibleChild(i);
536 XAccessibleContext xAccConCh = child.getAccessibleContext();
537 XAccessibleComponent xChAccComp = UnoRuntime.queryInterface(
538 XAccessibleComponent.class,
539 xAccConCh);
541 if (xChAccComp != null) {
542 childComp.add(xChAccComp);
544 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
548 return childComp.toArray(
549 new XAccessibleComponent[childComp.size()]);
553 * Gets the parent of the current component which support
554 * <code>XAccessibleComponent</code>.
556 * @return The parent or <code>null</code> if the component
557 * has no parent or some errors occurred.
559 private XAccessibleComponent getParentComponent() {
560 XAccessible xAcc = UnoRuntime.queryInterface(
561 XAccessible.class, oObj);
563 if (xAcc == null) {
564 log.println("Component doesn't support XAccessible.");
566 return null;
569 XAccessibleContext xAccCon = xAcc.getAccessibleContext();
570 XAccessible xAccPar = xAccCon.getAccessibleParent();
572 if (xAccPar == null) {
573 log.println("Component has no accessible parent.");
575 return null;
578 XAccessibleContext xAccConPar = xAccPar.getAccessibleContext();
579 XAccessibleComponent parent = UnoRuntime.queryInterface(
580 XAccessibleComponent.class,
581 xAccConPar);
583 if (parent == null) {
584 log.println(
585 "Accessible parent doesn't support XAccessibleComponent");
587 return null;
590 return parent;
594 * Just calls the method.
596 public void _getForeground() {
597 int forColor = oObj.getForeground();
598 log.println("getForeground(): " + forColor);
599 tRes.tested("getForeground()", true);
603 * Just calls the method.
605 public void _getBackground() {
606 int backColor = oObj.getBackground();
607 log.println("getBackground(): " + backColor);
608 tRes.tested("getBackground()", true);
612 * Restores initial component text.
614 protected void after() {
615 if (tEnv.getObjRelation("Destroy") != null) {
616 disposeEnvironment();
620 private boolean isCovered(Point p) {
621 int elements = KnownBounds.size();
622 boolean Covered = false;
623 for (int k=0;k<elements;k++) {
624 Rectangle known = KnownBounds.get(k);
625 Covered = (known.X < p.X);
626 Covered &= (known.Y < p.Y);
627 Covered &= (p.Y < known.Y+known.Height);
628 Covered &= (p.X < known.X+known.Width);
630 if (Covered) {
631 break;
634 return Covered;