1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package mod._toolkit;
28 
29 import com.sun.star.accessibility.AccessibleRole;
30 import com.sun.star.accessibility.XAccessible;
31 import com.sun.star.accessibility.XAccessibleComponent;
32 import com.sun.star.accessibility.XAccessibleSelection;
33 import com.sun.star.awt.Point;
34 import com.sun.star.awt.Rectangle;
35 import com.sun.star.awt.XExtendedToolkit;
36 import com.sun.star.awt.XWindow;
37 import com.sun.star.frame.XDesktop;
38 import com.sun.star.frame.XModel;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.text.XTextDocument;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 
44 import java.awt.Robot;
45 import java.awt.event.InputEvent;
46 
47 import java.io.PrintWriter;
48 
49 import lib.StatusException;
50 import lib.TestCase;
51 import lib.TestEnvironment;
52 import lib.TestParameters;
53 
54 import util.AccessibilityTools;
55 import util.DesktopTools;
56 import util.SOfficeFactory;
57 
58 
59 public class AccessiblePopupMenu extends TestCase {
60     private static XDesktop the_Desk;
61     private static XTextDocument xTextDoc;
62     private static Point point;
63 
64     /**
65      * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
66      */
67     protected void initialize(TestParameters Param, PrintWriter log) {
68         the_Desk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,
69                                                         DesktopTools.createDesktop(
70                                                                 (XMultiServiceFactory) Param.getMSF()));
71     }
72 
73     /**
74      * Disposes the document, if exists, created in
75      * <code>createTestEnvironment</code> method.
76      */
77     protected void cleanup(TestParameters Param, PrintWriter log) {
78 
79         log.println("release the popup menu");
80         try {
81             Robot rob = new Robot();
82             int x = point.X;
83             int y = point.Y;
84             rob.mouseMove(x, y);
85             rob.mousePress(InputEvent.BUTTON1_MASK);
86             rob.mouseRelease(InputEvent.BUTTON1_MASK);
87         } catch (java.awt.AWTException e) {
88             log.println("couldn't press mouse button");
89         }
90 
91         log.println("disposing xTextDoc");
92 
93         if (xTextDoc != null) {
94             closeDoc();
95         }
96     }
97 
98     /**
99      * Creates a text document.
100      * Then obtains an accessible object with
101      * the role <code>AccessibleRole.PUSHBUTTON</code> and with the name
102      * <code>"Bold"</code>.
103      * Object relations created :
104      * <ul>
105      *  <li> <code>'EventProducer'</code> for
106      *      {@link ifc.accessibility._XAccessibleEventBroadcaster}</li>
107      *  <li> <code>'XAccessibleText.Text'</code> for
108      *      {@link ifc.accessibility._XAccessibleText}: the name of button</li>
109      * </ul>
110      *
111      * @param tParam test parameters
112      * @param log writer to log information while testing
113      *
114      * @see com.sun.star.awt.Toolkit
115      * @see com.sun.star.accessibility.AccessibleRole
116      * @see ifc.accessibility._XAccessibleEventBroadcaster
117      * @see ifc.accessibility._XAccessibleText
118      * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
119      * @see com.sun.star.accessibility.XAccessibleText
120      */
121     protected TestEnvironment createTestEnvironment(TestParameters tParam,
122                                                     PrintWriter log) {
123         log.println("creating a test environment");
124 
125         if (xTextDoc != null) {
126             closeDoc();
127         }
128 
129         // get a soffice factory object
130         SOfficeFactory SOF = SOfficeFactory.getFactory(
131                                      (XMultiServiceFactory) tParam.getMSF());
132 
133         XInterface toolkit = null;
134 
135         try {
136             log.println("creating a text document");
137             xTextDoc = SOF.createTextDoc(null);
138             toolkit = (XInterface) ((XMultiServiceFactory) tParam.getMSF()).createInstance(
139                               "com.sun.star.awt.Toolkit");
140         } catch (com.sun.star.uno.Exception e) {
141             // Some exception occures.FAILED
142             e.printStackTrace(log);
143             throw new StatusException("Couldn't create document", e);
144         }
145 
146         shortWait(tParam);
147 
148         XModel aModel = (XModel) UnoRuntime.queryInterface(XModel.class,
149                                                            xTextDoc);
150 
151         XInterface oObj = null;
152 
153         AccessibilityTools at = new AccessibilityTools();
154 
155         XWindow xWindow = at.getCurrentWindow(
156                                   (XMultiServiceFactory) tParam.getMSF(),
157                                   aModel);
158 
159         XAccessible xRoot = at.getAccessibleObject(xWindow);
160 
161         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
162 
163         XAccessibleComponent window = (XAccessibleComponent) UnoRuntime.queryInterface(
164                                               XAccessibleComponent.class, oObj);
165 
166         point = window.getLocationOnScreen();
167         Rectangle rect = window.getBounds();
168 
169         try {
170             Robot rob = new Robot();
171             int x = point.X + (rect.Width / 2);
172             int y = point.Y + (rect.Height / 2);
173             rob.mouseMove(x, y);
174             rob.mousePress(InputEvent.BUTTON3_MASK);
175             rob.mouseRelease(InputEvent.BUTTON3_MASK);
176         } catch (java.awt.AWTException e) {
177             log.println("couldn't press mouse button");
178         }
179 
180         shortWait(tParam);
181 
182         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
183                                       XExtendedToolkit.class, toolkit);
184 
185         try {
186             xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
187                                                           tk.getTopWindow(0));
188 
189             xRoot = at.getAccessibleObject(xWindow);
190 
191         at.printAccessibleTree(log, xRoot, tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
192         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
193             log.println("Couldn't get Window");
194         }
195 
196         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.POPUP_MENU);
197 
198         log.println("ImplementationName: " + util.utils.getImplName(oObj));
199 
200         TestEnvironment tEnv = new TestEnvironment(oObj);
201 
202         tEnv.addObjRelation("XAccessibleSelection.multiSelection",
203                             new Boolean(false));
204 
205         final XAccessibleSelection sel = (XAccessibleSelection) UnoRuntime.queryInterface(
206                                                  XAccessibleSelection.class,
207                                                  oObj);
208 
209         tEnv.addObjRelation("EventProducer",
210                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
211             public void fireEvent() {
212                 try {
213                     sel.selectAccessibleChild(2);
214                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
215                     System.out.println("Couldn't fire event");
216                 }
217             }
218         });
219 
220         return tEnv;
221     }
222 
223     protected void closeDoc() {
224         util.DesktopTools.closeDoc(xTextDoc);
225     }
226 
227     private void shortWait(TestParameters tParam) {
228         util.utils.shortWait(tParam.getInt(util.PropertyName.SHORT_WAIT));
229     }
230 }
231