Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _dbaccess / TableWindowAccessibility.java
blobb7c5513b21457f10f8f761cd7b43159a8d21125f
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 @Override
97 protected TestEnvironment createTestEnvironment(TestParameters Param,
98 PrintWriter log) {
99 XInterface oObj = null;
101 Object oDBSource = null;
102 Object newQuery = null;
103 XStorable store = null;
105 try {
106 Param.getMSF()
107 .createInstance("com.sun.star.sdb.DatabaseContext");
108 oDBSource = Param.getMSF()
109 .createInstance("com.sun.star.sdb.DataSource");
110 newQuery = Param.getMSF()
111 .createInstance("com.sun.star.sdb.QueryDefinition");
112 Param.getMSF()
113 .createInstance("com.sun.star.awt.Toolkit");
114 } catch (com.sun.star.uno.Exception e) {
115 throw new StatusException(e, 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 throw new StatusException(e, Status.failed("Couldn't set property value"));
149 } catch (com.sun.star.lang.IllegalArgumentException e) {
150 throw new StatusException(e, Status.failed("Couldn't set property value"));
151 } catch (com.sun.star.beans.PropertyVetoException e) {
152 throw new StatusException(e, Status.failed("Couldn't set property value"));
153 } catch (com.sun.star.beans.UnknownPropertyException e) {
154 throw new StatusException(e, Status.failed("Couldn't set property value"));
157 try {
158 log.println ("writing database file ...");
159 XDocumentDataSource xDDS = UnoRuntime.queryInterface(XDocumentDataSource.class, oDBSource);
160 store = UnoRuntime.queryInterface(XStorable.class,
161 xDDS.getDatabaseDocument());
162 aFile = utils.getOfficeTemp (Param.getMSF ())+"TableWindow.odb";
163 log.println("... filename will be "+aFile);
164 store.storeAsURL(aFile,new PropertyValue[]{});
165 log.println("... done");
166 } catch (com.sun.star.uno.Exception e) {
167 throw new StatusException(e, Status.failed("Couldn't register object"));
170 isolConnection = UnoRuntime.queryInterface(
171 XIsolatedConnection.class,
172 oDBSource);
174 XConnection connection = null;
175 XStatement statement = null;
177 final String tbl_name1 = "tst_table1";
178 final String tbl_name2 = "tst_table2";
179 final String col_name1 = "id1";
180 final String col_name2 = "id2";
182 try {
183 connection = isolConnection.getIsolatedConnection(user, password);
184 statement = connection.createStatement();
185 statement.executeUpdate("drop table if exists " + tbl_name1);
186 statement.executeUpdate("drop table if exists " + tbl_name2);
187 statement.executeUpdate("create table " + tbl_name1 + " (" +
188 col_name1 + " int)");
189 statement.executeUpdate("create table " + tbl_name2 + " (" +
190 col_name2 + " int)");
191 } catch (com.sun.star.sdbc.SQLException e) {
192 try {
193 util.utils.pause(1500);
194 connection = isolConnection.getIsolatedConnection(user,
195 password);
196 statement = connection.createStatement();
197 statement.executeUpdate("drop table if exists " + tbl_name1);
198 statement.executeUpdate("drop table if exists " + tbl_name2);
199 statement.executeUpdate("create table " + tbl_name1 + " (" +
200 col_name1 + " int)");
201 statement.executeUpdate("create table " + tbl_name2 + " (" +
202 col_name2 + " int)");
203 } catch (com.sun.star.sdbc.SQLException e2) {
204 throw new StatusException(e2, Status.failed("SQLException"));
208 XQueryDefinitionsSupplier querySuppl = UnoRuntime.queryInterface(
209 XQueryDefinitionsSupplier.class,
210 oDBSource);
212 XNameAccess defContainer = querySuppl.getQueryDefinitions();
214 XPropertySet queryProp = UnoRuntime.queryInterface(
215 XPropertySet.class, newQuery);
217 try {
218 final String query = "select * from " + tbl_name1 + ", " +
219 tbl_name2 + " where " + tbl_name1 + "." +
220 col_name1 + "=" + tbl_name2 + "." +
221 col_name2;
222 queryProp.setPropertyValue("Command", query);
223 } catch (com.sun.star.lang.WrappedTargetException e) {
224 throw new StatusException(e, Status.failed("Couldn't set property value"));
225 } catch (com.sun.star.lang.IllegalArgumentException e) {
226 throw new StatusException(e, Status.failed("Couldn't set property value"));
227 } catch (com.sun.star.beans.PropertyVetoException e) {
228 throw new StatusException(e, Status.failed("Couldn't set property value"));
229 } catch (com.sun.star.beans.UnknownPropertyException e) {
230 throw new StatusException(e, Status.failed("Couldn't set property value"));
233 XNameContainer queryContainer = UnoRuntime.queryInterface(
234 XNameContainer.class,
235 defContainer);
237 try {
238 queryContainer.insertByName("Query1", newQuery);
239 store.store();
240 connection.close ();
241 } catch (com.sun.star.lang.WrappedTargetException e) {
242 throw new StatusException(e, Status.failed("Couldn't insert query"));
243 } catch (com.sun.star.container.ElementExistException e) {
244 throw new StatusException(e, Status.failed("Couldn't insert query"));
245 } catch (com.sun.star.lang.IllegalArgumentException e) {
246 throw new StatusException(e, Status.failed("Couldn't insert query"));
247 } catch (com.sun.star.io.IOException e) {
248 throw new StatusException(e, Status.failed("Couldn't insert query"));
249 } catch (com.sun.star.sdbc.SQLException e) {
250 throw new StatusException(e, Status.failed("Couldn't insert query"));
253 PropertyValue[] loadProps = new PropertyValue[3];
254 loadProps[0] = new PropertyValue();
255 loadProps[0].Name = "QueryDesignView";
256 loadProps[0].Value = Boolean.TRUE;
258 loadProps[1] = new PropertyValue();
259 loadProps[1].Name = "CurrentQuery";
260 loadProps[1].Value = "Query1";
262 loadProps[2] = new PropertyValue();
263 loadProps[2].Name = "DataSource";
264 loadProps[2].Value = oDBSource;
266 QueryComponent = DesktopTools.loadDoc(Param.getMSF (),".component:DB/QueryDesign",loadProps);
268 xWindow = UnoRuntime.queryInterface(XModel.class, QueryComponent).
269 getCurrentController().getFrame().getContainerWindow();
271 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
273 AccessibilityTools.printAccessibleTree (log,xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
275 oObj = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL, "",
276 "TableWindowAccessibility");
278 log.println("ImplementationName " + util.utils.getImplName(oObj));
280 log.println("creating TestEnvironment ... done");
282 TestEnvironment tEnv = new TestEnvironment(oObj);
284 util.utils.pause(1500);
286 XAccessibleComponent accComp = UnoRuntime.queryInterface(
287 XAccessibleComponent.class,
288 oObj);
291 final Point point = accComp.getLocationOnScreen();
293 tEnv.addObjRelation("EventProducer",
294 new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
295 public void fireEvent() {
296 try {
297 Robot rob = new Robot();
298 rob.mouseMove(point.X + 2, point.Y + 7);
299 rob.mousePress(InputEvent.BUTTON1_MASK);
300 rob.mouseMove(point.X + 400, point.Y);
301 rob.mouseRelease (InputEvent.BUTTON1_MASK);
302 } catch (java.awt.AWTException e) {
303 System.out.println("desired child doesn't exist");
308 return tEnv;
309 } // finish method getTestEnvironment
312 * Closes all open documents.
314 @Override
315 protected void cleanup(TestParameters Param, PrintWriter log) {
319 log.println ("closing QueryComponent ...");
320 DesktopTools.closeDoc (QueryComponent);
321 log.println ("... done");
322 XMultiServiceFactory xMSF = Param.getMSF ();
323 Object sfa = xMSF.createInstance ("com.sun.star.comp.ucb.SimpleFileAccess");
324 XSimpleFileAccess xSFA = UnoRuntime.queryInterface (XSimpleFileAccess.class, sfa);
325 log.println ("deleting database file");
326 xSFA.kill (aFile);
327 log.println ("Could delete file "+aFile+": "+!xSFA.exists (aFile));
328 } catch (Exception e)
330 e.printStackTrace ();