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.XAccessibleContext;
29 import com.sun.star.accessibility.XAccessibleSelection;
30 import com.sun.star.awt.XExtendedToolkit;
31 import com.sun.star.awt.XWindow;
32 import com.sun.star.beans.PropertyValue;
33 import com.sun.star.frame.XController;
34 import com.sun.star.frame.XDesktop;
35 import com.sun.star.frame.XDispatch;
36 import com.sun.star.frame.XDispatchProvider;
37 import com.sun.star.frame.XModel;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.text.XTextDocument;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 import com.sun.star.util.URL;
43 import com.sun.star.util.XURLTransformer;
44 
45 import java.io.PrintWriter;
46 
47 import lib.StatusException;
48 import lib.TestCase;
49 import lib.TestEnvironment;
50 import lib.TestParameters;
51 
52 import util.AccessibilityTools;
53 import util.DesktopTools;
54 import util.SOfficeFactory;
55 
56 
57 /**
58  * Test for object that implements the following interfaces :
59  * <ul>
60  *  <li><code>
61  *  ::com::sun::star::accessibility::XAccessibleContext</code></li>
62  *  <li><code>
63  *  ::com::sun::star::accessibility::XAccessibleEventBroadcaster
64  *  </code></li>
65  *  <li><code>
66  *  ::com::sun::star::accessibility::XAccessibleComponent</code></li>
67  *  <li><code>
68  *  ::com::sun::star::accessibility::XAccessibleExtendedComponent
69  *  </code></li>
70  *  <li><code>
71  *  ::com::sun::star::accessibility::XAccessibleSelection
72  *  </code></li>
73  * </ul> <p>
74  *
75  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
76  * @see com.sun.star.accessibility.XAccessibleContext
77  * @see com.sun.star.accessibility.XAccessibleComponent
78  * @see com.sun.star.accessibility.XAccessibleExtendedComponent
79  * @see com.sun.star.accessibility.XAccessibleSelection
80  * @see ifc.accessibility._XAccessibleEventBroadcaster
81  * @see ifc.accessibility._XAccessibleContext
82  * @see ifc.accessibility._XAccessibleComponent
83  * @see ifc.accessibility._XAccessibleExtendedComponent
84  * @see ifc.accessibility._XAccessibleSelection
85  */
86 public class AccessibleTabControl extends TestCase {
87     private static XDesktop the_Desk;
88     private static XTextDocument xTextDoc;
89     private static XAccessibleAction accCloseButton = null;
90 
91     /**
92      * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
93      */
initialize(TestParameters Param, PrintWriter log)94     protected void initialize(TestParameters Param, PrintWriter log) {
95         the_Desk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,
96                                                         DesktopTools.createDesktop(
97                                                                 (XMultiServiceFactory) Param.getMSF()));
98     }
99 
100     /**
101      * Closes an InsertFields dialog, disposes the document, if exists, created in
102      * <code>createTestEnvironment</code> method.
103      */
cleanup(TestParameters Param, PrintWriter log)104     protected void cleanup(TestParameters Param, PrintWriter log) {
105         try {
106             if (accCloseButton != null) {
107                 log.println("closing InsertFields Dialog");
108                 accCloseButton.doAccessibleAction(0);
109             }
110         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
111             e.printStackTrace(log);
112         }
113 
114         log.println("disposing xTextDoc");
115 
116         if (xTextDoc != null) {
117             util.DesktopTools.closeDoc(xTextDoc);
118         }
119     }
120 
121     /**
122      * Creates a text document, opens an InsertField dialog.
123      * Then obtains an accessible object with
124      * the role <code>AccessibleRole.PAGETABLIST</code>.
125      * Object relations created :
126      * <ul>
127      *  <li> <code>'EventProducer'</code> for
128      *      {@link ifc.accessibility._XAccessibleEventBroadcaster}:
129      *      selects items </li>
130      *   <li> <code>'XAccessibleSelection.multiSelection'</code>
131      *  of type <code>Boolean</code> for
132      *  {@link ifc.accessibility._XAccessibleSelection}:
133      *   indicates that component supports single selection mode.</li>
134      * </ul>
135      *
136      * @param tParam test parameters
137      * @param log writer to log information while testing
138      *
139      * @see com.sun.star.awt.Toolkit
140      * @see com.sun.star.accessibility.AccessibleRole
141      * @see ifc.accessibility._XAccessibleEventBroadcaster
142      * @see ifc.accessibility._XAccessibleSelection
143      * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
144      * @see com.sun.star.accessibility.XAccessibleSelection
145      */
createTestEnvironment(TestParameters tParam, PrintWriter log)146     protected TestEnvironment createTestEnvironment(TestParameters tParam,
147                                                     PrintWriter log) {
148         log.println("creating a test environment");
149 
150         if (xTextDoc != null) {
151             util.DesktopTools.closeDoc(xTextDoc);
152         }
153 
154         // get a soffice factory object
155         SOfficeFactory SOF = SOfficeFactory.getFactory(
156                                      (XMultiServiceFactory) tParam.getMSF());
157 
158         try {
159             log.println("creating a text document");
160             xTextDoc = SOF.createTextDoc(null);
161         } catch (com.sun.star.uno.Exception e) {
162             // Some exception occured.FAILED
163             e.printStackTrace(log);
164             throw new StatusException("Couldn't create document", e);
165         }
166 
167         shortWait();
168 
169         XModel aModel1 = (XModel) UnoRuntime.queryInterface(XModel.class,
170                                                             xTextDoc);
171 
172         XController secondController = aModel1.getCurrentController();
173 
174         XDispatchProvider aProv = (XDispatchProvider) UnoRuntime.queryInterface(
175                                           XDispatchProvider.class,
176                                           secondController);
177 
178         XURLTransformer urlTransf = null;
179 
180         try {
181             XInterface transf = (XInterface) ((XMultiServiceFactory) tParam.getMSF()).createInstance(
182                                         "com.sun.star.util.URLTransformer");
183             urlTransf = (XURLTransformer) UnoRuntime.queryInterface(
184                                 XURLTransformer.class, transf);
185         } catch (com.sun.star.uno.Exception e) {
186             e.printStackTrace(log);
187             throw new StatusException("Couldn't create URLTransformer", e);
188         }
189 
190         XDispatch getting = null;
191         log.println("opening InsertField dialog");
192 
193         URL[] url = new URL[1];
194         url[0] = new URL();
195         url[0].Complete = ".uno:InsertField";
196         urlTransf.parseStrict(url);
197         getting = aProv.queryDispatch(url[0], "", 0);
198 
199         PropertyValue[] noArgs = new PropertyValue[0];
200         getting.dispatch(url[0], noArgs);
201 
202         shortWait();
203 
204         XInterface oObj = null;
205 
206         try {
207             oObj = (XInterface) ((XMultiServiceFactory) tParam.getMSF()).createInstance(
208                            "com.sun.star.awt.Toolkit");
209         } catch (com.sun.star.uno.Exception e) {
210             log.println("Couldn't get toolkit");
211             e.printStackTrace(log);
212             throw new StatusException("Couldn't get toolkit", e);
213         }
214 
215         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
216                                       XExtendedToolkit.class, oObj);
217 
218         AccessibilityTools at = new AccessibilityTools();
219 
220         shortWait();
221 
222         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
223                                                               tk.getActiveTopWindow());
224 
225         XAccessible xRoot = at.getAccessibleObject(xWindow);
226 
227         oObj = at.getAccessibleObjectForRole(xRoot,
228                                              AccessibleRole.PAGE_TAB_LIST);
229 
230         XAccessibleContext closeButton = at.getAccessibleObjectForRole(xRoot,
231                                                                        AccessibleRole.PUSH_BUTTON,
232                                                                        "Close");
233 
234         accCloseButton = (XAccessibleAction) UnoRuntime.queryInterface(
235                                  XAccessibleAction.class, closeButton);
236 
237         log.println("ImplementationName: " + util.utils.getImplName(oObj));
238 
239         TestEnvironment tEnv = new TestEnvironment(oObj);
240 
241         final XAccessibleSelection selection = (XAccessibleSelection) (XAccessibleSelection) UnoRuntime.queryInterface(
242                                                                               XAccessibleSelection.class,
243                                                                               oObj);
244 
245         tEnv.addObjRelation("EventProducer",
246                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
247             public void fireEvent() {
248                 try {
249                     selection.selectAccessibleChild(1);
250                     selection.selectAccessibleChild(0);
251                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
252                 }
253             }
254         });
255 
256         tEnv.addObjRelation("XAccessibleSelection.OneAlwaysSelected",
257                             new Boolean(true));
258 
259         tEnv.addObjRelation("XAccessibleSelection.multiSelection",
260                             new Boolean(false));
261 
262         return tEnv;
263     }
264 
265     /**
266     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
267     * reset</code> call.
268     */
shortWait()269     private void shortWait() {
270         try {
271             Thread.currentThread().sleep(500);
272         } catch (InterruptedException e) {
273             System.out.println("While waiting :" + e);
274         }
275     }
276 }
277