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 package mod._dbaccess;
24 
25 import java.io.PrintWriter;
26 
27 import lib.Status;
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.AccessibilityTools;
33 
34 import com.sun.star.accessibility.AccessibleRole;
35 import com.sun.star.accessibility.XAccessible;
36 import com.sun.star.accessibility.XAccessibleComponent;
37 import com.sun.star.awt.Point;
38 import com.sun.star.awt.XWindow;
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.container.XNameAccess;
42 import com.sun.star.container.XNameContainer;
43 import com.sun.star.frame.XModel;
44 import com.sun.star.frame.XStorable;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.sdb.XDocumentDataSource;
48 import com.sun.star.sdb.XQueryDefinitionsSupplier;
49 import com.sun.star.sdbc.XConnection;
50 import com.sun.star.sdbc.XIsolatedConnection;
51 import com.sun.star.sdbc.XStatement;
52 import com.sun.star.ucb.XSimpleFileAccess;
53 import com.sun.star.uno.UnoRuntime;
54 import com.sun.star.uno.XInterface;
55 import java.awt.Robot;
56 import java.awt.event.InputEvent;
57 import util.DesktopTools;
58 import util.utils;
59 
60 
61 /**
62 * Object implements the following interfaces :
63 * <ul>
64 *   <li><code>::com::sun::star::accessibility::XAccessible</code></li>
65 *   <li><code>::com::sun::star::accessibility::XAccessibleContext
66 *   </code></li>
67 *   <li><code>::com::sun::star::accessibility::XAccessibleEventBroadcaster
68 *   </code></li>
69 * </ul><p>
70 * @see com.sun.star.accessibility.XAccessible
71 * @see com.sun.star.accessibility.XAccessibleContext
72 * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
73 * @see ifc.accessibility._XAccessible
74 * @see ifc.accessibility._XAccessibleContext
75 * @see ifc.accessibility._XAccessibleEventBroadcaster
76 */
77 public class TableWindowAccessibility extends TestCase {
78     XWindow xWindow = null;
79     Object oDBSource = null;
80     String aFile = "";
81     XConnection connection = null;
82     XIsolatedConnection isolConnection = null;
83     XComponent QueryComponent = null;
84     String user = "";
85     String password="";
86 
87     /**
88      * Creates a new DataSource and stores it.
89      * Creates a connection and using it
90      * creates two tables in database.
91      * Creates a new query and adds it to DefinitionContainer.
92      * Opens the QueryComponent.with loadComponentFromURL
93      * and gets the object with the role PANEL and the implementation
94      * name that contains TabelViewAccessibility
95      * @param Param test parameters
96      * @param log writer to log information while testing
97      * @return
98      * @throws StatusException
99      * @see TestEnvironment
100      */
createTestEnvironment(TestParameters Param, PrintWriter log)101     protected TestEnvironment createTestEnvironment(TestParameters Param,
102                                                     PrintWriter log) {
103         XInterface oObj = null;
104 
105         Object oDBContext = null;
106         Object oDBSource = null;
107         Object newQuery = null;
108         Object toolkit = null;
109         XStorable store = null;
110 
111         try {
112             oDBContext = ((XMultiServiceFactory) Param.getMSF())
113                               .createInstance("com.sun.star.sdb.DatabaseContext");
114             oDBSource = ((XMultiServiceFactory) Param.getMSF())
115                              .createInstance("com.sun.star.sdb.DataSource");
116             newQuery = ((XMultiServiceFactory) Param.getMSF())
117                             .createInstance("com.sun.star.sdb.QueryDefinition");
118             toolkit = ((XMultiServiceFactory) Param.getMSF())
119                            .createInstance("com.sun.star.awt.Toolkit");
120         } catch (com.sun.star.uno.Exception e) {
121             e.printStackTrace(log);
122             throw new StatusException(Status.failed("Couldn't create instance"));
123         }
124 
125         String mysqlURL = (String) Param.get("mysql.url");
126 
127         if (mysqlURL == null) {
128             throw new StatusException(Status.failed(
129                                               "Couldn't get 'mysql.url' from ini-file"));
130         }
131 
132         user = (String) Param.get("jdbc.user");
133         password = (String) Param.get("jdbc.password");
134 
135         if ((user == null) || (password == null)) {
136             throw new StatusException(Status.failed(
137                                               "Couldn't get 'jdbc.user' or 'jdbc.password' from ini-file"));
138         }
139 
140         PropertyValue[] info = new PropertyValue[2];
141         info[0] = new PropertyValue();
142         info[0].Name = "user";
143         info[0].Value = user;
144         info[1] = new PropertyValue();
145         info[1].Name = "password";
146         info[1].Value = password;
147 
148         XPropertySet propSetDBSource = (XPropertySet) UnoRuntime.queryInterface(
149                                                XPropertySet.class, oDBSource);
150 
151         try {
152             propSetDBSource.setPropertyValue("URL", mysqlURL);
153             propSetDBSource.setPropertyValue("Info", info);
154         } catch (com.sun.star.lang.WrappedTargetException e) {
155             e.printStackTrace(log);
156             throw new StatusException(Status.failed(
157                                               "Couldn't set property value"));
158         } catch (com.sun.star.lang.IllegalArgumentException e) {
159             e.printStackTrace(log);
160             throw new StatusException(Status.failed(
161                                               "Couldn't set property value"));
162         } catch (com.sun.star.beans.PropertyVetoException e) {
163             e.printStackTrace(log);
164             throw new StatusException(Status.failed(
165                                               "Couldn't set property value"));
166         } catch (com.sun.star.beans.UnknownPropertyException e) {
167             e.printStackTrace(log);
168             throw new StatusException(Status.failed(
169                                               "Couldn't set property value"));
170         }
171 
172         try {
173             log.println ("writing database file ...");
174             XDocumentDataSource xDDS = (XDocumentDataSource)
175             UnoRuntime.queryInterface(XDocumentDataSource.class, oDBSource);
176             store = (XStorable) UnoRuntime.queryInterface(XStorable.class,
177                     xDDS.getDatabaseDocument());
178             aFile = utils.getOfficeTemp ((XMultiServiceFactory) Param.getMSF ())+"TableWindow.odb";
179             log.println("... filename will be "+aFile);
180             store.storeAsURL(aFile,new PropertyValue[]{});
181             log.println("... done");
182         } catch (com.sun.star.uno.Exception e) {
183             e.printStackTrace(log);
184             throw new StatusException(Status.failed("Couldn't register object"));
185         }
186 
187         isolConnection = (XIsolatedConnection) UnoRuntime.queryInterface(
188                                                      XIsolatedConnection.class,
189                                                      oDBSource);
190 
191         XConnection connection = null;
192         XStatement statement = null;
193 
194         final String tbl_name1 = "tst_table1";
195         final String tbl_name2 = "tst_table2";
196         final String col_name1 = "id1";
197         final String col_name2 = "id2";
198 
199         try {
200             connection = isolConnection.getIsolatedConnection(user, password);
201             statement = connection.createStatement();
202             statement.executeUpdate("drop table if exists " + tbl_name1);
203             statement.executeUpdate("drop table if exists " + tbl_name2);
204             statement.executeUpdate("create table " + tbl_name1 + " (" +
205                                     col_name1 + " int)");
206             statement.executeUpdate("create table " + tbl_name2 + " (" +
207                                     col_name2 + " int)");
208         } catch (com.sun.star.sdbc.SQLException e) {
209             try {
210                 shortWait();
211                 connection = isolConnection.getIsolatedConnection(user,
212                                                                   password);
213                 statement = connection.createStatement();
214                 statement.executeUpdate("drop table if exists " + tbl_name1);
215                 statement.executeUpdate("drop table if exists " + tbl_name2);
216                 statement.executeUpdate("create table " + tbl_name1 + " (" +
217                                         col_name1 + " int)");
218                 statement.executeUpdate("create table " + tbl_name2 + " (" +
219                                         col_name2 + " int)");
220             } catch (com.sun.star.sdbc.SQLException e2) {
221                 e2.printStackTrace(log);
222                 throw new StatusException(Status.failed("SQLException"));
223             }
224         }
225 
226         XQueryDefinitionsSupplier querySuppl = (XQueryDefinitionsSupplier) UnoRuntime.queryInterface(
227                                                        XQueryDefinitionsSupplier.class,
228                                                        oDBSource);
229 
230         XNameAccess defContainer = querySuppl.getQueryDefinitions();
231 
232         XPropertySet queryProp = (XPropertySet) UnoRuntime.queryInterface(
233                                          XPropertySet.class, newQuery);
234 
235         try {
236             final String query = "select * from " + tbl_name1 + ", " +
237                                  tbl_name2 + " where " + tbl_name1 + "." +
238                                  col_name1 + "=" + tbl_name2 + "." +
239                                  col_name2;
240             queryProp.setPropertyValue("Command", query);
241         } catch (com.sun.star.lang.WrappedTargetException e) {
242             e.printStackTrace(log);
243             throw new StatusException(Status.failed(
244                                               "Couldn't set property value"));
245         } catch (com.sun.star.lang.IllegalArgumentException e) {
246             e.printStackTrace(log);
247             throw new StatusException(Status.failed(
248                                               "Couldn't set property value"));
249         } catch (com.sun.star.beans.PropertyVetoException e) {
250             e.printStackTrace(log);
251             throw new StatusException(Status.failed(
252                                               "Couldn't set property value"));
253         } catch (com.sun.star.beans.UnknownPropertyException e) {
254             e.printStackTrace(log);
255             throw new StatusException(Status.failed(
256                                               "Couldn't set property value"));
257         }
258 
259         XNameContainer queryContainer = (XNameContainer) UnoRuntime.queryInterface(
260                                                 XNameContainer.class,
261                                                 defContainer);
262 
263         try {
264             queryContainer.insertByName("Query1", newQuery);
265             store.store();
266             connection.close ();
267         } catch (com.sun.star.lang.WrappedTargetException e) {
268             e.printStackTrace(log);
269             throw new StatusException(Status.failed("Couldn't insert query"));
270         } catch (com.sun.star.container.ElementExistException e) {
271             e.printStackTrace(log);
272             throw new StatusException(Status.failed("Couldn't insert query"));
273         } catch (com.sun.star.lang.IllegalArgumentException e) {
274             e.printStackTrace(log);
275             throw new StatusException(Status.failed("Couldn't insert query"));
276         } catch (com.sun.star.io.IOException e) {
277             e.printStackTrace(log);
278             throw new StatusException(Status.failed("Couldn't insert query"));
279         } catch (com.sun.star.sdbc.SQLException e) {
280             e.printStackTrace(log);
281             throw new StatusException(Status.failed("Couldn't insert query"));
282         }
283 
284         PropertyValue[] loadProps = new PropertyValue[3];
285         loadProps[0] = new PropertyValue();
286         loadProps[0].Name = "QueryDesignView";
287         loadProps[0].Value = Boolean.TRUE;
288 
289         loadProps[1] = new PropertyValue();
290         loadProps[1].Name = "CurrentQuery";
291         loadProps[1].Value = "Query1";
292 
293         loadProps[2] = new PropertyValue();
294         loadProps[2].Name = "DataSource";
295         loadProps[2].Value = oDBSource;
296 
297         QueryComponent = DesktopTools.loadDoc((XMultiServiceFactory) Param.getMSF (),".component:DB/QueryDesign",loadProps);
298 
299         xWindow = UnoRuntime.queryInterface(XModel.class, QueryComponent).
300             getCurrentController().getFrame().getContainerWindow();
301 
302         XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
303 
304         AccessibilityTools.printAccessibleTree (log,xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
305 
306         oObj = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL, "",
307                                              "TableWindowAccessibility");
308 
309         log.println("ImplementationName " + util.utils.getImplName(oObj));
310 
311         log.println("creating TestEnvironment ... done");
312 
313         TestEnvironment tEnv = new TestEnvironment(oObj);
314 
315         shortWait();
316 
317         XAccessibleComponent accComp = (XAccessibleComponent) UnoRuntime.queryInterface(
318                                                XAccessibleComponent.class,
319                                                oObj);
320 
321 
322               final Point point = accComp.getLocationOnScreen();
323 
324         tEnv.addObjRelation("EventProducer",
325                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
326             public void fireEvent() {
327                 try {
328                     Robot rob = new Robot();
329                     rob.mouseMove(point.X + 2, point.Y + 7);
330                     rob.mousePress(InputEvent.BUTTON1_MASK);
331                     rob.mouseMove(point.X + 400, point.Y);
332                     rob.mouseRelease (InputEvent.BUTTON1_MASK);
333                 } catch (java.awt.AWTException e) {
334                     System.out.println("desired child doesn't exist");
335                 }
336             }
337         });
338 
339         return tEnv;
340     } // finish method getTestEnvironment
341 
342     /**
343      * Closes all open documents.
344      */
cleanup(TestParameters Param, PrintWriter log)345     protected void cleanup(TestParameters Param, PrintWriter log) {
346         try
347         {
348 
349             log.println ("closing QueryComponent ...");
350             DesktopTools.closeDoc (QueryComponent);
351             log.println ("... done");
352             XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF ();
353             Object sfa = xMSF.createInstance ("com.sun.star.comp.ucb.SimpleFileAccess");
354             XSimpleFileAccess xSFA = (XSimpleFileAccess) UnoRuntime.queryInterface (XSimpleFileAccess.class, sfa);
355             log.println ("deleting database file");
356             xSFA.kill (aFile);
357             log.println ("Could delete file "+aFile+": "+!xSFA.exists (aFile));
358         } catch (Exception e)
359         {
360             e.printStackTrace ();
361         }
362     }
363 
364 
365     /**
366     * Sleeps for 1.5 sec. to allow StarOffice to react on <code>
367     * reset</code> call.
368     */
shortWait()369     private void shortWait() {
370         try {
371             Thread.sleep(1500);
372         } catch (InterruptedException e) {
373             log.println("While waiting :" + e);
374         }
375     }
376 }
377