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