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.CommandButton</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::XApproveActionBroadcaster</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::XButton</code></li>
69  *  <li> <code>com::sun::star::awt::XView</code></li>
70  * </ul> <p>
71  *
72  * This object test <b> is NOT </b> designed to be run in several
73  * threads concurently.
74  *
75  * @see com.sun.star.lang.XComponent
76  * @see com.sun.star.awt.XWindow
77  * @see com.sun.star.form.XApproveActionBroadcaster
78  * @see com.sun.star.awt.XControl
79  * @see com.sun.star.awt.XLayoutConstrains
80  * @see com.sun.star.awt.XButton
81  * @see com.sun.star.awt.XView
82  * @see ifc.lang._XComponent
83  * @see ifc.awt._XWindow
84  * @see ifc.form._XApproveActionBroadcaster
85  * @see ifc.awt._XControl
86  * @see ifc.awt._XLayoutConstrains
87  * @see ifc.awt._XButton
88  * @see ifc.awt._XView
89  */
90 public class OButtonControl extends TestCase {
91     XTextDocument xTextDoc;
92 
93     /**
94      * Creates a text document.
95      */
96     protected void initialize(TestParameters Param, PrintWriter log) {
97         SOfficeFactory SOF = SOfficeFactory.getFactory((XMultiServiceFactory) Param.getMSF());
98 
99         try {
100             log.println("creating a textdocument");
101             xTextDoc = SOF.createTextDoc(null);
102         } catch (com.sun.star.uno.Exception e) {
103             // Some exception occures.FAILED
104             e.printStackTrace(log);
105             throw new StatusException("Couldn't create document", e);
106         }
107     }
108 
109     /**
110      * Disposes the text document created before
111      */
112     protected void cleanup(TestParameters tParam, PrintWriter log) {
113         log.println("    disposing xTextDoc ");
114 
115         try {
116             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
117                                         XCloseable.class, xTextDoc);
118             closer.close(true);
119         } catch (com.sun.star.util.CloseVetoException e) {
120             log.println("couldn't close document");
121         } catch (com.sun.star.lang.DisposedException e) {
122             log.println("couldn't close document");
123         }
124     }
125 
126     /**
127      * Creates two components and inserts them to the form of
128      * text document. One component
129      * (<code>com.sun.star.form.component.CommandButton</code>) is created
130      * for testing, another to be passed as relation. Using a controller
131      * of the text document the controller of the first component is
132      * obtained and returned in environment as a test object. <p>
133      *
134      *     Object relations created :
135      * <ul>
136      *  <li> <code>'GRAPHICS'</code> for
137      *      {@link ifc.awt._XView} : a graphics component
138      *      created using screen device of the window peer of
139      *      the controller tested. </li>
140      *  <li> <code>'CONTEXT'</code> for
141      *      {@link ifc.awt._XControl} : the text document
142      *      where the component is inserted. </li>
143      *  <li> <code>'WINPEER'</code> for
144      *      {@link ifc.awt._XControl} : Window peer of the
145      *      controller tested. </li>
146      *  <li> <code>'TOOLKIT'</code> for
147      *      {@link ifc.awt._XControl} : toolkit of the component.</li>
148      *  <li> <code>'MODEL'</code> for
149      *      {@link ifc.awt._XControl} : the model of the controller.</li>
150      *  <li> <code>'XWindow.AnotherWindow'</code> for
151      *      {@link ifc.awt._XWindow} : the controller of another
152      *      component. </li>
153      * </ul>
154      */
155     protected TestEnvironment createTestEnvironment(TestParameters Param,
156                                                     PrintWriter log) {
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                                                             "CommandButton");
168 
169         WriterTools.getDrawPage(xTextDoc).add((XShape) 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((XShape) aShape2);
178 
179         XControlModel the_Model2 = aShape2.getControl();
180 
181         //Try to query XControlAccess
182         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
183                                             XControlAccess.class,
184                                             xTextDoc.getCurrentController());
185 
186         //now get the OButtonControl
187         try {
188             oObj = the_access.getControl(the_Model);
189             aControl = the_access.getControl(the_Model2);
190             the_win = the_access.getControl(the_Model).getPeer();
191             the_kit = the_win.getToolkit();
192             aDevice = the_kit.createScreenCompatibleDevice(200, 200);
193             aGraphic = aDevice.createGraphics();
194         } catch (com.sun.star.container.NoSuchElementException e) {
195             log.println("Couldn't get OButtonControl");
196             e.printStackTrace(log);
197             throw new StatusException("Couldn't get OButtonControl", e);
198         }
199 
200         log.println("creating a new environment for OButtonControl object");
201 
202         TestEnvironment tEnv = new TestEnvironment(oObj);
203 
204 
205         //Adding ObjRelation for XView
206         tEnv.addObjRelation("GRAPHICS", aGraphic);
207 
208 
209         //Adding ObjRelation for XControl
210         tEnv.addObjRelation("CONTEXT", xTextDoc);
211         tEnv.addObjRelation("WINPEER", the_win);
212         tEnv.addObjRelation("TOOLKIT", the_kit);
213         tEnv.addObjRelation("MODEL", the_Model);
214 
215         XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class,
216                                                                 aControl);
217 
218         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
219         tEnv.addObjRelation("XWindow.ControlShape", aShape);
220 
221         return tEnv;
222     } // finish method getTestEnvironment
223 } // finish class OButtonControl
224