1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 package mod._forms;
19 
20 import java.io.PrintWriter;
21 
22 import lib.TestCase;
23 import lib.TestEnvironment;
24 import lib.TestParameters;
25 import util.FormTools;
26 import util.SOfficeFactory;
27 import util.WriterTools;
28 
29 import com.sun.star.awt.XControl;
30 import com.sun.star.awt.XControlModel;
31 import com.sun.star.awt.XDevice;
32 import com.sun.star.awt.XGraphics;
33 import com.sun.star.awt.XTextComponent;
34 import com.sun.star.awt.XToolkit;
35 import com.sun.star.awt.XWindow;
36 import com.sun.star.awt.XWindowPeer;
37 import com.sun.star.drawing.XControlShape;
38 import com.sun.star.text.XTextDocument;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 import com.sun.star.util.XCloseable;
42 import com.sun.star.view.XControlAccess;
43 
44 
45 /**
46  * Test for object which is represented by default controller
47  * of the <code>com.sun.star.form.component.CurrencyField</code>
48  * component. <p>
49  *
50  * Object implements the following interfaces :
51  * <ul>
52  *  <li> <code>com::sun::star::awt::XView</code></li>
53  *  <li> <code>com::sun::star::form::XBoundControl</code></li>
54  *  <li> <code>com::sun::star::awt::XControl</code></li>
55  *  <li> <code>com::sun::star::awt::XTextComponent</code></li>
56  *  <li> <code>com::sun::star::awt::XLayoutConstrains</code></li>
57  *  <li> <code>com::sun::star::awt::XTextListener</code></li>
58  *  <li> <code>com::sun::star::awt::XWindow</code></li>
59  *  <li> <code>com::sun::star::awt::XCurrencyField</code></li>
60  *  <li> <code>com::sun::star::lang::XComponent</code></li>
61  *  <li> <code>com::sun::star::awt::XTextLayoutConstrains</code></li>
62  *  <li> <code>com::sun::star::lang::XEventListener</code></li>
63  * </ul> <p>
64  * This object test <b> is NOT </b> designed to be run in several
65  * threads concurrently.
66  *
67  * @see com.sun.star.awt.XView
68  * @see com.sun.star.form.XBoundControl
69  * @see com.sun.star.awt.XControl
70  * @see com.sun.star.awt.XTextComponent
71  * @see com.sun.star.awt.XLayoutConstrains
72  * @see com.sun.star.awt.XTextListener
73  * @see com.sun.star.awt.XWindow
74  * @see com.sun.star.awt.XCurrencyField
75  * @see com.sun.star.lang.XComponent
76  * @see com.sun.star.awt.XTextLayoutConstrains
77  * @see com.sun.star.lang.XEventListener
78  * @see ifc.awt._XView
79  * @see ifc.form._XBoundControl
80  * @see ifc.awt._XControl
81  * @see ifc.awt._XTextComponent
82  * @see ifc.awt._XLayoutConstrains
83  * @see ifc.awt._XTextListener
84  * @see ifc.awt._XWindow
85  * @see ifc.awt._XCurrencyField
86  * @see ifc.lang._XComponent
87  * @see ifc.awt._XTextLayoutConstrains
88  * @see ifc.lang._XEventListener
89  */
90 public class OCurrencyControl extends TestCase {
91     XTextDocument xTextDoc;
92 
93     /**
94      * Creates a new text document.
95      */
96     @Override
initialize(TestParameters Param, PrintWriter log)97     protected void initialize(TestParameters Param, PrintWriter log) throws Exception {
98         SOfficeFactory SOF = SOfficeFactory.getFactory(Param.getMSF());
99 
100         log.println("creating a textdocument");
101         xTextDoc = SOF.createTextDoc(null);
102     }
103 
104     /**
105      * Disposes the text document created before
106      */
107     @Override
cleanup(TestParameters tParam, PrintWriter log)108     protected void cleanup(TestParameters tParam, PrintWriter log) {
109         log.println("    disposing xTextDoc ");
110 
111         try {
112             XCloseable closer = UnoRuntime.queryInterface(
113                                         XCloseable.class, xTextDoc);
114             closer.close(true);
115         } catch (com.sun.star.util.CloseVetoException e) {
116             log.println("couldn't close document");
117         } catch (com.sun.star.lang.DisposedException e) {
118             log.println("couldn't close document");
119         }
120     }
121 
122     /**
123      * Creates two components and inserts them to the form of
124      * text document. One component
125      * (<code>com.sun.star.form.component.CurrencyField</code>) is created
126      * for testing, another to be passed as relation. Using a controller
127      * of the text document the controller of the first component is
128      * obtained and returned in environment as a test object. <p>
129      *
130      *     Object relations created :
131      * <ul>
132      *  <li> <code>'GRAPHICS'</code> for
133      *      {@link ifc.awt._XView} : a graphics component
134      *      created using screen device of the window peer of
135      *      the controller tested. </li>
136      *  <li> <code>'CONTEXT'</code> for
137      *      {@link ifc.awt._XControl} : the text document
138      *      where the component is inserted. </li>
139      *  <li> <code>'WINPEER'</code> for
140      *      {@link ifc.awt._XControl} : Window peer of the
141      *      controller tested. </li>
142      *  <li> <code>'TOOLKIT'</code> for
143      *      {@link ifc.awt._XControl} : toolkit of the component.</li>
144      *  <li> <code>'MODEL'</code> for
145      *      {@link ifc.awt._XControl} : the model of the controller.</li>
146      *  <li> <code>'XWindow.AnotherWindow'</code> for
147      *      {@link ifc.awt._XWindow} : the controller of another
148      *      component. </li>
149      *  <li> <code>'XTextComponent.onlyNumbers'</code> for
150      *      {@link ifc.awt._XTextComponent} : as the currency field
151      *      can have only numeric values the relation must be specified. </li>
152      * </ul>
153      */
154     @Override
createTestEnvironment(TestParameters Param, PrintWriter log)155     protected TestEnvironment createTestEnvironment(TestParameters Param,
156                                                     PrintWriter log) throws Exception {
157         XInterface oObj = null;
158         XWindowPeer the_win = null;
159         XToolkit the_kit = null;
160         XDevice aDevice = null;
161         XGraphics aGraphic = null;
162         XControl aControl = null;
163 
164         //Insert a ControlShape and get the ControlModel
165         XControlShape aShape = FormTools.createControlShape(xTextDoc, 3000,
166                                                             4500, 15000, 10000,
167                                                             "CurrencyField");
168 
169         WriterTools.getDrawPage(xTextDoc).add(aShape);
170 
171         XControlModel the_Model = aShape.getControl();
172 
173         XControlShape aShape2 = FormTools.createControlShape(xTextDoc, 3000,
174                                                              4500, 5000, 10000,
175                                                              "TextField");
176 
177         WriterTools.getDrawPage(xTextDoc).add(aShape2);
178 
179         XControlModel the_Model2 = aShape2.getControl();
180 
181         //Try to query XControlAccess
182         XControlAccess the_access = UnoRuntime.queryInterface(
183                                             XControlAccess.class,
184                                             xTextDoc.getCurrentController());
185 
186         //now get the OCurrencyControl
187         oObj = the_access.getControl(the_Model);
188         aControl = the_access.getControl(the_Model2);
189         the_win = the_access.getControl(the_Model).getPeer();
190         the_kit = the_win.getToolkit();
191         aDevice = the_kit.createScreenCompatibleDevice(200, 200);
192         aGraphic = aDevice.createGraphics();
193 
194         log.println("creating a new environment for OCurrencyControl object");
195 
196         TestEnvironment tEnv = new TestEnvironment(oObj);
197 
198 
199         //Adding ObjRelation for XView
200         tEnv.addObjRelation("GRAPHICS", aGraphic);
201 
202 
203         //Adding ObjRelation for XControl
204         tEnv.addObjRelation("CONTEXT", xTextDoc);
205         tEnv.addObjRelation("WINPEER", the_win);
206         tEnv.addObjRelation("TOOLKIT", the_kit);
207         tEnv.addObjRelation("MODEL", the_Model);
208 
209 
210         // Adding relation for XTextComponent
211         tEnv.addObjRelation("XTextComponent.onlyNumbers", new Object());
212 
213         // Adding relation for XWindow
214         XWindow forObjRel = UnoRuntime.queryInterface(XWindow.class,
215                                                                 aControl);
216 
217         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
218         tEnv.addObjRelation("XWindow.ControlShape", aShape);
219 
220         // Adding relation for XTextListener
221         ifc.awt._XTextListener.TestTextListener listener =
222                 new ifc.awt._XTextListener.TestTextListener();
223         XTextComponent textComp = UnoRuntime.queryInterface(
224                                           XTextComponent.class, oObj);
225         textComp.addTextListener(listener);
226         tEnv.addObjRelation("TestTextListener", listener);
227 
228         return tEnv;
229     } // finish method getTestEnvironment
230 } // finish class OCurrencyControl
231