Debian Codegraph @

framework/qa/complex/contextMenuInterceptor/CheckContextMenuInterceptor.java

raw
1 /*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19 package complex.contextMenuInterceptor;
20
21 import com.sun.star.accessibility.AccessibleRole;
22 import com.sun.star.accessibility.XAccessible;
23 import com.sun.star.accessibility.XAccessibleComponent;
24 import com.sun.star.accessibility.XAccessibleContext;
25 import com.sun.star.awt.Point;
26 import com.sun.star.awt.Rectangle;
27 import com.sun.star.awt.XBitmap;
28 import com.sun.star.awt.XExtendedToolkit;
29 import com.sun.star.awt.XWindow;
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.drawing.XShape;
32 import com.sun.star.frame.XFrame;
33 import com.sun.star.frame.XModel;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.ui.XContextMenuInterception;
36 import com.sun.star.ui.XContextMenuInterceptor;
37 import com.sun.star.uno.AnyConverter;
38 import com.sun.star.uno.Type;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 import com.sun.star.util.XCloseable;
42 import java.awt.Robot;
43 import java.awt.event.InputEvent;
44 import java.io.File;
45 import util.AccessibilityTools;
46 import util.DesktopTools;
47 import util.DrawTools;
48 import util.SOfficeFactory;
49 import org.openoffice.test.OfficeFileUrl;
50
51 // ---------- junit imports -----------------
52 import org.junit.After;
53 import org.junit.AfterClass;
54 import org.junit.Before;
55 import org.junit.BeforeClass;
56 import org.junit.Test;
57 import org.openoffice.test.OfficeConnection;
58 import static org.junit.Assert.*;
59
60
61 /**
62 *
63 */
64 public class CheckContextMenuInterceptor
65 {
66
67 XMultiServiceFactory xMSF = null;
68 XFrame xFrame = null;
69 Point point = null;
70 XWindow xWindow = null;
71 com.sun.star.lang.XComponent xDrawDoc;
72
73 @Before
74 public void before()
75 {
76 xMSF = getMSF();
77 }
78
79 @After
80 public void after() throws Exception
81 {
82 System.out.println("release the popup menu");
83 try
84 {
85 Robot rob = new Robot();
86 int x = point.X;
87 int y = point.Y;
88 rob.mouseMove(x, y);
89 rob.mousePress(InputEvent.BUTTON1_MASK);
90 rob.mouseRelease(InputEvent.BUTTON1_MASK);
91 }
92 catch (java.awt.AWTException e)
93 {
94 System.out.println("couldn't press mouse button");
95 }
96
97 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xFrame);
98
99 xClose.close(true);
100
101 xFrame = null;
102 }
103
104 @Test
105 public void checkContextMenuInterceptor() throws Exception
106 {
107 System.out.println(" **** Context Menu Interceptor *** ");
108
109 // initialize the test document
110 xDrawDoc = DrawTools.createDrawDoc(xMSF);
111
112 SOfficeFactory SOF = SOfficeFactory.getFactory(xMSF);
113 XShape oShape = SOF.createShape(xDrawDoc, 5000, 5000, 1500, 1000, "GraphicObject");
114 DrawTools.getShapes(DrawTools.getDrawPage(xDrawDoc, 0)).add(oShape);
115
116 com.sun.star.frame.XModel xModel =
117 UnoRuntime.queryInterface(com.sun.star.frame.XModel.class, xDrawDoc);
118
119 // get the frame for later usage
120 xFrame = xModel.getCurrentController().getFrame();
121
122 // ensure that the document content is optimal visible
123 DesktopTools.zoomToEntirePage(xMSF, xDrawDoc);
124
125 XBitmap xBitmap = null;
126
127 // adding graphic as ObjRelation for GraphicObjectShape
128 XPropertySet oShapeProps = UnoRuntime.queryInterface(XPropertySet.class, oShape);
129 System.out.println("Inserting a shape into the document");
130
131 try
132 {
133 String sFile = OfficeFileUrl.getAbsolute(new File("space-metal.jpg"));
134 oShapeProps.setPropertyValue("GraphicURL", sFile);
135 Object oProp = oShapeProps.getPropertyValue("GraphicObjectFillBitmap");
136 xBitmap = (XBitmap) AnyConverter.toObject(new Type(XBitmap.class), oProp);
137 }
138 catch (com.sun.star.lang.WrappedTargetException e)
139 {
140 }
141 catch (com.sun.star.lang.IllegalArgumentException e)
142 {
143 }
144 catch (com.sun.star.beans.PropertyVetoException e)
145 {
146 }
147 catch (com.sun.star.beans.UnknownPropertyException e)
148 {
149 }
150
151 // reuse the frame
152 com.sun.star.frame.XController xController = xFrame.getController();
153 XContextMenuInterception xContextMenuInterception = null;
154 XContextMenuInterceptor xContextMenuInterceptor = null;
155
156 if (xController != null)
157 {
158 System.out.println("Creating context menu interceptor");
159
160 // add our context menu interceptor
161 xContextMenuInterception =
162 UnoRuntime.queryInterface(XContextMenuInterception.class, xController);
163
164 if (xContextMenuInterception != null)
165 {
166 ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor(xBitmap);
167 xContextMenuInterceptor =
168 UnoRuntime.queryInterface(XContextMenuInterceptor.class, aContextMenuInterceptor);
169
170 System.out.println("Register context menu interceptor");
171 xContextMenuInterception.registerContextMenuInterceptor(xContextMenuInterceptor);
172 }
173 }
174
175 openContextMenu(UnoRuntime.queryInterface(XModel.class, xDrawDoc));
176
177 checkHelpEntry();
178
179 // remove our context menu interceptor
180 if (xContextMenuInterception != null
181 && xContextMenuInterceptor != null)
182 {
183 System.out.println("Release context menu interceptor");
184 xContextMenuInterception.releaseContextMenuInterceptor(
185 xContextMenuInterceptor);
186 }
187 }
188
189 private void checkHelpEntry() throws Exception
190 {
191 XInterface toolkit = null;
192
193 System.out.println("get accessibility...");
194 try
195 {
196 toolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
197 }
198 catch (com.sun.star.uno.Exception e)
199 {
200 System.out.println("could not get Toolkit " + e.toString());
201 }
202 XExtendedToolkit tk = UnoRuntime.queryInterface(XExtendedToolkit.class, toolkit);
203
204 XAccessible xRoot = null;
205
206 try
207 {
208 xWindow = UnoRuntime.queryInterface(XWindow.class, tk.getTopWindow(0));
209
210 xRoot = AccessibilityTools.getAccessibleObject(xWindow);
211 }
212 catch (com.sun.star.lang.IndexOutOfBoundsException e)
213 {
214 System.out.println("Couldn't get Window");
215 }
216
217 XAccessibleContext oPopMenu = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.POPUP_MENU);
218
219 System.out.println("ImplementationName: " + util.utils.getImplName(oPopMenu));
220
221 XAccessible xHelp = null;
222 System.out.println("Try to get first entry of context menu...");
223 xHelp = oPopMenu.getAccessibleChild(0);
224 if (xHelp == null)
225 {
226 fail("first entry of context menu is NULL");
227 }
228
229 XAccessibleContext xHelpCont = xHelp.getAccessibleContext();
230 if (xHelpCont == null)
231 {
232 fail("No able to retrieve accessible context from first entry of context menu");
233 }
234
235 String aAccessibleName = xHelpCont.getAccessibleName();
236 if (!aAccessibleName.equals("Help"))
237 {
238 System.out.println("Accessible name found = " + aAccessibleName);
239 fail("First entry of context menu is not from context menu interceptor");
240 }
241
242 System.out.println("try to get first children of Help context...");
243 xHelpCont.getAccessibleChild(0);
244 }
245
246 private void openContextMenu(XModel aModel)
247 {
248
249 System.out.println("try to open context menu...");
250
251 xWindow = AccessibilityTools.getCurrentWindow(aModel);
252
253 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
254
255 XInterface oObj = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
256
257 XAccessibleComponent window = UnoRuntime.queryInterface(XAccessibleComponent.class, oObj);
258
259 point = window.getLocationOnScreen();
260 Rectangle rect = window.getBounds();
261
262 System.out.println("click mouse button...");
263 try
264 {
265 Robot rob = new Robot();
266 int x = point.X + (rect.Width / 2);
267 int y = point.Y + (rect.Height / 2);
268 rob.mouseMove(x, y);
269 System.out.println("Press Button");
270 rob.mousePress(InputEvent.BUTTON3_MASK);
271 System.out.println("Release Button");
272 rob.mouseRelease(InputEvent.BUTTON3_MASK);
273 System.out.println("done");
274 }
275 catch (java.awt.AWTException e)
276 {
277 System.out.println("couldn't press mouse button");
278 }
279
280 util.utils.waitForEventIdle(getMSF());
281 }
282
283 private XMultiServiceFactory getMSF()
284 {
285 return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
286 }
287
288 // setup and close connections
289 @BeforeClass
290 public static void setUpConnection() throws Exception
291 {
292 System.out.println("setUpConnection()");
293 connection.setUp();
294 }
295
296 @AfterClass
297 public static void tearDownConnection()
298 throws InterruptedException, com.sun.star.uno.Exception
299 {
300 System.out.println("tearDownConnection()");
301 connection.tearDown();
302 }
303 private static final OfficeConnection connection = new OfficeConnection();
304 }