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