1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package mod._forms; 28 29 import java.io.PrintWriter; 30 31 import lib.StatusException; 32 import lib.TestCase; 33 import lib.TestEnvironment; 34 import lib.TestParameters; 35 import util.FormTools; 36 import util.SOfficeFactory; 37 import util.WriterTools; 38 39 import com.sun.star.awt.XControl; 40 import com.sun.star.awt.XControlModel; 41 import com.sun.star.awt.XDevice; 42 import com.sun.star.awt.XGraphics; 43 import com.sun.star.awt.XTextComponent; 44 import com.sun.star.awt.XToolkit; 45 import com.sun.star.awt.XWindow; 46 import com.sun.star.awt.XWindowPeer; 47 import com.sun.star.drawing.XControlShape; 48 import com.sun.star.drawing.XShape; 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.XCloseable; 54 import com.sun.star.view.XControlAccess; 55 56 57 /** 58 * Test for object which is represented by default controller 59 * of the <code>com.sun.star.form.component.TimeField</code> 60 * component. <p> 61 * 62 * Object implements the following interfaces : 63 * <ul> 64 * <li> <code>com::sun::star::awt::XView</code></li> 65 * <li> <code>com::sun::star::form::XBoundControl</code></li> 66 * <li> <code>com::sun::star::awt::XControl</code></li> 67 * <li> <code>com::sun::star::awt::XTextComponent</code></li> 68 * <li> <code>com::sun::star::awt::XLayoutConstrains</code></li> 69 * <li> <code>com::sun::star::awt::XTimeField</code></li> 70 * <li> <code>com::sun::star::awt::XTextListener</code></li> 71 * <li> <code>com::sun::star::awt::XWindow</code></li> 72 * <li> <code>com::sun::star::lang::XComponent</code></li> 73 * <li> <code>com::sun::star::awt::XTextLayoutConstrains</code></li> 74 * <li> <code>com::sun::star::lang::XEventListener</code></li> 75 * </ul> <p> 76 * This object test <b> is NOT </b> designed to be run in several 77 * threads concurently. 78 * 79 * @see com.sun.star.awt.XView 80 * @see com.sun.star.form.XBoundControl 81 * @see com.sun.star.awt.XControl 82 * @see com.sun.star.awt.XTextComponent 83 * @see com.sun.star.awt.XLayoutConstrains 84 * @see com.sun.star.awt.XTimeField 85 * @see com.sun.star.awt.XTextListener 86 * @see com.sun.star.awt.XWindow 87 * @see com.sun.star.lang.XComponent 88 * @see com.sun.star.awt.XTextLayoutConstrains 89 * @see com.sun.star.lang.XEventListener 90 * @see ifc.awt._XView 91 * @see ifc.form._XBoundControl 92 * @see ifc.awt._XControl 93 * @see ifc.awt._XTextComponent 94 * @see ifc.awt._XLayoutConstrains 95 * @see ifc.awt._XTimeField 96 * @see ifc.awt._XTextListener 97 * @see ifc.awt._XWindow 98 * @see ifc.lang._XComponent 99 * @see ifc.awt._XTextLayoutConstrains 100 * @see ifc.lang._XEventListener 101 */ 102 public class OTimeControl extends TestCase { 103 XTextDocument xTextDoc; 104 105 /** 106 * Creates a new text document. 107 */ 108 protected void initialize(TestParameters Param, PrintWriter log) { 109 SOfficeFactory SOF = SOfficeFactory.getFactory(((XMultiServiceFactory) Param.getMSF())); 110 111 try { 112 log.println("creating a textdocument"); 113 xTextDoc = SOF.createTextDoc(null); 114 } catch (com.sun.star.uno.Exception e) { 115 // Some exception occures.FAILED 116 e.printStackTrace(log); 117 throw new StatusException("Couldn't create document", e); 118 } 119 } 120 121 /** 122 * Disposes the text document created before 123 */ 124 protected void cleanup(TestParameters tParam, PrintWriter log) { 125 log.println(" disposing xTextDoc "); 126 127 try { 128 XCloseable closer = (XCloseable) UnoRuntime.queryInterface( 129 XCloseable.class, xTextDoc); 130 closer.close(true); 131 } catch (com.sun.star.util.CloseVetoException e) { 132 log.println("couldn't close document"); 133 } catch (com.sun.star.lang.DisposedException e) { 134 log.println("couldn't close document"); 135 } 136 } 137 138 /** 139 * Creates two components and inserts them to the form of 140 * text document. One component 141 * (<code>com.sun.star.form.component.TimeField</code>) is created 142 * for testing, another to be passed as relation. Using a controller 143 * of the text document the controller of the first component is 144 * obtained and returned in environment as a test object. <p> 145 * 146 * Object relations created : 147 * <ul> 148 * <li> <code>'GRAPHICS'</code> for 149 * {@link ifc.awt._XView} : a graphics component 150 * created using screen device of the window peer of 151 * the controller tested. </li> 152 * <li> <code>'CONTEXT'</code> for 153 * {@link ifc.awt._XControl} : the text document 154 * where the component is inserted. </li> 155 * <li> <code>'WINPEER'</code> for 156 * {@link ifc.awt._XControl} : Window peer of the 157 * controller tested. </li> 158 * <li> <code>'TOOLKIT'</code> for 159 * {@link ifc.awt._XControl} : toolkit of the component.</li> 160 * <li> <code>'MODEL'</code> for 161 * {@link ifc.awt._XControl} : the model of the controller.</li> 162 * <li> <code>'XWindow.AnotherWindow'</code> for 163 * {@link ifc.awt._XWindow} : the controller of another 164 * component. </li> 165 * </ul> 166 */ 167 protected TestEnvironment createTestEnvironment(TestParameters Param, 168 PrintWriter log) { 169 XInterface oObj = null; 170 XWindowPeer the_win = null; 171 XToolkit the_kit = null; 172 XDevice aDevice = null; 173 XGraphics aGraphic = null; 174 XControl aControl = null; 175 176 //Insert a ControlShape and get the ControlModel 177 XControlShape aShape = FormTools.createControlShape(xTextDoc, 3000, 178 4500, 15000, 10000, 179 "TimeField"); 180 181 WriterTools.getDrawPage(xTextDoc).add((XShape) aShape); 182 183 XControlModel the_Model = aShape.getControl(); 184 185 XControlShape aShape2 = FormTools.createControlShape(xTextDoc, 3000, 186 4500, 5000, 10000, 187 "TextField"); 188 189 WriterTools.getDrawPage(xTextDoc).add((XShape) aShape2); 190 191 XControlModel the_Model2 = aShape2.getControl(); 192 193 //Try to query XControlAccess 194 XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface( 195 XControlAccess.class, 196 xTextDoc.getCurrentController()); 197 198 //now get the OTimeControl 199 try { 200 oObj = the_access.getControl(the_Model); 201 aControl = the_access.getControl(the_Model2); 202 the_win = the_access.getControl(the_Model).getPeer(); 203 the_kit = the_win.getToolkit(); 204 aDevice = the_kit.createScreenCompatibleDevice(200, 200); 205 aGraphic = aDevice.createGraphics(); 206 } catch (com.sun.star.container.NoSuchElementException e) { 207 log.println("Couldn't get OTimeControl"); 208 e.printStackTrace(log); 209 throw new StatusException("Couldn't get OTimeControl", e); 210 } 211 212 log.println("creating a new environment for OTimeControl object"); 213 214 TestEnvironment tEnv = new TestEnvironment(oObj); 215 216 217 //Adding ObjRelation for XView 218 tEnv.addObjRelation("GRAPHICS", aGraphic); 219 220 221 //Adding ObjRelation for XControl 222 tEnv.addObjRelation("CONTEXT", xTextDoc); 223 tEnv.addObjRelation("WINPEER", the_win); 224 tEnv.addObjRelation("TOOLKIT", the_kit); 225 tEnv.addObjRelation("MODEL", the_Model); 226 227 XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class, 228 aControl); 229 230 tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel); 231 tEnv.addObjRelation("XWindow.ControlShape", aShape); 232 233 // Adding relation for XTextListener 234 ifc.awt._XTextListener.TestTextListener listener = 235 new ifc.awt._XTextListener.TestTextListener(); 236 XTextComponent textComp = (XTextComponent) UnoRuntime.queryInterface( 237 XTextComponent.class, oObj); 238 textComp.addTextListener(listener); 239 tEnv.addObjRelation("TestTextListener", listener); 240 241 FormTools.switchDesignOf(((XMultiServiceFactory) Param.getMSF()), xTextDoc); 242 shortWait(); 243 244 return tEnv; 245 } // finish method getTestEnvironment 246 247 /** 248 * Sleeps for 0.2 sec. to allow StarOffice to react on <code> 249 * reset</code> call. 250 */ 251 private void shortWait() { 252 try { 253 Thread.sleep(200); 254 } catch (InterruptedException e) { 255 System.out.println("While waiting :" + e); 256 } 257 } 258 } // finish class OTimeControl 259