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