1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 package mod._forms;
19 
20 import java.io.PrintWriter;
21 
22 import lib.TestCase;
23 import lib.TestEnvironment;
24 import lib.TestParameters;
25 import util.FormTools;
26 import util.SOfficeFactory;
27 import util.WriterTools;
28 
29 import com.sun.star.awt.XControl;
30 import com.sun.star.awt.XControlModel;
31 import com.sun.star.awt.XDevice;
32 import com.sun.star.awt.XGraphics;
33 import com.sun.star.awt.XToolkit;
34 import com.sun.star.awt.XWindow;
35 import com.sun.star.awt.XWindowPeer;
36 import com.sun.star.drawing.XControlShape;
37 import com.sun.star.text.XTextDocument;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 import com.sun.star.util.XCloseable;
41 import com.sun.star.view.XControlAccess;
42 
43 
44 /**
45  * Test for object which is represented by default controller
46  * of the <code>com.sun.star.form.component.RadioButton</code>
47  * component. <p>
48  *
49  * Object implements the following interfaces :
50  * <ul>
51  *  <li> <code>com::sun::star::lang::XComponent</code></li>
52  *  <li> <code>com::sun::star::awt::XWindow</code></li>
53  *  <li> <code>com::sun::star::form::XBoundControl</code></li>
54  *  <li> <code>com::sun::star::awt::XRadioButton</code></li>
55  *  <li> <code>com::sun::star::awt::XControl</code></li>
56  *  <li> <code>com::sun::star::awt::XLayoutConstrains</code></li>
57  *  <li> <code>com::sun::star::awt::XView</code></li>
58  * </ul> <p>
59  * This object test <b> is NOT </b> designed to be run in several
60  * threads concurrently.
61  *
62  * @see com.sun.star.lang.XComponent
63  * @see com.sun.star.awt.XWindow
64  * @see com.sun.star.form.XBoundControl
65  * @see com.sun.star.awt.XRadioButton
66  * @see com.sun.star.awt.XControl
67  * @see com.sun.star.awt.XLayoutConstrains
68  * @see com.sun.star.awt.XView
69  * @see ifc.lang._XComponent
70  * @see ifc.awt._XWindow
71  * @see ifc.form._XBoundControl
72  * @see ifc.awt._XRadioButton
73  * @see ifc.awt._XControl
74  * @see ifc.awt._XLayoutConstrains
75  * @see ifc.awt._XView
76  */
77 public class ORadioButtonControl extends TestCase {
78     XTextDocument xTextDoc;
79 
80     /**
81      * Creates a new text document.
82      */
83     @Override
initialize(TestParameters Param, PrintWriter log)84     protected void initialize(TestParameters Param, PrintWriter log) throws Exception {
85         SOfficeFactory SOF = SOfficeFactory.getFactory(Param.getMSF());
86 
87         log.println("creating a textdocument");
88         xTextDoc = SOF.createTextDoc(null);
89     }
90 
91     /**
92      * Disposes the text document created before
93      */
94     @Override
cleanup(TestParameters tParam, PrintWriter log)95     protected void cleanup(TestParameters tParam, PrintWriter log) {
96         log.println("    disposing xTextDoc ");
97 
98         try {
99             XCloseable closer = UnoRuntime.queryInterface(
100                                         XCloseable.class, xTextDoc);
101             closer.close(true);
102         } catch (com.sun.star.util.CloseVetoException e) {
103             log.println("couldn't close document");
104         } catch (com.sun.star.lang.DisposedException e) {
105             log.println("couldn't close document");
106         }
107     }
108 
109     /**
110      * Creates two components and inserts them to the form of
111      * text document. One component
112      * (<code>com.sun.star.form.component.RadioButton</code>) is created
113      * for testing, another to be passed as relation. Using a controller
114      * of the text document the controller of the first component is
115      * obtained and returned in environment as a test object. <p>
116      *
117      *     Object relations created :
118      * <ul>
119      *  <li> <code>'GRAPHICS'</code> for
120      *      {@link ifc.awt._XView} : a graphics component
121      *      created using screen device of the window peer of
122      *      the controller tested. </li>
123      *  <li> <code>'CONTEXT'</code> for
124      *      {@link ifc.awt._XControl} : the text document
125      *      where the component is inserted. </li>
126      *  <li> <code>'WINPEER'</code> for
127      *      {@link ifc.awt._XControl} : Window peer of the
128      *      controller tested. </li>
129      *  <li> <code>'TOOLKIT'</code> for
130      *      {@link ifc.awt._XControl} : toolkit of the component.</li>
131      *  <li> <code>'MODEL'</code> for
132      *      {@link ifc.awt._XControl} : the model of the controller.</li>
133      *  <li> <code>'XWindow.AnotherWindow'</code> for
134      *      {@link ifc.awt._XWindow} : the controller of another
135      *      component. </li>
136      * </ul>
137      */
138     @Override
createTestEnvironment(TestParameters Param, PrintWriter log)139     protected TestEnvironment createTestEnvironment(TestParameters Param,
140                                                     PrintWriter log) throws Exception {
141         XInterface oObj = null;
142         XWindowPeer the_win = null;
143         XToolkit the_kit = null;
144         XDevice aDevice = null;
145         XGraphics aGraphic = null;
146         XControl aControl = null;
147 
148         //Insert a ControlShape and get the ControlModel
149         XControlShape aShape = FormTools.createControlShape(xTextDoc, 3000,
150                                                             4500, 15000, 10000,
151                                                             "RadioButton");
152 
153         WriterTools.getDrawPage(xTextDoc).add(aShape);
154 
155         XControlModel the_Model = aShape.getControl();
156 
157         XControlShape aShape2 = FormTools.createControlShape(xTextDoc, 3000,
158                                                              4500, 5000, 10000,
159                                                              "TextField");
160 
161         WriterTools.getDrawPage(xTextDoc).add(aShape2);
162 
163         XControlModel the_Model2 = aShape2.getControl();
164 
165         //Try to query XControlAccess
166         XControlAccess the_access = UnoRuntime.queryInterface(
167                                             XControlAccess.class,
168                                             xTextDoc.getCurrentController());
169 
170         //now get the ORadioButtonControl
171         oObj = the_access.getControl(the_Model);
172         aControl = the_access.getControl(the_Model2);
173         the_win = the_access.getControl(the_Model).getPeer();
174         the_kit = the_win.getToolkit();
175         aDevice = the_kit.createScreenCompatibleDevice(200, 200);
176         aGraphic = aDevice.createGraphics();
177 
178         log.println(
179                 "creating a new environment for ORadioButtonControl object");
180 
181         TestEnvironment tEnv = new TestEnvironment(oObj);
182 
183 
184         //Adding ObjRelation for XView
185         tEnv.addObjRelation("GRAPHICS", aGraphic);
186 
187 
188         //Adding ObjRelation for XControl
189         tEnv.addObjRelation("CONTEXT", xTextDoc);
190         tEnv.addObjRelation("WINPEER", the_win);
191         tEnv.addObjRelation("TOOLKIT", the_kit);
192         tEnv.addObjRelation("MODEL", the_Model);
193 
194         XWindow forObjRel = UnoRuntime.queryInterface(XWindow.class,
195                                                                 aControl);
196 
197         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
198 
199         return tEnv;
200     } // finish method getTestEnvironment
201 } // finish class ORadioButtonControl
202