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