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