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