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