xref: /AOO41X/main/qadevOOo/tests/java/mod/_toolkit/AccessibleTabPage.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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.XAccessibleContext;
34*cdf0e10cSrcweir import com.sun.star.awt.Point;
35*cdf0e10cSrcweir import com.sun.star.awt.XExtendedToolkit;
36*cdf0e10cSrcweir import com.sun.star.awt.XWindow;
37*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
38*cdf0e10cSrcweir import com.sun.star.frame.XController;
39*cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
40*cdf0e10cSrcweir import com.sun.star.frame.XDispatch;
41*cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider;
42*cdf0e10cSrcweir import com.sun.star.frame.XModel;
43*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
44*cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
45*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
46*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
47*cdf0e10cSrcweir import com.sun.star.util.URL;
48*cdf0e10cSrcweir import com.sun.star.util.XURLTransformer;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir import java.io.PrintWriter;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir import lib.StatusException;
54*cdf0e10cSrcweir import lib.TestCase;
55*cdf0e10cSrcweir import lib.TestEnvironment;
56*cdf0e10cSrcweir import lib.TestParameters;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir import util.AccessibilityTools;
59*cdf0e10cSrcweir import util.DesktopTools;
60*cdf0e10cSrcweir import util.SOfficeFactory;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir /**
64*cdf0e10cSrcweir  * Test for object that implements the following interfaces :
65*cdf0e10cSrcweir  * <ul>
66*cdf0e10cSrcweir  *  <li><code>
67*cdf0e10cSrcweir  *  ::com::sun::star::accessibility::XAccessibleContext</code></li>
68*cdf0e10cSrcweir  *  <li><code>
69*cdf0e10cSrcweir  *  ::com::sun::star::accessibility::XAccessibleEventBroadcaster
70*cdf0e10cSrcweir  *  </code></li>
71*cdf0e10cSrcweir  *  <li><code>
72*cdf0e10cSrcweir  *  ::com::sun::star::accessibility::XAccessibleComponent</code></li>
73*cdf0e10cSrcweir  *  <li><code>
74*cdf0e10cSrcweir  *  ::com::sun::star::accessibility::XAccessibleExtendedComponent
75*cdf0e10cSrcweir  *  </code></li>
76*cdf0e10cSrcweir  * </ul> <p>
77*cdf0e10cSrcweir  *
78*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
79*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleContext
80*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleComponent
81*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleExtendedComponent
82*cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleEventBroadcaster
83*cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleContext
84*cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleComponent
85*cdf0e10cSrcweir  * @see ifc.accessibility.XAccessibleExtendedComponent
86*cdf0e10cSrcweir  */
87*cdf0e10cSrcweir public class AccessibleTabPage extends TestCase {
88*cdf0e10cSrcweir     private static XDesktop the_Desk;
89*cdf0e10cSrcweir     private static XTextDocument xTextDoc;
90*cdf0e10cSrcweir     private static XAccessibleAction accCloseButton = null;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     /**
93*cdf0e10cSrcweir      * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
94*cdf0e10cSrcweir      */
95*cdf0e10cSrcweir     protected void initialize(TestParameters Param, PrintWriter log) {
96*cdf0e10cSrcweir         the_Desk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,
97*cdf0e10cSrcweir                                                         DesktopTools.createDesktop(
98*cdf0e10cSrcweir                                                                 (XMultiServiceFactory) Param.getMSF()));
99*cdf0e10cSrcweir     }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     /**
102*cdf0e10cSrcweir      * Closes an InsertFields dialog, disposes the document, if exists, created in
103*cdf0e10cSrcweir      * <code>createTestEnvironment</code> method.
104*cdf0e10cSrcweir      */
105*cdf0e10cSrcweir     protected void cleanup(TestParameters Param, PrintWriter log) {
106*cdf0e10cSrcweir         try {
107*cdf0e10cSrcweir             if (accCloseButton != null) {
108*cdf0e10cSrcweir                 log.println("closing InsertFields Dialog");
109*cdf0e10cSrcweir                 accCloseButton.doAccessibleAction(0);
110*cdf0e10cSrcweir                 shortWait();
111*cdf0e10cSrcweir             }
112*cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
113*cdf0e10cSrcweir             e.printStackTrace(log);
114*cdf0e10cSrcweir         } catch (com.sun.star.lang.DisposedException e) {
115*cdf0e10cSrcweir             log.println("Couldn't close dialog " + e.getMessage());
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         log.println("disposing xTextDoc");
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         if (xTextDoc != null) {
121*cdf0e10cSrcweir             closeDoc();
122*cdf0e10cSrcweir         }
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     /**
126*cdf0e10cSrcweir      * Creates a text document, opens an InsertField dialog.
127*cdf0e10cSrcweir      * Then obtains an accessible object with
128*cdf0e10cSrcweir      * the role <code>AccessibleRole.PAGETAB</code>.
129*cdf0e10cSrcweir      * Object relations created :
130*cdf0e10cSrcweir      * <ul>
131*cdf0e10cSrcweir      *  <li> <code>'EventProducer'</code> for
132*cdf0e10cSrcweir      *      {@link ifc.accessibility._XAccessibleEventBroadcaster}:
133*cdf0e10cSrcweir      *      grabs focus </li>
134*cdf0e10cSrcweir      * </ul>
135*cdf0e10cSrcweir      *
136*cdf0e10cSrcweir      * @param tParam test parameters
137*cdf0e10cSrcweir      * @param log writer to log information while testing
138*cdf0e10cSrcweir      *
139*cdf0e10cSrcweir      * @see com.sun.star.awt.Toolkit
140*cdf0e10cSrcweir      * @see com.sun.star.accessibility.AccessibleRole
141*cdf0e10cSrcweir      * @see ifc.accessibility._XAccessibleEventBroadcaster
142*cdf0e10cSrcweir      * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
143*cdf0e10cSrcweir      */
144*cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters tParam,
145*cdf0e10cSrcweir                                                     PrintWriter log) {
146*cdf0e10cSrcweir         log.println("creating a test environment");
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir         try {
149*cdf0e10cSrcweir             if (accCloseButton != null) {
150*cdf0e10cSrcweir                 log.println("closing InsertFields Dialog");
151*cdf0e10cSrcweir                 accCloseButton.doAccessibleAction(0);
152*cdf0e10cSrcweir                 shortWait();
153*cdf0e10cSrcweir             }
154*cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
155*cdf0e10cSrcweir             e.printStackTrace(log);
156*cdf0e10cSrcweir         } catch (com.sun.star.lang.DisposedException e) {
157*cdf0e10cSrcweir             log.println("Couldn't close dialog " + e.getMessage());
158*cdf0e10cSrcweir         }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         log.println("disposing xTextDoc");
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         if (xTextDoc != null) {
163*cdf0e10cSrcweir             closeDoc();
164*cdf0e10cSrcweir         }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir         // get a soffice factory object
167*cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(
168*cdf0e10cSrcweir                                      (XMultiServiceFactory) tParam.getMSF());
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         try {
171*cdf0e10cSrcweir             log.println("creating a text document");
172*cdf0e10cSrcweir             xTextDoc = SOF.createTextDoc(null);
173*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
174*cdf0e10cSrcweir             // Some exception occures.FAILED
175*cdf0e10cSrcweir             e.printStackTrace(log);
176*cdf0e10cSrcweir             throw new StatusException("Couldn't create document", e);
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir         shortWait();
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir         XModel aModel1 = (XModel) UnoRuntime.queryInterface(XModel.class,
182*cdf0e10cSrcweir                                                             xTextDoc);
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir         XController secondController = aModel1.getCurrentController();
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         XDispatchProvider aProv = (XDispatchProvider) UnoRuntime.queryInterface(
187*cdf0e10cSrcweir                                           XDispatchProvider.class,
188*cdf0e10cSrcweir                                           secondController);
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         XURLTransformer urlTransf = null;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir         try {
193*cdf0e10cSrcweir             XInterface transf = (XInterface) ((XMultiServiceFactory) tParam.getMSF()).createInstance(
194*cdf0e10cSrcweir                                         "com.sun.star.util.URLTransformer");
195*cdf0e10cSrcweir             urlTransf = (XURLTransformer) UnoRuntime.queryInterface(
196*cdf0e10cSrcweir                                 XURLTransformer.class, transf);
197*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
198*cdf0e10cSrcweir             e.printStackTrace(log);
199*cdf0e10cSrcweir             throw new StatusException("Couldn't create URLTransformer", e);
200*cdf0e10cSrcweir         }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir         XDispatch getting = null;
203*cdf0e10cSrcweir         log.println("opening InsertField dialog");
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir         URL[] url = new URL[1];
206*cdf0e10cSrcweir         url[0] = new URL();
207*cdf0e10cSrcweir         url[0].Complete = ".uno:InsertField";
208*cdf0e10cSrcweir         urlTransf.parseStrict(url);
209*cdf0e10cSrcweir         getting = aProv.queryDispatch(url[0], "", 0);
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir         PropertyValue[] noArgs = new PropertyValue[0];
212*cdf0e10cSrcweir         getting.dispatch(url[0], noArgs);
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir         shortWait();
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         XInterface oObj = null;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir         try {
219*cdf0e10cSrcweir             oObj = (XInterface) ((XMultiServiceFactory) tParam.getMSF()).createInstance(
220*cdf0e10cSrcweir                            "com.sun.star.awt.Toolkit");
221*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
222*cdf0e10cSrcweir             log.println("Couldn't get toolkit");
223*cdf0e10cSrcweir             e.printStackTrace(log);
224*cdf0e10cSrcweir             throw new StatusException("Couldn't get toolkit", e);
225*cdf0e10cSrcweir         }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
228*cdf0e10cSrcweir                                       XExtendedToolkit.class, oObj);
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir         shortWait();
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
235*cdf0e10cSrcweir                                                               tk.getActiveTopWindow());
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir         XAccessibleContext closeButton = at.getAccessibleObjectForRole(xRoot,
240*cdf0e10cSrcweir                                                                        AccessibleRole.PUSH_BUTTON,
241*cdf0e10cSrcweir                                                                        "Close");
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir         accCloseButton = (XAccessibleAction) UnoRuntime.queryInterface(
244*cdf0e10cSrcweir                                  XAccessibleAction.class, closeButton);
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PAGE_TAB);
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir         log.println("ImplementationName: " + util.utils.getImplName(oObj));
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(oObj);
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir         tEnv.addObjRelation("EditOnly", "toolkit.AccessibleTabPage");
253*cdf0e10cSrcweir         tEnv.addObjRelation("LimitedBounds", "toolkit.AccessibleTabPage");
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir         XAccessibleComponent accComp = (XAccessibleComponent) UnoRuntime.queryInterface(
256*cdf0e10cSrcweir                                                XAccessibleComponent.class,
257*cdf0e10cSrcweir                                                oObj);
258*cdf0e10cSrcweir         final Point point = accComp.getLocationOnScreen();
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir         shortWait();
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         XInterface xEventInt = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PAGE_TAB, "Variables");
263*cdf0e10cSrcweir         final XAccessibleComponent eventAccComp = (XAccessibleComponent) UnoRuntime.queryInterface(
264*cdf0e10cSrcweir                                                XAccessibleComponent.class,
265*cdf0e10cSrcweir                                                xEventInt);
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir         tEnv.addObjRelation("EventProducer",
268*cdf0e10cSrcweir                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
269*cdf0e10cSrcweir             public void fireEvent() {
270*cdf0e10cSrcweir                eventAccComp.grabFocus();
271*cdf0e10cSrcweir             }
272*cdf0e10cSrcweir         });
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir         return tEnv;
275*cdf0e10cSrcweir     }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir     /**
278*cdf0e10cSrcweir     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
279*cdf0e10cSrcweir     * reset</code> call.
280*cdf0e10cSrcweir     */
281*cdf0e10cSrcweir     private void shortWait() {
282*cdf0e10cSrcweir         try {
283*cdf0e10cSrcweir             Thread.currentThread().sleep(1000);
284*cdf0e10cSrcweir         } catch (InterruptedException e) {
285*cdf0e10cSrcweir             System.out.println("While waiting :" + e);
286*cdf0e10cSrcweir         }
287*cdf0e10cSrcweir     }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir     protected void closeDoc() {
290*cdf0e10cSrcweir         util.DesktopTools.closeDoc(xTextDoc);
291*cdf0e10cSrcweir     }
292*cdf0e10cSrcweir }
293