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