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