xref: /AOO41X/main/qadevOOo/tests/java/mod/_dbaccess/ODatasourceBrowser.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 
31 import lib.StatusException;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
35 import util.DesktopTools;
36 import util.FormTools;
37 import util.SOfficeFactory;
38 import util.WriterTools;
39 
40 import com.sun.star.awt.XControl;
41 import com.sun.star.awt.XControlModel;
42 import com.sun.star.awt.XWindow;
43 import com.sun.star.beans.PropertyValue;
44 import com.sun.star.drawing.XControlShape;
45 import com.sun.star.drawing.XShape;
46 import com.sun.star.frame.XController;
47 import com.sun.star.frame.XDesktop;
48 import com.sun.star.frame.XDispatch;
49 import com.sun.star.frame.XDispatchProvider;
50 import com.sun.star.frame.XFrame;
51 import com.sun.star.frame.XModel;
52 import com.sun.star.lang.XInitialization;
53 import com.sun.star.lang.XMultiServiceFactory;
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;
60 
61 
62 /**
63  * Test for object which is represented by service
64  * <code>com.sun.star.sdb.DataSourceBrowser</code>. <p>
65  * Object implements the following interfaces :
66  * <ul>
67  *  <li> <code>com::sun::star::container::XChild</code></li>
68  *  <li> <code>com::sun::star::lang::XInitialization</code></li>
69  *  <li> <code>com::sun::star::util::XModifyBroadcaster</code></li>
70  *  <li> <code>com::sun::star::awt::XTabController</code></li>
71  *  <li> <code>com::sun::star::form::XFormController</code></li>
72  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
73  *  <li> <code>com::sun::star::frame::XDispatchProvider</code></li>
74  *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
75  *  <li> <code>com::sun::star::frame::XController</code></li>
76  *  <li> <code>com::sun::star::lang::XComponent</code></li>
77  * </ul> <p>
78  * This object test <b> is NOT </b> designed to be run in several
79  * threads concurently.
80  *
81  * @see com.sun.star.container.XChild
82  * @see com.sun.star.lang.XInitialization
83  * @see com.sun.star.util.XModifyBroadcaster
84  * @see com.sun.star.awt.XTabController
85  * @see com.sun.star.form.XFormController
86  * @see com.sun.star.container.XElementAccess
87  * @see com.sun.star.frame.XDispatchProvider
88  * @see com.sun.star.container.XEnumerationAccess
89  * @see com.sun.star.frame.XController
90  * @see com.sun.star.lang.XComponent
91  * @see ifc.container._XChild
92  * @see ifc.lang._XInitialization
93  * @see ifc.util._XModifyBroadcaster
94  * @see ifc.awt._XTabController
95  * @see ifc.form._XFormController
96  * @see ifc.container._XElementAccess
97  * @see ifc.frame._XDispatchProvider
98  * @see ifc.container._XEnumerationAccess
99  * @see ifc.frame._XController
100  * @see ifc.lang._XComponent
101  */
102 public class ODatasourceBrowser extends TestCase {
103     XDesktop the_Desk;
104     XTextDocument xTextDoc;
105 
106     /**
107      * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
108      */
109     protected void initialize(TestParameters Param, PrintWriter log) {
110         the_Desk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,
111                                                         DesktopTools.createDesktop(
112                                                                 (XMultiServiceFactory)Param.getMSF()));
113         System.setProperty("hideMe", "false");
114     }
115 
116     /**
117      * Disposes the document, if exists, created in
118      * <code>createTestEnvironment</code> method.
119      */
120     protected void cleanup(TestParameters Param, PrintWriter log) {
121         log.println("disposing xTextDoc");
122         System.setProperty("hideMe", "true");
123 
124         if (xTextDoc != null) {
125             log.println("    disposing xTextDoc ");
126 
127             try {
128                 XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
129                                             XCloseable.class, xTextDoc);
130                 closer.close(true);
131             } catch (com.sun.star.util.CloseVetoException e) {
132                 log.println("couldn't close document");
133             } catch (com.sun.star.lang.DisposedException e) {
134                 log.println("couldn't close document");
135             }
136         }
137     }
138 
139     /**
140      * Creating a Testenvironment for the interfaces to be tested.
141      *
142      * Creates a new text document disposing the old one if it was
143      * created. Using <code>Desktop</code> service get the frame
144      * of the document and with its help creates
145      * <code>DataSourceBrowser</code> dispatching the URL
146      * <code>'.component:DB/DataSourceBrowser'</code>. The
147      * component for testing is the controller of the
148      * <code>Browser</code> and it's got by searching its
149      * frame with the help of TextDocument frame, and obtaining
150      * the frame's controller. <p>
151      *
152      * <b>Note</b>: after creating the text document a short
153      * pause is needed to give a possibility to a frame to be
154      * created for the document. Else
155      * <code>Desktop.getCurrentFrame()</code> method can return
156      * <code>null</code> value. <p>
157      *
158      *     Object relations created :
159      * <ul>
160      *  <li> <code>'XDispatchProvider.URL'</code> for
161      *      {@link ifc.frame._XDispatchProvider} </li>
162      *  <li> <code>'SecondModel'</code> for
163      *      {@link ifc.frame._XController} : the model of
164      *      the TextDocument. </li>
165      *  <li> <code>'otherWindow'</code> for
166      *      {@link ifc.frame._XController} : the window of
167      *      the added shape.  </li>
168      *  <li> <code>'SecondController'</code> for
169      *      {@link ifc.frame._XController} : the controller of
170      *      the TextDocument. </li>
171      *  <li> <code>'HasViewData'</code> for
172      *      {@link ifc.frame._XController} : the
173      *      <code>DataSourceBrowser</code> has no view data. </li>
174      *  <li> <code>'XInitialization.args'</code> for
175      *      {@link ifc.lang._XInitialization} : the arguments for
176      *      tbe initialization</li>
177      * </ul>
178      *
179      * @see com.sun.star.frame.Desktop
180      * @see com.sun.star.frame.XModel
181      * @see com.sun.star.frame.XFrame
182      * @see com.sun.star.frame.XController
183      * @see com.sun.star.frame.XDispatchProvider
184      */
185     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
186                                                                  PrintWriter log) {
187         log.println("creating a test environment");
188 
189         if (xTextDoc != null) {
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         // get a soffice factory object
204         SOfficeFactory SOF = SOfficeFactory.getFactory(((XMultiServiceFactory) Param.getMSF()));
205 
206         try {
207             log.println("creating a text document");
208             xTextDoc = SOF.createTextDoc(null);
209         } catch (com.sun.star.uno.Exception e) {
210             // Some exception occures.FAILED
211             e.printStackTrace(log);
212             throw new StatusException("Couldn't create document", e);
213         }
214 
215         shortWait();
216 
217         XModel aModel1 = (XModel) UnoRuntime.queryInterface(XModel.class,
218                                                             xTextDoc);
219 
220         XController secondController = aModel1.getCurrentController();
221 
222         XFrame the_frame1 = the_Desk.getCurrentFrame();
223 
224         if (the_frame1 == null) {
225             log.println("Current frame was not found !!!");
226         }
227 
228         XDispatchProvider aProv = (XDispatchProvider) UnoRuntime.queryInterface(
229                                           XDispatchProvider.class, the_frame1);
230 
231         XDispatch getting = null;
232 
233         log.println("opening DatasourceBrowser");
234 
235         URL the_url = new URL();
236         the_url.Complete = ".component:DB/DataSourceBrowser";
237         getting = aProv.queryDispatch(the_url, "_beamer", 12);
238 
239         PropertyValue[] noArgs = new PropertyValue[0];
240         getting.dispatch(the_url, noArgs);
241 
242         XFrame the_frame2 = the_frame1.findFrame("_beamer", 4);
243 
244         the_frame2.setName("DatasourceBrowser");
245 
246         XInterface oObj = the_frame2.getController();
247 
248         Object[] params = new Object[3];
249         PropertyValue param1 = new PropertyValue();
250         param1.Name = "DataSourceName";
251         param1.Value = "Bibliography";
252         params[0] = param1;
253 
254         PropertyValue param2 = new PropertyValue();
255         param2.Name = "CommandType";
256         param2.Value = new Integer(com.sun.star.sdb.CommandType.TABLE);
257         params[1] = param2;
258 
259         PropertyValue param3 = new PropertyValue();
260         param3.Name = "Command";
261         param3.Value = "biblio";
262         params[2] = param3;
263 
264         try {
265             XInitialization xInit = (XInitialization) UnoRuntime.queryInterface(
266                                             XInitialization.class, oObj);
267             xInit.initialize(params);
268         } catch (com.sun.star.uno.Exception e) {
269             // Some exception occures.FAILED
270             e.printStackTrace(log);
271             throw new StatusException("Couldn't initialize document", e);
272         }
273 
274         shortWait();
275 
276         XControlShape aShape = FormTools.createControlShape(xTextDoc, 3000,
277                                                             4500, 15000, 10000,
278                                                             "CommandButton");
279         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
280 
281         XControlModel shapeModel = aShape.getControl();
282 
283         XControlAccess xCtrlAccess = (XControlAccess) UnoRuntime.queryInterface(
284                                              XControlAccess.class,
285                                              secondController);
286         XControl xCtrl = null;
287 
288         try {
289             xCtrl = xCtrlAccess.getControl(shapeModel);
290         } catch (com.sun.star.uno.Exception e) {
291             // Some exception occures.FAILED
292             e.printStackTrace(log);
293         }
294 
295         XWindow docWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
296                                                                 xCtrl);
297         log.println("creating a new environment for ODatasourceBrowser object");
298 
299         TestEnvironment tEnv = new TestEnvironment(oObj);
300 
301 
302         //Adding ObjRelations for XInitialization
303         tEnv.addObjRelation("XInitialization.args", params);
304 
305 
306         //Adding ObjRelations for XController
307         tEnv.addObjRelation("Frame", the_frame1);
308         tEnv.addObjRelation("SecondModel", aModel1);
309         tEnv.addObjRelation("otherWindow", docWindow);
310         tEnv.addObjRelation("SecondController", secondController);
311         tEnv.addObjRelation("HasViewData", new Boolean(false));
312 
313 
314         // Addig relation for XDispatchProvider
315         tEnv.addObjRelation("XDispatchProvider.URL",
316                             ".uno:DataSourceBrowser/FormLetter");
317 
318         return tEnv;
319     } // finish method getTestEnvironment
320 
321     /**
322     * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
323     * reset</code> call.
324     */
325     private void shortWait() {
326         try {
327             Thread.sleep(5000);
328         } catch (InterruptedException e) {
329             System.out.println("While waiting :" + e);
330         }
331     }
332 } // finish class oDatasourceBrowser
333