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