Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _dbaccess / SbaXGridControl.java
blob98ff707de924c1b8e86dea71414544b6d689bd02
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;
21 import java.util.Comparator;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.AccessibilityTools;
28 import util.FormTools;
29 import util.SOfficeFactory;
30 import util.WriterTools;
31 import util.utils;
33 import com.sun.star.accessibility.AccessibleRole;
34 import com.sun.star.accessibility.XAccessible;
35 import com.sun.star.accessibility.XAccessibleAction;
36 import com.sun.star.awt.Point;
37 import com.sun.star.awt.Size;
38 import com.sun.star.awt.XControlModel;
39 import com.sun.star.awt.XDevice;
40 import com.sun.star.awt.XExtendedToolkit;
41 import com.sun.star.awt.XGraphics;
42 import com.sun.star.awt.XToolkit;
43 import com.sun.star.awt.XWindow;
44 import com.sun.star.awt.XWindowPeer;
45 import com.sun.star.beans.XPropertySet;
46 import com.sun.star.container.XNameContainer;
47 import com.sun.star.drawing.XControlShape;
48 import com.sun.star.form.XBoundComponent;
49 import com.sun.star.form.XGridColumnFactory;
50 import com.sun.star.form.XLoadable;
51 import com.sun.star.lang.XComponent;
52 import com.sun.star.lang.XMultiServiceFactory;
53 import com.sun.star.sdbc.XResultSetUpdate;
54 import com.sun.star.text.XTextDocument;
55 import com.sun.star.uno.UnoRuntime;
56 import com.sun.star.uno.XInterface;
57 import com.sun.star.util.URL;
58 import com.sun.star.util.XCloseable;
59 import com.sun.star.view.XControlAccess;
62 /**
63 * Test for object which represents the control of the Grid model. <p>
64 * Object implements the following interfaces :
65 * <ul>
66 * <li> <code>com::sun::star::util::XModifyBroadcaster</code></li>
67 * <li> <code>com::sun::star::form::XGridFieldDataSupplier</code></li>
68 * <li> <code>com::sun::star::view::XSelectionSupplier</code></li>
69 * <li> <code>com::sun::star::form::XGrid</code></li>
70 * <li> <code>com::sun::star::awt::XControl</code></li>
71 * <li> <code>com::sun::star::util::XModeSelector</code></li>
72 * <li> <code>com::sun::star::container::XElementAccess</code></li>
73 * <li> <code>com::sun::star::awt::XWindow</code></li>
74 * <li> <code>com::sun::star::form::XUpdateBroadcaster</code></li>
75 * <li> <code>com::sun::star::frame::XDispatch</code></li>
76 * <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
77 * <li> <code>com::sun::star::form::XBoundComponent</code></li>
78 * <li> <code>com::sun::star::frame::XDispatchProviderInterception</code></li>
79 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
80 * <li> <code>com::sun::star::lang::XComponent</code></li>
81 * <li> <code>com::sun::star::awt::XView</code></li>
82 * <li> <code>com::sun::star::container::XContainer</code></li>
83 * </ul>
84 * This object test <b> is NOT </b> designed to be run in several
85 * threads concurently.
86 * @see com.sun.star.util.XModifyBroadcaster
87 * @see com.sun.star.form.XGridFieldDataSupplier
88 * @see com.sun.star.view.XSelectionSupplier
89 * @see com.sun.star.form.XGrid
90 * @see com.sun.star.awt.XControl
91 * @see com.sun.star.util.XModeSelector
92 * @see com.sun.star.container.XElementAccess
93 * @see com.sun.star.awt.XWindow
94 * @see com.sun.star.form.XUpdateBroadcaster
95 * @see com.sun.star.frame.XDispatch
96 * @see com.sun.star.container.XEnumerationAccess
97 * @see com.sun.star.form.XBoundComponent
98 * @see com.sun.star.frame.XDispatchProviderInterception
99 * @see com.sun.star.container.XIndexAccess
100 * @see com.sun.star.lang.XComponent
101 * @see com.sun.star.awt.XView
102 * @see com.sun.star.container.XContainer
103 * @see ifc.util._XModifyBroadcaster
104 * @see ifc.form._XGridFieldDataSupplier
105 * @see ifc.view._XSelectionSupplier
106 * @see ifc.form._XGrid
107 * @see ifc.awt._XControl
108 * @see ifc.util._XModeSelector
109 * @see ifc.container._XElementAccess
110 * @see ifc.awt._XWindow
111 * @see ifc.form._XUpdateBroadcaster
112 * @see ifc.frame._XDispatch
113 * @see ifc.container._XEnumerationAccess
114 * @see ifc.form._XBoundComponent
115 * @see ifc.frame._XDispatchProviderInterception
116 * @see ifc.container._XIndexAccess
117 * @see ifc.lang._XComponent
118 * @see ifc.awt._XView
119 * @see ifc.container._XContainer
121 public class SbaXGridControl extends TestCase {
122 XTextDocument xTextDoc;
125 * Creates Writer document.
127 @Override
128 protected void initialize(TestParameters Param, PrintWriter log) {
129 SOfficeFactory SOF = SOfficeFactory.getFactory(Param.getMSF());
131 try {
132 log.println("creating a textdocument");
133 xTextDoc = SOF.createTextDoc(null);
134 } catch (com.sun.star.uno.Exception e) {
135 // Some exception occurs.FAILED
136 e.printStackTrace(log);
137 throw new StatusException("Couldn't create document", e);
142 * Disposes Writer document.
144 @Override
145 protected void cleanup(TestParameters tParam, PrintWriter log) {
146 //closing the appearing dialog before disposing the document
147 XInterface toolkit = null;
149 try {
150 toolkit = (XInterface) tParam.getMSF()
151 .createInstance("com.sun.star.awt.Toolkit");
152 } catch (com.sun.star.uno.Exception e) {
153 log.println("Couldn't get toolkit");
154 e.printStackTrace(log);
155 throw new StatusException("Couldn't get toolkit", e);
158 XExtendedToolkit tk = UnoRuntime.queryInterface(
159 XExtendedToolkit.class, toolkit);
161 Object atw = tk.getActiveTopWindow();
163 XWindow xWindow = UnoRuntime.queryInterface(XWindow.class,
164 atw);
166 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
168 XInterface button = AccessibilityTools.getAccessibleObjectForRole(xRoot,
169 AccessibleRole.PUSH_BUTTON);
171 XAccessibleAction action = UnoRuntime.queryInterface(
172 XAccessibleAction.class, button);
174 try {
175 action.doAccessibleAction(0);
176 } catch (com.sun.star.lang.IndexOutOfBoundsException iob) {
177 log.println("couldn't close dialog");
178 } catch (com.sun.star.lang.DisposedException e) {
179 log.println("couldn't close dialog");
182 log.println(" disposing xTextDoc ");
184 try {
185 XCloseable closer = UnoRuntime.queryInterface(
186 XCloseable.class, xTextDoc);
187 closer.close(true);
188 } catch (com.sun.star.util.CloseVetoException e) {
189 log.println("couldn't close document");
190 } catch (com.sun.star.lang.DisposedException e) {
191 log.println("couldn't close document");
196 * Creating a Testenvironment for the interfaces to be tested.
197 * For object creation first a
198 * <code>com.sun.star.form.component.GridControl<code> instance
199 * is added to the <code>ControlShape</code>. Then this model's
200 * control is retrieved.
202 * Object relations created :
203 * <ul>
204 * <li> <code>'GRAPHICS'</code> for
205 * {@link ifc.awt._XView} test : <code>XGraphics</code>
206 * object different that belong to the object tested.</li>
207 * <li> <code>'CONTEXT'</code> for
208 * {@link ifc.awt._XControl} </li>
209 * <li> <code>'WINPEER'</code> for
210 * {@link ifc.awt._XControl} </li>
211 * <li> <code>'TOOLKIT'</code> for
212 * {@link ifc.awt._XControl} </li>
213 * <li> <code>'MODEL'</code> for
214 * {@link ifc.awt._XControl} </li>
215 * <li> <code>'XWindow.AnotherWindow'</code> for
216 * {@link ifc.awt._XWindow} for switching focus.</li>
217 * <li> <code>'XDispatch.URL'</code> for
218 * {@link ifc.frame._XDispatch} the url which moves
219 * DB cursor to the next row (".uno:FormSlots/moveToNext").</li>
220 * <li> <code>'XContainer.Container'</code> for
221 * {@link ifc.container._XContainer} as the component created
222 * doesn't support <code>XContainer</code> itself, but
223 * it is supported by its model. So this model is passed.</li>
224 * <li> <code>'INSTANCE'</code> for
225 * {@link ifc.container._XContainer} the instance to be
226 * inserted into collection. Is a column instance.</li>
227 * </ul>
229 @Override
230 protected TestEnvironment createTestEnvironment(TestParameters Param,
231 PrintWriter log) {
232 XInterface oObj = null;
233 XWindowPeer the_win = null;
234 XToolkit the_kit = null;
235 XDevice aDevice = null;
236 XGraphics aGraphic = null;
237 XPropertySet aControl = null;
238 XPropertySet aControl2 = null;
239 XPropertySet aControl3 = null;
240 XPropertySet aControl4 = null;
241 XGridColumnFactory columns = null;
243 //Insert a ControlShape and get the ControlModel
244 XControlShape aShape = createGrid(xTextDoc, 3000, 4500, 15000, 10000);
246 XControlModel the_Model = aShape.getControl();
248 WriterTools.getDrawPage(xTextDoc).add(aShape);
250 XLoadable formLoader = FormTools.bindForm(xTextDoc);
252 //Try to query XControlAccess
253 XControlAccess the_access = UnoRuntime.queryInterface(
254 XControlAccess.class,
255 xTextDoc.getCurrentController());
257 try {
258 columns = UnoRuntime.queryInterface(
259 XGridColumnFactory.class, the_Model);
260 aControl = columns.createColumn("TextField");
261 aControl.setPropertyValue("DataField", "Identifier");
262 aControl.setPropertyValue("Label", "Identifier");
263 aControl2 = columns.createColumn("TextField");
264 aControl2.setPropertyValue("DataField", "Publisher");
265 aControl2.setPropertyValue("Label", "Publisher");
266 aControl3 = columns.createColumn("TextField");
267 aControl3.setPropertyValue("DataField", "Author");
268 aControl3.setPropertyValue("Label", "Author");
269 aControl4 = columns.createColumn("TextField");
270 aControl4.setPropertyValue("DataField", "Title");
271 aControl4.setPropertyValue("Label", "Title");
272 } catch (com.sun.star.lang.IllegalArgumentException e) {
273 // Some exception occurs.FAILED
274 log.println("!!! Couldn't create instance : " + e);
275 throw new StatusException("Can't create column instances.", e);
276 } catch (com.sun.star.lang.WrappedTargetException e) {
277 // Some exception occurs.FAILED
278 log.println("!!! Couldn't create instance : " + e);
279 throw new StatusException("Can't create column instances.", e);
280 } catch (com.sun.star.beans.PropertyVetoException e) {
281 // Some exception occurs.FAILED
282 log.println("!!! Couldn't create instance : " + e);
283 throw new StatusException("Can't create column instances.", e);
284 } catch (com.sun.star.beans.UnknownPropertyException e) {
285 // Some exception occurs.FAILED
286 log.println("!!! Couldn't create instance : " + e);
287 throw new StatusException("Can't create column instances.", e);
290 XNameContainer aContainer = UnoRuntime.queryInterface(
291 XNameContainer.class, the_Model);
293 try {
294 aContainer.insertByName("First", aControl);
295 aContainer.insertByName("Second", aControl2);
296 } catch (com.sun.star.uno.Exception e) {
297 log.println("!!! Could't insert column Instance");
298 e.printStackTrace(log);
299 throw new StatusException("Can't insert columns", e);
302 //now get the OGridControl
303 try {
304 oObj = the_access.getControl(the_Model);
305 the_win = the_access.getControl(the_Model).getPeer();
306 the_kit = the_win.getToolkit();
307 aDevice = the_kit.createScreenCompatibleDevice(200, 200);
308 aGraphic = aDevice.createGraphics();
309 } catch (com.sun.star.uno.Exception e) {
310 log.println("Couldn't get GridControl");
311 e.printStackTrace(log);
312 throw new StatusException("Couldn't get GridControl", e);
316 // creating another window
317 aShape = FormTools.createControlShape(xTextDoc, 3000, 4500, 15000,
318 10000, "TextField");
320 WriterTools.getDrawPage(xTextDoc).add(aShape);
322 the_Model = aShape.getControl();
325 //Try to query XControlAccess
326 the_access = UnoRuntime.queryInterface(
327 XControlAccess.class,
328 xTextDoc.getCurrentController());
330 //now get the TextControl
331 XWindow win = null;
332 Object cntrl = null;
334 try {
335 cntrl = the_access.getControl(the_Model);
336 win = UnoRuntime.queryInterface(XWindow.class, cntrl);
337 } catch (com.sun.star.uno.Exception e) {
338 log.println("Couldn't get Control");
339 e.printStackTrace(log);
340 throw new StatusException("Couldn't get Control", e);
343 log.println("creating a new environment for object");
345 TestEnvironment tEnv = new TestEnvironment(oObj);
348 //Relations for XSelectionSupplier
349 tEnv.addObjRelation("Selections",
350 new Object[] {
351 new Object[] { Integer.valueOf(0) }, new Object[] { Integer.valueOf(1) }
353 tEnv.addObjRelation("Comparer",
354 new Comparator<Integer>() {
355 public int compare(Integer o1, Integer o2) {
356 return o1.compareTo(o2);
361 //Relation for XContainer
362 tEnv.addObjRelation("XContainer.Container", aContainer);
363 tEnv.addObjRelation("INSTANCE", aControl3);
364 tEnv.addObjRelation("INSTANCE2", aControl4);
367 //Adding ObjRelation for XView
368 tEnv.addObjRelation("GRAPHICS", aGraphic);
371 //Adding ObjRelation for XControl
372 tEnv.addObjRelation("CONTEXT", xTextDoc);
373 tEnv.addObjRelation("WINPEER", the_win);
374 tEnv.addObjRelation("TOOLKIT", the_kit);
375 tEnv.addObjRelation("MODEL", the_Model);
378 // Adding relation for XWindow
379 tEnv.addObjRelation("XWindow.AnotherWindow", win);
381 // Adding relation for XDispatch
382 URL url = new URL();
383 url.Complete = ".uno:FormSlots/moveToNext";
386 //url.Complete = ".uno:GridSlots/RowHeight";
387 //url.Complete = ".uno:GridSlots/RowHeight" ;
388 tEnv.addObjRelation("XDispatch.URL", url);
390 log.println("ImplName: " + utils.getImplName(oObj));
392 FormTools.switchDesignOf(Param.getMSF(), xTextDoc);
394 // adding relation for XUpdateBroadcaster
395 final XInterface ctrl = oObj;
396 final XLoadable formLoaderF = formLoader;
397 final XPropertySet ps = UnoRuntime.queryInterface(
398 XPropertySet.class, aControl2);
399 tEnv.addObjRelation("XUpdateBroadcaster.Checker",
400 new ifc.form._XUpdateBroadcaster.UpdateChecker() {
401 private String lastText = "";
403 public void update() throws com.sun.star.uno.Exception {
404 if (!formLoaderF.isLoaded()) {
405 formLoaderF.load();
408 lastText = "_" + ps.getPropertyValue("Text");
409 ps.setPropertyValue("Text", lastText);
412 public void commit() throws com.sun.star.sdbc.SQLException {
413 XBoundComponent bound = UnoRuntime.queryInterface(
414 XBoundComponent.class, ctrl);
415 XResultSetUpdate update = UnoRuntime.queryInterface(
416 XResultSetUpdate.class,
417 formLoaderF);
419 bound.commit();
420 update.updateRow();
423 public boolean wasCommited() throws com.sun.star.uno.Exception {
424 String getS = (String) ps.getPropertyValue("Text");
426 return lastText.equals(getS);
430 return tEnv;
431 } // finish method getTestEnvironment
433 public static XControlShape createGrid(XComponent oDoc, int height,
434 int width, int x, int y) {
435 Size size = new Size();
436 Point position = new Point();
437 XControlShape oCShape = null;
438 XControlModel aControl = null;
440 //get MSF
441 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface(
442 XMultiServiceFactory.class,
443 oDoc);
445 try {
446 Object oInt = oDocMSF.createInstance(
447 "com.sun.star.drawing.ControlShape");
448 Object aCon = oDocMSF.createInstance(
449 "com.sun.star.form.component.GridControl");
450 XPropertySet model_props = UnoRuntime.queryInterface(
451 XPropertySet.class, aCon);
452 model_props.setPropertyValue("DefaultControl",
453 "com.sun.star.form.control.InteractionGridControl");
454 aControl = UnoRuntime.queryInterface(
455 XControlModel.class, aCon);
456 oCShape = UnoRuntime.queryInterface(
457 XControlShape.class, oInt);
458 size.Height = height;
459 size.Width = width;
460 position.X = x;
461 position.Y = y;
462 oCShape.setSize(size);
463 oCShape.setPosition(position);
464 } catch (com.sun.star.uno.Exception e) {
465 // Some exception occurs.FAILED
466 System.out.println("Couldn't create Grid" + e);
467 throw new StatusException("Couldn't create Grid", e);
470 oCShape.setControl(aControl);
472 return oCShape;
473 } // finish createGrid