merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / ui / _XContextMenuInterception.java
blob696e3a45aaa9fac5c0aa7d653ffbc96bfaf8cdd6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XContextMenuInterception.java,v $
10 * $Revision: 1.3.8.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 package ifc.ui;
34 import com.sun.star.accessibility.AccessibleRole;
35 import com.sun.star.accessibility.XAccessible;
36 import com.sun.star.accessibility.XAccessibleComponent;
37 import com.sun.star.accessibility.XAccessibleContext;
38 import com.sun.star.awt.Point;
39 import com.sun.star.awt.Rectangle;
40 import com.sun.star.awt.XExtendedToolkit;
41 import com.sun.star.awt.XWindow;
42 import com.sun.star.frame.XModel;
43 import com.sun.star.lang.IndexOutOfBoundsException;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.ui.XContextMenuInterception;
46 import com.sun.star.ui.XContextMenuInterceptor;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 import helper.ContextMenuInterceptor;
50 import java.awt.Robot;
51 import java.awt.event.InputEvent;
52 import lib.MultiMethodTest;
53 import lib.Status;
54 import lib.StatusException;
55 import util.AccessibilityTools;
56 import util.DesktopTools;
57 import util.utils;
59 public class _XContextMenuInterception extends MultiMethodTest {
61 private XModel docModel = null;
62 private XContextMenuInterceptor xCI = null;
63 public XContextMenuInterception oObj = null;
64 private XWindow xWindow = null;
65 private XMultiServiceFactory xMSF = null;
66 private Point point = null;
68 public void before() {
69 docModel = (XModel) UnoRuntime.queryInterface(
70 XModel.class,tEnv.getObjRelation("FirstModel"));
72 xCI = (XContextMenuInterceptor) UnoRuntime.queryInterface(
73 XContextMenuInterceptor.class, new ContextMenuInterceptor());
75 xMSF = (XMultiServiceFactory)tParam.getMSF();
77 //ensure that the first model is focused
79 log.println("ensure that the first model is focused");
80 DesktopTools.bringWindowToFront(docModel);
82 utils.shortWait(3000);
85 public void after() {
86 if (xCI != null) {
87 oObj.releaseContextMenuInterceptor(xCI);
91 public void _registerContextMenuInterceptor() {
92 oObj.registerContextMenuInterceptor(xCI);
93 openContextMenu(docModel);
94 boolean res = checkHelpEntry();
95 releasePopUp();
96 tRes.tested("registerContextMenuInterceptor()",res);
99 public void _releaseContextMenuInterceptor() {
100 requiredMethod("registerContextMenuInterceptor()");
101 oObj.releaseContextMenuInterceptor(xCI);
102 openContextMenu(docModel);
103 boolean res = checkHelpEntry();
104 releasePopUp();
105 tRes.tested("releaseContextMenuInterceptor()",!res);
108 private boolean checkHelpEntry(){
109 XInterface toolkit = null;
110 boolean res = true;
112 log.println("get accesibility...");
113 try{
114 toolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
115 } catch (com.sun.star.uno.Exception e){
116 log.println("could not get Toolkit " + e.toString());
118 XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
119 XExtendedToolkit.class, toolkit);
121 XAccessible xRoot = null;
123 AccessibilityTools at = new AccessibilityTools();
125 try {
126 xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
127 tk.getTopWindow(0));
129 xRoot = at.getAccessibleObject(xWindow);
130 at.printAccessibleTree(log, xRoot, tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
131 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
132 log.println("Couldn't get Window");
135 XAccessibleContext oPopMenu = at.getAccessibleObjectForRole(xRoot, AccessibleRole.POPUP_MENU,true);
137 log.println("ImplementationName: " + util.utils.getImplName(oPopMenu));
139 XAccessible xHelp = null;
140 try{
141 log.println("Try to get second entry of context menu...");
142 xHelp = oPopMenu.getAccessibleChild(1);
144 } catch (IndexOutOfBoundsException e){
145 throw new StatusException("Not possible to get second entry of context menu",e);
148 if (xHelp == null) throw new StatusException(new Status("second entry of context menu is NULL", false));
150 XAccessibleContext xHelpCont = xHelp.getAccessibleContext();
152 if ( xHelpCont == null )
153 throw new StatusException(new Status("No able to retrieve accessible context from first entry of context menu",false));
155 String aAccessibleName = xHelpCont.getAccessibleName();
156 if ( !aAccessibleName.equals( "Help" )) {
157 log.println("Accessible name found = "+aAccessibleName );
158 log.println("Second entry of context menu is not from context menu interceptor");
159 res=false;
162 return res;
166 private void openContextMenu(XModel xModel){
168 log.println("try to open contex menu...");
169 AccessibilityTools at = new AccessibilityTools();
171 xWindow = at.getCurrentWindow(xMSF, xModel);
173 XAccessible xRoot = at.getAccessibleObject(xWindow);
175 XInterface oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
177 XAccessibleComponent window = (XAccessibleComponent) UnoRuntime.queryInterface(
178 XAccessibleComponent.class, oObj);
180 point = window.getLocationOnScreen();
181 Rectangle rect = window.getBounds();
183 log.println("klick mouse button...");
184 try {
185 Robot rob = new Robot();
186 int x = point.X + (rect.Width / 2);
187 int y = point.Y + (rect.Height / 2);
188 rob.mouseMove(x, y);
189 System.out.println("Press Button");
190 rob.mousePress(InputEvent.BUTTON3_MASK);
191 System.out.println("Release Button");
192 rob.mouseRelease(InputEvent.BUTTON3_MASK);
193 System.out.println("done");
194 } catch (java.awt.AWTException e) {
195 log.println("couldn't press mouse button");
198 utils.shortWait(1000);
202 private void releasePopUp() {
203 log.println("release the popup menu");
204 try {
205 Robot rob = new Robot();
206 int x = point.X;
207 int y = point.Y;
208 rob.mouseMove(x, y);
209 rob.mousePress(InputEvent.BUTTON1_MASK);
210 rob.mouseRelease(InputEvent.BUTTON1_MASK);
211 } catch (java.awt.AWTException e) {
212 log.println("couldn't press mouse button");