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