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