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.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.DatabaseImageControl</code>
59  * component. <p>
60  *
61  * Object implements the following interfaces :
62  * <ul>
63  *  <li> <code>com::sun::star::lang::XComponent</code></li>
64  *  <li> <code>com::sun::star::awt::XWindow</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::XLayoutConstrains</code></li>
68  *  <li> <code>com::sun::star::awt::XView</code></li>
69  * </ul> <p>
70  * This object test <b> is NOT </b> designed to be run in several
71  * threads concurently.
72  *
73  * @see com.sun.star.lang.XComponent
74  * @see com.sun.star.awt.XWindow
75  * @see com.sun.star.form.XBoundControl
76  * @see com.sun.star.awt.XControl
77  * @see com.sun.star.awt.XLayoutConstrains
78  * @see com.sun.star.awt.XView
79  * @see ifc.lang._XComponent
80  * @see ifc.awt._XWindow
81  * @see ifc.form._XBoundControl
82  * @see ifc.awt._XControl
83  * @see ifc.awt._XLayoutConstrains
84  * @see ifc.awt._XView
85  */
86 public class OImageControlControl extends TestCase {
87     XTextDocument xTextDoc;
88 
89     /**
90      * Creates a new text document.
91      */
92     protected void initialize(TestParameters Param, PrintWriter log) {
93         SOfficeFactory SOF = SOfficeFactory.getFactory(((XMultiServiceFactory) Param.getMSF()));
94 
95         try {
96             log.println("creating a textdocument");
97             xTextDoc = SOF.createTextDoc(null);
98         } catch (com.sun.star.uno.Exception e) {
99             // Some exception occures.FAILED
100             e.printStackTrace(log);
101             throw new StatusException("Couldn't create document", e);
102         }
103     }
104 
105     /**
106      * Disposes the text document created before
107      */
108     protected void cleanup(TestParameters tParam, PrintWriter log) {
109         log.println("    disposing xTextDoc ");
110 
111         try {
112             XCloseable closer = (XCloseable) 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.DatabaseImageControl</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      * </ul>
150      */
151     protected TestEnvironment createTestEnvironment(TestParameters Param,
152                                                     PrintWriter log) {
153         XInterface oObj = null;
154         XWindowPeer the_win = null;
155         XToolkit the_kit = null;
156         XDevice aDevice = null;
157         XGraphics aGraphic = null;
158         XControl aControl = null;
159 
160         //Insert a ControlShape and get the ControlModel
161         XControlShape aShape = FormTools.createControlShapeWithDefaultControl(
162                                        xTextDoc, 3000, 4500, 15000, 10000,
163                                        "DatabaseImageControl");
164 
165         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
166 
167         XControlModel the_Model = aShape.getControl();
168 
169         XControlShape aShape2 = FormTools.createControlShape(xTextDoc, 3000,
170                                                              4500, 5000, 10000,
171                                                              "TextField");
172 
173         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape2);
174 
175         XControlModel the_Model2 = aShape2.getControl();
176 
177         //Try to query XControlAccess
178         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
179                                             XControlAccess.class,
180                                             xTextDoc.getCurrentController());
181 
182         //now get the OImageControlControl
183         try {
184             oObj = the_access.getControl(the_Model);
185             the_win = the_access.getControl(the_Model).getPeer();
186             aControl = the_access.getControl(the_Model2);
187             the_kit = the_win.getToolkit();
188             aDevice = the_kit.createScreenCompatibleDevice(200, 200);
189             aGraphic = aDevice.createGraphics();
190         } catch (com.sun.star.container.NoSuchElementException e) {
191             log.println("Couldn't get OImageControlControl");
192             e.printStackTrace(log);
193             throw new StatusException("Couldn't get OImageControlControl", e);
194         }
195 
196         log.println(
197                 "creating a new environment for OImageControlControl object");
198 
199         TestEnvironment tEnv = new TestEnvironment(oObj);
200 
201 
202         //Adding ObjRelation for XView
203         tEnv.addObjRelation("GRAPHICS", aGraphic);
204 
205 
206         //Adding ObjRelation for XControl
207         tEnv.addObjRelation("CONTEXT", xTextDoc);
208         tEnv.addObjRelation("WINPEER", the_win);
209         tEnv.addObjRelation("TOOLKIT", the_kit);
210         tEnv.addObjRelation("MODEL", the_Model);
211 
212         // Adding relation for XWindow
213         XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class,
214                                                                 aControl);
215 
216         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
217 
218         return tEnv;
219     } // finish method getTestEnvironment
220 } // finish class OImageControlControl
221