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