1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package mod._toolkit;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRole;
26cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
27cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleAction;
28cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleComponent;
29cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleText;
30cdf0e10cSrcweir import com.sun.star.awt.XExtendedToolkit;
31cdf0e10cSrcweir import com.sun.star.awt.XWindow;
32cdf0e10cSrcweir import com.sun.star.frame.XController;
33cdf0e10cSrcweir import com.sun.star.frame.XDispatch;
34cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider;
35cdf0e10cSrcweir import com.sun.star.frame.XModel;
36cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
37cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
38cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
39cdf0e10cSrcweir import com.sun.star.uno.XInterface;
40cdf0e10cSrcweir import com.sun.star.util.URL;
41cdf0e10cSrcweir import com.sun.star.util.XURLTransformer;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir import java.io.PrintWriter;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir import lib.StatusException;
46cdf0e10cSrcweir import lib.TestEnvironment;
47cdf0e10cSrcweir import lib.TestParameters;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir import util.AccessibilityTools;
50cdf0e10cSrcweir import util.SOfficeFactory;
51cdf0e10cSrcweir import util.utils;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
54cdf0e10cSrcweir /**
55cdf0e10cSrcweir  * Object implements the following interfaces :
56cdf0e10cSrcweir  * <ul>
57cdf0e10cSrcweir  *  <li> <code>::com::sun::star::accessibility::XAccessibleExtendedComponent</code></li>
58cdf0e10cSrcweir  *  <li> <code>::com::sun::star::accessibility::XAccessibleEventBroadcaster</code></li>
59cdf0e10cSrcweir  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code></li>
60cdf0e10cSrcweir  *  <li> <code>::com::sun::star::accessibility::XAccessibleValue</code></li>
61cdf0e10cSrcweir  *  <li> <code>::com::sun::star::accessibility::XAccessibleAction</code></li>
62cdf0e10cSrcweir  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code></li>
63cdf0e10cSrcweir  *  <li> <code>::com::sun::star::accessibility::XAccessibleText</code></li>
64cdf0e10cSrcweir  * </ul> <p>
65cdf0e10cSrcweir  *
66cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleExtendedComponent
67cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
68cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleComponent
69cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleValue
70cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleAction
71cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleContext
72cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleText
73cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleExtendedComponent
74cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleEventBroadcaster
75cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleComponent
76cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleValue
77cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleAction
78cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleContext
79cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleText
80cdf0e10cSrcweir  */
81cdf0e10cSrcweir public class AccessibleButton extends lib.TestCase {
82cdf0e10cSrcweir     private static XTextDocument xTextDoc = null;
83cdf0e10cSrcweir     private static XAccessibleAction action = null;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     /**
86cdf0e10cSrcweir      * Opens 'Printer Settings' dialog using document dispatch provider
87cdf0e10cSrcweir      * running in a separate thread. Finds active top window (the dialog
88cdf0e10cSrcweir      * window) and finds accessible button 'OK' walking through the
89cdf0e10cSrcweir      * accessible component tree.
90cdf0e10cSrcweir      */
91cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters Param,
92cdf0e10cSrcweir                                                     PrintWriter log) {
93cdf0e10cSrcweir         XInterface oObj = null;
94cdf0e10cSrcweir         XMultiServiceFactory msf = (XMultiServiceFactory) Param.getMSF();
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         try {
97cdf0e10cSrcweir             oObj = (XInterface) msf.createInstance("com.sun.star.awt.Toolkit");
98cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
99cdf0e10cSrcweir             log.println("Couldn't get toolkit");
100cdf0e10cSrcweir             e.printStackTrace(log);
101cdf0e10cSrcweir             throw new StatusException("Couldn't get toolkit", e);
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
105cdf0e10cSrcweir                                       XExtendedToolkit.class, oObj);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         util.utils.shortWait(Param.getInt("ShortWait"));
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         DiagThread psDiag = new DiagThread(xTextDoc, msf);
110cdf0e10cSrcweir         psDiag.start();
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         util.utils.shortWait(Param.getInt("ShortWait"));
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         util.utils.shortWait(Param.getInt("ShortWait"));
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         Object atw = tk.getActiveTopWindow();
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
121cdf0e10cSrcweir                                                               atw);
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
127cdf0e10cSrcweir         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PUSH_BUTTON,
128cdf0e10cSrcweir                                              "Cancel");
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         log.println("ImplementationName " + utils.getImplName(oObj));
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(oObj);
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         action = (XAccessibleAction) UnoRuntime.queryInterface(
135cdf0e10cSrcweir                          XAccessibleAction.class, oObj);
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         final XAccessibleComponent acomp = (XAccessibleComponent) UnoRuntime.queryInterface(
138cdf0e10cSrcweir                                                    XAccessibleComponent.class,
139cdf0e10cSrcweir                                                    oObj);
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         tEnv.addObjRelation("EventProducer",
142cdf0e10cSrcweir                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
143cdf0e10cSrcweir             public void fireEvent() {
144cdf0e10cSrcweir                 System.out.println("Grabbing focus ... ");
145cdf0e10cSrcweir                 acomp.grabFocus();
146cdf0e10cSrcweir             }
147cdf0e10cSrcweir         });
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         XAccessibleText text = (XAccessibleText) UnoRuntime.queryInterface(
150cdf0e10cSrcweir                                        XAccessibleText.class, oObj);
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         tEnv.addObjRelation("XAccessibleText.Text", text.getText());
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         tEnv.addObjRelation("EditOnly",
155cdf0e10cSrcweir                             "This method isn't supported in this component");
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         tEnv.addObjRelation("LimitedBounds", "yes");
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         return tEnv;
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     /**
163cdf0e10cSrcweir      * Closes the dialog using accessible button 'OK' found in
164cdf0e10cSrcweir      * <code>createTestEnvironment()</code>.
165cdf0e10cSrcweir      */
166cdf0e10cSrcweir     protected void cleanup(TestParameters Param, PrintWriter log) {
167cdf0e10cSrcweir         log.println("    disposing xTextDoc ");
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         try {
170cdf0e10cSrcweir             action.doAccessibleAction(0);
171cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException ioe) {
172cdf0e10cSrcweir             log.println("Couldn't close dialog");
173cdf0e10cSrcweir         } catch (com.sun.star.lang.DisposedException de) {
174cdf0e10cSrcweir             log.println("Dialog already disposed");
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir         util.DesktopTools.closeDoc(xTextDoc);
178cdf0e10cSrcweir         ;
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     /**
182cdf0e10cSrcweir      * Opens new writer document.
183cdf0e10cSrcweir      */
184cdf0e10cSrcweir     protected void initialize(TestParameters Param, PrintWriter log) {
185cdf0e10cSrcweir         try {
186cdf0e10cSrcweir             SOfficeFactory SOF = SOfficeFactory.getFactory(
187cdf0e10cSrcweir                                          (XMultiServiceFactory) Param.getMSF());
188cdf0e10cSrcweir             xTextDoc = SOF.createTextDoc(null);
189cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
190cdf0e10cSrcweir             throw new StatusException("Can't create document", e);
191cdf0e10cSrcweir         }
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     /**
196cdf0e10cSrcweir      * Thread for opening modal dialog 'Print Settings'.
197cdf0e10cSrcweir      */
198cdf0e10cSrcweir     public class DiagThread extends Thread {
199cdf0e10cSrcweir         public XTextDocument xTextDoc = null;
200cdf0e10cSrcweir         public XMultiServiceFactory msf = null;
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         public DiagThread(XTextDocument xTextDoc, XMultiServiceFactory msf) {
203cdf0e10cSrcweir             this.xTextDoc = xTextDoc;
204cdf0e10cSrcweir             this.msf = msf;
205cdf0e10cSrcweir         }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir         public void run() {
208cdf0e10cSrcweir             XModel aModel = (XModel) UnoRuntime.queryInterface(XModel.class,
209cdf0e10cSrcweir                                                                xTextDoc);
210cdf0e10cSrcweir 
211cdf0e10cSrcweir             XController xController = aModel.getCurrentController();
212cdf0e10cSrcweir 
213cdf0e10cSrcweir             //Opening PrinterSetupDialog
214cdf0e10cSrcweir             try {
215cdf0e10cSrcweir                 String aSlotID = ".uno:Zoom";
216cdf0e10cSrcweir                 XDispatchProvider xDispProv = (XDispatchProvider) UnoRuntime.queryInterface(
217cdf0e10cSrcweir                                                       XDispatchProvider.class,
218cdf0e10cSrcweir                                                       xController);
219cdf0e10cSrcweir                 XURLTransformer xParser = (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
220cdf0e10cSrcweir                                                   XURLTransformer.class,
221cdf0e10cSrcweir                                                   msf.createInstance(
222cdf0e10cSrcweir                                                           "com.sun.star.util.URLTransformer"));
223cdf0e10cSrcweir 
224cdf0e10cSrcweir                 // Because it's an in/out parameter
225cdf0e10cSrcweir                 // we must use an array of URL objects.
226cdf0e10cSrcweir                 URL[] aParseURL = new URL[1];
227cdf0e10cSrcweir                 aParseURL[0] = new URL();
228cdf0e10cSrcweir                 aParseURL[0].Complete = aSlotID;
229cdf0e10cSrcweir                 xParser.parseStrict(aParseURL);
230cdf0e10cSrcweir 
231cdf0e10cSrcweir                 URL aURL = aParseURL[0];
232cdf0e10cSrcweir                 XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
233cdf0e10cSrcweir 
234cdf0e10cSrcweir                 if (xDispatcher != null) {
235cdf0e10cSrcweir                     xDispatcher.dispatch(aURL, null);
236cdf0e10cSrcweir                 }
237cdf0e10cSrcweir             } catch (com.sun.star.uno.Exception e) {
238cdf0e10cSrcweir                 log.println("Couldn't open dialog");
239cdf0e10cSrcweir             }
240cdf0e10cSrcweir         }
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir }