1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package mod._forms;
24 
25 import java.io.PrintWriter;
26 
27 import lib.StatusException;
28 import lib.TestCase;
29 import lib.TestEnvironment;
30 import lib.TestParameters;
31 import util.FormTools;
32 import util.SOfficeFactory;
33 import util.WriterTools;
34 
35 import com.sun.star.awt.XControl;
36 import com.sun.star.awt.XControlModel;
37 import com.sun.star.awt.XDevice;
38 import com.sun.star.awt.XGraphics;
39 import com.sun.star.awt.XToolkit;
40 import com.sun.star.awt.XWindow;
41 import com.sun.star.awt.XWindowPeer;
42 import com.sun.star.drawing.XControlShape;
43 import com.sun.star.drawing.XShape;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.text.XTextDocument;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
48 import com.sun.star.util.XCloseable;
49 import com.sun.star.view.XControlAccess;
50 
51 
52 /**
53  * Test for object which is represented by default controller
54  * of the <code>com.sun.star.form.component.CommandButton</code>
55  * component. <p>
56  *
57  * Object implements the following interfaces :
58  * <ul>
59  *  <li> <code>com::sun::star::lang::XComponent</code></li>
60  *  <li> <code>com::sun::star::awt::XWindow</code></li>
61  *  <li> <code>com::sun::star::form::XApproveActionBroadcaster</code></li>
62  *  <li> <code>com::sun::star::awt::XControl</code></li>
63  *  <li> <code>com::sun::star::awt::XLayoutConstrains</code></li>
64  *  <li> <code>com::sun::star::awt::XButton</code></li>
65  *  <li> <code>com::sun::star::awt::XView</code></li>
66  * </ul> <p>
67  *
68  * This object test <b> is NOT </b> designed to be run in several
69  * threads concurently.
70  *
71  * @see com.sun.star.lang.XComponent
72  * @see com.sun.star.awt.XWindow
73  * @see com.sun.star.form.XApproveActionBroadcaster
74  * @see com.sun.star.awt.XControl
75  * @see com.sun.star.awt.XLayoutConstrains
76  * @see com.sun.star.awt.XButton
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._XButton
84  * @see ifc.awt._XView
85  */
86 public class OButtonControl extends TestCase {
87     XTextDocument xTextDoc;
88 
89     /**
90      * Creates a text document.
91      */
initialize(TestParameters Param, PrintWriter log)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      */
cleanup(TestParameters tParam, PrintWriter log)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.CommandButton</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      */
createTestEnvironment(TestParameters Param, PrintWriter log)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.createControlShape(xTextDoc, 3000,
162                                                             4500, 15000, 10000,
163                                                             "CommandButton");
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 OButtonControl
183         try {
184             oObj = the_access.getControl(the_Model);
185             aControl = the_access.getControl(the_Model2);
186             the_win = the_access.getControl(the_Model).getPeer();
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 OButtonControl");
192             e.printStackTrace(log);
193             throw new StatusException("Couldn't get OButtonControl", e);
194         }
195 
196         log.println("creating a new environment for OButtonControl 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         XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class,
212                                                                 aControl);
213 
214         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
215         tEnv.addObjRelation("XWindow.ControlShape", aShape);
216 
217         return tEnv;
218     } // finish method getTestEnvironment
219 } // finish class OButtonControl
220