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 .
20 import com
.sun
.star
.accessibility
.AccessibleRole
;
21 import com
.sun
.star
.accessibility
.XAccessible
;
22 import com
.sun
.star
.accessibility
.XAccessibleComponent
;
23 import com
.sun
.star
.accessibility
.XAccessibleContext
;
24 import com
.sun
.star
.awt
.Point
;
25 import com
.sun
.star
.awt
.PosSize
;
26 import com
.sun
.star
.awt
.Rectangle
;
27 import com
.sun
.star
.awt
.XExtendedToolkit
;
28 import com
.sun
.star
.awt
.XTopWindow
;
29 import com
.sun
.star
.awt
.XWindow
;
30 import com
.sun
.star
.beans
.PropertyValue
;
31 import com
.sun
.star
.frame
.XModel
;
32 import com
.sun
.star
.lang
.EventObject
;
33 import com
.sun
.star
.lang
.XComponent
;
34 import com
.sun
.star
.lang
.XMultiServiceFactory
;
35 import com
.sun
.star
.sheet
.RangeSelectionEvent
;
36 import com
.sun
.star
.sheet
.XRangeSelection
;
37 import com
.sun
.star
.sheet
.XRangeSelectionChangeListener
;
38 import com
.sun
.star
.sheet
.XRangeSelectionListener
;
39 import com
.sun
.star
.uno
.UnoRuntime
;
40 import java
.awt
.Robot
;
41 import java
.awt
.event
.InputEvent
;
42 import java
.io
.PrintWriter
;
43 import lib
.MultiMethodTest
;
45 import lib
.StatusException
;
46 import util
.AccessibilityTools
;
49 * Check the XRangeSelection interface.
51 public class _XRangeSelection
extends MultiMethodTest
{
52 public XRangeSelection oObj
= null;
53 MyRangeSelectionListener aListener
= null;
55 public void before() {
56 aListener
= new _XRangeSelection
.MyRangeSelectionListener(log
);
57 // workaround for i34499
58 XModel xModel
= (XModel
)tEnv
.getObjRelation("FirstModel");
60 throw new StatusException(Status
.failed("Object relation FirstModel' not set."));
61 XWindow xWindow
= xModel
.getCurrentController().getFrame().getContainerWindow();
62 XTopWindow xTopWindow
= UnoRuntime
.queryInterface(XTopWindow
.class, xWindow
);
64 util
.utils
.shortWait(500);
68 public void _abortRangeSelection() {
69 requiredMethod("removeRangeSelectionChangeListener()");
70 requiredMethod("removeRangeSelectionListener()");
71 oObj
.abortRangeSelection();
72 tRes
.tested("abortRangeSelection()", true);
75 public void _addRangeSelectionChangeListener() {
76 oObj
.addRangeSelectionChangeListener(aListener
);
77 tRes
.tested("addRangeSelectionChangeListener()", true);
80 public void _addRangeSelectionListener() {
81 oObj
.addRangeSelectionListener(aListener
);
82 tRes
.tested("addRangeSelectionListener()", true);
85 public void _removeRangeSelectionChangeListener() {
86 oObj
.removeRangeSelectionChangeListener(aListener
);
87 tRes
.tested("removeRangeSelectionChangeListener()", true);
90 public void _removeRangeSelectionListener() {
91 oObj
.removeRangeSelectionListener(aListener
);
92 tRes
.tested("removeRangeSelectionListener()", true);
95 public void _startRangeSelection() {
96 requiredMethod("addRangeSelectionChangeListener()");
97 requiredMethod("addRangeSelectionListener()");
99 // get the sheet center
100 Point center
= getSheetCenter();
102 throw new StatusException(Status
.failed("Couldn't get the sheet center."));
104 PropertyValue
[] props
= new PropertyValue
[3];
105 props
[0] = new PropertyValue();
106 props
[0].Name
= "InitialValue";
107 props
[0].Value
= "B3:D5";
108 props
[1] = new PropertyValue();
109 props
[1].Name
= "Title";
110 props
[1].Value
= "the title";
111 props
[2] = new PropertyValue();
112 props
[2].Name
= "CloseOnMouseRelease";
113 props
[2].Value
= Boolean
.FALSE
;
114 oObj
.startRangeSelection(props
);
115 // wait for listeners
116 util
.utils
.shortWait(1000);
118 // get closer button: move if window cobvers the sheet center
119 Point closer
= getCloser(center
);
121 throw new StatusException(Status
.failed("Couldn't get the close Button."));
123 // do something to trigger the listeners
124 clickOnSheet(center
);
125 util
.utils
.shortWait(5000);
128 clickOnSheet(closer
);
129 util
.utils
.shortWait(5000);
131 // System.out.println("X: " + closer.X + " Y: " + closer.Y);
133 // just check that we do not have the page instead of the range descriptor
134 /* int childCount = xRoot.getAccessibleContext().getAccessibleChildCount();
135 if (childCount > 3) {// too many children: wrong type
136 throw new StatusException(Status.failed("Could not get the Range Descriptor"));
138 XAccessible xAcc = null;
140 xAcc = xRoot.getAccessibleContext().getAccessibleChild(1);
142 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
145 accTools.printAccessibleTree(log, xAcc);
147 // open a new range selection
148 props
[0].Value
= "C4:E6";
149 oObj
.startRangeSelection(props
);
150 util
.utils
.shortWait(1000);
151 props
[0].Value
= "C2:E3";
152 oObj
.startRangeSelection(props
);
153 util
.utils
.shortWait(1000);
155 oObj
.startRangeSelection(props
);
156 util
.utils
.shortWait(1000);
157 oObj
.abortRangeSelection();
159 System
.out
.println("Listener called: " + aListener
.bAbortCalled
);
161 tRes
.tested("startRangeSelection()", aListener
.listenerCalled());
165 * Determine the current top window center and return this as a point.
166 * @return a point representing the sheet center.
168 protected Point
getSheetCenter() {
169 log
.println("Trying to get AccessibleSpreadsheet");
170 AccessibilityTools at
= new AccessibilityTools();
171 XComponent xSheetDoc
= (XComponent
) tEnv
.getObjRelation("DOCUMENT");
173 XModel xModel
= UnoRuntime
.queryInterface(XModel
.class, xSheetDoc
);
174 System
.out
.println("Name: " + xModel
.getCurrentController().getFrame().getName());
176 XWindow xWindow
= AccessibilityTools
.getCurrentWindow((XMultiServiceFactory
)tParam
.getMSF(), xModel
);
177 XAccessible xRoot
= AccessibilityTools
.getAccessibleObject(xWindow
);
179 XAccessibleContext ctx
= AccessibilityTools
.getAccessibleObjectForRole(xRoot
, AccessibleRole
.TABLE
);
181 XAccessibleComponent AccessibleSpreadsheet
= UnoRuntime
.queryInterface(XAccessibleComponent
.class,ctx
);
183 log
.println("Got " + util
.utils
.getImplName(AccessibleSpreadsheet
));
185 Object toolkit
= null;
188 toolkit
= ((XMultiServiceFactory
)tParam
.getMSF()).createInstance("com.sun.star.awt.Toolkit");
189 } catch (com
.sun
.star
.uno
.Exception e
) {
190 log
.println("Couldn't get toolkit");
191 e
.printStackTrace(log
);
192 throw new StatusException("Couldn't get toolkit", e
);
195 XExtendedToolkit tk
= UnoRuntime
.queryInterface(
196 XExtendedToolkit
.class, toolkit
);
198 XTopWindow tw
= null;
200 int k
= tk
.getTopWindowCount();
201 for (int i
=0;i
<k
;i
++) {
203 XTopWindow tw_temp
= tk
.getTopWindow(i
);
204 XAccessible xacc
= UnoRuntime
.queryInterface(XAccessible
.class, tw_temp
);
206 if (xacc
.getAccessibleContext().getAccessibleName().indexOf("d2")>0) {
210 log
.println("\t unknown window");
213 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
217 System
.out
.println("No TopWindow :-(");
221 Point point
= AccessibleSpreadsheet
.getLocationOnScreen();
222 Rectangle rect
= AccessibleSpreadsheet
.getBounds();
223 Point retPoint
= new Point();
224 retPoint
.X
= point
.X
+ (rect
.Width
/ 2);
225 retPoint
.Y
= point
.Y
+ (rect
.Height
/ 2);
230 * Get the closer button on the right top of the current window.
231 * @return A point representing the closer button.
233 private Point
getCloser(Point center
) {
234 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)tParam
.getMSF();
235 Object aToolkit
= null;
237 aToolkit
= xMSF
.createInstance("com.sun.star.awt.Toolkit");
239 catch(com
.sun
.star
.uno
.Exception e
) {
240 throw new StatusException("Could not create 'com.sun.star.awt.Toolkit'.", e
);
242 XExtendedToolkit xExtendedToolkit
= UnoRuntime
.queryInterface(XExtendedToolkit
.class, aToolkit
);
243 XTopWindow tw
= null;
245 XAccessibleComponent xAccessibleComponent
= null;
246 int k
= xExtendedToolkit
.getTopWindowCount();
247 for (int i
=0;i
<k
;i
++) {
249 XTopWindow tw_temp
= xExtendedToolkit
.getTopWindow(i
);
250 XAccessible xacc
= UnoRuntime
.queryInterface(XAccessible
.class, tw_temp
);
252 System
.out
.println("Name: " + xacc
.getAccessibleContext().getAccessibleName());
253 if (xacc
.getAccessibleContext().getAccessibleName().startsWith("the title")) {
255 XAccessibleContext xContext
= xacc
.getAccessibleContext();
256 xAccessibleComponent
= UnoRuntime
.queryInterface(XAccessibleComponent
.class, xContext
);
257 if (xAccessibleComponent
== null)
258 System
.out
.println("!!!! MIST !!!!");
260 System
.out
.println("########## KLAPPT ########## ");
264 log
.println("\t unknown window");
267 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
271 System
.out
.println("No TopWindow :-(");
275 XWindow xWindow
= UnoRuntime
.queryInterface(XWindow
.class, tw
);
276 Rectangle posSize
= xWindow
.getPosSize();
278 // compare the center point with the dimensions of the current top window
279 boolean windowOK
= false;
281 if (posSize
.X
<= center
.X
&& center
.X
<= posSize
.X
+ posSize
.Width
) {
282 if (posSize
.Y
<= center
.Y
&& center
.Y
<= posSize
.Y
+posSize
.Height
) {
283 // move window out of the way
284 posSize
.X
= posSize
.X
+ 10;
285 posSize
.Y
= posSize
.Y
+10;
286 xWindow
.setPosSize(posSize
.X
, posSize
.Y
, posSize
.Width
, posSize
.Height
, PosSize
.POS
);
298 Point p
= xAccessibleComponent
.getLocationOnScreen();
299 // System.out.println("ScreenPoint: " + p.X + " " + p.Y );
300 // System.out.println("WindowPoint: " + posSize.X + " " + posSize.Y + " " + posSize.Width + " " + posSize.Height);
301 Point closer
= new Point();
302 closer
.X
= p
.X
+ posSize
.Width
- 2;
304 System
.out
.println("Closer: " + closer
.X
+ " " + closer
.Y
);
308 protected boolean clickOnSheet(Point point
) {
309 log
.println("Clicking in the center of the AccessibleSpreadsheet");
312 Robot rob
= new Robot();
313 rob
.mouseMove(point
.X
, point
.Y
);
314 rob
.mousePress(InputEvent
.BUTTON1_MASK
);
315 util
.utils
.shortWait(1000);
316 rob
.mouseRelease(InputEvent
.BUTTON1_MASK
);
317 util
.utils
.shortWait(1000);
318 } catch (java
.awt
.AWTException e
) {
319 log
.println("couldn't press mouse button");
328 public static class MyRangeSelectionListener
implements XRangeSelectionListener
, XRangeSelectionChangeListener
{
329 boolean bAbortCalled
= false;
330 boolean bChangeCalled
= false;
331 boolean bDoneCalled
= false;
332 PrintWriter log
= null;
334 public MyRangeSelectionListener(PrintWriter log
) {
338 public void aborted(RangeSelectionEvent rangeSelectionEvent
) {
339 log
.println("Called 'aborted' with: " + rangeSelectionEvent
.RangeDescriptor
);
343 public void descriptorChanged(RangeSelectionEvent rangeSelectionEvent
) {
344 log
.println("Called 'descriptorChanged' with: " + rangeSelectionEvent
.RangeDescriptor
);
345 bChangeCalled
= true;
348 public void done(RangeSelectionEvent rangeSelectionEvent
) {
349 log
.println("Called 'done' with: " + rangeSelectionEvent
.RangeDescriptor
);
353 public boolean listenerCalled() {
354 return bAbortCalled
& bChangeCalled
& bDoneCalled
;
357 public void reset() {
358 bAbortCalled
= false;
359 bChangeCalled
= false;
365 * @param eventObject The event.
367 public void disposing(EventObject eventObject
) {