bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / ui / _XContextMenuInterception.java
blobc065f059a583357773d724b28f84da072ab29eed
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 .
20 package ifc.ui;
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;
41 import lib.Status;
42 import lib.StatusException;
43 import util.AccessibilityTools;
44 import util.DesktopTools;
45 import util.utils;
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;
56 public void before() {
57 docModel = UnoRuntime.queryInterface(
58 XModel.class,tEnv.getObjRelation("FirstModel"));
60 xCI = UnoRuntime.queryInterface(
61 XContextMenuInterceptor.class, new ContextMenuInterceptor());
63 xMSF = (XMultiServiceFactory)tParam.getMSF();
65 //ensure that the first model is focused
67 log.println("ensure that the first model is focused");
68 DesktopTools.bringWindowToFront(docModel);
70 utils.shortWait(3000);
73 public void after() {
74 if (xCI != null) {
75 oObj.releaseContextMenuInterceptor(xCI);
79 public void _registerContextMenuInterceptor() {
80 oObj.registerContextMenuInterceptor(xCI);
81 openContextMenu(docModel);
82 boolean res = checkHelpEntry();
83 releasePopUp();
84 tRes.tested("registerContextMenuInterceptor()",res);
87 public void _releaseContextMenuInterceptor() {
88 requiredMethod("registerContextMenuInterceptor()");
89 oObj.releaseContextMenuInterceptor(xCI);
90 openContextMenu(docModel);
91 boolean res = checkHelpEntry();
92 releasePopUp();
93 tRes.tested("releaseContextMenuInterceptor()",!res);
96 private boolean checkHelpEntry(){
97 XInterface toolkit = null;
98 boolean res = true;
100 log.println("get accesibility...");
101 try{
102 toolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
103 } catch (com.sun.star.uno.Exception e){
104 log.println("could not get Toolkit " + e.toString());
106 XExtendedToolkit tk = UnoRuntime.queryInterface(
107 XExtendedToolkit.class, toolkit);
109 XAccessible xRoot = null;
111 AccessibilityTools at = new AccessibilityTools();
113 try {
114 xWindow = UnoRuntime.queryInterface(XWindow.class,
115 tk.getTopWindow(0));
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;
128 try{
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");
147 res=false;
150 return res;
154 private void openContextMenu(XModel xModel){
156 log.println("try to open contex menu...");
157 AccessibilityTools at = new AccessibilityTools();
159 xWindow = AccessibilityTools.getCurrentWindow(xMSF, xModel);
161 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
163 XInterface oObj = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
165 XAccessibleComponent window = UnoRuntime.queryInterface(
166 XAccessibleComponent.class, oObj);
168 point = window.getLocationOnScreen();
169 Rectangle rect = window.getBounds();
171 log.println("klick mouse button...");
172 try {
173 Robot rob = new Robot();
174 int x = point.X + (rect.Width / 2);
175 int y = point.Y + (rect.Height / 2);
176 rob.mouseMove(x, y);
177 System.out.println("Press Button");
178 rob.mousePress(InputEvent.BUTTON3_MASK);
179 System.out.println("Release Button");
180 rob.mouseRelease(InputEvent.BUTTON3_MASK);
181 System.out.println("done");
182 } catch (java.awt.AWTException e) {
183 log.println("couldn't press mouse button");
186 utils.shortWait(1000);
190 private void releasePopUp() {
191 log.println("release the popup menu");
192 try {
193 Robot rob = new Robot();
194 int x = point.X;
195 int y = point.Y;
196 rob.mouseMove(x, y);
197 rob.mousePress(InputEvent.BUTTON1_MASK);
198 rob.mouseRelease(InputEvent.BUTTON1_MASK);
199 } catch (java.awt.AWTException e) {
200 log.println("couldn't press mouse button");