Update git submodules
[LibreOffice.git] / framework / qa / complex / contextMenuInterceptor / CheckContextMenuInterceptor.java
blobe1f59fdf7faaba26d5bbfd12e9d892ee240fad23
1 /*
2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 package complex.contextMenuInterceptor;
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;
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.*;
61 /**
64 public class CheckContextMenuInterceptor
67 XMultiServiceFactory xMSF = null;
68 XFrame xFrame = null;
69 Point point = null;
70 XWindow xWindow = null;
71 com.sun.star.lang.XComponent xDrawDoc;
73 @Before
74 public void before()
76 xMSF = getMSF();
79 @After
80 public void after() throws Exception
82 System.out.println("release the popup menu");
83 try
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);
92 catch (java.awt.AWTException e)
94 System.out.println("couldn't press mouse button");
97 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xFrame);
99 xClose.close(true);
101 xFrame = null;
104 @Test
105 public void checkContextMenuInterceptor() throws Exception
107 System.out.println(" **** Context Menu Interceptor *** ");
109 // initialize the test document
110 xDrawDoc = DrawTools.createDrawDoc(xMSF);
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);
116 com.sun.star.frame.XModel xModel =
117 UnoRuntime.queryInterface(com.sun.star.frame.XModel.class, xDrawDoc);
119 // get the frame for later usage
120 xFrame = xModel.getCurrentController().getFrame();
122 // ensure that the document content is optimal visible
123 DesktopTools.zoomToEntirePage(xMSF, xDrawDoc);
125 XBitmap xBitmap = null;
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");
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);
138 catch (com.sun.star.lang.WrappedTargetException e)
141 catch (com.sun.star.lang.IllegalArgumentException e)
144 catch (com.sun.star.beans.PropertyVetoException e)
147 catch (com.sun.star.beans.UnknownPropertyException e)
151 // reuse the frame
152 com.sun.star.frame.XController xController = xFrame.getController();
153 XContextMenuInterception xContextMenuInterception = null;
154 XContextMenuInterceptor xContextMenuInterceptor = null;
156 if (xController != null)
158 System.out.println("Creating context menu interceptor");
160 // add our context menu interceptor
161 xContextMenuInterception =
162 UnoRuntime.queryInterface(XContextMenuInterception.class, xController);
164 if (xContextMenuInterception != null)
166 ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor(xBitmap);
167 xContextMenuInterceptor =
168 UnoRuntime.queryInterface(XContextMenuInterceptor.class, aContextMenuInterceptor);
170 System.out.println("Register context menu interceptor");
171 xContextMenuInterception.registerContextMenuInterceptor(xContextMenuInterceptor);
175 openContextMenu(UnoRuntime.queryInterface(XModel.class, xDrawDoc));
177 checkHelpEntry();
179 // remove our context menu interceptor
180 if (xContextMenuInterception != null
181 && xContextMenuInterceptor != null)
183 System.out.println("Release context menu interceptor");
184 xContextMenuInterception.releaseContextMenuInterceptor(
185 xContextMenuInterceptor);
189 private void checkHelpEntry() throws Exception
191 XInterface toolkit = null;
193 System.out.println("get accessibility...");
196 toolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
198 catch (com.sun.star.uno.Exception e)
200 System.out.println("could not get Toolkit " + e.toString());
202 XExtendedToolkit tk = UnoRuntime.queryInterface(XExtendedToolkit.class, toolkit);
204 XAccessible xRoot = null;
208 xWindow = UnoRuntime.queryInterface(XWindow.class, tk.getTopWindow(0));
210 xRoot = AccessibilityTools.getAccessibleObject(xWindow);
212 catch (com.sun.star.lang.IndexOutOfBoundsException e)
214 System.out.println("Couldn't get Window");
217 XAccessibleContext oPopMenu = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.POPUP_MENU);
219 System.out.println("ImplementationName: " + util.utils.getImplName(oPopMenu));
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)
226 fail("first entry of context menu is NULL");
229 XAccessibleContext xHelpCont = xHelp.getAccessibleContext();
230 if (xHelpCont == null)
232 fail("No able to retrieve accessible context from first entry of context menu");
235 String aAccessibleName = xHelpCont.getAccessibleName();
236 if (!aAccessibleName.equals("Help"))
238 System.out.println("Accessible name found = " + aAccessibleName);
239 fail("First entry of context menu is not from context menu interceptor");
242 System.out.println("try to get first children of Help context...");
243 xHelpCont.getAccessibleChild(0);
246 private void openContextMenu(XModel aModel)
249 System.out.println("try to open context menu...");
251 xWindow = AccessibilityTools.getCurrentWindow(aModel);
253 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
255 XInterface oObj = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
257 XAccessibleComponent window = UnoRuntime.queryInterface(XAccessibleComponent.class, oObj);
259 point = window.getLocationOnScreen();
260 Rectangle rect = window.getBounds();
262 System.out.println("click mouse button...");
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");
275 catch (java.awt.AWTException e)
277 System.out.println("couldn't press mouse button");
280 util.utils.waitForEventIdle(getMSF());
283 private XMultiServiceFactory getMSF()
285 return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
288 // setup and close connections
289 @BeforeClass
290 public static void setUpConnection() throws Exception
292 System.out.println("setUpConnection()");
293 connection.setUp();
296 @AfterClass
297 public static void tearDownConnection()
298 throws InterruptedException, com.sun.star.uno.Exception
300 System.out.println("tearDownConnection()");
301 connection.tearDown();
303 private static final OfficeConnection connection = new OfficeConnection();