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