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