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