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
;
25 import lib
.TestEnvironment
;
26 import lib
.TestParameters
;
27 import util
.AccessibilityTools
;
28 import util
.FormTools
;
29 import util
.SOfficeFactory
;
30 import util
.WriterTools
;
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
;
63 * Test for object which represents the control of the Grid model. <p>
64 * Object implements the following interfaces :
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>
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 protected void initialize(TestParameters Param
, PrintWriter log
) {
128 SOfficeFactory SOF
= SOfficeFactory
.getFactory((XMultiServiceFactory
)Param
.getMSF());
131 log
.println("creating a textdocument");
132 xTextDoc
= SOF
.createTextDoc(null);
133 } catch (com
.sun
.star
.uno
.Exception e
) {
134 // Some exception occurs.FAILED
135 e
.printStackTrace(log
);
136 throw new StatusException("Couldn't create document", e
);
141 * Disposes Writer document.
143 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
144 //closing the appearing dialog before disposing the document
145 XInterface toolkit
= null;
148 toolkit
= (XInterface
) ((XMultiServiceFactory
)tParam
.getMSF())
149 .createInstance("com.sun.star.awt.Toolkit");
150 } catch (com
.sun
.star
.uno
.Exception e
) {
151 log
.println("Couldn't get toolkit");
152 e
.printStackTrace(log
);
153 throw new StatusException("Couldn't get toolkit", e
);
156 XExtendedToolkit tk
= UnoRuntime
.queryInterface(
157 XExtendedToolkit
.class, toolkit
);
159 Object atw
= tk
.getActiveTopWindow();
161 XWindow xWindow
= UnoRuntime
.queryInterface(XWindow
.class,
164 XAccessible xRoot
= AccessibilityTools
.getAccessibleObject(xWindow
);
166 XInterface button
= AccessibilityTools
.getAccessibleObjectForRole(xRoot
,
167 AccessibleRole
.PUSH_BUTTON
);
169 XAccessibleAction action
= UnoRuntime
.queryInterface(
170 XAccessibleAction
.class, button
);
173 action
.doAccessibleAction(0);
174 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException iob
) {
175 log
.println("couldn't close dialog");
176 } catch (com
.sun
.star
.lang
.DisposedException e
) {
177 log
.println("couldn't close dialog");
180 log
.println(" disposing xTextDoc ");
183 XCloseable closer
= UnoRuntime
.queryInterface(
184 XCloseable
.class, xTextDoc
);
186 } catch (com
.sun
.star
.util
.CloseVetoException e
) {
187 log
.println("couldn't close document");
188 } catch (com
.sun
.star
.lang
.DisposedException e
) {
189 log
.println("couldn't close document");
194 * Creating a Testenvironment for the interfaces to be tested.
195 * For object creation first a
196 * <code>com.sun.star.form.component.GridControl<code> instance
197 * is added to the <code>ControlShape</code>. Then this model's
198 * control is retrieved.
200 * Object relations created :
202 * <li> <code>'GRAPHICS'</code> for
203 * {@link ifc.awt._XView} test : <code>XGraphics</code>
204 * object different that belong to the object tested.</li>
205 * <li> <code>'CONTEXT'</code> for
206 * {@link ifc.awt._XControl} </li>
207 * <li> <code>'WINPEER'</code> for
208 * {@link ifc.awt._XControl} </li>
209 * <li> <code>'TOOLKIT'</code> for
210 * {@link ifc.awt._XControl} </li>
211 * <li> <code>'MODEL'</code> for
212 * {@link ifc.awt._XControl} </li>
213 * <li> <code>'XWindow.AnotherWindow'</code> for
214 * {@link ifc.awt._XWindow} for switching focus.</li>
215 * <li> <code>'XDispatch.URL'</code> for
216 * {@link ifc.frame._XDispatch} the url which moves
217 * DB cursor to the next row (".uno:FormSlots/moveToNext").</li>
218 * <li> <code>'XContainer.Container'</code> for
219 * {@link ifc.container._XContainer} as the component created
220 * doesn't support <code>XContainer</code> itself, but
221 * it is supported by its model. So this model is passed.</li>
222 * <li> <code>'INSTANCE'</code> for
223 * {@link ifc.container._XContainer} the instance to be
224 * inserted into collection. Is a column instance.</li>
227 protected TestEnvironment
createTestEnvironment(TestParameters Param
,
229 XInterface oObj
= null;
230 XWindowPeer the_win
= null;
231 XToolkit the_kit
= null;
232 XDevice aDevice
= null;
233 XGraphics aGraphic
= null;
234 XPropertySet aControl
= null;
235 XPropertySet aControl2
= null;
236 XPropertySet aControl3
= null;
237 XPropertySet aControl4
= null;
238 XGridColumnFactory columns
= null;
240 //Insert a ControlShape and get the ControlModel
241 XControlShape aShape
= createGrid(xTextDoc
, 3000, 4500, 15000, 10000);
243 XControlModel the_Model
= aShape
.getControl();
245 WriterTools
.getDrawPage(xTextDoc
).add(aShape
);
247 XLoadable formLoader
= FormTools
.bindForm(xTextDoc
);
249 //Try to query XControlAccess
250 XControlAccess the_access
= UnoRuntime
.queryInterface(
251 XControlAccess
.class,
252 xTextDoc
.getCurrentController());
255 columns
= UnoRuntime
.queryInterface(
256 XGridColumnFactory
.class, the_Model
);
257 aControl
= columns
.createColumn("TextField");
258 aControl
.setPropertyValue("DataField", "Identifier");
259 aControl
.setPropertyValue("Label", "Identifier");
260 aControl2
= columns
.createColumn("TextField");
261 aControl2
.setPropertyValue("DataField", "Publisher");
262 aControl2
.setPropertyValue("Label", "Publisher");
263 aControl3
= columns
.createColumn("TextField");
264 aControl3
.setPropertyValue("DataField", "Author");
265 aControl3
.setPropertyValue("Label", "Author");
266 aControl4
= columns
.createColumn("TextField");
267 aControl4
.setPropertyValue("DataField", "Title");
268 aControl4
.setPropertyValue("Label", "Title");
269 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
270 // Some exception occurs.FAILED
271 log
.println("!!! Couldn't create instance : " + e
);
272 throw new StatusException("Can't create column instances.", e
);
273 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
274 // Some exception occurs.FAILED
275 log
.println("!!! Couldn't create instance : " + e
);
276 throw new StatusException("Can't create column instances.", e
);
277 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
278 // Some exception occurs.FAILED
279 log
.println("!!! Couldn't create instance : " + e
);
280 throw new StatusException("Can't create column instances.", e
);
281 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
282 // Some exception occurs.FAILED
283 log
.println("!!! Couldn't create instance : " + e
);
284 throw new StatusException("Can't create column instances.", e
);
287 XNameContainer aContainer
= UnoRuntime
.queryInterface(
288 XNameContainer
.class, the_Model
);
291 aContainer
.insertByName("First", aControl
);
292 aContainer
.insertByName("Second", aControl2
);
293 } catch (com
.sun
.star
.uno
.Exception e
) {
294 log
.println("!!! Could't insert column Instance");
295 e
.printStackTrace(log
);
296 throw new StatusException("Can't insert columns", e
);
299 //now get the OGridControl
301 oObj
= the_access
.getControl(the_Model
);
302 the_win
= the_access
.getControl(the_Model
).getPeer();
303 the_kit
= the_win
.getToolkit();
304 aDevice
= the_kit
.createScreenCompatibleDevice(200, 200);
305 aGraphic
= aDevice
.createGraphics();
306 } catch (com
.sun
.star
.uno
.Exception e
) {
307 log
.println("Couldn't get GridControl");
308 e
.printStackTrace(log
);
309 throw new StatusException("Couldn't get GridControl", e
);
313 // creating another window
314 aShape
= FormTools
.createControlShape(xTextDoc
, 3000, 4500, 15000,
317 WriterTools
.getDrawPage(xTextDoc
).add(aShape
);
319 the_Model
= aShape
.getControl();
322 //Try to query XControlAccess
323 the_access
= UnoRuntime
.queryInterface(
324 XControlAccess
.class,
325 xTextDoc
.getCurrentController());
327 //now get the TextControl
332 cntrl
= the_access
.getControl(the_Model
);
333 win
= UnoRuntime
.queryInterface(XWindow
.class, cntrl
);
334 } catch (com
.sun
.star
.uno
.Exception e
) {
335 log
.println("Couldn't get Control");
336 e
.printStackTrace(log
);
337 throw new StatusException("Couldn't get Control", e
);
340 log
.println("creating a new environment for object");
342 TestEnvironment tEnv
= new TestEnvironment(oObj
);
345 //Relations for XSelectionSupplier
346 tEnv
.addObjRelation("Selections",
348 new Object
[] { new Integer(0) }, new Object
[] { new Integer(1) }
350 tEnv
.addObjRelation("Comparer",
351 new Comparator
<Integer
>() {
352 public int compare(Integer o1
, Integer o2
) {
353 return o1
.compareTo(o2
);
358 //Realtion for XContainer
359 tEnv
.addObjRelation("XContainer.Container", aContainer
);
360 tEnv
.addObjRelation("INSTANCE", aControl3
);
361 tEnv
.addObjRelation("INSTANCE2", aControl4
);
364 //Adding ObjRelation for XView
365 tEnv
.addObjRelation("GRAPHICS", aGraphic
);
368 //Adding ObjRelation for XControl
369 tEnv
.addObjRelation("CONTEXT", xTextDoc
);
370 tEnv
.addObjRelation("WINPEER", the_win
);
371 tEnv
.addObjRelation("TOOLKIT", the_kit
);
372 tEnv
.addObjRelation("MODEL", the_Model
);
375 // Adding relation for XWindow
376 tEnv
.addObjRelation("XWindow.AnotherWindow", win
);
378 // Adding relation for XDispatch
380 url
.Complete
= ".uno:FormSlots/moveToNext";
383 //url.Complete = ".uno:GridSlots/RowHeight";
384 //url.Complete = ".uno:GridSlots/RowHeight" ;
385 tEnv
.addObjRelation("XDispatch.URL", url
);
387 log
.println("ImplName: " + utils
.getImplName(oObj
));
389 FormTools
.switchDesignOf((XMultiServiceFactory
)Param
.getMSF(), xTextDoc
);
391 // adding relation for XUpdateBroadcaster
392 final XInterface ctrl
= oObj
;
393 final XLoadable formLoaderF
= formLoader
;
394 final XPropertySet ps
= UnoRuntime
.queryInterface(
395 XPropertySet
.class, aControl2
);
396 tEnv
.addObjRelation("XUpdateBroadcaster.Checker",
397 new ifc
.form
._XUpdateBroadcaster
.UpdateChecker() {
398 private String lastText
= "";
400 public void update() throws com
.sun
.star
.uno
.Exception
{
401 if (!formLoaderF
.isLoaded()) {
405 lastText
= "_" + ps
.getPropertyValue("Text");
406 ps
.setPropertyValue("Text", lastText
);
409 public void commit() throws com
.sun
.star
.sdbc
.SQLException
{
410 XBoundComponent bound
= UnoRuntime
.queryInterface(
411 XBoundComponent
.class, ctrl
);
412 XResultSetUpdate update
= UnoRuntime
.queryInterface(
413 XResultSetUpdate
.class,
420 public boolean wasCommited() throws com
.sun
.star
.uno
.Exception
{
421 String getS
= (String
) ps
.getPropertyValue("Text");
423 return lastText
.equals(getS
);
428 } // finish method getTestEnvironment
430 public static XControlShape
createGrid(XComponent oDoc
, int height
,
431 int width
, int x
, int y
) {
432 Size size
= new Size();
433 Point position
= new Point();
434 XControlShape oCShape
= null;
435 XControlModel aControl
= null;
438 XMultiServiceFactory oDocMSF
= UnoRuntime
.queryInterface(
439 XMultiServiceFactory
.class,
443 Object oInt
= oDocMSF
.createInstance(
444 "com.sun.star.drawing.ControlShape");
445 Object aCon
= oDocMSF
.createInstance(
446 "com.sun.star.form.component.GridControl");
447 XPropertySet model_props
= UnoRuntime
.queryInterface(
448 XPropertySet
.class, aCon
);
449 model_props
.setPropertyValue("DefaultControl",
450 "com.sun.star.form.control.InteractionGridControl");
451 aControl
= UnoRuntime
.queryInterface(
452 XControlModel
.class, aCon
);
453 oCShape
= UnoRuntime
.queryInterface(
454 XControlShape
.class, oInt
);
455 size
.Height
= height
;
459 oCShape
.setSize(size
);
460 oCShape
.setPosition(position
);
461 } catch (com
.sun
.star
.uno
.Exception e
) {
462 // Some exception occurs.FAILED
463 System
.out
.println("Couldn't create Grid" + e
);
464 throw new StatusException("Couldn't create Grid", e
);
467 oCShape
.setControl(aControl
);
470 } // finish createGrid