merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _dbaccess / ODatasourceBrowser.java
blob1efd08e611d880204ff1fb91c213217d8de2e034
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ODatasourceBrowser.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package mod._dbaccess;
32 import java.io.PrintWriter;
34 import lib.StatusException;
35 import lib.TestCase;
36 import lib.TestEnvironment;
37 import lib.TestParameters;
38 import util.DesktopTools;
39 import util.FormTools;
40 import util.SOfficeFactory;
41 import util.WriterTools;
43 import com.sun.star.awt.XControl;
44 import com.sun.star.awt.XControlModel;
45 import com.sun.star.awt.XWindow;
46 import com.sun.star.beans.PropertyValue;
47 import com.sun.star.drawing.XControlShape;
48 import com.sun.star.drawing.XShape;
49 import com.sun.star.frame.XController;
50 import com.sun.star.frame.XDesktop;
51 import com.sun.star.frame.XDispatch;
52 import com.sun.star.frame.XDispatchProvider;
53 import com.sun.star.frame.XFrame;
54 import com.sun.star.frame.XModel;
55 import com.sun.star.lang.XInitialization;
56 import com.sun.star.lang.XMultiServiceFactory;
57 import com.sun.star.text.XTextDocument;
58 import com.sun.star.uno.UnoRuntime;
59 import com.sun.star.uno.XInterface;
60 import com.sun.star.util.URL;
61 import com.sun.star.util.XCloseable;
62 import com.sun.star.view.XControlAccess;
65 /**
66 * Test for object which is represented by service
67 * <code>com.sun.star.sdb.DataSourceBrowser</code>. <p>
68 * Object implements the following interfaces :
69 * <ul>
70 * <li> <code>com::sun::star::container::XChild</code></li>
71 * <li> <code>com::sun::star::lang::XInitialization</code></li>
72 * <li> <code>com::sun::star::util::XModifyBroadcaster</code></li>
73 * <li> <code>com::sun::star::awt::XTabController</code></li>
74 * <li> <code>com::sun::star::form::XFormController</code></li>
75 * <li> <code>com::sun::star::container::XElementAccess</code></li>
76 * <li> <code>com::sun::star::frame::XDispatchProvider</code></li>
77 * <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
78 * <li> <code>com::sun::star::frame::XController</code></li>
79 * <li> <code>com::sun::star::lang::XComponent</code></li>
80 * </ul> <p>
81 * This object test <b> is NOT </b> designed to be run in several
82 * threads concurently.
84 * @see com.sun.star.container.XChild
85 * @see com.sun.star.lang.XInitialization
86 * @see com.sun.star.util.XModifyBroadcaster
87 * @see com.sun.star.awt.XTabController
88 * @see com.sun.star.form.XFormController
89 * @see com.sun.star.container.XElementAccess
90 * @see com.sun.star.frame.XDispatchProvider
91 * @see com.sun.star.container.XEnumerationAccess
92 * @see com.sun.star.frame.XController
93 * @see com.sun.star.lang.XComponent
94 * @see ifc.container._XChild
95 * @see ifc.lang._XInitialization
96 * @see ifc.util._XModifyBroadcaster
97 * @see ifc.awt._XTabController
98 * @see ifc.form._XFormController
99 * @see ifc.container._XElementAccess
100 * @see ifc.frame._XDispatchProvider
101 * @see ifc.container._XEnumerationAccess
102 * @see ifc.frame._XController
103 * @see ifc.lang._XComponent
105 public class ODatasourceBrowser extends TestCase {
106 XDesktop the_Desk;
107 XTextDocument xTextDoc;
110 * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
112 protected void initialize(TestParameters Param, PrintWriter log) {
113 the_Desk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,
114 DesktopTools.createDesktop(
115 (XMultiServiceFactory)Param.getMSF()));
116 System.setProperty("hideMe", "false");
120 * Disposes the document, if exists, created in
121 * <code>createTestEnvironment</code> method.
123 protected void cleanup(TestParameters Param, PrintWriter log) {
124 log.println("disposing xTextDoc");
125 System.setProperty("hideMe", "true");
127 if (xTextDoc != null) {
128 log.println(" disposing xTextDoc ");
130 try {
131 XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
132 XCloseable.class, xTextDoc);
133 closer.close(true);
134 } catch (com.sun.star.util.CloseVetoException e) {
135 log.println("couldn't close document");
136 } catch (com.sun.star.lang.DisposedException e) {
137 log.println("couldn't close document");
143 * Creating a Testenvironment for the interfaces to be tested.
145 * Creates a new text document disposing the old one if it was
146 * created. Using <code>Desktop</code> service get the frame
147 * of the document and with its help creates
148 * <code>DataSourceBrowser</code> dispatching the URL
149 * <code>'.component:DB/DataSourceBrowser'</code>. The
150 * component for testing is the controller of the
151 * <code>Browser</code> and it's got by searching its
152 * frame with the help of TextDocument frame, and obtaining
153 * the frame's controller. <p>
155 * <b>Note</b>: after creating the text document a short
156 * pause is needed to give a possibility to a frame to be
157 * created for the document. Else
158 * <code>Desktop.getCurrentFrame()</code> method can return
159 * <code>null</code> value. <p>
161 * Object relations created :
162 * <ul>
163 * <li> <code>'XDispatchProvider.URL'</code> for
164 * {@link ifc.frame._XDispatchProvider} </li>
165 * <li> <code>'SecondModel'</code> for
166 * {@link ifc.frame._XController} : the model of
167 * the TextDocument. </li>
168 * <li> <code>'otherWindow'</code> for
169 * {@link ifc.frame._XController} : the window of
170 * the added shape. </li>
171 * <li> <code>'SecondController'</code> for
172 * {@link ifc.frame._XController} : the controller of
173 * the TextDocument. </li>
174 * <li> <code>'HasViewData'</code> for
175 * {@link ifc.frame._XController} : the
176 * <code>DataSourceBrowser</code> has no view data. </li>
177 * <li> <code>'XInitialization.args'</code> for
178 * {@link ifc.lang._XInitialization} : the arguments for
179 * tbe initialization</li>
180 * </ul>
182 * @see com.sun.star.frame.Desktop
183 * @see com.sun.star.frame.XModel
184 * @see com.sun.star.frame.XFrame
185 * @see com.sun.star.frame.XController
186 * @see com.sun.star.frame.XDispatchProvider
188 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
189 PrintWriter log) {
190 log.println("creating a test environment");
192 if (xTextDoc != null) {
193 log.println(" disposing xTextDoc ");
195 try {
196 XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
197 XCloseable.class, xTextDoc);
198 closer.close(true);
199 } catch (com.sun.star.util.CloseVetoException e) {
200 log.println("couldn't close document");
201 } catch (com.sun.star.lang.DisposedException e) {
202 log.println("couldn't close document");
206 // get a soffice factory object
207 SOfficeFactory SOF = SOfficeFactory.getFactory(((XMultiServiceFactory) Param.getMSF()));
209 try {
210 log.println("creating a text document");
211 xTextDoc = SOF.createTextDoc(null);
212 } catch (com.sun.star.uno.Exception e) {
213 // Some exception occures.FAILED
214 e.printStackTrace(log);
215 throw new StatusException("Couldn't create document", e);
218 shortWait();
220 XModel aModel1 = (XModel) UnoRuntime.queryInterface(XModel.class,
221 xTextDoc);
223 XController secondController = aModel1.getCurrentController();
225 XFrame the_frame1 = the_Desk.getCurrentFrame();
227 if (the_frame1 == null) {
228 log.println("Current frame was not found !!!");
231 XDispatchProvider aProv = (XDispatchProvider) UnoRuntime.queryInterface(
232 XDispatchProvider.class, the_frame1);
234 XDispatch getting = null;
236 log.println("opening DatasourceBrowser");
238 URL the_url = new URL();
239 the_url.Complete = ".component:DB/DataSourceBrowser";
240 getting = aProv.queryDispatch(the_url, "_beamer", 12);
242 PropertyValue[] noArgs = new PropertyValue[0];
243 getting.dispatch(the_url, noArgs);
245 XFrame the_frame2 = the_frame1.findFrame("_beamer", 4);
247 the_frame2.setName("DatasourceBrowser");
249 XInterface oObj = the_frame2.getController();
251 Object[] params = new Object[3];
252 PropertyValue param1 = new PropertyValue();
253 param1.Name = "DataSourceName";
254 param1.Value = "Bibliography";
255 params[0] = param1;
257 PropertyValue param2 = new PropertyValue();
258 param2.Name = "CommandType";
259 param2.Value = new Integer(com.sun.star.sdb.CommandType.TABLE);
260 params[1] = param2;
262 PropertyValue param3 = new PropertyValue();
263 param3.Name = "Command";
264 param3.Value = "biblio";
265 params[2] = param3;
267 try {
268 XInitialization xInit = (XInitialization) UnoRuntime.queryInterface(
269 XInitialization.class, oObj);
270 xInit.initialize(params);
271 } catch (com.sun.star.uno.Exception e) {
272 // Some exception occures.FAILED
273 e.printStackTrace(log);
274 throw new StatusException("Couldn't initialize document", e);
277 shortWait();
279 XControlShape aShape = FormTools.createControlShape(xTextDoc, 3000,
280 4500, 15000, 10000,
281 "CommandButton");
282 WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
284 XControlModel shapeModel = aShape.getControl();
286 XControlAccess xCtrlAccess = (XControlAccess) UnoRuntime.queryInterface(
287 XControlAccess.class,
288 secondController);
289 XControl xCtrl = null;
291 try {
292 xCtrl = xCtrlAccess.getControl(shapeModel);
293 } catch (com.sun.star.uno.Exception e) {
294 // Some exception occures.FAILED
295 e.printStackTrace(log);
298 XWindow docWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
299 xCtrl);
300 log.println("creating a new environment for ODatasourceBrowser object");
302 TestEnvironment tEnv = new TestEnvironment(oObj);
305 //Adding ObjRelations for XInitialization
306 tEnv.addObjRelation("XInitialization.args", params);
309 //Adding ObjRelations for XController
310 tEnv.addObjRelation("Frame", the_frame1);
311 tEnv.addObjRelation("SecondModel", aModel1);
312 tEnv.addObjRelation("otherWindow", docWindow);
313 tEnv.addObjRelation("SecondController", secondController);
314 tEnv.addObjRelation("HasViewData", new Boolean(false));
317 // Addig relation for XDispatchProvider
318 tEnv.addObjRelation("XDispatchProvider.URL",
319 ".uno:DataSourceBrowser/FormLetter");
321 return tEnv;
322 } // finish method getTestEnvironment
325 * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
326 * reset</code> call.
328 private void shortWait() {
329 try {
330 Thread.sleep(5000);
331 } catch (InterruptedException e) {
332 System.out.println("While waiting :" + e);
335 } // finish class oDatasourceBrowser