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