tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / qadevOOo / tests / java / ifc / accessibility / _XAccessibleComponent.java
blob8faccfe447bc8df82baed830b2a121e7f956dcc8
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 final ArrayList<Rectangle> KnownBounds = new ArrayList<Rectangle>();
54 /**
55 * First checks 4 inner bounds (upper, lower, left and right)
56 * of component bounding box to contain
57 * at least one point of the component. Second 4 outer bounds
58 * are checked to not contain any component points.<p>
60 * Has <b> OK </b> status if inner bounds contain component points
61 * and outer bounds don't contain any component points. <p>
63 * The following method tests are to be completed successfully before :
64 * <ul>
65 * <li> <code> getBounds() </code> : to have size of a component.</li>
66 * </ul>
68 public void _containsPoint() {
69 Rectangle bounds = oObj.getBounds();
71 boolean result = true;
73 int curX = 0;
75 while (!oObj.containsPoint(new Point(curX, 0)) &&
76 (curX < bounds.Width)) {
77 curX++;
80 if (curX < bounds.Width) {
81 log.println("Upper bound of box containsPoint point (" + curX +
82 ",0) - OK");
83 } else {
84 log.println(
85 "Upper bound of box containsPoint no component points - FAILED");
86 result = false;
89 curX = 0;
91 while (!oObj.containsPoint(new Point(curX, bounds.Height - 1)) &&
92 (curX < bounds.Width)) {
93 log.println("containsPoint returns false for (" + curX + "," +
94 bounds.Height + ")");
95 curX++;
98 if (curX < bounds.Width) {
99 log.println("Lower bound of box containsPoint point (" + curX +
100 "," + (bounds.Height - 1) + ") - OK");
101 } else {
102 log.println(
103 "Lower bound of box containsPoint no component points - FAILED");
104 result = false;
107 int curY = 0;
109 while (!oObj.containsPoint(new Point(0, curY)) &&
110 (curY < bounds.Height)) {
111 curY++;
114 if (curY < bounds.Height) {
115 log.println("Left bound of box containsPoint point (0," + curY +
116 ") - OK");
117 } else {
118 log.println(
119 "Left bound of box containsPoint no component points - FAILED");
120 result = false;
123 curY = 0;
125 while (!oObj.containsPoint(new Point(bounds.Width - 1, curY)) &&
126 (curY < bounds.Height)) {
127 curY++;
130 if (curY < bounds.Height) {
131 log.println("Right bound of box containsPoint point (" +
132 (bounds.Width - 1) + "," + curY + ") - OK");
133 } else {
134 log.println(
135 "Right bound of box containsPoint no component points - FAILED");
136 result = false;
139 boolean locRes = true;
141 for (int x = -1; x <= bounds.Width; x++) {
142 if (oObj.containsPoint(new Point(x, -1))) {
143 log.println(
144 "Outer upper and lower bounds CONTAIN some component point"
145 + " (" + x + ", -1) - FAILED");
146 locRes = false;
147 break;
149 if (oObj.containsPoint(new Point(x, bounds.Height + bounds.Y))) {
150 log.println(
151 "Outer upper and lower bounds CONTAIN some component point"
152 + " (" + x + ", " + (bounds.Height + bounds.Y)
153 + ") - FAILED");
154 locRes = false;
155 break;
159 if (locRes) {
160 log.println("Outer upper and lower bounds contain no component " +
161 "points - OK");
162 } else {
163 result = false;
166 locRes = true;
168 for (int y = -1; y <= bounds.Height; y++) {
169 if (oObj.containsPoint(new Point(-1, y))) {
170 log.println(
171 "Outer left and right bounds CONTAIN some component point"
172 + " (-1, " + y + ") - FAILED");
173 locRes = false;
174 break;
176 if (oObj.containsPoint(new Point(bounds.X + bounds.Width, y))) {
177 log.println(
178 "Outer left and right bounds CONTAIN some component point"
179 + " (" + (bounds.X + bounds.Width) + ", " + y + ") - FAILED");
180 locRes = false;
181 break;
185 if (locRes) {
186 log.println("Outer left and right bounds contain no component " +
187 "points - OK");
188 } else {
189 result = false;
192 tRes.tested("containsPoint()", result);
196 * Iterates through all children which implement
197 * <code>XAccessibleComponent</code> (if they exist) determines their
198 * boundaries and tries to get each child by <code>getAccessibleAtPoint</code>
199 * passing point which belongs to the child.
200 * Also the point is checked which doesn't belong to child boundary
201 * box. <p>
203 * Has <b> OK </b> status if in the first cases the right children
204 * are returned, and in the second <code>null</code> or
205 * another child is returned.
207 public void _getAccessibleAtPoint() {
208 boolean result = true;
209 XAccessibleComponent[] children = getChildrenComponents();
211 if (children.length == 0) {
212 log.println("There are no children supporting XAccessibleComponent");
213 tRes.tested("getAccessibleAtPoint()", result);
214 return;
217 for (int i = 0; i < children.length; i++) {
218 Rectangle chBnd = children[i].getBounds();
220 if (chBnd.X == -1) {
221 continue;
224 log.println("Checking child with bounds " + "(" + chBnd.X +
225 "," + chBnd.Y + "),(" + chBnd.Width + "," +
226 chBnd.Height + "): " +
227 util.AccessibilityTools.accessibleToString(
228 children[i]));
230 XAccessibleContext xAc = UnoRuntime.queryInterface(
231 XAccessibleContext.class,
232 children[i]);
234 boolean MightBeCovered = false;
235 boolean isShowing = (xAc.getAccessibleStateSet() &
236 com.sun.star.accessibility.AccessibleStateType.SHOWING) != 0;
237 log.println("\tStateType containsPoint SHOWING: " +
238 isShowing);
240 if (!isShowing) {
241 log.println("Child is invisible - OK");
242 continue;
245 log.println("finding the point which lies on the component");
247 int curX = chBnd.Width / 2;
248 int curY = chBnd.Height / 2;
250 while (!children[i].containsPoint(new Point(curX, curY)) &&
251 (curX > 0) && (curY > 0)) {
252 curX--;
253 curY--;
256 if (curX == chBnd.Width) {
257 log.println("Couldn't find a point with containsPoint");
259 continue;
262 // trying the point laying on child
263 XAccessible xAcc = oObj.getAccessibleAtPoint(
264 new Point(chBnd.X + curX,
265 chBnd.Y + curY));
268 Point p = new Point(chBnd.X + curX,chBnd.Y + curY);
270 if (isCovered(p)) {
271 log.println(
272 "Child might be covered by another and can't be reached");
273 MightBeCovered = true;
276 KnownBounds.add(chBnd);
278 if (xAcc == null) {
279 log.println("The child not found at point (" +
280 (chBnd.X + curX) + "," + (chBnd.Y + curY) +
281 ") - FAILED");
283 result = false;
284 } else {
285 XAccessible xAccCh = UnoRuntime.queryInterface(
286 XAccessible.class,
287 children[i]);
288 XAccessibleContext xAccC = UnoRuntime.queryInterface(
289 XAccessibleContext.class,
290 children[i]);
291 log.println("Child found at point (" + (chBnd.X + curX) +
292 "," + (chBnd.Y + curY) + ") - OK");
294 boolean res = false;
295 long expIndex;
296 String expName;
297 String expDesc;
299 if (xAccCh != null) {
300 res = util.AccessibilityTools.equals(xAccCh, xAcc);
301 expIndex = xAccCh.getAccessibleContext()
302 .getAccessibleIndexInParent();
303 expName = xAccCh.getAccessibleContext()
304 .getAccessibleName();
305 expDesc = xAccCh.getAccessibleContext()
306 .getAccessibleDescription();
307 } else {
308 res = xAccC.getAccessibleName()
309 .equals(xAcc.getAccessibleContext()
310 .getAccessibleName());
311 expIndex = xAccC.getAccessibleIndexInParent();
312 expName = xAccC.getAccessibleName();
313 expDesc = xAccC.getAccessibleDescription();
316 if (!res) {
317 long gotIndex = xAcc.getAccessibleContext()
318 .getAccessibleIndexInParent();
320 if (expIndex < gotIndex) {
321 log.println("The children found is not the same");
322 log.println("The expected child " + expName);
323 log.print("is hidden behind the found Child ");
324 log.println(xAcc.getAccessibleContext()
325 .getAccessibleName() + " - OK");
326 } else {
327 log.println(
328 "The children found is not the same");
329 log.println("Expected: " + expName);
330 log.println("Description: " + expDesc);
331 log.println("Found: " +
332 xAcc.getAccessibleContext()
333 .getAccessibleName());
334 log.println("Description: " +
335 xAcc.getAccessibleContext()
336 .getAccessibleDescription());
337 if (MightBeCovered) {
338 log.println("... Child is covered by another - OK");
339 } else {
340 log.println(
341 "(xAccCh==null: " + (xAccCh == null) + "; expIndex: " + expIndex
342 + "; gotIndex: " + gotIndex + ")");
343 log.println("... FAILED");
344 result = false;
352 // trying the point NOT laying on child
353 xAcc = oObj.getAccessibleAtPoint(
354 new Point(chBnd.X - 1, chBnd.Y - 1));
356 if (xAcc == null) {
357 log.println("No children found at point (" +
358 (chBnd.X - 1) + "," + (chBnd.Y - 1) +
359 ") - OK");
360 result &= true;
361 } else {
362 XAccessible xAccCh = UnoRuntime.queryInterface(
363 XAccessible.class,
364 children[i]);
365 boolean res = util.AccessibilityTools.equals(xAccCh, xAcc);
367 if (res) {
368 log.println("The same child found outside " +
369 "its bounds at (" + (chBnd.X - 1) + "," +
370 (chBnd.Y - 1) + ") - FAILED");
371 result = false;
376 tRes.tested("getAccessibleAtPoint()", result);
380 * Retrieves the component bounds and discards it. <p>
382 * Has <b> OK </b> status if boundary position (x,y) is not negative
383 * and size (Width, Height) is greater than 0.
385 public void _getBounds() {
386 boolean result = true;
388 Rectangle bounds = oObj.getBounds();
389 result &= ((bounds != null) && (bounds.X >= 0) && (bounds.Y >= 0) && (bounds.Width > 0) && (bounds.Height > 0));
391 log.println("Bounds = " +
392 ((bounds != null)
393 ? ("(" + bounds.X + "," + bounds.Y + "),(" +
394 bounds.Width + "," + bounds.Height + ")") : "null"));
396 tRes.tested("getBounds()", result);
400 * Gets the location. <p>
402 * Has <b> OK </b> status if the location is the same as location
403 * of boundary obtained by <code>getBounds()</code> method.
405 public void _getLocation() {
406 Rectangle bounds = oObj.getBounds();
407 Point loc = oObj.getLocation();
408 boolean result = loc.X == bounds.X && loc.Y == bounds.Y;
409 log.println(
410 "loc.X=" + loc.X + " vs. bounds.X=" + bounds.X + ", loc.Y="
411 + loc.Y + " vs. bounds.Y=" + bounds.Y);
412 tRes.tested("getLocation()", result);
416 * Get the screen location of the component and its parent
417 * (if it exists and supports <code>XAccessibleComponent</code>). <p>
419 * Has <b> OK </b> status if component screen location equals
420 * to screen location of its parent plus location of the component
421 * relative to the parent. <p>
423 public void _getLocationOnScreen() {
424 XAccessibleComponent parent = getParentComponent();
426 boolean result = true;
427 Rectangle bounds = oObj.getBounds();
428 Point loc = oObj.getLocationOnScreen();
429 log.println("Location is (" + loc.X + "," + loc.Y + ")");
431 if (parent != null) {
432 Point parLoc = parent.getLocationOnScreen();
433 log.println("Parent location is (" + parLoc.X + "," + parLoc.Y +
434 ")");
436 result &= ((parLoc.X + bounds.X) == loc.X);
437 result &= ((parLoc.Y + bounds.Y) == loc.Y);
440 tRes.tested("getLocationOnScreen()", result);
444 * Obtains the size of the component. <p>
446 * Has <b> OK </b> status if the size is the same as in bounds. <p>
448 public void _getSize() {
449 requiredMethod("getBounds()");
451 boolean result = true;
452 Rectangle bounds = oObj.getBounds();
453 Size size = oObj.getSize();
455 result &= (size.Width == bounds.Width);
456 result &= (size.Height == bounds.Height);
458 tRes.tested("getSize()", result);
462 * Just calls the method. <p>
464 * Has <b> OK </b> status if no runtime exceptions occurred.
466 public void _grabFocus() {
467 boolean result = true;
468 oObj.grabFocus();
470 tRes.tested("grabFocus()", result);
474 * Retrieves all children (not more than 50) of the current
475 * component which support <code>XAccessibleComponent</code>.
477 * @return The array of children. Empty array returned if
478 * such children were not found or some error occurred.
480 private XAccessibleComponent[] getChildrenComponents() {
481 XAccessible xAcc = UnoRuntime.queryInterface(
482 XAccessible.class, oObj);
484 if (xAcc == null) {
485 log.println("Component doesn't support XAccessible.");
487 return new XAccessibleComponent[0];
490 XAccessibleContext xAccCon = xAcc.getAccessibleContext();
491 long cnt = xAccCon.getAccessibleChildCount();
493 // for cases when too many children exist checking only first 50
494 if (cnt > 50) {
495 cnt = 50;
498 ArrayList<XAccessibleComponent> childComp = new ArrayList<XAccessibleComponent>();
500 for (long i = 0; i < cnt; i++) {
501 try {
502 XAccessible child = xAccCon.getAccessibleChild(i);
503 XAccessibleContext xAccConCh = child.getAccessibleContext();
504 XAccessibleComponent xChAccComp = UnoRuntime.queryInterface(
505 XAccessibleComponent.class,
506 xAccConCh);
508 if (xChAccComp != null) {
509 childComp.add(xChAccComp);
511 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
515 return childComp.toArray(
516 new XAccessibleComponent[childComp.size()]);
520 * Gets the parent of the current component which support
521 * <code>XAccessibleComponent</code>.
523 * @return The parent or <code>null</code> if the component
524 * has no parent or some errors occurred.
526 private XAccessibleComponent getParentComponent() {
527 XAccessible xAcc = UnoRuntime.queryInterface(
528 XAccessible.class, oObj);
530 if (xAcc == null) {
531 log.println("Component doesn't support XAccessible.");
533 return null;
536 XAccessibleContext xAccCon = xAcc.getAccessibleContext();
537 XAccessible xAccPar = xAccCon.getAccessibleParent();
539 if (xAccPar == null) {
540 log.println("Component has no accessible parent.");
542 return null;
545 XAccessibleContext xAccConPar = xAccPar.getAccessibleContext();
546 XAccessibleComponent parent = UnoRuntime.queryInterface(
547 XAccessibleComponent.class,
548 xAccConPar);
550 if (parent == null) {
551 log.println(
552 "Accessible parent doesn't support XAccessibleComponent");
554 return null;
557 return parent;
561 * Just calls the method.
563 public void _getForeground() {
564 int forColor = oObj.getForeground();
565 log.println("getForeground(): " + forColor);
566 tRes.tested("getForeground()", true);
570 * Just calls the method.
572 public void _getBackground() {
573 int backColor = oObj.getBackground();
574 log.println("getBackground(): " + backColor);
575 tRes.tested("getBackground()", true);
579 * Restores initial component text.
581 @Override
582 protected void after() {
583 if (tEnv.getObjRelation("Destroy") != null) {
584 disposeEnvironment();
588 private boolean isCovered(Point p) {
589 int elements = KnownBounds.size();
590 boolean Covered = false;
591 for (int k=0;k<elements;k++) {
592 Rectangle known = KnownBounds.get(k);
593 Covered = (known.X < p.X);
594 Covered &= (known.Y < p.Y);
595 Covered &= (p.Y < known.Y+known.Height);
596 Covered &= (p.X < known.X+known.Width);
598 if (Covered) {
599 break;
602 return Covered;