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.XAccessibleAction;
28 import com.sun.star.accessibility.XAccessibleComponent;
29 import com.sun.star.accessibility.XAccessibleText;
30 import com.sun.star.awt.XExtendedToolkit;
31 import com.sun.star.awt.XWindow;
32 import com.sun.star.frame.XController;
33 import com.sun.star.frame.XDispatch;
34 import com.sun.star.frame.XDispatchProvider;
35 import com.sun.star.frame.XModel;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.text.XTextDocument;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 import com.sun.star.util.URL;
41 import com.sun.star.util.XURLTransformer;
42 
43 import java.io.PrintWriter;
44 
45 import lib.StatusException;
46 import lib.TestEnvironment;
47 import lib.TestParameters;
48 
49 import util.AccessibilityTools;
50 import util.SOfficeFactory;
51 import util.utils;
52 
53 
54 /**
55  * Object implements the following interfaces :
56  * <ul>
57  *  <li> <code>::com::sun::star::accessibility::XAccessibleExtendedComponent</code></li>
58  *  <li> <code>::com::sun::star::accessibility::XAccessibleEventBroadcaster</code></li>
59  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code></li>
60  *  <li> <code>::com::sun::star::accessibility::XAccessibleValue</code></li>
61  *  <li> <code>::com::sun::star::accessibility::XAccessibleAction</code></li>
62  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code></li>
63  *  <li> <code>::com::sun::star::accessibility::XAccessibleText</code></li>
64  * </ul> <p>
65  *
66  * @see com.sun.star.accessibility.XAccessibleExtendedComponent
67  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
68  * @see com.sun.star.accessibility.XAccessibleComponent
69  * @see com.sun.star.accessibility.XAccessibleValue
70  * @see com.sun.star.accessibility.XAccessibleAction
71  * @see com.sun.star.accessibility.XAccessibleContext
72  * @see com.sun.star.accessibility.XAccessibleText
73  * @see ifc.accessibility._XAccessibleExtendedComponent
74  * @see ifc.accessibility._XAccessibleEventBroadcaster
75  * @see ifc.accessibility._XAccessibleComponent
76  * @see ifc.accessibility._XAccessibleValue
77  * @see ifc.accessibility._XAccessibleAction
78  * @see ifc.accessibility._XAccessibleContext
79  * @see ifc.accessibility._XAccessibleText
80  */
81 public class AccessibleButton extends lib.TestCase {
82     private static XTextDocument xTextDoc = null;
83     private static XAccessibleAction action = null;
84 
85     /**
86      * Opens 'Printer Settings' dialog using document dispatch provider
87      * running in a separate thread. Finds active top window (the dialog
88      * window) and finds accessible button 'OK' walking through the
89      * accessible component tree.
90      */
createTestEnvironment(TestParameters Param, PrintWriter log)91     protected TestEnvironment createTestEnvironment(TestParameters Param,
92                                                     PrintWriter log) {
93         XInterface oObj = null;
94         XMultiServiceFactory msf = (XMultiServiceFactory) Param.getMSF();
95 
96         try {
97             oObj = (XInterface) msf.createInstance("com.sun.star.awt.Toolkit");
98         } catch (com.sun.star.uno.Exception e) {
99             log.println("Couldn't get toolkit");
100             e.printStackTrace(log);
101             throw new StatusException("Couldn't get toolkit", e);
102         }
103 
104         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
105                                       XExtendedToolkit.class, oObj);
106 
107         util.utils.shortWait(Param.getInt("ShortWait"));
108 
109         DiagThread psDiag = new DiagThread(xTextDoc, msf);
110         psDiag.start();
111 
112         util.utils.shortWait(Param.getInt("ShortWait"));
113 
114         AccessibilityTools at = new AccessibilityTools();
115 
116         util.utils.shortWait(Param.getInt("ShortWait"));
117 
118         Object atw = tk.getActiveTopWindow();
119 
120         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
121                                                               atw);
122 
123         XAccessible xRoot = at.getAccessibleObject(xWindow);
124 
125 
126         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
127         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PUSH_BUTTON,
128                                              "Cancel");
129 
130         log.println("ImplementationName " + utils.getImplName(oObj));
131 
132         TestEnvironment tEnv = new TestEnvironment(oObj);
133 
134         action = (XAccessibleAction) UnoRuntime.queryInterface(
135                          XAccessibleAction.class, oObj);
136 
137         final XAccessibleComponent acomp = (XAccessibleComponent) UnoRuntime.queryInterface(
138                                                    XAccessibleComponent.class,
139                                                    oObj);
140 
141         tEnv.addObjRelation("EventProducer",
142                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
143             public void fireEvent() {
144                 System.out.println("Grabbing focus ... ");
145                 acomp.grabFocus();
146             }
147         });
148 
149         XAccessibleText text = (XAccessibleText) UnoRuntime.queryInterface(
150                                        XAccessibleText.class, oObj);
151 
152         tEnv.addObjRelation("XAccessibleText.Text", text.getText());
153 
154         tEnv.addObjRelation("EditOnly",
155                             "This method isn't supported in this component");
156 
157         tEnv.addObjRelation("LimitedBounds", "yes");
158 
159         return tEnv;
160     }
161 
162     /**
163      * Closes the dialog using accessible button 'OK' found in
164      * <code>createTestEnvironment()</code>.
165      */
cleanup(TestParameters Param, PrintWriter log)166     protected void cleanup(TestParameters Param, PrintWriter log) {
167         log.println("    disposing xTextDoc ");
168 
169         try {
170             action.doAccessibleAction(0);
171         } catch (com.sun.star.lang.IndexOutOfBoundsException ioe) {
172             log.println("Couldn't close dialog");
173         } catch (com.sun.star.lang.DisposedException de) {
174             log.println("Dialog already disposed");
175         }
176 
177         util.DesktopTools.closeDoc(xTextDoc);
178     }
179 
180     /**
181      * Opens new writer document.
182      */
initialize(TestParameters Param, PrintWriter log)183     protected void initialize(TestParameters Param, PrintWriter log) {
184         try {
185             SOfficeFactory SOF = SOfficeFactory.getFactory(
186                                          (XMultiServiceFactory) Param.getMSF());
187             xTextDoc = SOF.createTextDoc(null);
188         } catch (com.sun.star.uno.Exception e) {
189             throw new StatusException("Can't create document", e);
190         }
191     }
192 
193 
194     /**
195      * Thread for opening modal dialog 'Print Settings'.
196      */
197     public class DiagThread extends Thread {
198         public XTextDocument xTextDoc = null;
199         public XMultiServiceFactory msf = null;
200 
DiagThread(XTextDocument xTextDoc, XMultiServiceFactory msf)201         public DiagThread(XTextDocument xTextDoc, XMultiServiceFactory msf) {
202             this.xTextDoc = xTextDoc;
203             this.msf = msf;
204         }
205 
run()206         public void run() {
207             XModel aModel = (XModel) UnoRuntime.queryInterface(XModel.class,
208                                                                xTextDoc);
209 
210             XController xController = aModel.getCurrentController();
211 
212             //Opening PrinterSetupDialog
213             try {
214                 String aSlotID = ".uno:Zoom";
215                 XDispatchProvider xDispProv = (XDispatchProvider) UnoRuntime.queryInterface(
216                                                       XDispatchProvider.class,
217                                                       xController);
218                 XURLTransformer xParser = (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
219                                                   XURLTransformer.class,
220                                                   msf.createInstance(
221                                                           "com.sun.star.util.URLTransformer"));
222 
223                 // Because it's an in/out parameter
224                 // we must use an array of URL objects.
225                 URL[] aParseURL = new URL[1];
226                 aParseURL[0] = new URL();
227                 aParseURL[0].Complete = aSlotID;
228                 xParser.parseStrict(aParseURL);
229 
230                 URL aURL = aParseURL[0];
231                 XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
232 
233                 if (xDispatcher != null) {
234                     xDispatcher.dispatch(aURL, null);
235                 }
236             } catch (com.sun.star.uno.Exception e) {
237                 log.println("Couldn't open dialog");
238             }
239         }
240     }
241 }
242