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