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 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir package ifc.ui;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRole;
28cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
29cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleComponent;
30cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
31cdf0e10cSrcweir import com.sun.star.awt.Point;
32cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
33cdf0e10cSrcweir import com.sun.star.awt.XExtendedToolkit;
34cdf0e10cSrcweir import com.sun.star.awt.XWindow;
35cdf0e10cSrcweir import com.sun.star.frame.XModel;
36cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
37cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
38cdf0e10cSrcweir import com.sun.star.ui.XContextMenuInterception;
39cdf0e10cSrcweir import com.sun.star.ui.XContextMenuInterceptor;
40cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
41cdf0e10cSrcweir import com.sun.star.uno.XInterface;
42cdf0e10cSrcweir import helper.ContextMenuInterceptor;
43cdf0e10cSrcweir import java.awt.Robot;
44cdf0e10cSrcweir import java.awt.event.InputEvent;
45cdf0e10cSrcweir import lib.MultiMethodTest;
46cdf0e10cSrcweir import lib.Status;
47cdf0e10cSrcweir import lib.StatusException;
48cdf0e10cSrcweir import util.AccessibilityTools;
49cdf0e10cSrcweir import util.DesktopTools;
50cdf0e10cSrcweir import util.utils;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir public class _XContextMenuInterception extends MultiMethodTest {
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     private XModel docModel = null;
55cdf0e10cSrcweir     private XContextMenuInterceptor xCI = null;
56cdf0e10cSrcweir     public XContextMenuInterception oObj = null;
57cdf0e10cSrcweir     private XWindow xWindow = null;
58cdf0e10cSrcweir     private XMultiServiceFactory xMSF = null;
59cdf0e10cSrcweir     private Point point = null;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     public void before() {
62cdf0e10cSrcweir         docModel = (XModel) UnoRuntime.queryInterface(
63cdf0e10cSrcweir                 XModel.class,tEnv.getObjRelation("FirstModel"));
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         xCI = (XContextMenuInterceptor) UnoRuntime.queryInterface(
66cdf0e10cSrcweir                 XContextMenuInterceptor.class, new ContextMenuInterceptor());
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         xMSF = (XMultiServiceFactory)tParam.getMSF();
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         //ensure that the first model is focused
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         log.println("ensure that the first model is focused");
73cdf0e10cSrcweir         DesktopTools.bringWindowToFront(docModel);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         utils.shortWait(3000);
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     public void after() {
79cdf0e10cSrcweir         if (xCI != null) {
80cdf0e10cSrcweir             oObj.releaseContextMenuInterceptor(xCI);
81cdf0e10cSrcweir         }
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     public void _registerContextMenuInterceptor() {
85cdf0e10cSrcweir         oObj.registerContextMenuInterceptor(xCI);
86cdf0e10cSrcweir         openContextMenu(docModel);
87cdf0e10cSrcweir         boolean res = checkHelpEntry();
88cdf0e10cSrcweir         releasePopUp();
89cdf0e10cSrcweir         tRes.tested("registerContextMenuInterceptor()",res);
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     public void _releaseContextMenuInterceptor() {
93cdf0e10cSrcweir         requiredMethod("registerContextMenuInterceptor()");
94cdf0e10cSrcweir         oObj.releaseContextMenuInterceptor(xCI);
95cdf0e10cSrcweir         openContextMenu(docModel);
96cdf0e10cSrcweir         boolean res = checkHelpEntry();
97cdf0e10cSrcweir         releasePopUp();
98cdf0e10cSrcweir         tRes.tested("releaseContextMenuInterceptor()",!res);
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     private boolean checkHelpEntry(){
102cdf0e10cSrcweir         XInterface toolkit = null;
103cdf0e10cSrcweir         boolean res = true;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         log.println("get accesibility...");
106cdf0e10cSrcweir         try{
107cdf0e10cSrcweir             toolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
108cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e){
109cdf0e10cSrcweir             log.println("could not get Toolkit " + e.toString());
110cdf0e10cSrcweir         }
111cdf0e10cSrcweir         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
112cdf0e10cSrcweir                 XExtendedToolkit.class, toolkit);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         XAccessible xRoot = null;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         try {
119cdf0e10cSrcweir             xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
120cdf0e10cSrcweir                     tk.getTopWindow(0));
121cdf0e10cSrcweir 
122cdf0e10cSrcweir             xRoot = at.getAccessibleObject(xWindow);
123cdf0e10cSrcweir             at.printAccessibleTree(log, xRoot, tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
124cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
125cdf0e10cSrcweir             log.println("Couldn't get Window");
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         XAccessibleContext oPopMenu = at.getAccessibleObjectForRole(xRoot, AccessibleRole.POPUP_MENU,true);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         log.println("ImplementationName: " + util.utils.getImplName(oPopMenu));
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         XAccessible xHelp = null;
133cdf0e10cSrcweir         try{
134cdf0e10cSrcweir             log.println("Try to get second entry of context menu...");
135cdf0e10cSrcweir             xHelp = oPopMenu.getAccessibleChild(1);
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         } catch (IndexOutOfBoundsException e){
138cdf0e10cSrcweir             throw new StatusException("Not possible to get second entry of context menu",e);
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         if (xHelp == null) throw new StatusException(new Status("second entry of context menu is NULL", false));
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         XAccessibleContext xHelpCont = xHelp.getAccessibleContext();
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         if ( xHelpCont == null )
146cdf0e10cSrcweir             throw new StatusException(new Status("No able to retrieve accessible context from first entry of context menu",false));
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         String aAccessibleName = xHelpCont.getAccessibleName();
149cdf0e10cSrcweir         if ( !aAccessibleName.equals( "Help" )) {
150cdf0e10cSrcweir             log.println("Accessible name found = "+aAccessibleName );
151cdf0e10cSrcweir             log.println("Second entry of context menu is not from context menu interceptor");
152cdf0e10cSrcweir             res=false;
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         return res;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     private void openContextMenu(XModel xModel){
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         log.println("try to open contex menu...");
162cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         xWindow = at.getCurrentWindow(xMSF, xModel);
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         XInterface oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         XAccessibleComponent window = (XAccessibleComponent) UnoRuntime.queryInterface(
171cdf0e10cSrcweir                 XAccessibleComponent.class, oObj);
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         point = window.getLocationOnScreen();
174cdf0e10cSrcweir         Rectangle rect = window.getBounds();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         log.println("klick mouse button...");
177cdf0e10cSrcweir         try {
178cdf0e10cSrcweir             Robot rob = new Robot();
179cdf0e10cSrcweir             int x = point.X + (rect.Width / 2);
180cdf0e10cSrcweir             int y = point.Y + (rect.Height / 2);
181cdf0e10cSrcweir             rob.mouseMove(x, y);
182cdf0e10cSrcweir             System.out.println("Press Button");
183cdf0e10cSrcweir             rob.mousePress(InputEvent.BUTTON3_MASK);
184cdf0e10cSrcweir             System.out.println("Release Button");
185cdf0e10cSrcweir             rob.mouseRelease(InputEvent.BUTTON3_MASK);
186cdf0e10cSrcweir             System.out.println("done");
187cdf0e10cSrcweir         } catch (java.awt.AWTException e) {
188cdf0e10cSrcweir             log.println("couldn't press mouse button");
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         utils.shortWait(1000);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     private void releasePopUp() {
196cdf0e10cSrcweir         log.println("release the popup menu");
197cdf0e10cSrcweir         try {
198cdf0e10cSrcweir             Robot rob = new Robot();
199cdf0e10cSrcweir             int x = point.X;
200cdf0e10cSrcweir             int y = point.Y;
201cdf0e10cSrcweir             rob.mouseMove(x, y);
202cdf0e10cSrcweir             rob.mousePress(InputEvent.BUTTON1_MASK);
203cdf0e10cSrcweir             rob.mouseRelease(InputEvent.BUTTON1_MASK);
204cdf0e10cSrcweir         } catch (java.awt.AWTException e) {
205cdf0e10cSrcweir             log.println("couldn't press mouse button");
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir     }
208cdf0e10cSrcweir }
209