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 package complex.contextMenuInterceptor; 23 24 import com.sun.star.accessibility.AccessibleRole; 25 import com.sun.star.accessibility.XAccessible; 26 import com.sun.star.accessibility.XAccessibleComponent; 27 import com.sun.star.accessibility.XAccessibleContext; 28 import com.sun.star.awt.Point; 29 import com.sun.star.awt.Rectangle; 30 import com.sun.star.awt.XBitmap; 31 import com.sun.star.awt.XExtendedToolkit; 32 import com.sun.star.awt.XWindow; 33 import com.sun.star.beans.XPropertySet; 34 import com.sun.star.drawing.XShape; 35 import com.sun.star.frame.XFrame; 36 import com.sun.star.frame.XModel; 37 import com.sun.star.lang.IndexOutOfBoundsException; 38 import com.sun.star.lang.XMultiServiceFactory; 39 import com.sun.star.ui.XContextMenuInterception; 40 import com.sun.star.ui.XContextMenuInterceptor; 41 import com.sun.star.uno.AnyConverter; 42 import com.sun.star.uno.Type; 43 import com.sun.star.uno.UnoRuntime; 44 import com.sun.star.uno.XInterface; 45 import com.sun.star.util.XCloseable; 46 import java.awt.Robot; 47 import java.awt.event.InputEvent; 48 import java.io.File; 49 import util.AccessibilityTools; 50 import util.DesktopTools; 51 import util.DrawTools; 52 import util.SOfficeFactory; 53 import util.utils; 54 import org.openoffice.test.OfficeFileUrl; 55 56 // ---------- junit imports ----------------- 57 import org.junit.After; 58 import org.junit.AfterClass; 59 import org.junit.Before; 60 import org.junit.BeforeClass; 61 import org.junit.Test; 62 import org.openoffice.test.OfficeConnection; 63 import static org.junit.Assert.*; 64 // ------------------------------------------ 65 66 /** 67 * 68 */ 69 public class CheckContextMenuInterceptor 70 { 71 72 XMultiServiceFactory xMSF = null; 73 XFrame xFrame = null; 74 Point point = null; 75 XWindow xWindow = null; 76 com.sun.star.lang.XComponent xDrawDoc; 77 78 @Before 79 public void before() 80 { 81 xMSF = getMSF(); 82 } 83 84 @After 85 public void after() 86 { 87 System.out.println("release the popup menu"); 88 try 89 { 90 Robot rob = new Robot(); 91 int x = point.X; 92 int y = point.Y; 93 rob.mouseMove(x, y); 94 rob.mousePress(InputEvent.BUTTON1_MASK); 95 rob.mouseRelease(InputEvent.BUTTON1_MASK); 96 } 97 catch (java.awt.AWTException e) 98 { 99 System.out.println("couldn't press mouse button"); 100 } 101 102 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xFrame); 103 104 try 105 { 106 xClose.close(true); 107 } 108 catch (com.sun.star.util.CloseVetoException exVeto) 109 { 110 fail("Test frame couldn't be closed successfully."); 111 } 112 113 xFrame = null; 114 115 // xClose = UnoRuntime.queryInterface(XCloseable.class, xDrawDoc); 116 // try 117 // { 118 // xClose.close(true); 119 // } 120 // catch (com.sun.star.util.CloseVetoException exVeto) 121 // { 122 // fail("Test DrawDoc couldn't be closed successfully."); 123 // } 124 125 } 126 127 // public String[] getTestMethodNames() { 128 // return new String[]{"checkContextMenuInterceptor"}; 129 // } 130 @Test 131 public void checkContextMenuInterceptor() 132 { 133 System.out.println(" **** Context Menu Interceptor *** "); 134 135 try 136 { 137 // intialize the test document 138 xDrawDoc = DrawTools.createDrawDoc(xMSF); 139 140 SOfficeFactory SOF = SOfficeFactory.getFactory(xMSF); 141 XShape oShape = SOF.createShape(xDrawDoc, 5000, 5000, 1500, 1000, "GraphicObject"); 142 DrawTools.getShapes(DrawTools.getDrawPage(xDrawDoc, 0)).add(oShape); 143 144 com.sun.star.frame.XModel xModel = 145 UnoRuntime.queryInterface(com.sun.star.frame.XModel.class, xDrawDoc); 146 147 // get the frame for later usage 148 xFrame = xModel.getCurrentController().getFrame(); 149 150 // ensure that the document content is optimal visible 151 DesktopTools.zoomToEntirePage(xDrawDoc); 152 153 XBitmap xBitmap = null; 154 155 // adding graphic as ObjRelation for GraphicObjectShape 156 XPropertySet oShapeProps = UnoRuntime.queryInterface(XPropertySet.class, oShape); 157 System.out.println("Inserting a shape into the document"); 158 159 try 160 { 161 String sFile = OfficeFileUrl.getAbsolute(new File("space-metal.jpg")); 162 // String sFile = util.utils.getFullTestURL("space-metal.jpg"); 163 oShapeProps.setPropertyValue("GraphicURL", sFile); 164 Object oProp = oShapeProps.getPropertyValue("GraphicObjectFillBitmap"); 165 xBitmap = (XBitmap) AnyConverter.toObject(new Type(XBitmap.class), oProp); 166 } 167 catch (com.sun.star.lang.WrappedTargetException e) 168 { 169 } 170 catch (com.sun.star.lang.IllegalArgumentException e) 171 { 172 } 173 catch (com.sun.star.beans.PropertyVetoException e) 174 { 175 } 176 catch (com.sun.star.beans.UnknownPropertyException e) 177 { 178 } 179 180 // reuse the frame 181 com.sun.star.frame.XController xController = xFrame.getController(); 182 XContextMenuInterception xContextMenuInterception = null; 183 XContextMenuInterceptor xContextMenuInterceptor = null; 184 185 if (xController != null) 186 { 187 System.out.println("Creating context menu interceptor"); 188 189 // add our context menu interceptor 190 xContextMenuInterception = 191 UnoRuntime.queryInterface(XContextMenuInterception.class, xController); 192 193 if (xContextMenuInterception != null) 194 { 195 ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor(xBitmap); 196 xContextMenuInterceptor = 197 UnoRuntime.queryInterface(XContextMenuInterceptor.class, aContextMenuInterceptor); 198 199 System.out.println("Register context menu interceptor"); 200 xContextMenuInterception.registerContextMenuInterceptor(xContextMenuInterceptor); 201 } 202 } 203 204 // utils.shortWait(10000); 205 206 openContextMenu(UnoRuntime.queryInterface(XModel.class, xDrawDoc)); 207 208 checkHelpEntry(); 209 210 // remove our context menu interceptor 211 if (xContextMenuInterception != null 212 && xContextMenuInterceptor != null) 213 { 214 System.out.println("Release context menu interceptor"); 215 xContextMenuInterception.releaseContextMenuInterceptor( 216 xContextMenuInterceptor); 217 } 218 } 219 catch (com.sun.star.uno.RuntimeException ex) 220 { 221 // ex.printStackTrace(); 222 fail("Runtime exception caught!" + ex.getMessage()); 223 } 224 catch (java.lang.Exception ex) 225 { 226 // ex.printStackTrace(); 227 fail("Java lang exception caught!" + ex.getMessage()); 228 } 229 } 230 231 private void checkHelpEntry() 232 { 233 XInterface toolkit = null; 234 235 System.out.println("get accesibility..."); 236 try 237 { 238 toolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit"); 239 } 240 catch (com.sun.star.uno.Exception e) 241 { 242 System.out.println("could not get Toolkit " + e.toString()); 243 } 244 XExtendedToolkit tk = UnoRuntime.queryInterface(XExtendedToolkit.class, toolkit); 245 246 XAccessible xRoot = null; 247 248 AccessibilityTools at = new AccessibilityTools(); 249 250 try 251 { 252 xWindow = UnoRuntime.queryInterface(XWindow.class, tk.getTopWindow(0)); 253 254 xRoot = at.getAccessibleObject(xWindow); 255 256 // at.printAccessibleTree((PrintWriter)log, xRoot, param.getBool(util.PropertyName.DEBUG_IS_ACTIVE)); 257 // at.printAccessibleTree(System.out, xRoot, true); 258 } 259 catch (com.sun.star.lang.IndexOutOfBoundsException e) 260 { 261 System.out.println("Couldn't get Window"); 262 } 263 264 XAccessibleContext oPopMenu = at.getAccessibleObjectForRole(xRoot, AccessibleRole.POPUP_MENU); 265 266 System.out.println("ImplementationName: " + util.utils.getImplName(oPopMenu)); 267 268 XAccessible xHelp = null; 269 try 270 { 271 System.out.println("Try to get first entry of context menu..."); 272 xHelp = oPopMenu.getAccessibleChild(0); 273 274 } 275 catch (IndexOutOfBoundsException e) 276 { 277 fail("Not possible to get first entry of context menu"); 278 } 279 280 if (xHelp == null) 281 { 282 fail("first entry of context menu is NULL"); 283 } 284 285 XAccessibleContext xHelpCont = xHelp.getAccessibleContext(); 286 287 if (xHelpCont == null) 288 { 289 fail("No able to retrieve accessible context from first entry of context menu"); 290 } 291 292 String aAccessibleName = xHelpCont.getAccessibleName(); 293 if (!aAccessibleName.equals("Help")) 294 { 295 System.out.println("Accessible name found = " + aAccessibleName); 296 fail("First entry of context menu is not from context menu interceptor"); 297 } 298 299 try 300 { 301 System.out.println("try to get first children of Help context..."); 302 XAccessible xHelpChild = xHelpCont.getAccessibleChild(0); 303 304 } 305 catch (IndexOutOfBoundsException e) 306 { 307 fail("not possible to get first children of Help context"); 308 } 309 310 } 311 312 private void openContextMenu(XModel aModel) 313 { 314 315 System.out.println("try to open contex menu..."); 316 AccessibilityTools at = new AccessibilityTools(); 317 318 xWindow = at.getCurrentWindow(xMSF, aModel); 319 320 XAccessible xRoot = at.getAccessibleObject(xWindow); 321 322 XInterface oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL); 323 324 XAccessibleComponent window = UnoRuntime.queryInterface(XAccessibleComponent.class, oObj); 325 326 point = window.getLocationOnScreen(); 327 Rectangle rect = window.getBounds(); 328 329 System.out.println("klick mouse button..."); 330 try 331 { 332 Robot rob = new Robot(); 333 int x = point.X + (rect.Width / 2); 334 int y = point.Y + (rect.Height / 2); 335 rob.mouseMove(x, y); 336 System.out.println("Press Button"); 337 rob.mousePress(InputEvent.BUTTON3_MASK); 338 System.out.println("Release Button"); 339 rob.mouseRelease(InputEvent.BUTTON3_MASK); 340 System.out.println("done"); 341 } 342 catch (java.awt.AWTException e) 343 { 344 System.out.println("couldn't press mouse button"); 345 } 346 347 utils.shortWait(3000); 348 349 } 350 351 private XMultiServiceFactory getMSF() 352 { 353 final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 354 return xMSF1; 355 } 356 357 // setup and close connections 358 @BeforeClass 359 public static void setUpConnection() throws Exception 360 { 361 System.out.println("setUpConnection()"); 362 connection.setUp(); 363 } 364 365 @AfterClass 366 public static void tearDownConnection() 367 throws InterruptedException, com.sun.star.uno.Exception 368 { 369 System.out.println("tearDownConnection()"); 370 connection.tearDown(); 371 } 372 private static final OfficeConnection connection = new OfficeConnection(); 373 } 374