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