xref: /AOO41X/main/qadevOOo/tests/java/ifc/ui/_XContextMenuInterception.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir package ifc.ui;
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRole;
32*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
33*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleComponent;
34*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
35*cdf0e10cSrcweir import com.sun.star.awt.Point;
36*cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
37*cdf0e10cSrcweir import com.sun.star.awt.XExtendedToolkit;
38*cdf0e10cSrcweir import com.sun.star.awt.XWindow;
39*cdf0e10cSrcweir import com.sun.star.frame.XModel;
40*cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
41*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
42*cdf0e10cSrcweir import com.sun.star.ui.XContextMenuInterception;
43*cdf0e10cSrcweir import com.sun.star.ui.XContextMenuInterceptor;
44*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
45*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
46*cdf0e10cSrcweir import helper.ContextMenuInterceptor;
47*cdf0e10cSrcweir import java.awt.Robot;
48*cdf0e10cSrcweir import java.awt.event.InputEvent;
49*cdf0e10cSrcweir import lib.MultiMethodTest;
50*cdf0e10cSrcweir import lib.Status;
51*cdf0e10cSrcweir import lib.StatusException;
52*cdf0e10cSrcweir import util.AccessibilityTools;
53*cdf0e10cSrcweir import util.DesktopTools;
54*cdf0e10cSrcweir import util.utils;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir public class _XContextMenuInterception extends MultiMethodTest {
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir     private XModel docModel = null;
59*cdf0e10cSrcweir     private XContextMenuInterceptor xCI = null;
60*cdf0e10cSrcweir     public XContextMenuInterception oObj = null;
61*cdf0e10cSrcweir     private XWindow xWindow = null;
62*cdf0e10cSrcweir     private XMultiServiceFactory xMSF = null;
63*cdf0e10cSrcweir     private Point point = null;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     public void before() {
66*cdf0e10cSrcweir         docModel = (XModel) UnoRuntime.queryInterface(
67*cdf0e10cSrcweir                 XModel.class,tEnv.getObjRelation("FirstModel"));
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir         xCI = (XContextMenuInterceptor) UnoRuntime.queryInterface(
70*cdf0e10cSrcweir                 XContextMenuInterceptor.class, new ContextMenuInterceptor());
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir         xMSF = (XMultiServiceFactory)tParam.getMSF();
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir         //ensure that the first model is focused
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir         log.println("ensure that the first model is focused");
77*cdf0e10cSrcweir         DesktopTools.bringWindowToFront(docModel);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         utils.shortWait(3000);
80*cdf0e10cSrcweir     }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     public void after() {
83*cdf0e10cSrcweir         if (xCI != null) {
84*cdf0e10cSrcweir             oObj.releaseContextMenuInterceptor(xCI);
85*cdf0e10cSrcweir         }
86*cdf0e10cSrcweir     }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     public void _registerContextMenuInterceptor() {
89*cdf0e10cSrcweir         oObj.registerContextMenuInterceptor(xCI);
90*cdf0e10cSrcweir         openContextMenu(docModel);
91*cdf0e10cSrcweir         boolean res = checkHelpEntry();
92*cdf0e10cSrcweir         releasePopUp();
93*cdf0e10cSrcweir         tRes.tested("registerContextMenuInterceptor()",res);
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     public void _releaseContextMenuInterceptor() {
97*cdf0e10cSrcweir         requiredMethod("registerContextMenuInterceptor()");
98*cdf0e10cSrcweir         oObj.releaseContextMenuInterceptor(xCI);
99*cdf0e10cSrcweir         openContextMenu(docModel);
100*cdf0e10cSrcweir         boolean res = checkHelpEntry();
101*cdf0e10cSrcweir         releasePopUp();
102*cdf0e10cSrcweir         tRes.tested("releaseContextMenuInterceptor()",!res);
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     private boolean checkHelpEntry(){
106*cdf0e10cSrcweir         XInterface toolkit = null;
107*cdf0e10cSrcweir         boolean res = true;
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         log.println("get accesibility...");
110*cdf0e10cSrcweir         try{
111*cdf0e10cSrcweir             toolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
112*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e){
113*cdf0e10cSrcweir             log.println("could not get Toolkit " + e.toString());
114*cdf0e10cSrcweir         }
115*cdf0e10cSrcweir         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
116*cdf0e10cSrcweir                 XExtendedToolkit.class, toolkit);
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         XAccessible xRoot = null;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         try {
123*cdf0e10cSrcweir             xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
124*cdf0e10cSrcweir                     tk.getTopWindow(0));
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir             xRoot = at.getAccessibleObject(xWindow);
127*cdf0e10cSrcweir             at.printAccessibleTree(log, xRoot, tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
128*cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
129*cdf0e10cSrcweir             log.println("Couldn't get Window");
130*cdf0e10cSrcweir         }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         XAccessibleContext oPopMenu = at.getAccessibleObjectForRole(xRoot, AccessibleRole.POPUP_MENU,true);
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir         log.println("ImplementationName: " + util.utils.getImplName(oPopMenu));
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         XAccessible xHelp = null;
137*cdf0e10cSrcweir         try{
138*cdf0e10cSrcweir             log.println("Try to get second entry of context menu...");
139*cdf0e10cSrcweir             xHelp = oPopMenu.getAccessibleChild(1);
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir         } catch (IndexOutOfBoundsException e){
142*cdf0e10cSrcweir             throw new StatusException("Not possible to get second entry of context menu",e);
143*cdf0e10cSrcweir         }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         if (xHelp == null) throw new StatusException(new Status("second entry of context menu is NULL", false));
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         XAccessibleContext xHelpCont = xHelp.getAccessibleContext();
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         if ( xHelpCont == null )
150*cdf0e10cSrcweir             throw new StatusException(new Status("No able to retrieve accessible context from first entry of context menu",false));
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         String aAccessibleName = xHelpCont.getAccessibleName();
153*cdf0e10cSrcweir         if ( !aAccessibleName.equals( "Help" )) {
154*cdf0e10cSrcweir             log.println("Accessible name found = "+aAccessibleName );
155*cdf0e10cSrcweir             log.println("Second entry of context menu is not from context menu interceptor");
156*cdf0e10cSrcweir             res=false;
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         return res;
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     private void openContextMenu(XModel xModel){
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         log.println("try to open contex menu...");
166*cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         xWindow = at.getCurrentWindow(xMSF, xModel);
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir         XInterface oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         XAccessibleComponent window = (XAccessibleComponent) UnoRuntime.queryInterface(
175*cdf0e10cSrcweir                 XAccessibleComponent.class, oObj);
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir         point = window.getLocationOnScreen();
178*cdf0e10cSrcweir         Rectangle rect = window.getBounds();
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir         log.println("klick mouse button...");
181*cdf0e10cSrcweir         try {
182*cdf0e10cSrcweir             Robot rob = new Robot();
183*cdf0e10cSrcweir             int x = point.X + (rect.Width / 2);
184*cdf0e10cSrcweir             int y = point.Y + (rect.Height / 2);
185*cdf0e10cSrcweir             rob.mouseMove(x, y);
186*cdf0e10cSrcweir             System.out.println("Press Button");
187*cdf0e10cSrcweir             rob.mousePress(InputEvent.BUTTON3_MASK);
188*cdf0e10cSrcweir             System.out.println("Release Button");
189*cdf0e10cSrcweir             rob.mouseRelease(InputEvent.BUTTON3_MASK);
190*cdf0e10cSrcweir             System.out.println("done");
191*cdf0e10cSrcweir         } catch (java.awt.AWTException e) {
192*cdf0e10cSrcweir             log.println("couldn't press mouse button");
193*cdf0e10cSrcweir         }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir         utils.shortWait(1000);
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     private void releasePopUp() {
200*cdf0e10cSrcweir         log.println("release the popup menu");
201*cdf0e10cSrcweir         try {
202*cdf0e10cSrcweir             Robot rob = new Robot();
203*cdf0e10cSrcweir             int x = point.X;
204*cdf0e10cSrcweir             int y = point.Y;
205*cdf0e10cSrcweir             rob.mouseMove(x, y);
206*cdf0e10cSrcweir             rob.mousePress(InputEvent.BUTTON1_MASK);
207*cdf0e10cSrcweir             rob.mouseRelease(InputEvent.BUTTON1_MASK);
208*cdf0e10cSrcweir         } catch (java.awt.AWTException e) {
209*cdf0e10cSrcweir             log.println("couldn't press mouse button");
210*cdf0e10cSrcweir         }
211*cdf0e10cSrcweir     }
212*cdf0e10cSrcweir }
213