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