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.RadioButton</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::XBoundControl</code></li>
62  *  <li> <code>com::sun::star::awt::XRadioButton</code></li>
63  *  <li> <code>com::sun::star::awt::XControl</code></li>
64  *  <li> <code>com::sun::star::awt::XLayoutConstrains</code></li>
65  *  <li> <code>com::sun::star::awt::XView</code></li>
66  * </ul> <p>
67  * This object test <b> is NOT </b> designed to be run in several
68  * threads concurently.
69  *
70  * @see com.sun.star.lang.XComponent
71  * @see com.sun.star.awt.XWindow
72  * @see com.sun.star.form.XBoundControl
73  * @see com.sun.star.awt.XRadioButton
74  * @see com.sun.star.awt.XControl
75  * @see com.sun.star.awt.XLayoutConstrains
76  * @see com.sun.star.awt.XView
77  * @see ifc.lang._XComponent
78  * @see ifc.awt._XWindow
79  * @see ifc.form._XBoundControl
80  * @see ifc.awt._XRadioButton
81  * @see ifc.awt._XControl
82  * @see ifc.awt._XLayoutConstrains
83  * @see ifc.awt._XView
84  */
85 public class ORadioButtonControl extends TestCase {
86     XTextDocument xTextDoc;
87 
88     /**
89      * Creates a new text document.
90      */
initialize(TestParameters Param, PrintWriter log)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      */
cleanup(TestParameters tParam, PrintWriter log)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.RadioButton</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      */
createTestEnvironment(TestParameters Param, PrintWriter log)150     protected TestEnvironment createTestEnvironment(TestParameters Param,
151                                                     PrintWriter log) {
152         XInterface oObj = null;
153         XWindowPeer the_win = null;
154         XToolkit the_kit = null;
155         XDevice aDevice = null;
156         XGraphics aGraphic = null;
157         XControl aControl = null;
158 
159         //Insert a ControlShape and get the ControlModel
160         XControlShape aShape = FormTools.createControlShape(xTextDoc, 3000,
161                                                             4500, 15000, 10000,
162                                                             "RadioButton");
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 ORadioButtonControl
182         try {
183             oObj = the_access.getControl(the_Model);
184             aControl = 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 ORadioButtonControl");
191             e.printStackTrace(log);
192             throw new StatusException("Couldn't get ORadioButtonControl", e);
193         }
194 
195         log.println(
196                 "creating a new environment for ORadioButtonControl 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 
216         return tEnv;
217     } // finish method getTestEnvironment
218 } // finish class ORadioButtonControl
219