Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / ui / _XContextMenuInterception.java
blob0628107305a2eed7a0c10a3d2def9c3dbbfbb5eb
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;
46 public class _XContextMenuInterception extends MultiMethodTest {
48 private XModel docModel = null;
49 private XContextMenuInterceptor xCI = null;
50 public XContextMenuInterception oObj = null;
51 private XWindow xWindow = null;
52 private XMultiServiceFactory xMSF = null;
53 private Point point = null;
55 @Override
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 = 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 waitForEventIdle();
73 @Override
74 public void after() {
75 if (xCI != null) {
76 oObj.releaseContextMenuInterceptor(xCI);
80 public void _registerContextMenuInterceptor() {
81 oObj.registerContextMenuInterceptor(xCI);
82 openContextMenu(docModel);
83 boolean res = checkHelpEntry();
84 releasePopUp();
85 tRes.tested("registerContextMenuInterceptor()",res);
88 public void _releaseContextMenuInterceptor() {
89 requiredMethod("registerContextMenuInterceptor()");
90 oObj.releaseContextMenuInterceptor(xCI);
91 openContextMenu(docModel);
92 boolean res = checkHelpEntry();
93 releasePopUp();
94 tRes.tested("releaseContextMenuInterceptor()",!res);
97 private boolean checkHelpEntry(){
98 XInterface toolkit = null;
99 boolean res = true;
101 log.println("get accessibility...");
102 try{
103 toolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
104 } catch (com.sun.star.uno.Exception e){
105 log.println("could not get Toolkit " + e.toString());
107 XExtendedToolkit tk = UnoRuntime.queryInterface(
108 XExtendedToolkit.class, toolkit);
110 XAccessible xRoot = null;
112 try {
113 xWindow = UnoRuntime.queryInterface(XWindow.class,
114 tk.getTopWindow(0));
116 xRoot = AccessibilityTools.getAccessibleObject(xWindow);
117 AccessibilityTools.printAccessibleTree(log, xRoot, tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
118 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
119 log.println("Couldn't get Window");
122 XAccessibleContext oPopMenu = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.POPUP_MENU,true);
124 log.println("ImplementationName: " + util.utils.getImplName(oPopMenu));
126 XAccessible xHelp = null;
127 try{
128 log.println("Try to get second entry of context menu...");
129 xHelp = oPopMenu.getAccessibleChild(1);
131 } catch (IndexOutOfBoundsException e){
132 throw new StatusException("Not possible to get second entry of context menu",e);
135 if (xHelp == null) throw new StatusException(new Status("second entry of context menu is NULL", false));
137 XAccessibleContext xHelpCont = xHelp.getAccessibleContext();
139 if ( xHelpCont == null )
140 throw new StatusException(new Status("No able to retrieve accessible context from first entry of context menu",false));
142 String aAccessibleName = xHelpCont.getAccessibleName();
143 if ( !aAccessibleName.equals( "Help" )) {
144 log.println("Accessible name found = "+aAccessibleName );
145 log.println("Second entry of context menu is not from context menu interceptor");
146 res=false;
149 return res;
153 private void openContextMenu(XModel xModel){
155 log.println("try to open context menu...");
157 xWindow = AccessibilityTools.getCurrentWindow(xModel);
159 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
161 XInterface oObj = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
163 XAccessibleComponent window = UnoRuntime.queryInterface(
164 XAccessibleComponent.class, oObj);
166 point = window.getLocationOnScreen();
167 Rectangle rect = window.getBounds();
169 log.println("click mouse button...");
170 try {
171 Robot rob = new Robot();
172 int x = point.X + (rect.Width / 2);
173 int y = point.Y + (rect.Height / 2);
174 rob.mouseMove(x, y);
175 System.out.println("Press Button");
176 rob.mousePress(InputEvent.BUTTON3_MASK);
177 System.out.println("Release Button");
178 rob.mouseRelease(InputEvent.BUTTON3_MASK);
179 System.out.println("done");
180 } catch (java.awt.AWTException e) {
181 log.println("couldn't press mouse button");
184 waitForEventIdle();
188 private void releasePopUp() {
189 log.println("release the popup menu");
190 try {
191 Robot rob = new Robot();
192 int x = point.X;
193 int y = point.Y;
194 rob.mouseMove(x, y);
195 rob.mousePress(InputEvent.BUTTON1_MASK);
196 rob.mouseRelease(InputEvent.BUTTON1_MASK);
197 } catch (java.awt.AWTException e) {
198 log.println("couldn't press mouse button");