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 .
22 import com
.sun
.star
.accessibility
.AccessibleRole
;
23 import com
.sun
.star
.accessibility
.XAccessible
;
24 import com
.sun
.star
.accessibility
.XAccessibleComponent
;
25 import com
.sun
.star
.accessibility
.XAccessibleContext
;
26 import com
.sun
.star
.awt
.Point
;
27 import com
.sun
.star
.awt
.Rectangle
;
28 import com
.sun
.star
.awt
.XExtendedToolkit
;
29 import com
.sun
.star
.awt
.XWindow
;
30 import com
.sun
.star
.frame
.XModel
;
31 import com
.sun
.star
.lang
.IndexOutOfBoundsException
;
32 import com
.sun
.star
.lang
.XMultiServiceFactory
;
33 import com
.sun
.star
.ui
.XContextMenuInterception
;
34 import com
.sun
.star
.ui
.XContextMenuInterceptor
;
35 import com
.sun
.star
.uno
.UnoRuntime
;
36 import com
.sun
.star
.uno
.XInterface
;
37 import helper
.ContextMenuInterceptor
;
38 import java
.awt
.Robot
;
39 import java
.awt
.event
.InputEvent
;
40 import lib
.MultiMethodTest
;
42 import lib
.StatusException
;
43 import util
.AccessibilityTools
;
44 import util
.DesktopTools
;
47 public class _XContextMenuInterception
extends MultiMethodTest
{
49 private XModel docModel
= null;
50 private XContextMenuInterceptor xCI
= null;
51 public XContextMenuInterception oObj
= null;
52 private XWindow xWindow
= null;
53 private XMultiServiceFactory xMSF
= null;
54 private Point point
= null;
57 public void before() {
58 docModel
= UnoRuntime
.queryInterface(
59 XModel
.class,tEnv
.getObjRelation("FirstModel"));
61 xCI
= UnoRuntime
.queryInterface(
62 XContextMenuInterceptor
.class, new ContextMenuInterceptor());
64 xMSF
= tParam
.getMSF();
66 //ensure that the first model is focused
68 log
.println("ensure that the first model is focused");
69 DesktopTools
.bringWindowToFront(docModel
);
77 oObj
.releaseContextMenuInterceptor(xCI
);
81 public void _registerContextMenuInterceptor() {
82 oObj
.registerContextMenuInterceptor(xCI
);
83 openContextMenu(docModel
);
84 boolean res
= checkHelpEntry();
86 tRes
.tested("registerContextMenuInterceptor()",res
);
89 public void _releaseContextMenuInterceptor() {
90 requiredMethod("registerContextMenuInterceptor()");
91 oObj
.releaseContextMenuInterceptor(xCI
);
92 openContextMenu(docModel
);
93 boolean res
= checkHelpEntry();
95 tRes
.tested("releaseContextMenuInterceptor()",!res
);
98 private boolean checkHelpEntry(){
99 XInterface toolkit
= null;
102 log
.println("get accesibility...");
104 toolkit
= (XInterface
) xMSF
.createInstance("com.sun.star.awt.Toolkit");
105 } catch (com
.sun
.star
.uno
.Exception e
){
106 log
.println("could not get Toolkit " + e
.toString());
108 XExtendedToolkit tk
= UnoRuntime
.queryInterface(
109 XExtendedToolkit
.class, toolkit
);
111 XAccessible xRoot
= null;
114 xWindow
= UnoRuntime
.queryInterface(XWindow
.class,
117 xRoot
= AccessibilityTools
.getAccessibleObject(xWindow
);
118 AccessibilityTools
.printAccessibleTree(log
, xRoot
, tParam
.getBool(util
.PropertyName
.DEBUG_IS_ACTIVE
));
119 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
120 log
.println("Couldn't get Window");
123 XAccessibleContext oPopMenu
= AccessibilityTools
.getAccessibleObjectForRole(xRoot
, AccessibleRole
.POPUP_MENU
,true);
125 log
.println("ImplementationName: " + util
.utils
.getImplName(oPopMenu
));
127 XAccessible xHelp
= null;
129 log
.println("Try to get second entry of context menu...");
130 xHelp
= oPopMenu
.getAccessibleChild(1);
132 } catch (IndexOutOfBoundsException e
){
133 throw new StatusException("Not possible to get second entry of context menu",e
);
136 if (xHelp
== null) throw new StatusException(new Status("second entry of context menu is NULL", false));
138 XAccessibleContext xHelpCont
= xHelp
.getAccessibleContext();
140 if ( xHelpCont
== null )
141 throw new StatusException(new Status("No able to retrieve accessible context from first entry of context menu",false));
143 String aAccessibleName
= xHelpCont
.getAccessibleName();
144 if ( !aAccessibleName
.equals( "Help" )) {
145 log
.println("Accessible name found = "+aAccessibleName
);
146 log
.println("Second entry of context menu is not from context menu interceptor");
154 private void openContextMenu(XModel xModel
){
156 log
.println("try to open contex menu...");
158 xWindow
= AccessibilityTools
.getCurrentWindow(xModel
);
160 XAccessible xRoot
= AccessibilityTools
.getAccessibleObject(xWindow
);
162 XInterface oObj
= AccessibilityTools
.getAccessibleObjectForRole(xRoot
, AccessibleRole
.PANEL
);
164 XAccessibleComponent window
= UnoRuntime
.queryInterface(
165 XAccessibleComponent
.class, oObj
);
167 point
= window
.getLocationOnScreen();
168 Rectangle rect
= window
.getBounds();
170 log
.println("click mouse button...");
172 Robot rob
= new Robot();
173 int x
= point
.X
+ (rect
.Width
/ 2);
174 int y
= point
.Y
+ (rect
.Height
/ 2);
176 System
.out
.println("Press Button");
177 rob
.mousePress(InputEvent
.BUTTON3_MASK
);
178 System
.out
.println("Release Button");
179 rob
.mouseRelease(InputEvent
.BUTTON3_MASK
);
180 System
.out
.println("done");
181 } catch (java
.awt
.AWTException e
) {
182 log
.println("couldn't press mouse button");
189 private void releasePopUp() {
190 log
.println("release the popup menu");
192 Robot rob
= new Robot();
196 rob
.mousePress(InputEvent
.BUTTON1_MASK
);
197 rob
.mouseRelease(InputEvent
.BUTTON1_MASK
);
198 } catch (java
.awt
.AWTException e
) {
199 log
.println("couldn't press mouse button");