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