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