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