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.XAccessibleContext;
30 import com.sun.star.accessibility.XAccessibleSelection;
31 import com.sun.star.awt.XExtendedToolkit;
32 import com.sun.star.awt.XWindow;
33 import com.sun.star.beans.PropertyValue;
34 import com.sun.star.frame.XController;
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.SOfficeFactory;
54 import util.utils;
55 
56 
57 public class AccessibleList extends TestCase {
58     private static XTextDocument xTextDoc = null;
59     private static XAccessibleAction action = null;
60     private static XMultiServiceFactory msf = null;
61 
62     /**
63      * Opens 'Insert Hyperlink' dialog using document dispatch provider.
64      * Finds active top window (the dialog
65      * window) and finds button 'Close' (for closing this dialog when
66      * disposing) walking through the accessible component tree.
67      * Then the TREE component is found and the 'New Document' tab is
68      * selected to make list box visible. After that list box is obtained.
69      */
createTestEnvironment(TestParameters Param, PrintWriter log)70     protected TestEnvironment createTestEnvironment(TestParameters Param,
71                                                     PrintWriter log) {
72         XInterface oObj = null;
73 
74         try {
75             oObj = (XInterface) msf.createInstance("com.sun.star.awt.Toolkit");
76         } catch (com.sun.star.uno.Exception e) {
77             log.println("Couldn't get toolkit");
78             e.printStackTrace(log);
79             throw new StatusException("Couldn't get toolkit", e);
80         }
81 
82         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
83                                       XExtendedToolkit.class, oObj);
84 
85         shortWait();
86 
87         XModel aModel1 = (XModel) UnoRuntime.queryInterface(XModel.class,
88                                                             xTextDoc);
89 
90         XController secondController = aModel1.getCurrentController();
91 
92         XDispatchProvider aProv = (XDispatchProvider) UnoRuntime.queryInterface(
93                                           XDispatchProvider.class,
94                                           secondController);
95 
96         XURLTransformer urlTransf = null;
97 
98         try {
99             XInterface transf = (XInterface) msf.createInstance(
100                                         "com.sun.star.util.URLTransformer");
101             urlTransf = (XURLTransformer) UnoRuntime.queryInterface(
102                                 XURLTransformer.class, transf);
103         } catch (com.sun.star.uno.Exception e) {
104             e.printStackTrace(log);
105             throw new StatusException("Couldn't create URLTransformer", e);
106         }
107 
108         XDispatch getting = null;
109         log.println("opening HyperlinkDialog");
110 
111         URL[] url = new URL[1];
112         url[0] = new URL();
113         url[0].Complete = ".uno:HyperlinkDialog";
114         urlTransf.parseStrict(url);
115         getting = aProv.queryDispatch(url[0], "", 0);
116 
117         PropertyValue[] noArgs = new PropertyValue[0];
118         getting.dispatch(url[0], noArgs);
119 
120         shortWait();
121 
122         AccessibilityTools at = new AccessibilityTools();
123 
124         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
125                                                               tk.getActiveTopWindow());
126 
127         XAccessible xRoot = at.getAccessibleObject(xWindow);
128 
129 
130         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
131         // obtaining 'Close' button
132         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PUSH_BUTTON,
133                                              "Close");
134         action = (XAccessibleAction) UnoRuntime.queryInterface(
135                          XAccessibleAction.class, oObj);
136 
137         // Selecting 'New Document' tab
138         try {
139             oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.TREE);
140 
141             XAccessibleSelection xAccSel = (XAccessibleSelection) UnoRuntime.queryInterface(
142                                                    XAccessibleSelection.class,
143                                                    oObj);
144             xAccSel.selectAccessibleChild(3);
145             shortWait();
146         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
147             throw new StatusException("Can't switch to required tab", e);
148         }
149 
150         log.println("# Getting the ListBox");
151 
152         XAccessibleContext parent = at.getAccessibleObjectForRole(xRoot,
153                                                                   AccessibleRole.PANEL,
154                                                                   "",
155                                                                   "com.sun.star.comp.toolkit.AccessibleListBox");
156 
157         log.println("# Getting the first child");
158 
159         try {
160             oObj = parent.getAccessibleChild(0);
161         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
162         }
163 
164         log.println("ImplementationName " + utils.getImplName(oObj));
165 
166         TestEnvironment tEnv = new TestEnvironment(oObj);
167 
168         tEnv.addObjRelation("XAccessibleSelection.multiSelection",
169                             new Boolean(false));
170 
171         final XAccessibleComponent acomp = (XAccessibleComponent) UnoRuntime.queryInterface(
172                                                    XAccessibleComponent.class,
173                                                    oObj);
174         final XAccessibleComponent acomp1 = (XAccessibleComponent) UnoRuntime.queryInterface(
175                                                     XAccessibleComponent.class,
176                                                     action);
177 
178         tEnv.addObjRelation("EventProducer",
179                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
180             public void fireEvent() {
181                 System.out.println("Grabbing focus ... ");
182                 acomp1.grabFocus();
183                 acomp.grabFocus();
184             }
185         });
186 
187         return tEnv;
188     }
189 
190     /**
191      * Closes dialog using action of button 'Close'
192      */
cleanup(TestParameters Param, PrintWriter log)193     protected void cleanup(TestParameters Param, PrintWriter log) {
194         log.println("    Closing dialog ... ");
195 
196         try {
197             action.doAccessibleAction(0);
198         } catch (com.sun.star.lang.IndexOutOfBoundsException ioe) {
199             log.println("Couldn't close dialog");
200         } catch (com.sun.star.lang.DisposedException de) {
201             log.println("Dialog already disposed");
202         }
203 
204         util.DesktopTools.closeDoc(xTextDoc);
205     }
206 
207     /**
208      * Creates writer document
209      */
initialize(TestParameters Param, PrintWriter log)210     protected void initialize(TestParameters Param, PrintWriter log) {
211         try {
212             msf = (XMultiServiceFactory) Param.getMSF();
213 
214             SOfficeFactory SOF = SOfficeFactory.getFactory(msf);
215             xTextDoc = SOF.createTextDoc(null);
216         } catch (com.sun.star.uno.Exception e) {
217             throw new StatusException("Can't create document", e);
218         }
219     }
220 
221     /**
222     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
223     * reset</code> call.
224     */
shortWait()225     private void shortWait() {
226         try {
227             Thread.sleep(1000);
228         } catch (InterruptedException e) {
229             log.println("While waiting :" + e);
230         }
231     }
232 }
233