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.frame.XController;
33 import com.sun.star.frame.XDispatch;
34 import com.sun.star.frame.XDispatchProvider;
35 import com.sun.star.frame.XModel;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.text.XTextDocument;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 import com.sun.star.util.URL;
41 import com.sun.star.util.XURLTransformer;
42 
43 import java.io.PrintWriter;
44 
45 import lib.StatusException;
46 import lib.TestCase;
47 import lib.TestEnvironment;
48 import lib.TestParameters;
49 
50 import util.AccessibilityTools;
51 import util.SOfficeFactory;
52 import util.utils;
53 
54 
55 /**
56  * Test for object which is represented by accessible component
57  * of the 'Font' combo box in Format->Character... dislog box. <p>
58  *
59  * Object implements the following interfaces :
60  * <ul>
61  *  <li> <code>::com::sun::star::accessibility::XAccessibleExtendedComponent</code></li>
62  *  <li> <code>::com::sun::star::accessibility::XAccessibleEventBroadcaster</code></li>
63  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code></li>
64  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code></li>
65  * </ul> <p>
66  *
67  * @see com.sun.star.accessibility.XAccessibleExtendedComponent
68  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
69  * @see com.sun.star.accessibility.XAccessibleComponent
70  * @see com.sun.star.accessibility.XAccessibleContext
71  * @see ifc.accessibility._XAccessibleExtendedComponent
72  * @see ifc.accessibility._XAccessibleEventBroadcaster
73  * @see ifc.accessibility._XAccessibleComponent
74  * @see ifc.accessibility._XAccessibleContext
75  */
76 public class AccessibleComboBox extends TestCase {
77     private static XTextDocument xTextDoc = null;
78     private static XAccessibleAction action = null;
79 
80     /**
81      * Opens CharacterStyle dialog and
82      * finds AccessibleComboBox walking through the
83      * accessible component tree.
84      */
createTestEnvironment(TestParameters Param, PrintWriter log)85     protected TestEnvironment createTestEnvironment(TestParameters Param,
86                                                     PrintWriter log) {
87         XInterface oObj = null;
88 
89         try {
90             oObj = (XInterface) ((XMultiServiceFactory) Param.getMSF()).createInstance(
91                            "com.sun.star.awt.Toolkit");
92         } catch (com.sun.star.uno.Exception e) {
93             log.println("Couldn't get toolkit");
94             e.printStackTrace(log);
95             throw new StatusException("Couldn't get toolkit", e);
96         }
97 
98         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
99                                       XExtendedToolkit.class, oObj);
100 
101         DiagThread psDiag = new DiagThread(xTextDoc,
102                                            (XMultiServiceFactory) Param.getMSF());
103         psDiag.start();
104 
105         util.utils.shortWait(Param.getInt("ShortWait"));
106 
107         AccessibilityTools at = new AccessibilityTools();
108 
109         util.utils.shortWait(Param.getInt("ShortWait"));
110 
111         Object atw = tk.getActiveTopWindow();
112 
113         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
114                                                               atw);
115 
116         XAccessible xRoot = at.getAccessibleObject(xWindow);
117 
118         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PUSH_BUTTON,
119                                              "Cancel");
120         action = (XAccessibleAction) UnoRuntime.queryInterface(
121                          XAccessibleAction.class, oObj);
122 
123         oObj = at.getAccessibleObjectForRole(xRoot,
124                                              AccessibleRole.PAGE_TAB_LIST);
125 
126         XAccessibleSelection xAccSel = (XAccessibleSelection) UnoRuntime.queryInterface(
127                                                XAccessibleSelection.class,
128                                                oObj);
129 
130         try {
131             xAccSel.selectAccessibleChild(0);
132         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
133             e.printStackTrace(log);
134         }
135         util.utils.shortWait(Param.getInt("ShortWait"));
136 
137         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
138         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL, "",
139                                              "AccessibleComboBox");
140 
141         log.println("ImplementationName " + utils.getImplName(oObj));
142 
143         TestEnvironment tEnv = new TestEnvironment(oObj);
144 
145         final XAccessibleComponent acomp = (XAccessibleComponent) UnoRuntime.queryInterface(
146                                                    XAccessibleComponent.class,
147                                                    oObj);
148         final XAccessibleComponent acomp1 = (XAccessibleComponent) UnoRuntime.queryInterface(
149                                                     XAccessibleComponent.class,
150                                                     action);
151 
152         tEnv.addObjRelation("EventProducer",
153                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
154             public void fireEvent() {
155                 acomp1.grabFocus();
156                 acomp.grabFocus();
157             }
158         });
159 
160         return tEnv;
161     }
162 
163     /**
164      * Closes the dialog using accessible button 'Cancel' found in
165      * <code>createTestEnvironment()</code>.
166      */
cleanup(TestParameters Param, PrintWriter log)167     protected void cleanup(TestParameters Param, PrintWriter log) {
168         log.println("    closing dialog ");
169 
170         try {
171             action.doAccessibleAction(0);
172         } catch (com.sun.star.lang.IndexOutOfBoundsException ioe) {
173             log.println("Couldn't close dialog");
174         } catch (com.sun.star.lang.DisposedException de) {
175             log.println("Dialog already disposed");
176         }
177 
178         util.DesktopTools.closeDoc(xTextDoc);
179     }
180 
181     /**
182      * Creates writer doc
183      */
initialize(TestParameters Param, PrintWriter log)184     protected void initialize(TestParameters Param, PrintWriter log) {
185         try {
186             SOfficeFactory SOF = SOfficeFactory.getFactory(
187                                          (XMultiServiceFactory) Param.getMSF());
188             xTextDoc = SOF.createTextDoc(null);
189         } catch (com.sun.star.uno.Exception e) {
190             throw new StatusException("Can't create document", e);
191         }
192     }
193 
194 
195     /**
196      * Thread for opening modal dialog 'Character style'.
197      */
198     public class DiagThread extends Thread {
199         public XTextDocument xTextDoc = null;
200         public XMultiServiceFactory msf = null;
201 
DiagThread(XTextDocument xTextDoc, XMultiServiceFactory msf)202         public DiagThread(XTextDocument xTextDoc, XMultiServiceFactory msf) {
203             this.xTextDoc = xTextDoc;
204             this.msf = msf;
205         }
206 
run()207         public void run() {
208             XModel aModel = (XModel) UnoRuntime.queryInterface(XModel.class,
209                                                                xTextDoc);
210 
211             XController xController = aModel.getCurrentController();
212 
213             //Opening PrinterSetupDialog
214             try {
215                 String aSlotID = ".uno:FontDialog";
216                 XDispatchProvider xDispProv = (XDispatchProvider) UnoRuntime.queryInterface(
217                                                       XDispatchProvider.class,
218                                                       xController);
219                 XURLTransformer xParser = (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
220                                                   XURLTransformer.class,
221                                                   msf.createInstance(
222                                                           "com.sun.star.util.URLTransformer"));
223 
224                 // Because it's an in/out parameter we must use an array of URL objects.
225                 URL[] aParseURL = new URL[1];
226                 aParseURL[0] = new URL();
227                 aParseURL[0].Complete = aSlotID;
228                 xParser.parseStrict(aParseURL);
229 
230                 URL aURL = aParseURL[0];
231                 XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
232 
233                 if (xDispatcher != null) {
234                     xDispatcher.dispatch(aURL, null);
235                 }
236             } catch (com.sun.star.uno.Exception e) {
237                 log.println("Couldn't open dialog");
238             }
239         }
240     }
241 }
242