176b6b121SAndrew Rist /**************************************************************
276b6b121SAndrew Rist  *
376b6b121SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
476b6b121SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
576b6b121SAndrew Rist  * distributed with this work for additional information
676b6b121SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
776b6b121SAndrew Rist  * to you under the Apache License, Version 2.0 (the
876b6b121SAndrew Rist  * "License"); you may not use this file except in compliance
976b6b121SAndrew Rist  * with the License.  You may obtain a copy of the License at
1076b6b121SAndrew Rist  *
1176b6b121SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1276b6b121SAndrew Rist  *
1376b6b121SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1476b6b121SAndrew Rist  * software distributed under the License is distributed on an
1576b6b121SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1676b6b121SAndrew Rist  * KIND, either express or implied.  See the License for the
1776b6b121SAndrew Rist  * specific language governing permissions and limitations
1876b6b121SAndrew Rist  * under the License.
1976b6b121SAndrew Rist  *
2076b6b121SAndrew Rist  *************************************************************/
2176b6b121SAndrew Rist 
22cdf0e10cSrcweir package complex.contextMenuInterceptor;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRole;
25cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
26cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleComponent;
27cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
28cdf0e10cSrcweir import com.sun.star.awt.Point;
29cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
30cdf0e10cSrcweir import com.sun.star.awt.XBitmap;
31cdf0e10cSrcweir import com.sun.star.awt.XExtendedToolkit;
32cdf0e10cSrcweir import com.sun.star.awt.XWindow;
33cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
34cdf0e10cSrcweir import com.sun.star.drawing.XShape;
35cdf0e10cSrcweir import com.sun.star.frame.XFrame;
36cdf0e10cSrcweir import com.sun.star.frame.XModel;
37cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
38cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
39cdf0e10cSrcweir import com.sun.star.ui.XContextMenuInterception;
40cdf0e10cSrcweir import com.sun.star.ui.XContextMenuInterceptor;
41cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
42cdf0e10cSrcweir import com.sun.star.uno.Type;
43cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
44cdf0e10cSrcweir import com.sun.star.uno.XInterface;
45cdf0e10cSrcweir import com.sun.star.util.XCloseable;
46cdf0e10cSrcweir import java.awt.Robot;
47cdf0e10cSrcweir import java.awt.event.InputEvent;
48cdf0e10cSrcweir import java.io.File;
49cdf0e10cSrcweir import util.AccessibilityTools;
50cdf0e10cSrcweir import util.DesktopTools;
51cdf0e10cSrcweir import util.DrawTools;
52cdf0e10cSrcweir import util.SOfficeFactory;
53cdf0e10cSrcweir import util.utils;
54cdf0e10cSrcweir import org.openoffice.test.OfficeFileUrl;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir // ---------- junit imports -----------------
57cdf0e10cSrcweir import org.junit.After;
58cdf0e10cSrcweir import org.junit.AfterClass;
59cdf0e10cSrcweir import org.junit.Before;
60cdf0e10cSrcweir import org.junit.BeforeClass;
61cdf0e10cSrcweir import org.junit.Test;
62cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
63cdf0e10cSrcweir import static org.junit.Assert.*;
64cdf0e10cSrcweir // ------------------------------------------
65cdf0e10cSrcweir 
66cdf0e10cSrcweir /**
67cdf0e10cSrcweir  *
68cdf0e10cSrcweir  */
69cdf0e10cSrcweir public class CheckContextMenuInterceptor
70cdf0e10cSrcweir {
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     XMultiServiceFactory xMSF = null;
73cdf0e10cSrcweir     XFrame xFrame = null;
74cdf0e10cSrcweir     Point point = null;
75cdf0e10cSrcweir     XWindow xWindow = null;
76cdf0e10cSrcweir     com.sun.star.lang.XComponent xDrawDoc;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     @Before
before()79cdf0e10cSrcweir     public void before()
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         xMSF = getMSF();
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     @After
after()85cdf0e10cSrcweir     public void after()
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir         System.out.println("release the popup menu");
88cdf0e10cSrcweir         try
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             Robot rob = new Robot();
91cdf0e10cSrcweir             int x = point.X;
92cdf0e10cSrcweir             int y = point.Y;
93cdf0e10cSrcweir             rob.mouseMove(x, y);
94cdf0e10cSrcweir             rob.mousePress(InputEvent.BUTTON1_MASK);
95cdf0e10cSrcweir             rob.mouseRelease(InputEvent.BUTTON1_MASK);
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir         catch (java.awt.AWTException e)
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             System.out.println("couldn't press mouse button");
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xFrame);
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         try
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             xClose.close(true);
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir         catch (com.sun.star.util.CloseVetoException exVeto)
109cdf0e10cSrcweir         {
110cdf0e10cSrcweir             fail("Test frame couldn't be closed successfully.");
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         xFrame = null;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir //        xClose = UnoRuntime.queryInterface(XCloseable.class, xDrawDoc);
116cdf0e10cSrcweir //        try
117cdf0e10cSrcweir //        {
118cdf0e10cSrcweir //            xClose.close(true);
119cdf0e10cSrcweir //        }
120cdf0e10cSrcweir //        catch (com.sun.star.util.CloseVetoException exVeto)
121cdf0e10cSrcweir //        {
122cdf0e10cSrcweir //            fail("Test DrawDoc couldn't be closed successfully.");
123cdf0e10cSrcweir //        }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir //    public String[] getTestMethodNames() {
128cdf0e10cSrcweir //        return new String[]{"checkContextMenuInterceptor"};
129cdf0e10cSrcweir //    }
130cdf0e10cSrcweir     @Test
checkContextMenuInterceptor()131cdf0e10cSrcweir     public void checkContextMenuInterceptor()
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         System.out.println(" **** Context Menu Interceptor *** ");
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         try
136cdf0e10cSrcweir         {
137*509df7cbSJohn Bampton             // initialize the test document
138cdf0e10cSrcweir             xDrawDoc = DrawTools.createDrawDoc(xMSF);
139cdf0e10cSrcweir 
140cdf0e10cSrcweir             SOfficeFactory SOF = SOfficeFactory.getFactory(xMSF);
141cdf0e10cSrcweir             XShape oShape = SOF.createShape(xDrawDoc, 5000, 5000, 1500, 1000, "GraphicObject");
142cdf0e10cSrcweir             DrawTools.getShapes(DrawTools.getDrawPage(xDrawDoc, 0)).add(oShape);
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             com.sun.star.frame.XModel xModel =
145cdf0e10cSrcweir                     UnoRuntime.queryInterface(com.sun.star.frame.XModel.class, xDrawDoc);
146cdf0e10cSrcweir 
147cdf0e10cSrcweir             // get the frame for later usage
148cdf0e10cSrcweir             xFrame = xModel.getCurrentController().getFrame();
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             // ensure that the document content is optimal visible
151cdf0e10cSrcweir             DesktopTools.zoomToEntirePage(xDrawDoc);
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             XBitmap xBitmap = null;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir             // adding graphic as ObjRelation for GraphicObjectShape
156cdf0e10cSrcweir             XPropertySet oShapeProps = UnoRuntime.queryInterface(XPropertySet.class, oShape);
157cdf0e10cSrcweir             System.out.println("Inserting a shape into the document");
158cdf0e10cSrcweir 
159cdf0e10cSrcweir             try
160cdf0e10cSrcweir             {
161cdf0e10cSrcweir                 String sFile = OfficeFileUrl.getAbsolute(new File("space-metal.jpg"));
162cdf0e10cSrcweir                 // String sFile = util.utils.getFullTestURL("space-metal.jpg");
163cdf0e10cSrcweir                 oShapeProps.setPropertyValue("GraphicURL", sFile);
164cdf0e10cSrcweir                 Object oProp = oShapeProps.getPropertyValue("GraphicObjectFillBitmap");
165cdf0e10cSrcweir                 xBitmap = (XBitmap) AnyConverter.toObject(new Type(XBitmap.class), oProp);
166cdf0e10cSrcweir             }
167cdf0e10cSrcweir             catch (com.sun.star.lang.WrappedTargetException e)
168cdf0e10cSrcweir             {
169cdf0e10cSrcweir             }
170cdf0e10cSrcweir             catch (com.sun.star.lang.IllegalArgumentException e)
171cdf0e10cSrcweir             {
172cdf0e10cSrcweir             }
173cdf0e10cSrcweir             catch (com.sun.star.beans.PropertyVetoException e)
174cdf0e10cSrcweir             {
175cdf0e10cSrcweir             }
176cdf0e10cSrcweir             catch (com.sun.star.beans.UnknownPropertyException e)
177cdf0e10cSrcweir             {
178cdf0e10cSrcweir             }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir             // reuse the frame
181cdf0e10cSrcweir             com.sun.star.frame.XController xController = xFrame.getController();
182cdf0e10cSrcweir             XContextMenuInterception xContextMenuInterception = null;
183cdf0e10cSrcweir             XContextMenuInterceptor xContextMenuInterceptor = null;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir             if (xController != null)
186cdf0e10cSrcweir             {
187cdf0e10cSrcweir                 System.out.println("Creating context menu interceptor");
188cdf0e10cSrcweir 
189cdf0e10cSrcweir                 // add our context menu interceptor
190cdf0e10cSrcweir                 xContextMenuInterception =
191cdf0e10cSrcweir                         UnoRuntime.queryInterface(XContextMenuInterception.class, xController);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir                 if (xContextMenuInterception != null)
194cdf0e10cSrcweir                 {
195cdf0e10cSrcweir                     ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor(xBitmap);
196cdf0e10cSrcweir                     xContextMenuInterceptor =
197cdf0e10cSrcweir                             UnoRuntime.queryInterface(XContextMenuInterceptor.class, aContextMenuInterceptor);
198cdf0e10cSrcweir 
199cdf0e10cSrcweir                     System.out.println("Register context menu interceptor");
200cdf0e10cSrcweir                     xContextMenuInterception.registerContextMenuInterceptor(xContextMenuInterceptor);
201cdf0e10cSrcweir                 }
202cdf0e10cSrcweir             }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir             //	utils.shortWait(10000);
205cdf0e10cSrcweir 
206cdf0e10cSrcweir             openContextMenu(UnoRuntime.queryInterface(XModel.class, xDrawDoc));
207cdf0e10cSrcweir 
208cdf0e10cSrcweir             checkHelpEntry();
209cdf0e10cSrcweir 
210cdf0e10cSrcweir             // remove our context menu interceptor
211cdf0e10cSrcweir             if (xContextMenuInterception != null
212cdf0e10cSrcweir                     && xContextMenuInterceptor != null)
213cdf0e10cSrcweir             {
214cdf0e10cSrcweir                 System.out.println("Release context menu interceptor");
215cdf0e10cSrcweir                 xContextMenuInterception.releaseContextMenuInterceptor(
216cdf0e10cSrcweir                         xContextMenuInterceptor);
217cdf0e10cSrcweir             }
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir         catch (com.sun.star.uno.RuntimeException ex)
220cdf0e10cSrcweir         {
221cdf0e10cSrcweir             // ex.printStackTrace();
222cdf0e10cSrcweir             fail("Runtime exception caught!" + ex.getMessage());
223cdf0e10cSrcweir         }
224cdf0e10cSrcweir         catch (java.lang.Exception ex)
225cdf0e10cSrcweir         {
226cdf0e10cSrcweir             // ex.printStackTrace();
227cdf0e10cSrcweir             fail("Java lang exception caught!" + ex.getMessage());
228cdf0e10cSrcweir         }
229cdf0e10cSrcweir     }
230cdf0e10cSrcweir 
checkHelpEntry()231cdf0e10cSrcweir     private void checkHelpEntry()
232cdf0e10cSrcweir     {
233cdf0e10cSrcweir         XInterface toolkit = null;
234cdf0e10cSrcweir 
235cdf0e10cSrcweir         System.out.println("get accesibility...");
236cdf0e10cSrcweir         try
237cdf0e10cSrcweir         {
238cdf0e10cSrcweir             toolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
239cdf0e10cSrcweir         }
240cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e)
241cdf0e10cSrcweir         {
242cdf0e10cSrcweir             System.out.println("could not get Toolkit " + e.toString());
243cdf0e10cSrcweir         }
244cdf0e10cSrcweir         XExtendedToolkit tk = UnoRuntime.queryInterface(XExtendedToolkit.class, toolkit);
245cdf0e10cSrcweir 
246cdf0e10cSrcweir         XAccessible xRoot = null;
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
249cdf0e10cSrcweir 
250cdf0e10cSrcweir         try
251cdf0e10cSrcweir         {
252cdf0e10cSrcweir             xWindow = UnoRuntime.queryInterface(XWindow.class, tk.getTopWindow(0));
253cdf0e10cSrcweir 
254cdf0e10cSrcweir             xRoot = at.getAccessibleObject(xWindow);
255cdf0e10cSrcweir 
256cdf0e10cSrcweir //            at.printAccessibleTree((PrintWriter)log, xRoot, param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
257cdf0e10cSrcweir             // at.printAccessibleTree(System.out, xRoot, true);
258cdf0e10cSrcweir         }
259cdf0e10cSrcweir         catch (com.sun.star.lang.IndexOutOfBoundsException e)
260cdf0e10cSrcweir         {
261cdf0e10cSrcweir             System.out.println("Couldn't get Window");
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir         XAccessibleContext oPopMenu = at.getAccessibleObjectForRole(xRoot, AccessibleRole.POPUP_MENU);
265cdf0e10cSrcweir 
266cdf0e10cSrcweir         System.out.println("ImplementationName: " + util.utils.getImplName(oPopMenu));
267cdf0e10cSrcweir 
268cdf0e10cSrcweir         XAccessible xHelp = null;
269cdf0e10cSrcweir         try
270cdf0e10cSrcweir         {
271cdf0e10cSrcweir             System.out.println("Try to get first entry of context menu...");
272cdf0e10cSrcweir             xHelp = oPopMenu.getAccessibleChild(0);
273cdf0e10cSrcweir 
274cdf0e10cSrcweir         }
275cdf0e10cSrcweir         catch (IndexOutOfBoundsException e)
276cdf0e10cSrcweir         {
277cdf0e10cSrcweir             fail("Not possible to get first entry of context menu");
278cdf0e10cSrcweir         }
279cdf0e10cSrcweir 
280cdf0e10cSrcweir         if (xHelp == null)
281cdf0e10cSrcweir         {
282cdf0e10cSrcweir             fail("first entry of context menu is NULL");
283cdf0e10cSrcweir         }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir         XAccessibleContext xHelpCont = xHelp.getAccessibleContext();
286cdf0e10cSrcweir 
287cdf0e10cSrcweir         if (xHelpCont == null)
288cdf0e10cSrcweir         {
289cdf0e10cSrcweir             fail("No able to retrieve accessible context from first entry of context menu");
290cdf0e10cSrcweir         }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir         String aAccessibleName = xHelpCont.getAccessibleName();
293cdf0e10cSrcweir         if (!aAccessibleName.equals("Help"))
294cdf0e10cSrcweir         {
295cdf0e10cSrcweir             System.out.println("Accessible name found = " + aAccessibleName);
296cdf0e10cSrcweir             fail("First entry of context menu is not from context menu interceptor");
297cdf0e10cSrcweir         }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir         try
300cdf0e10cSrcweir         {
301cdf0e10cSrcweir             System.out.println("try to get first children of Help context...");
302cdf0e10cSrcweir             XAccessible xHelpChild = xHelpCont.getAccessibleChild(0);
303cdf0e10cSrcweir 
304cdf0e10cSrcweir         }
305cdf0e10cSrcweir         catch (IndexOutOfBoundsException e)
306cdf0e10cSrcweir         {
307cdf0e10cSrcweir             fail("not possible to get first children of Help context");
308cdf0e10cSrcweir         }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir 
openContextMenu(XModel aModel)312cdf0e10cSrcweir     private void openContextMenu(XModel aModel)
313cdf0e10cSrcweir     {
314cdf0e10cSrcweir 
31526cf26ddSmseidel         System.out.println("try to open context menu...");
316cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
317cdf0e10cSrcweir 
318cdf0e10cSrcweir         xWindow = at.getCurrentWindow(xMSF, aModel);
319cdf0e10cSrcweir 
320cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
321cdf0e10cSrcweir 
322cdf0e10cSrcweir         XInterface oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
323cdf0e10cSrcweir 
324cdf0e10cSrcweir         XAccessibleComponent window = UnoRuntime.queryInterface(XAccessibleComponent.class, oObj);
325cdf0e10cSrcweir 
326cdf0e10cSrcweir         point = window.getLocationOnScreen();
327cdf0e10cSrcweir         Rectangle rect = window.getBounds();
328cdf0e10cSrcweir 
329cdf0e10cSrcweir         System.out.println("klick mouse button...");
330cdf0e10cSrcweir         try
331cdf0e10cSrcweir         {
332cdf0e10cSrcweir             Robot rob = new Robot();
333cdf0e10cSrcweir             int x = point.X + (rect.Width / 2);
334cdf0e10cSrcweir             int y = point.Y + (rect.Height / 2);
335cdf0e10cSrcweir             rob.mouseMove(x, y);
336cdf0e10cSrcweir             System.out.println("Press Button");
337cdf0e10cSrcweir             rob.mousePress(InputEvent.BUTTON3_MASK);
338cdf0e10cSrcweir             System.out.println("Release Button");
339cdf0e10cSrcweir             rob.mouseRelease(InputEvent.BUTTON3_MASK);
340cdf0e10cSrcweir             System.out.println("done");
341cdf0e10cSrcweir         }
342cdf0e10cSrcweir         catch (java.awt.AWTException e)
343cdf0e10cSrcweir         {
344cdf0e10cSrcweir             System.out.println("couldn't press mouse button");
345cdf0e10cSrcweir         }
346cdf0e10cSrcweir 
347cdf0e10cSrcweir         utils.shortWait(3000);
348cdf0e10cSrcweir 
349cdf0e10cSrcweir     }
350cdf0e10cSrcweir 
getMSF()351cdf0e10cSrcweir     private XMultiServiceFactory getMSF()
352cdf0e10cSrcweir     {
353cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
354cdf0e10cSrcweir         return xMSF1;
355cdf0e10cSrcweir     }
356cdf0e10cSrcweir 
357cdf0e10cSrcweir     // setup and close connections
358cdf0e10cSrcweir     @BeforeClass
setUpConnection()359cdf0e10cSrcweir     public static void setUpConnection() throws Exception
360cdf0e10cSrcweir     {
361cdf0e10cSrcweir         System.out.println("setUpConnection()");
362cdf0e10cSrcweir         connection.setUp();
363cdf0e10cSrcweir     }
364cdf0e10cSrcweir 
365cdf0e10cSrcweir     @AfterClass
tearDownConnection()366cdf0e10cSrcweir     public static void tearDownConnection()
367cdf0e10cSrcweir             throws InterruptedException, com.sun.star.uno.Exception
368cdf0e10cSrcweir     {
369cdf0e10cSrcweir         System.out.println("tearDownConnection()");
370cdf0e10cSrcweir         connection.tearDown();
371cdf0e10cSrcweir     }
372cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
373cdf0e10cSrcweir }
374