Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / sheet / _XRangeSelection.java
blob74eb522ef0888e5aa1fd53c947fc2e2fb59cb8e4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XRangeSelection.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package ifc.sheet;
32 import com.sun.star.accessibility.AccessibleRole;
33 import com.sun.star.accessibility.XAccessible;
34 import com.sun.star.accessibility.XAccessibleComponent;
35 import com.sun.star.accessibility.XAccessibleContext;
36 import com.sun.star.awt.Point;
37 import com.sun.star.awt.PosSize;
38 import com.sun.star.awt.Rectangle;
39 import com.sun.star.awt.XExtendedToolkit;
40 import com.sun.star.awt.XTopWindow;
41 import com.sun.star.awt.XWindow;
42 import com.sun.star.beans.PropertyValue;
43 import com.sun.star.frame.XModel;
44 import com.sun.star.lang.EventObject;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.sheet.RangeSelectionEvent;
48 import com.sun.star.sheet.XRangeSelection;
49 import com.sun.star.sheet.XRangeSelectionChangeListener;
50 import com.sun.star.sheet.XRangeSelectionListener;
51 import com.sun.star.uno.UnoRuntime;
52 import java.awt.Robot;
53 import java.awt.event.InputEvent;
54 import java.io.PrintWriter;
55 import lib.MultiMethodTest;
56 import lib.Status;
57 import lib.StatusException;
58 import util.AccessibilityTools;
60 /**
61 * Check the XRangeSelection interface.
63 public class _XRangeSelection extends MultiMethodTest {
64 public XRangeSelection oObj = null;
65 MyRangeSelectionListener aListener = null;
67 public void before() {
68 aListener = new _XRangeSelection.MyRangeSelectionListener(log);
69 // workaround for i34499
70 XModel xModel = (XModel)tEnv.getObjRelation("FirstModel");
71 if (xModel == null)
72 throw new StatusException(Status.failed("Object relation FirstModel' not set."));
73 XWindow xWindow = xModel.getCurrentController().getFrame().getContainerWindow();
74 XTopWindow xTopWindow = (XTopWindow)UnoRuntime.queryInterface(XTopWindow.class, xWindow);
75 xTopWindow.toFront();
76 util.utils.shortWait(500);
80 public void _abortRangeSelection() {
81 requiredMethod("removeRangeSelectionChangeListener()");
82 requiredMethod("removeRangeSelectionListener()");
83 oObj.abortRangeSelection();
84 tRes.tested("abortRangeSelection()", true);
87 public void _addRangeSelectionChangeListener() {
88 oObj.addRangeSelectionChangeListener(aListener);
89 tRes.tested("addRangeSelectionChangeListener()", true);
92 public void _addRangeSelectionListener() {
93 oObj.addRangeSelectionListener(aListener);
94 tRes.tested("addRangeSelectionListener()", true);
97 public void _removeRangeSelectionChangeListener() {
98 oObj.removeRangeSelectionChangeListener(aListener);
99 tRes.tested("removeRangeSelectionChangeListener()", true);
102 public void _removeRangeSelectionListener() {
103 oObj.removeRangeSelectionListener(aListener);
104 tRes.tested("removeRangeSelectionListener()", true);
107 public void _startRangeSelection() {
108 requiredMethod("addRangeSelectionChangeListener()");
109 requiredMethod("addRangeSelectionListener()");
111 // get the sheet center
112 Point center = getSheetCenter();;
113 if (center == null)
114 throw new StatusException(Status.failed("Couldn't get the sheet center."));
116 PropertyValue[] props = new PropertyValue[3];
117 props[0] = new PropertyValue();
118 props[0].Name = "InitialValue";
119 props[0].Value = "B3:D5";
120 props[1] = new PropertyValue();
121 props[1].Name = "Title";
122 props[1].Value = "the title";
123 props[2] = new PropertyValue();
124 props[2].Name = "CloseOnMouseRelease";
125 props[2].Value = Boolean.FALSE;
126 oObj.startRangeSelection(props);
127 // wait for listeners
128 util.utils.shortWait(1000);
130 // get closer button: move if window cobvers the sheet center
131 Point closer = getCloser(center);
132 if (closer == null)
133 throw new StatusException(Status.failed("Couldn't get the close Button."));
135 // do something to trigger the listeners
136 clickOnSheet(center);
137 util.utils.shortWait(5000);
139 // click on closer
140 clickOnSheet(closer);
141 util.utils.shortWait(5000);
143 // System.out.println("X: " + closer.X + " Y: " + closer.Y);
145 // just check that we do not have the page instead of the range descriptor
146 /* int childCount = xRoot.getAccessibleContext().getAccessibleChildCount();
147 if (childCount > 3) {// too many children: wrong type
148 throw new StatusException(Status.failed("Could not get the Range Descriptor"));
150 XAccessible xAcc = null;
151 try {
152 xAcc = xRoot.getAccessibleContext().getAccessibleChild(1);
154 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
157 accTools.printAccessibleTree(log, xAcc);
159 // open a new range selection
160 props[0].Value = "C4:E6";
161 oObj.startRangeSelection(props);
162 util.utils.shortWait(1000);
163 props[0].Value = "C2:E3";
164 oObj.startRangeSelection(props);
165 util.utils.shortWait(1000);
167 oObj.startRangeSelection(props);
168 util.utils.shortWait(1000);
169 oObj.abortRangeSelection();
170 aListener.reset();
171 System.out.println("Listener called: " + aListener.bAbortCalled);
173 tRes.tested("startRangeSelection()", aListener.listenerCalled());
177 * Determine the current top window center and return this as a point.
178 * @ return a point representing the sheet center.
180 protected Point getSheetCenter() {
181 log.println("Trying to get AccessibleSpreadsheet");
182 AccessibilityTools at = new AccessibilityTools();
183 XComponent xSheetDoc = (XComponent) tEnv.getObjRelation("DOCUMENT");
185 XModel xModel = (XModel)
186 UnoRuntime.queryInterface(XModel.class, xSheetDoc);
187 System.out.println("Name: " + xModel.getCurrentController().getFrame().getName());
189 XWindow xWindow = at.getCurrentWindow((XMultiServiceFactory)tParam.getMSF(), xModel);
190 XAccessible xRoot = at.getAccessibleObject(xWindow);
192 at.getAccessibleObjectForRole(xRoot, AccessibleRole.TABLE );
194 XAccessibleComponent AccessibleSpreadsheet = (XAccessibleComponent) UnoRuntime.queryInterface(XAccessibleComponent.class,AccessibilityTools.SearchedContext);
196 log.println("Got " + util.utils.getImplName(AccessibleSpreadsheet));
198 Object toolkit = null;
200 try {
201 toolkit = ((XMultiServiceFactory)tParam.getMSF()).createInstance("com.sun.star.awt.Toolkit");
202 } catch (com.sun.star.uno.Exception e) {
203 log.println("Couldn't get toolkit");
204 e.printStackTrace(log);
205 throw new StatusException("Couldn't get toolkit", e);
208 XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
209 XExtendedToolkit.class, toolkit);
211 XTopWindow tw = null;
213 int k = tk.getTopWindowCount();
214 for (int i=0;i<k;i++) {
215 try {
216 XTopWindow tw_temp = tk.getTopWindow(i);
217 XAccessible xacc = (XAccessible) UnoRuntime.queryInterface(XAccessible.class, tw_temp);
218 if (xacc != null) {
219 if (xacc.getAccessibleContext().getAccessibleName().indexOf("d2")>0) {
220 tw=tw_temp;
222 } else {
223 log.println("\t unknown window");
226 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
229 if (tw == null) {
230 System.out.println("No TopWindow :-(");
231 return null;
234 Point point = AccessibleSpreadsheet.getLocationOnScreen();
235 Rectangle rect = AccessibleSpreadsheet.getBounds();
236 Point retPoint = new Point();
237 retPoint.X = point.X + (rect.Width / 2);
238 retPoint.Y = point.Y + (rect.Height / 2);
239 return retPoint;
243 * Get the closer button on the right top of the current window.
244 * @return A point representing the closer button.
246 private Point getCloser(Point center) {
247 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
248 Object aToolkit = null;
249 try {
250 aToolkit = xMSF.createInstance("com.sun.star.awt.Toolkit");
252 catch(com.sun.star.uno.Exception e) {
253 throw new StatusException("Could not create 'com.sun.star.awt.Toolkit'.", e);
255 XExtendedToolkit xExtendedToolkit = (XExtendedToolkit)UnoRuntime.queryInterface(XExtendedToolkit.class, aToolkit);
256 AccessibilityTools accTools = new AccessibilityTools();
257 XWindow xWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, xExtendedToolkit.getActiveTopWindow());
258 XAccessible xRoot = accTools.getAccessibleObject(xWindow);
260 XTopWindow tw = null;
262 XAccessibleComponent xAccessibleComponent = null;
263 int k = xExtendedToolkit.getTopWindowCount();
264 for (int i=0;i<k;i++) {
265 try {
266 XTopWindow tw_temp = xExtendedToolkit.getTopWindow(i);
267 XAccessible xacc = (XAccessible)UnoRuntime.queryInterface(XAccessible.class, tw_temp);
268 if (xacc != null) {
269 System.out.println("Name: " + xacc.getAccessibleContext().getAccessibleName());
270 if (xacc.getAccessibleContext().getAccessibleName().startsWith("the title")) {
271 tw = tw_temp;
272 XAccessibleContext xContext = xacc.getAccessibleContext();
273 xAccessibleComponent = (XAccessibleComponent)UnoRuntime.queryInterface(XAccessibleComponent.class, xContext);
274 if (xAccessibleComponent == null)
275 System.out.println("!!!! MIST !!!!");
276 else
277 System.out.println("########## KLAPPT ########## ");
280 else {
281 log.println("\t unknown window");
284 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
287 if (tw == null) {
288 System.out.println("No TopWindow :-(");
289 return null;
292 xWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, tw);
293 Rectangle posSize = xWindow.getPosSize();
295 // compare the center point with the dimensions of the current top window
296 boolean windowOK = false;
297 while(!windowOK) {
298 if (posSize.X <= center.X && center.X <= posSize.X + posSize.Width) {
299 if (posSize.Y <= center.Y && center.Y <= posSize.Y +posSize.Height) {
300 // move window out of the way
301 posSize.X = posSize.X + 10;
302 posSize.Y = posSize.Y +10;
303 xWindow.setPosSize(posSize.X, posSize.Y, posSize.Width, posSize.Height, PosSize.POS);
305 else {
306 windowOK = true;
309 else {
310 windowOK = true;
315 Point p = xAccessibleComponent.getLocationOnScreen();
316 // System.out.println("ScreenPoint: " + p.X + " " + p.Y );
317 // System.out.println("WindowPoint: " + posSize.X + " " + posSize.Y + " " + posSize.Width + " " + posSize.Height);
318 Point closer = new Point();
319 closer.X = p.X + posSize.Width - 2;
320 closer.Y = p.Y + 5;
321 System.out.println("Closer: " + closer.X + " " + closer.Y);
322 return closer;
325 protected boolean clickOnSheet(Point point) {
326 log.println("Clicking in the center of the AccessibleSpreadsheet");
328 try {
329 Robot rob = new Robot();
330 rob.mouseMove(point.X, point.Y);
331 rob.mousePress(InputEvent.BUTTON1_MASK);
332 util.utils.shortWait(1000);
333 rob.mouseRelease(InputEvent.BUTTON1_MASK);
334 util.utils.shortWait(1000);
335 } catch (java.awt.AWTException e) {
336 log.println("couldn't press mouse button");
339 return true;
345 public static class MyRangeSelectionListener implements XRangeSelectionListener, XRangeSelectionChangeListener {
346 boolean bAbortCalled = false;
347 boolean bChangeCalled = false;
348 boolean bDoneCalled = false;
349 PrintWriter log = null;
351 public MyRangeSelectionListener(PrintWriter log) {
352 this.log = log;
355 public void aborted(RangeSelectionEvent rangeSelectionEvent) {
356 log.println("Called 'aborted' with: " + rangeSelectionEvent.RangeDescriptor);
357 bAbortCalled = true;
360 public void descriptorChanged(RangeSelectionEvent rangeSelectionEvent) {
361 log.println("Called 'descriptorChanged' with: " + rangeSelectionEvent.RangeDescriptor);
362 bChangeCalled = true;
365 public void done(RangeSelectionEvent rangeSelectionEvent) {
366 log.println("Called 'done' with: " + rangeSelectionEvent.RangeDescriptor);
367 bDoneCalled = true;
370 public boolean listenerCalled() {
371 return bAbortCalled & bChangeCalled & bDoneCalled;
374 public void reset() {
375 bAbortCalled = false;
376 bChangeCalled = false;
377 bDoneCalled = false;
381 * ignore disposing
382 * @param eventObject The event.
384 public void disposing(EventObject eventObject) {