1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package mod._toolkit;
28 
29 import com.sun.star.accessibility.AccessibleRole;
30 import com.sun.star.accessibility.XAccessible;
31 import com.sun.star.accessibility.XAccessibleAction;
32 import com.sun.star.accessibility.XAccessibleComponent;
33 import com.sun.star.accessibility.XAccessibleText;
34 import com.sun.star.awt.XExtendedToolkit;
35 import com.sun.star.awt.XWindow;
36 import com.sun.star.frame.XController;
37 import com.sun.star.frame.XDispatch;
38 import com.sun.star.frame.XDispatchProvider;
39 import com.sun.star.frame.XModel;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.text.XTextDocument;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 import com.sun.star.util.URL;
45 import com.sun.star.util.XURLTransformer;
46 
47 import java.io.PrintWriter;
48 
49 import lib.StatusException;
50 import lib.TestEnvironment;
51 import lib.TestParameters;
52 
53 import util.AccessibilityTools;
54 import util.SOfficeFactory;
55 import util.utils;
56 
57 
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::XAccessibleValue</code></li>
65  *  <li> <code>::com::sun::star::accessibility::XAccessibleAction</code></li>
66  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code></li>
67  *  <li> <code>::com::sun::star::accessibility::XAccessibleText</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.XAccessibleValue
74  * @see com.sun.star.accessibility.XAccessibleAction
75  * @see com.sun.star.accessibility.XAccessibleContext
76  * @see com.sun.star.accessibility.XAccessibleText
77  * @see ifc.accessibility._XAccessibleExtendedComponent
78  * @see ifc.accessibility._XAccessibleEventBroadcaster
79  * @see ifc.accessibility._XAccessibleComponent
80  * @see ifc.accessibility._XAccessibleValue
81  * @see ifc.accessibility._XAccessibleAction
82  * @see ifc.accessibility._XAccessibleContext
83  * @see ifc.accessibility._XAccessibleText
84  */
85 public class AccessibleButton extends lib.TestCase {
86     private static XTextDocument xTextDoc = null;
87     private static XAccessibleAction action = null;
88 
89     /**
90      * Opens 'Printer Settings' dialog using document dispatch provider
91      * running in a separate thread. Finds active top window (the dialog
92      * window) and finds accessible button 'OK' walking through the
93      * accessible component tree.
94      */
95     protected TestEnvironment createTestEnvironment(TestParameters Param,
96                                                     PrintWriter log) {
97         XInterface oObj = null;
98         XMultiServiceFactory msf = (XMultiServiceFactory) Param.getMSF();
99 
100         try {
101             oObj = (XInterface) msf.createInstance("com.sun.star.awt.Toolkit");
102         } catch (com.sun.star.uno.Exception e) {
103             log.println("Couldn't get toolkit");
104             e.printStackTrace(log);
105             throw new StatusException("Couldn't get toolkit", e);
106         }
107 
108         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
109                                       XExtendedToolkit.class, oObj);
110 
111         util.utils.shortWait(Param.getInt("ShortWait"));
112 
113         DiagThread psDiag = new DiagThread(xTextDoc, msf);
114         psDiag.start();
115 
116         util.utils.shortWait(Param.getInt("ShortWait"));
117 
118         AccessibilityTools at = new AccessibilityTools();
119 
120         util.utils.shortWait(Param.getInt("ShortWait"));
121 
122         Object atw = tk.getActiveTopWindow();
123 
124         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
125                                                               atw);
126 
127         XAccessible xRoot = at.getAccessibleObject(xWindow);
128 
129 
130         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
131         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PUSH_BUTTON,
132                                              "Cancel");
133 
134         log.println("ImplementationName " + utils.getImplName(oObj));
135 
136         TestEnvironment tEnv = new TestEnvironment(oObj);
137 
138         action = (XAccessibleAction) UnoRuntime.queryInterface(
139                          XAccessibleAction.class, oObj);
140 
141         final XAccessibleComponent acomp = (XAccessibleComponent) UnoRuntime.queryInterface(
142                                                    XAccessibleComponent.class,
143                                                    oObj);
144 
145         tEnv.addObjRelation("EventProducer",
146                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
147             public void fireEvent() {
148                 System.out.println("Grabbing focus ... ");
149                 acomp.grabFocus();
150             }
151         });
152 
153         XAccessibleText text = (XAccessibleText) UnoRuntime.queryInterface(
154                                        XAccessibleText.class, oObj);
155 
156         tEnv.addObjRelation("XAccessibleText.Text", text.getText());
157 
158         tEnv.addObjRelation("EditOnly",
159                             "This method isn't supported in this component");
160 
161         tEnv.addObjRelation("LimitedBounds", "yes");
162 
163         return tEnv;
164     }
165 
166     /**
167      * Closes the dialog using accessible button 'OK' found in
168      * <code>createTestEnvironment()</code>.
169      */
170     protected void cleanup(TestParameters Param, PrintWriter log) {
171         log.println("    disposing xTextDoc ");
172 
173         try {
174             action.doAccessibleAction(0);
175         } catch (com.sun.star.lang.IndexOutOfBoundsException ioe) {
176             log.println("Couldn't close dialog");
177         } catch (com.sun.star.lang.DisposedException de) {
178             log.println("Dialog already disposed");
179         }
180 
181         util.DesktopTools.closeDoc(xTextDoc);
182         ;
183     }
184 
185     /**
186      * Opens new writer document.
187      */
188     protected void initialize(TestParameters Param, PrintWriter log) {
189         try {
190             SOfficeFactory SOF = SOfficeFactory.getFactory(
191                                          (XMultiServiceFactory) Param.getMSF());
192             xTextDoc = SOF.createTextDoc(null);
193         } catch (com.sun.star.uno.Exception e) {
194             throw new StatusException("Can't create document", e);
195         }
196     }
197 
198 
199     /**
200      * Thread for opening modal dialog 'Print Settings'.
201      */
202     public class DiagThread extends Thread {
203         public XTextDocument xTextDoc = null;
204         public XMultiServiceFactory msf = null;
205 
206         public DiagThread(XTextDocument xTextDoc, XMultiServiceFactory msf) {
207             this.xTextDoc = xTextDoc;
208             this.msf = msf;
209         }
210 
211         public void run() {
212             XModel aModel = (XModel) UnoRuntime.queryInterface(XModel.class,
213                                                                xTextDoc);
214 
215             XController xController = aModel.getCurrentController();
216 
217             //Opening PrinterSetupDialog
218             try {
219                 String aSlotID = ".uno:Zoom";
220                 XDispatchProvider xDispProv = (XDispatchProvider) UnoRuntime.queryInterface(
221                                                       XDispatchProvider.class,
222                                                       xController);
223                 XURLTransformer xParser = (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
224                                                   XURLTransformer.class,
225                                                   msf.createInstance(
226                                                           "com.sun.star.util.URLTransformer"));
227 
228                 // Because it's an in/out parameter
229                 // we must use an array of URL objects.
230                 URL[] aParseURL = new URL[1];
231                 aParseURL[0] = new URL();
232                 aParseURL[0].Complete = aSlotID;
233                 xParser.parseStrict(aParseURL);
234 
235                 URL aURL = aParseURL[0];
236                 XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
237 
238                 if (xDispatcher != null) {
239                     xDispatcher.dispatch(aURL, null);
240                 }
241             } catch (com.sun.star.uno.Exception e) {
242                 log.println("Couldn't open dialog");
243             }
244         }
245     }
246 }