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.awt.Point;
29 import com.sun.star.awt.Rectangle;
30 import com.sun.star.awt.XEnhancedMouseClickHandler;
31 import com.sun.star.awt.XExtendedToolkit;
32 import com.sun.star.awt.XTopWindow;
33 import com.sun.star.awt.XWindow;
34 import com.sun.star.frame.XModel;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sheet.XEnhancedMouseClickBroadcaster;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 
41 import lib.MultiMethodTest;
42 import lib.StatusException;
43 
44 import util.AccessibilityTools;
45 import util.DesktopTools;
46 import util.utils;
47 
48 import java.awt.Robot;
49 import java.awt.event.InputEvent;
50 
51 
52 public class _XEnhancedMouseClickBroadcaster extends MultiMethodTest {
53     public XEnhancedMouseClickBroadcaster oObj;
54     protected boolean mousePressed = false;
55     protected boolean mouseReleased = false;
56     protected XEnhancedMouseClickHandler listener = new MyListener();
57     private XModel docModel = null;
58 
before()59     public void before() {
60         docModel = (XModel) UnoRuntime.queryInterface(
61                 XModel.class,tEnv.getObjRelation("FirstModel"));
62         DesktopTools.bringWindowToFront(docModel);
63     }
64 
_addEnhancedMouseClickHandler()65     public void _addEnhancedMouseClickHandler() {
66         oObj.addEnhancedMouseClickHandler(listener);
67         clickOnSheet();
68 
69         //make sure that the listener is removed even if the test fails
70         if ((!mousePressed) || (!mouseReleased)) {
71             oObj.removeEnhancedMouseClickHandler(listener);
72         }
73 
74         tRes.tested("addEnhancedMouseClickHandler()",
75             mousePressed && mouseReleased);
76     }
77 
_removeEnhancedMouseClickHandler()78     public void _removeEnhancedMouseClickHandler() {
79         requiredMethod("addEnhancedMouseClickHandler()");
80         mousePressed = false;
81         mouseReleased = false;
82         oObj.removeEnhancedMouseClickHandler(listener);
83         clickOnSheet();
84         tRes.tested("removeEnhancedMouseClickHandler()",
85             (!mousePressed) && (!mouseReleased));
86     }
87 
clickOnSheet()88     protected boolean clickOnSheet() {
89         log.println("try to open contex menu...");
90         AccessibilityTools at = new AccessibilityTools();
91 
92         XWindow xWindow = at.getCurrentWindow((XMultiServiceFactory)tParam.getMSF(), docModel);
93 
94         XAccessible xRoot = at.getAccessibleObject(xWindow);
95 
96         XInterface oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
97 
98         XAccessibleComponent window = (XAccessibleComponent) UnoRuntime.queryInterface(
99                 XAccessibleComponent.class, oObj);
100 
101         Point point = window.getLocationOnScreen();
102         Rectangle rect = window.getBounds();
103 
104         log.println("klick mouse button...");
105         try {
106             Robot rob = new Robot();
107             int x = point.X + (rect.Width / 2)+50;
108             int y = point.Y + (rect.Height / 2)+50;
109             rob.mouseMove(x, y);
110             System.out.println("Press Button");
111             rob.mousePress(InputEvent.BUTTON3_MASK);
112             System.out.println("Release Button");
113             rob.mouseRelease(InputEvent.BUTTON3_MASK);
114             System.out.println("done");
115             System.out.println("warte");
116             shortWait();
117             System.out.println("Press Button");
118             rob.mousePress(InputEvent.BUTTON1_MASK);
119             System.out.println("Release Button");
120             rob.mouseRelease(InputEvent.BUTTON1_MASK);
121             System.out.println("done "+rob.getAutoDelay());
122         } catch (java.awt.AWTException e) {
123             log.println("couldn't press mouse button");
124         }
125 
126 
127         return true;
128     }
129 
shortWait()130     private void shortWait() {
131         try {
132             Thread.currentThread().sleep(2000);
133         } catch (InterruptedException e) {
134             System.out.println("While waiting :" + e);
135         }
136     }
137 
138     protected class MyListener implements XEnhancedMouseClickHandler {
disposing( com.sun.star.lang.EventObject eventObject)139         public void disposing(
140             com.sun.star.lang.EventObject eventObject) {
141         }
142 
mousePressed( com.sun.star.awt.EnhancedMouseEvent enhancedMouseEvent)143         public boolean mousePressed(
144             com.sun.star.awt.EnhancedMouseEvent enhancedMouseEvent) {
145             log.println("mousePressed");
146             mousePressed = true;
147 
148             return true;
149         }
150 
mouseReleased( com.sun.star.awt.EnhancedMouseEvent enhancedMouseEvent)151         public boolean mouseReleased(
152             com.sun.star.awt.EnhancedMouseEvent enhancedMouseEvent) {
153             log.println("mouseReleased");
154             mouseReleased = true;
155 
156             return true;
157         }
158     }
159 }
160