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