bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _dbaccess / TableWindowAccessibility.java
blob5887999090dfc450d2c7f778676f04255ec64a63
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package mod._dbaccess;
20 import java.io.PrintWriter;
22 import lib.Status;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.AccessibilityTools;
29 import com.sun.star.accessibility.AccessibleRole;
30 import com.sun.star.accessibility.XAccessible;
31 import com.sun.star.accessibility.XAccessibleComponent;
32 import com.sun.star.awt.Point;
33 import com.sun.star.awt.XWindow;
34 import com.sun.star.beans.PropertyValue;
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.container.XNameAccess;
37 import com.sun.star.container.XNameContainer;
38 import com.sun.star.frame.XModel;
39 import com.sun.star.frame.XStorable;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.sdb.XDocumentDataSource;
43 import com.sun.star.sdb.XQueryDefinitionsSupplier;
44 import com.sun.star.sdbc.XConnection;
45 import com.sun.star.sdbc.XIsolatedConnection;
46 import com.sun.star.sdbc.XStatement;
47 import com.sun.star.ucb.XSimpleFileAccess;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 import java.awt.Robot;
51 import java.awt.event.InputEvent;
52 import util.DesktopTools;
53 import util.utils;
56 /**
57 * Object implements the following interfaces :
58 * <ul>
59 * <li><code>::com::sun::star::accessibility::XAccessible</code></li>
60 * <li><code>::com::sun::star::accessibility::XAccessibleContext
61 * </code></li>
62 * <li><code>::com::sun::star::accessibility::XAccessibleEventBroadcaster
63 * </code></li>
64 * </ul><p>
65 * @see com.sun.star.accessibility.XAccessible
66 * @see com.sun.star.accessibility.XAccessibleContext
67 * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
68 * @see ifc.accessibility._XAccessible
69 * @see ifc.accessibility._XAccessibleContext
70 * @see ifc.accessibility._XAccessibleEventBroadcaster
72 public class TableWindowAccessibility extends TestCase {
73 XWindow xWindow = null;
74 Object oDBSource = null;
75 String aFile = "";
76 XConnection connection = null;
77 XIsolatedConnection isolConnection = null;
78 XComponent QueryComponent = null;
79 String user = "";
80 String password="";
82 /**
83 * Creates a new DataSource and stores it.
84 * Creates a connection and using it
85 * creates two tables in database.
86 * Creates a new query and adds it to DefinitionContainer.
87 * Opens the QueryComponent.with loadComponentFromURL
88 * and gets the object with the role PANEL and the implementation
89 * name that contains TabelViewAccessibility
90 * @param Param test parameters
91 * @param log writer to log information while testing
92 * @return
93 * @throws StatusException
94 * @see TestEnvironment
96 protected TestEnvironment createTestEnvironment(TestParameters Param,
97 PrintWriter log) {
98 XInterface oObj = null;
100 Object oDBSource = null;
101 Object newQuery = null;
102 XStorable store = null;
104 try {
105 ((XMultiServiceFactory) Param.getMSF())
106 .createInstance("com.sun.star.sdb.DatabaseContext");
107 oDBSource = ((XMultiServiceFactory) Param.getMSF())
108 .createInstance("com.sun.star.sdb.DataSource");
109 newQuery = ((XMultiServiceFactory) Param.getMSF())
110 .createInstance("com.sun.star.sdb.QueryDefinition");
111 ((XMultiServiceFactory) Param.getMSF())
112 .createInstance("com.sun.star.awt.Toolkit");
113 } catch (com.sun.star.uno.Exception e) {
114 e.printStackTrace(log);
115 throw new StatusException(Status.failed("Couldn't create instance"));
118 String mysqlURL = (String) Param.get("mysql.url");
120 if (mysqlURL == null) {
121 throw new StatusException(Status.failed(
122 "Couldn't get 'mysql.url' from ini-file"));
125 user = (String) Param.get("jdbc.user");
126 password = (String) Param.get("jdbc.password");
128 if ((user == null) || (password == null)) {
129 throw new StatusException(Status.failed(
130 "Couldn't get 'jdbc.user' or 'jdbc.password' from ini-file"));
133 PropertyValue[] info = new PropertyValue[2];
134 info[0] = new PropertyValue();
135 info[0].Name = "user";
136 info[0].Value = user;
137 info[1] = new PropertyValue();
138 info[1].Name = "password";
139 info[1].Value = password;
141 XPropertySet propSetDBSource = UnoRuntime.queryInterface(
142 XPropertySet.class, oDBSource);
144 try {
145 propSetDBSource.setPropertyValue("URL", mysqlURL);
146 propSetDBSource.setPropertyValue("Info", info);
147 } catch (com.sun.star.lang.WrappedTargetException e) {
148 e.printStackTrace(log);
149 throw new StatusException(Status.failed(
150 "Couldn't set property value"));
151 } catch (com.sun.star.lang.IllegalArgumentException e) {
152 e.printStackTrace(log);
153 throw new StatusException(Status.failed(
154 "Couldn't set property value"));
155 } catch (com.sun.star.beans.PropertyVetoException e) {
156 e.printStackTrace(log);
157 throw new StatusException(Status.failed(
158 "Couldn't set property value"));
159 } catch (com.sun.star.beans.UnknownPropertyException e) {
160 e.printStackTrace(log);
161 throw new StatusException(Status.failed(
162 "Couldn't set property value"));
165 try {
166 log.println ("writing database file ...");
167 XDocumentDataSource xDDS = UnoRuntime.queryInterface(XDocumentDataSource.class, oDBSource);
168 store = UnoRuntime.queryInterface(XStorable.class,
169 xDDS.getDatabaseDocument());
170 aFile = utils.getOfficeTemp ((XMultiServiceFactory) Param.getMSF ())+"TableWindow.odb";
171 log.println("... filename will be "+aFile);
172 store.storeAsURL(aFile,new PropertyValue[]{});
173 log.println("... done");
174 } catch (com.sun.star.uno.Exception e) {
175 e.printStackTrace(log);
176 throw new StatusException(Status.failed("Couldn't register object"));
179 isolConnection = UnoRuntime.queryInterface(
180 XIsolatedConnection.class,
181 oDBSource);
183 XConnection connection = null;
184 XStatement statement = null;
186 final String tbl_name1 = "tst_table1";
187 final String tbl_name2 = "tst_table2";
188 final String col_name1 = "id1";
189 final String col_name2 = "id2";
191 try {
192 connection = isolConnection.getIsolatedConnection(user, password);
193 statement = connection.createStatement();
194 statement.executeUpdate("drop table if exists " + tbl_name1);
195 statement.executeUpdate("drop table if exists " + tbl_name2);
196 statement.executeUpdate("create table " + tbl_name1 + " (" +
197 col_name1 + " int)");
198 statement.executeUpdate("create table " + tbl_name2 + " (" +
199 col_name2 + " int)");
200 } catch (com.sun.star.sdbc.SQLException e) {
201 try {
202 shortWait();
203 connection = isolConnection.getIsolatedConnection(user,
204 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 e2) {
213 e2.printStackTrace(log);
214 throw new StatusException(Status.failed("SQLException"));
218 XQueryDefinitionsSupplier querySuppl = UnoRuntime.queryInterface(
219 XQueryDefinitionsSupplier.class,
220 oDBSource);
222 XNameAccess defContainer = querySuppl.getQueryDefinitions();
224 XPropertySet queryProp = UnoRuntime.queryInterface(
225 XPropertySet.class, newQuery);
227 try {
228 final String query = "select * from " + tbl_name1 + ", " +
229 tbl_name2 + " where " + tbl_name1 + "." +
230 col_name1 + "=" + tbl_name2 + "." +
231 col_name2;
232 queryProp.setPropertyValue("Command", query);
233 } catch (com.sun.star.lang.WrappedTargetException e) {
234 e.printStackTrace(log);
235 throw new StatusException(Status.failed(
236 "Couldn't set property value"));
237 } catch (com.sun.star.lang.IllegalArgumentException e) {
238 e.printStackTrace(log);
239 throw new StatusException(Status.failed(
240 "Couldn't set property value"));
241 } catch (com.sun.star.beans.PropertyVetoException e) {
242 e.printStackTrace(log);
243 throw new StatusException(Status.failed(
244 "Couldn't set property value"));
245 } catch (com.sun.star.beans.UnknownPropertyException e) {
246 e.printStackTrace(log);
247 throw new StatusException(Status.failed(
248 "Couldn't set property value"));
251 XNameContainer queryContainer = UnoRuntime.queryInterface(
252 XNameContainer.class,
253 defContainer);
255 try {
256 queryContainer.insertByName("Query1", newQuery);
257 store.store();
258 connection.close ();
259 } catch (com.sun.star.lang.WrappedTargetException e) {
260 e.printStackTrace(log);
261 throw new StatusException(Status.failed("Couldn't insert query"));
262 } catch (com.sun.star.container.ElementExistException e) {
263 e.printStackTrace(log);
264 throw new StatusException(Status.failed("Couldn't insert query"));
265 } catch (com.sun.star.lang.IllegalArgumentException e) {
266 e.printStackTrace(log);
267 throw new StatusException(Status.failed("Couldn't insert query"));
268 } catch (com.sun.star.io.IOException e) {
269 e.printStackTrace(log);
270 throw new StatusException(Status.failed("Couldn't insert query"));
271 } catch (com.sun.star.sdbc.SQLException e) {
272 e.printStackTrace(log);
273 throw new StatusException(Status.failed("Couldn't insert query"));
276 PropertyValue[] loadProps = new PropertyValue[3];
277 loadProps[0] = new PropertyValue();
278 loadProps[0].Name = "QueryDesignView";
279 loadProps[0].Value = Boolean.TRUE;
281 loadProps[1] = new PropertyValue();
282 loadProps[1].Name = "CurrentQuery";
283 loadProps[1].Value = "Query1";
285 loadProps[2] = new PropertyValue();
286 loadProps[2].Name = "DataSource";
287 loadProps[2].Value = oDBSource;
289 QueryComponent = DesktopTools.loadDoc((XMultiServiceFactory) Param.getMSF (),".component:DB/QueryDesign",loadProps);
291 xWindow = UnoRuntime.queryInterface(XModel.class, QueryComponent).
292 getCurrentController().getFrame().getContainerWindow();
294 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
296 AccessibilityTools.printAccessibleTree (log,xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
298 oObj = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL, "",
299 "TableWindowAccessibility");
301 log.println("ImplementationName " + util.utils.getImplName(oObj));
303 log.println("creating TestEnvironment ... done");
305 TestEnvironment tEnv = new TestEnvironment(oObj);
307 shortWait();
309 XAccessibleComponent accComp = UnoRuntime.queryInterface(
310 XAccessibleComponent.class,
311 oObj);
314 final Point point = accComp.getLocationOnScreen();
316 tEnv.addObjRelation("EventProducer",
317 new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
318 public void fireEvent() {
319 try {
320 Robot rob = new Robot();
321 rob.mouseMove(point.X + 2, point.Y + 7);
322 rob.mousePress(InputEvent.BUTTON1_MASK);
323 rob.mouseMove(point.X + 400, point.Y);
324 rob.mouseRelease (InputEvent.BUTTON1_MASK);
325 } catch (java.awt.AWTException e) {
326 System.out.println("desired child doesn't exist");
331 return tEnv;
332 } // finish method getTestEnvironment
335 * Closes all open documents.
337 protected void cleanup(TestParameters Param, PrintWriter log) {
341 log.println ("closing QueryComponent ...");
342 DesktopTools.closeDoc (QueryComponent);
343 log.println ("... done");
344 XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF ();
345 Object sfa = xMSF.createInstance ("com.sun.star.comp.ucb.SimpleFileAccess");
346 XSimpleFileAccess xSFA = UnoRuntime.queryInterface (XSimpleFileAccess.class, sfa);
347 log.println ("deleting database file");
348 xSFA.kill (aFile);
349 log.println ("Could delete file "+aFile+": "+!xSFA.exists (aFile));
350 } catch (Exception e)
352 e.printStackTrace ();
358 * Sleeps for 1.5 sec. to allow StarOffice to react on <code>
359 * reset</code> call.
361 private void shortWait() {
362 try {
363 Thread.sleep(1500);
364 } catch (InterruptedException e) {
365 log.println("While waiting :" + e);