1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package mod._svtools;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.AccessibilityTools;
33 import util.DesktopTools;
34 import util.SOfficeFactory;
35 
36 import com.sun.star.accessibility.AccessibleRole;
37 import com.sun.star.accessibility.XAccessible;
38 import com.sun.star.awt.XWindow;
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.frame.XController;
41 import com.sun.star.frame.XDesktop;
42 import com.sun.star.frame.XDispatch;
43 import com.sun.star.frame.XDispatchProvider;
44 import com.sun.star.frame.XFrame;
45 import com.sun.star.frame.XModel;
46 import com.sun.star.lang.XInitialization;
47 import com.sun.star.lang.XMultiServiceFactory;
48 import com.sun.star.text.XTextDocument;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XInterface;
51 import com.sun.star.util.URL;
52 
53 /**
54  * Test for object that implements the following interfaces :
55  * <ul>
56  *  <li>
57  *      <code>::com::sun::star::accessibility::XAccessibleContext
58  *  </code></li>
59  *  <li>
60  *      <code>::com::sun::star::accessibility::XAccessibleEventBroadcaster
61  *  </code></li>
62  * </ul> <p>
63  *
64  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
65  * @see com.sun.star.accessibility.XAccessibleContext
66  * @see ifc.accessibility._XAccessibleEventBroadcaster
67  * @see ifc.accessibility._XAccessibleContext
68  */
69 public class AccessibleBrowseBoxTableCell extends TestCase {
70 
71     static XDesktop the_Desk;
72     static XTextDocument xTextDoc;
73 
74     /**
75      * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
76      */
77     protected void initialize(TestParameters Param, PrintWriter log) {
78         the_Desk = (XDesktop) UnoRuntime.queryInterface(
79                     XDesktop.class, DesktopTools.createDesktop((XMultiServiceFactory)Param.getMSF()) );
80     }
81 
82     /**
83      * Disposes the document, if exists, created in
84      * <code>createTestEnvironment</code> method.
85      */
86     protected void cleanup( TestParameters Param, PrintWriter log) {
87 
88         log.println("disposing xTextDoc");
89 
90         if (xTextDoc != null) {
91             xTextDoc.dispose();
92         }
93     }
94 
95     /**
96      * Creates a text document. Opens the DataSource browser and loads
97      * the table. Creates an instance of the service
98      * <code>com.sun.star.awt.Toolkit</code> and gets active top window.
99      * Then obtains an accessible object with the role
100      * <code>AccessibleRole.TABLE_CELL</code>.
101      * Object relations created :
102      * <ul>
103      *  <li> <code>'EventProducer'</code> for
104      *      {@link ifc.accessibility._XAccessibleEventBroadcaster}:
105      *      method <code>fireEvent()</code> is empty because object is transient
106      *   </li>
107      * </ul>
108      *
109      * @param tParam test parameters
110      * @param log writer to log information while testing
111      *
112      * @see com.sun.star.awt.Toolkit
113      * @see com.sun.star.accessibility.AccessibleRole
114      * @see ifc.accessibility._XAccessibleEventBroadcaster
115      * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
116      */
117     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
118 
119         log.println( "creating a test environment" );
120 
121         if (xTextDoc != null) xTextDoc.dispose();
122 
123         // get a soffice factory object
124         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
125 
126         try {
127             log.println( "creating a text document" );
128             xTextDoc = SOF.createTextDoc(null);
129         } catch ( com.sun.star.uno.Exception e ) {
130             // Some exception occures.FAILED
131             e.printStackTrace( log );
132             throw new StatusException( "Couldn't create document", e );
133         }
134 
135         shortWait();
136 
137         XModel aModel1 = (XModel)
138                     UnoRuntime.queryInterface(XModel.class, xTextDoc);
139 
140         XController secondController = aModel1.getCurrentController();
141 
142 
143         XDispatchProvider aProv = (XDispatchProvider)
144             UnoRuntime.queryInterface(XDispatchProvider.class,secondController);
145 
146         XDispatch getting = null;
147 
148         log.println( "opening DatasourceBrowser" );
149         URL the_url = new URL();
150         the_url.Complete = ".component:DB/DataSourceBrowser";
151         getting = aProv.queryDispatch(the_url,"_beamer",12);
152         PropertyValue[] noArgs = new PropertyValue[0];
153         getting.dispatch(the_url,noArgs);
154 
155         shortWait();
156 
157         XFrame the_frame1 = the_Desk.getCurrentFrame();
158 
159         if (the_frame1 == null) {
160             log.println("Current frame was not found !!!");
161         }
162 
163         XFrame the_frame2 = the_frame1.findFrame("_beamer",4);
164 
165         the_frame2.setName("DatasourceBrowser");
166 
167         XInterface oObj = null;
168 
169         final XInitialization xInit = (XInitialization)
170                 UnoRuntime.queryInterface(
171                         XInitialization.class, the_frame2.getController());
172 
173         Object[] params = new Object[3];
174         PropertyValue param1 = new PropertyValue();
175         param1.Name = "DataSourceName";
176         param1.Value = "Bibliography";
177         params[0] = param1;
178         PropertyValue param2 = new PropertyValue();
179         param2.Name = "CommandType";
180         param2.Value = new Integer(com.sun.star.sdb.CommandType.TABLE);
181         params[1] = param2;
182         PropertyValue param3 = new PropertyValue();
183         param3.Name = "Command";
184         param3.Value = "biblio";
185         params[2] = param3;
186 
187         shortWait();
188 
189         AccessibilityTools at = new AccessibilityTools();
190 
191         XWindow xWindow = secondController.getFrame().getContainerWindow();
192 
193         XAccessible xRoot = at.getAccessibleObject(xWindow);
194 
195         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.TABLE_CELL);
196 
197         log.println("ImplementationName: "+util.utils.getImplName(oObj));
198 
199         TestEnvironment tEnv = new TestEnvironment( oObj );
200 
201         tEnv.addObjRelation("EventProducer",
202             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer(){
203                 public void fireEvent() {
204                 }
205             });
206 
207         return tEnv;
208     }
209 
210     /**
211     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
212     * reset</code> call.
213     */
214     private void shortWait() {
215         try {
216             Thread.sleep(5000);
217         } catch (InterruptedException e) {
218             System.out.println("While waiting :" + e) ;
219         }
220     }
221 
222 }
223