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