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