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 package mod._forms;
24 
25 import java.io.PrintWriter;
26 
27 import lib.StatusException;
28 import lib.TestCase;
29 import lib.TestEnvironment;
30 import lib.TestParameters;
31 import util.FormTools;
32 import util.SOfficeFactory;
33 import util.WriterTools;
34 
35 import com.sun.star.awt.XControl;
36 import com.sun.star.awt.XControlModel;
37 import com.sun.star.awt.XDevice;
38 import com.sun.star.awt.XGraphics;
39 import com.sun.star.awt.XTextComponent;
40 import com.sun.star.awt.XToolkit;
41 import com.sun.star.awt.XWindow;
42 import com.sun.star.awt.XWindowPeer;
43 import com.sun.star.drawing.XControlShape;
44 import com.sun.star.drawing.XShape;
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.text.XTextDocument;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 import com.sun.star.util.XCloseable;
50 import com.sun.star.view.XControlAccess;
51 
52 
53 /**
54  * Test for object which is represented by default controller
55  * of the <code>com.sun.star.form.component.TextField</code>
56  * component. <p>
57  *
58  * Object implements the following interfaces :
59  * <ul>
60  *  <li> <code>com::sun::star::awt::XView</code></li>
61  *  <li> <code>com::sun::star::form::XBoundControl</code></li>
62  *  <li> <code>com::sun::star::awt::XControl</code></li>
63  *  <li> <code>com::sun::star::awt::XTextComponent</code></li>
64  *  <li> <code>com::sun::star::awt::XLayoutConstrains</code></li>
65  *  <li> <code>com::sun::star::awt::XTextListener</code></li>
66  *  <li> <code>com::sun::star::form::XChangeBroadcaster</code></li>
67  *  <li> <code>com::sun::star::awt::XWindow</code></li>
68  *  <li> <code>com::sun::star::lang::XComponent</code></li>
69  *  <li> <code>com::sun::star::awt::XTextLayoutConstrains</code></li>
70  *  <li> <code>com::sun::star::lang::XEventListener</code></li>
71  * </ul> <p>
72  * This object test <b> is NOT </b> designed to be run in several
73  * threads concurently.
74  *
75  * @see com.sun.star.awt.XView
76  * @see com.sun.star.form.XBoundControl
77  * @see com.sun.star.awt.XControl
78  * @see com.sun.star.awt.XTextComponent
79  * @see com.sun.star.awt.XLayoutConstrains
80  * @see com.sun.star.awt.XTextListener
81  * @see com.sun.star.form.XChangeBroadcaster
82  * @see com.sun.star.awt.XWindow
83  * @see com.sun.star.lang.XComponent
84  * @see com.sun.star.awt.XTextLayoutConstrains
85  * @see com.sun.star.lang.XEventListener
86  * @see ifc.awt._XView
87  * @see ifc.form._XBoundControl
88  * @see ifc.awt._XControl
89  * @see ifc.awt._XTextComponent
90  * @see ifc.awt._XLayoutConstrains
91  * @see ifc.awt._XTextListener
92  * @see ifc.form._XChangeBroadcaster
93  * @see ifc.awt._XWindow
94  * @see ifc.lang._XComponent
95  * @see ifc.awt._XTextLayoutConstrains
96  * @see ifc.lang._XEventListener
97  */
98 public class OEditControl extends TestCase {
99     XTextDocument xTextDoc;
100 
101     /**
102      * Creates a new text document.
103      */
initialize(TestParameters Param, PrintWriter log)104     protected void initialize(TestParameters Param, PrintWriter log) {
105         SOfficeFactory SOF = SOfficeFactory.getFactory(((XMultiServiceFactory) Param.getMSF()));
106 
107         try {
108             log.println("creating a textdocument");
109             xTextDoc = SOF.createTextDoc(null);
110         } catch (com.sun.star.uno.Exception e) {
111             // Some exception occured.FAILED
112             e.printStackTrace(log);
113             throw new StatusException("Couldn't create document", e);
114         }
115     }
116 
117     /**
118      * Disposes the text document created before
119      */
cleanup(TestParameters tParam, PrintWriter log)120     protected void cleanup(TestParameters tParam, PrintWriter log) {
121         log.println("    disposing xTextDoc ");
122 
123         try {
124             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
125                                         XCloseable.class, xTextDoc);
126             closer.close(true);
127         } catch (com.sun.star.util.CloseVetoException e) {
128             log.println("couldn't close document");
129         } catch (com.sun.star.lang.DisposedException e) {
130             log.println("couldn't close document");
131         }
132     }
133 
134     /**
135      * Creates two components and inserts them to the form of
136      * text document. One component
137      * (<code>com.sun.star.form.component.TextField</code>) is created
138      * for testing, another to be passed as relation. Using a controller
139      * of the text document the controller of the first component is
140      * obtained and returned in environment as a test object. <p>
141      *
142      *     Object relations created :
143      * <ul>
144      *  <li> <code>'GRAPHICS'</code> for
145      *      {@link ifc.awt._XView} : a graphics component
146      *      created using screen device of the window peer of
147      *      the controller tested. </li>
148      *  <li> <code>'CONTEXT'</code> for
149      *      {@link ifc.awt._XControl} : the text document
150      *      where the component is inserted. </li>
151      *  <li> <code>'WINPEER'</code> for
152      *      {@link ifc.awt._XControl} : Window peer of the
153      *      controller tested. </li>
154      *  <li> <code>'TOOLKIT'</code> for
155      *      {@link ifc.awt._XControl} : toolkit of the component.</li>
156      *  <li> <code>'MODEL'</code> for
157      *      {@link ifc.awt._XControl} : the model of the controller.</li>
158      *  <li> <code>'XWindow.AnotherWindow'</code> for
159      *      {@link ifc.awt._XWindow} : the controller of another
160      *      component. </li>
161      *  <li> <code>'Win1'</code> for
162      *      {@link ifc.form._XChangeBroadcaster} : the window (controller)
163      *      of the tested component. </li>
164      *  <li> <code>'Win2'</code> for
165      *      {@link ifc.form._XChangeBroadcaster} : the window (controller)
166      *      of another component. </li>
167      *  <li> <code>'CONTROL'</code> for
168      *      {@link ifc.form._XChangeBroadcaster} : the controller
169      *      of another component. </li>
170      * </ul>
171      */
createTestEnvironment(TestParameters Param, PrintWriter log)172     protected TestEnvironment createTestEnvironment(TestParameters Param,
173                                                     PrintWriter log) {
174         XInterface oObj = null;
175         XControl aControl = null;
176         XWindowPeer the_win = null;
177         XToolkit the_kit = null;
178         XDevice aDevice = null;
179         XGraphics aGraphic = null;
180 
181         //Insert a ControlShape and get the ControlModel
182         XControlShape aShape = FormTools.createControlShape(xTextDoc, 3000,
183                                                             4500, 15000, 10000,
184                                                             "TextField");
185 
186         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
187 
188         XControlModel the_Model = aShape.getControl();
189 
190         XControlShape aShape2 = FormTools.createControlShape(xTextDoc, 3000,
191                                                              4500, 5000, 10000,
192                                                              "TextField");
193 
194         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape2);
195 
196         XControlModel the_Model2 = aShape2.getControl();
197 
198         //Try to query XControlAccess
199         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
200                                             XControlAccess.class,
201                                             xTextDoc.getCurrentController());
202 
203         //now get the OEditControl
204         try {
205             oObj = the_access.getControl(the_Model);
206             aControl = the_access.getControl(the_Model2);
207             the_win = the_access.getControl(the_Model).getPeer();
208             the_kit = the_win.getToolkit();
209             aDevice = the_kit.createScreenCompatibleDevice(200, 200);
210             aGraphic = aDevice.createGraphics();
211         } catch (com.sun.star.container.NoSuchElementException e) {
212             log.println("Couldn't get OEditControl");
213             e.printStackTrace(log);
214             throw new StatusException("Couldn't get OEditControl", e);
215         }
216 
217         log.println("creating a new environment for OEditControl object");
218 
219         TestEnvironment tEnv = new TestEnvironment(oObj);
220 
221 
222         //Adding ObjRelation for XView
223         tEnv.addObjRelation("GRAPHICS", aGraphic);
224 
225 
226         //Adding ObjRelation for XControl
227         tEnv.addObjRelation("CONTEXT", xTextDoc);
228         tEnv.addObjRelation("WINPEER", the_win);
229         tEnv.addObjRelation("TOOLKIT", the_kit);
230         tEnv.addObjRelation("MODEL", the_Model);
231 
232         // relations for XChangeBroadcaster
233         XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class,
234                                                                 aControl);
235 
236         tEnv.addObjRelation("Win1",
237                             (XWindow) UnoRuntime.queryInterface(XWindow.class,
238                                                                 oObj));
239         tEnv.addObjRelation("Win2", forObjRel);
240         tEnv.addObjRelation("CONTROL", aControl);
241 
242 
243         // relation for XWindow
244         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
245         tEnv.addObjRelation("XWindow.ControlShape", aShape);
246 
247         // Adding relation for XTextListener
248         ifc.awt._XTextListener.TestTextListener listener =
249                 new ifc.awt._XTextListener.TestTextListener();
250         XTextComponent textComp = (XTextComponent) UnoRuntime.queryInterface(
251                                           XTextComponent.class, oObj);
252         textComp.addTextListener(listener);
253         tEnv.addObjRelation("TestTextListener", listener);
254 
255         return tEnv;
256     } // finish method getTestEnvironment
257 } // finish class OEditControl
258