1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package ifc.sheet;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRole;
26cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
27cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleComponent;
28cdf0e10cSrcweir import com.sun.star.awt.Point;
29cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
30cdf0e10cSrcweir import com.sun.star.awt.XEnhancedMouseClickHandler;
31cdf0e10cSrcweir import com.sun.star.awt.XExtendedToolkit;
32cdf0e10cSrcweir import com.sun.star.awt.XTopWindow;
33cdf0e10cSrcweir import com.sun.star.awt.XWindow;
34cdf0e10cSrcweir import com.sun.star.frame.XModel;
35cdf0e10cSrcweir import com.sun.star.lang.XComponent;
36cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
37cdf0e10cSrcweir import com.sun.star.sheet.XEnhancedMouseClickBroadcaster;
38cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
39cdf0e10cSrcweir import com.sun.star.uno.XInterface;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir import lib.MultiMethodTest;
42cdf0e10cSrcweir import lib.StatusException;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir import util.AccessibilityTools;
45cdf0e10cSrcweir import util.DesktopTools;
46cdf0e10cSrcweir import util.utils;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir import java.awt.Robot;
49cdf0e10cSrcweir import java.awt.event.InputEvent;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 
52cdf0e10cSrcweir public class _XEnhancedMouseClickBroadcaster extends MultiMethodTest {
53cdf0e10cSrcweir     public XEnhancedMouseClickBroadcaster oObj;
54cdf0e10cSrcweir     protected boolean mousePressed = false;
55cdf0e10cSrcweir     protected boolean mouseReleased = false;
56cdf0e10cSrcweir     protected XEnhancedMouseClickHandler listener = new MyListener();
57cdf0e10cSrcweir     private XModel docModel = null;
58cdf0e10cSrcweir 
before()59cdf0e10cSrcweir     public void before() {
60cdf0e10cSrcweir         docModel = (XModel) UnoRuntime.queryInterface(
61cdf0e10cSrcweir                 XModel.class,tEnv.getObjRelation("FirstModel"));
62cdf0e10cSrcweir         DesktopTools.bringWindowToFront(docModel);
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
_addEnhancedMouseClickHandler()65cdf0e10cSrcweir     public void _addEnhancedMouseClickHandler() {
66cdf0e10cSrcweir         oObj.addEnhancedMouseClickHandler(listener);
67cdf0e10cSrcweir         clickOnSheet();
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         //make sure that the listener is removed even if the test fails
70cdf0e10cSrcweir         if ((!mousePressed) || (!mouseReleased)) {
71cdf0e10cSrcweir             oObj.removeEnhancedMouseClickHandler(listener);
72cdf0e10cSrcweir         }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir         tRes.tested("addEnhancedMouseClickHandler()",
75cdf0e10cSrcweir             mousePressed && mouseReleased);
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
_removeEnhancedMouseClickHandler()78cdf0e10cSrcweir     public void _removeEnhancedMouseClickHandler() {
79cdf0e10cSrcweir         requiredMethod("addEnhancedMouseClickHandler()");
80cdf0e10cSrcweir         mousePressed = false;
81cdf0e10cSrcweir         mouseReleased = false;
82cdf0e10cSrcweir         oObj.removeEnhancedMouseClickHandler(listener);
83cdf0e10cSrcweir         clickOnSheet();
84cdf0e10cSrcweir         tRes.tested("removeEnhancedMouseClickHandler()",
85cdf0e10cSrcweir             (!mousePressed) && (!mouseReleased));
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
clickOnSheet()88cdf0e10cSrcweir     protected boolean clickOnSheet() {
89cdf0e10cSrcweir         log.println("try to open contex menu...");
90cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         XWindow xWindow = at.getCurrentWindow((XMultiServiceFactory)tParam.getMSF(), docModel);
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         XInterface oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         XAccessibleComponent window = (XAccessibleComponent) UnoRuntime.queryInterface(
99cdf0e10cSrcweir                 XAccessibleComponent.class, oObj);
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         Point point = window.getLocationOnScreen();
102cdf0e10cSrcweir         Rectangle rect = window.getBounds();
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         log.println("klick mouse button...");
105cdf0e10cSrcweir         try {
106cdf0e10cSrcweir             Robot rob = new Robot();
107cdf0e10cSrcweir             int x = point.X + (rect.Width / 2)+50;
108cdf0e10cSrcweir             int y = point.Y + (rect.Height / 2)+50;
109cdf0e10cSrcweir             rob.mouseMove(x, y);
110cdf0e10cSrcweir             System.out.println("Press Button");
111cdf0e10cSrcweir             rob.mousePress(InputEvent.BUTTON3_MASK);
112cdf0e10cSrcweir             System.out.println("Release Button");
113cdf0e10cSrcweir             rob.mouseRelease(InputEvent.BUTTON3_MASK);
114cdf0e10cSrcweir             System.out.println("done");
115cdf0e10cSrcweir             System.out.println("warte");
116cdf0e10cSrcweir             shortWait();
117cdf0e10cSrcweir             System.out.println("Press Button");
118cdf0e10cSrcweir             rob.mousePress(InputEvent.BUTTON1_MASK);
119cdf0e10cSrcweir             System.out.println("Release Button");
120cdf0e10cSrcweir             rob.mouseRelease(InputEvent.BUTTON1_MASK);
121cdf0e10cSrcweir             System.out.println("done "+rob.getAutoDelay());
122cdf0e10cSrcweir         } catch (java.awt.AWTException e) {
123cdf0e10cSrcweir             log.println("couldn't press mouse button");
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         return true;
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir 
shortWait()130cdf0e10cSrcweir     private void shortWait() {
131cdf0e10cSrcweir         try {
132cdf0e10cSrcweir             Thread.currentThread().sleep(2000);
133cdf0e10cSrcweir         } catch (InterruptedException e) {
134cdf0e10cSrcweir             System.out.println("While waiting :" + e);
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     protected class MyListener implements XEnhancedMouseClickHandler {
disposing( com.sun.star.lang.EventObject eventObject)139cdf0e10cSrcweir         public void disposing(
140cdf0e10cSrcweir             com.sun.star.lang.EventObject eventObject) {
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir 
mousePressed( com.sun.star.awt.EnhancedMouseEvent enhancedMouseEvent)143cdf0e10cSrcweir         public boolean mousePressed(
144cdf0e10cSrcweir             com.sun.star.awt.EnhancedMouseEvent enhancedMouseEvent) {
145cdf0e10cSrcweir             log.println("mousePressed");
146cdf0e10cSrcweir             mousePressed = true;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir             return true;
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir 
mouseReleased( com.sun.star.awt.EnhancedMouseEvent enhancedMouseEvent)151cdf0e10cSrcweir         public boolean mouseReleased(
152cdf0e10cSrcweir             com.sun.star.awt.EnhancedMouseEvent enhancedMouseEvent) {
153cdf0e10cSrcweir             log.println("mouseReleased");
154cdf0e10cSrcweir             mouseReleased = true;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir             return true;
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir }
160