merge the formfield patch from ooo-build
[ooovba.git] / framework / qa / complex / contextMenuInterceptor / CheckContextMenuInterceptor.java
blob8cad076b2d24b81213f65b7651f59c7785fffcfd
1 package contextMenuInterceptor;
3 import com.sun.star.accessibility.AccessibleRole;
4 import com.sun.star.accessibility.XAccessible;
5 import com.sun.star.accessibility.XAccessibleComponent;
6 import com.sun.star.accessibility.XAccessibleContext;
7 import com.sun.star.awt.Point;
8 import com.sun.star.awt.Rectangle;
9 import com.sun.star.awt.XBitmap;
10 import com.sun.star.awt.XExtendedToolkit;
11 import com.sun.star.awt.XWindow;
12 import com.sun.star.beans.PropertyValue;
13 import com.sun.star.beans.XPropertySet;
14 import com.sun.star.beans.XPropertySetInfo;
15 import com.sun.star.container.XIndexAccess;
16 import com.sun.star.drawing.XShape;
17 import com.sun.star.frame.XComponentLoader;
18 import com.sun.star.frame.XController;
19 import com.sun.star.frame.XDesktop;
20 import com.sun.star.frame.XFrame;
21 import com.sun.star.frame.XModel;
22 import com.sun.star.lang.IndexOutOfBoundsException;
23 import com.sun.star.lang.XComponent;
24 import com.sun.star.lang.XInitialization;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.lang.XTypeProvider;
27 import com.sun.star.text.XTextDocument;
28 import com.sun.star.ui.XContextMenuInterceptor;
29 import com.sun.star.ui.XContextMenuInterception;
30 import com.sun.star.uno.AnyConverter;
31 import com.sun.star.uno.Type;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.uno.XInterface;
34 import com.sun.star.uno.Exception;
35 import com.sun.star.util.CloseVetoException;
36 import com.sun.star.util.XCloseable;
37 import com.sun.star.view.XViewSettingsSupplier;
38 import complexlib.ComplexTestCase;
39 import java.awt.Robot;
40 import java.awt.event.InputEvent;
41 import java.io.PrintWriter;
42 import share.LogWriter;
43 import util.AccessibilityTools;
44 import util.DesktopTools;
45 import util.DrawTools;
46 import util.SOfficeFactory;
47 import util.utils;
49 /**
52 public class CheckContextMenuInterceptor extends ComplexTestCase {
53 XMultiServiceFactory xMSF = null;
54 XFrame xFrame = null;
55 Point point = null;
56 XWindow xWindow = null;
58 public void before() {
59 xMSF = (XMultiServiceFactory)param.getMSF();
62 public void after() {
63 log.println("release the popup menu");
64 try {
65 Robot rob = new Robot();
66 int x = point.X;
67 int y = point.Y;
68 rob.mouseMove(x, y);
69 rob.mousePress(InputEvent.BUTTON1_MASK);
70 rob.mouseRelease(InputEvent.BUTTON1_MASK);
71 } catch (java.awt.AWTException e) {
72 log.println("couldn't press mouse button");
75 com.sun.star.util.XCloseable xClose = (com.sun.star.util.XCloseable)UnoRuntime.queryInterface(
76 com.sun.star.util.XCloseable.class, xFrame);
78 try
80 xClose.close(false);
82 catch(com.sun.star.util.CloseVetoException exVeto)
84 failed("Test frame couldn't be closed successfully.");
87 xFrame = null;
90 public String[] getTestMethodNames() {
91 return new String[]{"checkContextMenuInterceptor"};
94 public void checkContextMenuInterceptor() {
95 log.println(" **** Context Menu Interceptor *** ");
97 try {
98 // intialize the test document
99 com.sun.star.lang.XComponent xDrawDoc = DrawTools.createDrawDoc(xMSF);
101 SOfficeFactory SOF = SOfficeFactory.getFactory( xMSF);
102 XShape oShape = SOF.createShape(xDrawDoc,5000,5000,1500,1000,"GraphicObject");
103 DrawTools.getShapes(DrawTools.getDrawPage(xDrawDoc,0)).add(oShape);
105 com.sun.star.frame.XModel xModel =
106 (com.sun.star.frame.XModel)UnoRuntime.queryInterface(
107 com.sun.star.frame.XModel.class, xDrawDoc);
109 // get the frame for later usage
110 xFrame = xModel.getCurrentController().getFrame();
112 // ensure that the document content is optimal visible
113 DesktopTools.zoomToEntirePage(xDrawDoc);
115 XBitmap xBitmap = null;
117 // adding graphic as ObjRelation for GraphicObjectShape
118 XPropertySet oShapeProps = (XPropertySet)
119 UnoRuntime.queryInterface(XPropertySet.class,oShape);
120 log.println( "Inserting a shape into the document");
122 try
124 oShapeProps.setPropertyValue(
125 "GraphicURL",util.utils.getFullTestURL("space-metal.jpg"));
126 xBitmap = (XBitmap) AnyConverter.toObject(
127 new Type(XBitmap.class),oShapeProps.getPropertyValue
128 ("GraphicObjectFillBitmap"));
129 } catch (com.sun.star.lang.WrappedTargetException e) {
130 } catch (com.sun.star.lang.IllegalArgumentException e) {
131 } catch (com.sun.star.beans.PropertyVetoException e) {
132 } catch (com.sun.star.beans.UnknownPropertyException e) {
135 // reuse the frame
136 com.sun.star.frame.XController xController = xFrame.getController();
137 com.sun.star.ui.XContextMenuInterception xContextMenuInterception = null;
138 com.sun.star.ui.XContextMenuInterceptor xContextMenuInterceptor = null;
140 if ( xController != null )
142 log.println( "Creating context menu interceptor");
144 // add our context menu interceptor
145 xContextMenuInterception =
146 (com.sun.star.ui.XContextMenuInterception)UnoRuntime.queryInterface(
147 com.sun.star.ui.XContextMenuInterception.class, xController );
149 if( xContextMenuInterception != null )
151 ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor( xBitmap );
152 xContextMenuInterceptor =
153 (com.sun.star.ui.XContextMenuInterceptor)UnoRuntime.queryInterface(
154 com.sun.star.ui.XContextMenuInterceptor.class, aContextMenuInterceptor );
156 log.println( "Register context menu interceptor");
157 xContextMenuInterception.registerContextMenuInterceptor( xContextMenuInterceptor );
161 // utils.shortWait(10000);
163 openContextMenu((XModel) UnoRuntime.queryInterface(XModel.class, xDrawDoc));
165 checkHelpEntry();
167 // remove our context menu interceptor
168 if ( xContextMenuInterception != null &&
169 xContextMenuInterceptor != null ) {
170 log.println( "Release context menu interceptor");
171 xContextMenuInterception.releaseContextMenuInterceptor(
172 xContextMenuInterceptor );
175 catch ( com.sun.star.uno.RuntimeException ex ) {
176 ex.printStackTrace((PrintWriter)log);
177 failed("Runtime exception caught!" + ex.getMessage());
179 catch ( java.lang.Exception ex ) {
180 ex.printStackTrace((PrintWriter)log);
181 failed("Java lang exception caught!" + ex.getMessage());
185 private void checkHelpEntry(){
186 XInterface toolkit = null;
188 log.println("get accesibility...");
189 try{
190 toolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
191 } catch (com.sun.star.uno.Exception e){
192 log.println("could not get Toolkit " + e.toString());
194 XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
195 XExtendedToolkit.class, toolkit);
197 XAccessible xRoot = null;
199 AccessibilityTools at = new AccessibilityTools();
201 try {
202 xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
203 tk.getTopWindow(0));
205 xRoot = at.getAccessibleObject(xWindow);
207 // at.printAccessibleTree((PrintWriter)log, xRoot, param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
208 at.printAccessibleTree((PrintWriter)log, xRoot, true);
210 catch (com.sun.star.lang.IndexOutOfBoundsException e)
212 log.println("Couldn't get Window");
215 XAccessibleContext oPopMenu = at.getAccessibleObjectForRole(xRoot, AccessibleRole.POPUP_MENU);
217 log.println("ImplementationName: " + util.utils.getImplName(oPopMenu));
219 XAccessible xHelp = null;
220 try{
221 log.println("Try to get first entry of context menu...");
222 xHelp = oPopMenu.getAccessibleChild(0);
224 } catch (IndexOutOfBoundsException e){
225 failed("Not possible to get first entry of context menu");
228 if (xHelp == null) failed("first entry of context menu is NULL");
230 XAccessibleContext xHelpCont = xHelp.getAccessibleContext();
232 if ( xHelpCont == null )
233 failed("No able to retrieve accessible context from first entry of context menu");
235 String aAccessibleName = xHelpCont.getAccessibleName();
236 if ( !aAccessibleName.equals( "Help" )) {
237 log.println("Accessible name found = "+aAccessibleName );
238 failed("First entry of context menu is not from context menu interceptor");
243 log.println("try to get first children of Help context...");
244 XAccessible xHelpChild = xHelpCont.getAccessibleChild(0);
246 } catch (IndexOutOfBoundsException e){
247 failed("not possible to get first children of Help context");
252 private void openContextMenu(XModel aModel){
254 log.println("try to open contex menu...");
255 AccessibilityTools at = new AccessibilityTools();
257 xWindow = at.getCurrentWindow(xMSF, aModel);
259 XAccessible xRoot = at.getAccessibleObject(xWindow);
261 XInterface oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
263 XAccessibleComponent window = (XAccessibleComponent) UnoRuntime.queryInterface(
264 XAccessibleComponent.class, oObj);
266 point = window.getLocationOnScreen();
267 Rectangle rect = window.getBounds();
269 log.println("klick mouse button...");
270 try {
271 Robot rob = new Robot();
272 int x = point.X + (rect.Width / 2);
273 int y = point.Y + (rect.Height / 2);
274 rob.mouseMove(x, y);
275 System.out.println("Press Button");
276 rob.mousePress(InputEvent.BUTTON3_MASK);
277 System.out.println("Release Button");
278 rob.mouseRelease(InputEvent.BUTTON3_MASK);
279 System.out.println("done");
280 } catch (java.awt.AWTException e) {
281 log.println("couldn't press mouse button");
284 utils.shortWait(3000);