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.awt.XWindow;
43 import com.sun.star.beans.PropertyValue;
44 import com.sun.star.frame.XController;
45 import com.sun.star.frame.XDesktop;
46 import com.sun.star.frame.XDispatch;
47 import com.sun.star.frame.XDispatchProvider;
48 import com.sun.star.frame.XFrame;
49 import com.sun.star.frame.XModel;
50 import com.sun.star.lang.XInitialization;
51 import com.sun.star.lang.XMultiServiceFactory;
52 import com.sun.star.text.XTextDocument;
53 import com.sun.star.uno.UnoRuntime;
54 import com.sun.star.uno.XInterface;
55 import com.sun.star.util.URL;
56 
57 /**
58  * Test for object that implements the following interfaces :
59  * <ul>
60  *  <li>
61  *      <code>::com::sun::star::accessibility::XAccessibleContext
62  *  </code></li>
63  *  <li>
64  *      <code>::com::sun::star::accessibility::XAccessibleEventBroadcaster
65  *  </code></li>
66  * </ul> <p>
67  *
68  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
69  * @see com.sun.star.accessibility.XAccessibleContext
70  * @see ifc.accessibility._XAccessibleEventBroadcaster
71  * @see ifc.accessibility._XAccessibleContext
72  */
73 public class AccessibleBrowseBoxTableCell extends TestCase {
74 
75     static XDesktop the_Desk;
76     static XTextDocument xTextDoc;
77 
78     /**
79      * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
80      */
81     protected void initialize(TestParameters Param, PrintWriter log) {
82         the_Desk = (XDesktop) UnoRuntime.queryInterface(
83                     XDesktop.class, DesktopTools.createDesktop((XMultiServiceFactory)Param.getMSF()) );
84     }
85 
86     /**
87      * Disposes the document, if exists, created in
88      * <code>createTestEnvironment</code> method.
89      */
90     protected void cleanup( TestParameters Param, PrintWriter log) {
91 
92         log.println("disposing xTextDoc");
93 
94         if (xTextDoc != null) {
95             xTextDoc.dispose();
96         }
97     }
98 
99     /**
100      * Creates a text document. Opens the DataSource browser and loads
101      * the table. Creates an instance of the service
102      * <code>com.sun.star.awt.Toolkit</code> and gets active top window.
103      * Then obtains an accessible object with the role
104      * <code>AccessibleRole.TABLE_CELL</code>.
105      * Object relations created :
106      * <ul>
107      *  <li> <code>'EventProducer'</code> for
108      *      {@link ifc.accessibility._XAccessibleEventBroadcaster}:
109      *      method <code>fireEvent()</code> is empty because object is transient
110      *   </li>
111      * </ul>
112      *
113      * @param tParam test parameters
114      * @param log writer to log information while testing
115      *
116      * @see com.sun.star.awt.Toolkit
117      * @see com.sun.star.accessibility.AccessibleRole
118      * @see ifc.accessibility._XAccessibleEventBroadcaster
119      * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
120      */
121     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
122 
123         log.println( "creating a test environment" );
124 
125         if (xTextDoc != null) xTextDoc.dispose();
126 
127         // get a soffice factory object
128         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
129 
130         try {
131             log.println( "creating a text document" );
132             xTextDoc = SOF.createTextDoc(null);
133         } catch ( com.sun.star.uno.Exception e ) {
134             // Some exception occures.FAILED
135             e.printStackTrace( log );
136             throw new StatusException( "Couldn't create document", e );
137         }
138 
139         shortWait();
140 
141         XModel aModel1 = (XModel)
142                     UnoRuntime.queryInterface(XModel.class, xTextDoc);
143 
144         XController secondController = aModel1.getCurrentController();
145 
146 
147         XDispatchProvider aProv = (XDispatchProvider)
148             UnoRuntime.queryInterface(XDispatchProvider.class,secondController);
149 
150         XDispatch getting = null;
151 
152         log.println( "opening DatasourceBrowser" );
153         URL the_url = new URL();
154         the_url.Complete = ".component:DB/DataSourceBrowser";
155         getting = aProv.queryDispatch(the_url,"_beamer",12);
156         PropertyValue[] noArgs = new PropertyValue[0];
157         getting.dispatch(the_url,noArgs);
158 
159         shortWait();
160 
161         XFrame the_frame1 = the_Desk.getCurrentFrame();
162 
163         if (the_frame1 == null) {
164             log.println("Current frame was not found !!!");
165         }
166 
167         XFrame the_frame2 = the_frame1.findFrame("_beamer",4);
168 
169         the_frame2.setName("DatasourceBrowser");
170 
171         XInterface oObj = null;
172 
173         final XInitialization xInit = (XInitialization)
174                 UnoRuntime.queryInterface(
175                         XInitialization.class, the_frame2.getController());
176 
177         Object[] params = new Object[3];
178         PropertyValue param1 = new PropertyValue();
179         param1.Name = "DataSourceName";
180         param1.Value = "Bibliography";
181         params[0] = param1;
182         PropertyValue param2 = new PropertyValue();
183         param2.Name = "CommandType";
184         param2.Value = new Integer(com.sun.star.sdb.CommandType.TABLE);
185         params[1] = param2;
186         PropertyValue param3 = new PropertyValue();
187         param3.Name = "Command";
188         param3.Value = "biblio";
189         params[2] = param3;
190 
191         shortWait();
192 
193         AccessibilityTools at = new AccessibilityTools();
194 
195         XWindow xWindow = secondController.getFrame().getContainerWindow();
196 
197         XAccessible xRoot = at.getAccessibleObject(xWindow);
198 
199         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.TABLE_CELL);
200 
201         log.println("ImplementationName: "+util.utils.getImplName(oObj));
202 
203         TestEnvironment tEnv = new TestEnvironment( oObj );
204 
205         tEnv.addObjRelation("EventProducer",
206             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer(){
207                 public void fireEvent() {
208                 }
209             });
210 
211         return tEnv;
212     }
213 
214     /**
215     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
216     * reset</code> call.
217     */
218     private void shortWait() {
219         try {
220             Thread.sleep(5000); ;
221         } catch (InterruptedException e) {
222             System.out.println("While waiting :" + e) ;
223         }
224     }
225 
226 }
227