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 
24 package mod._forms;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.FormTools;
33 import util.SOfficeFactory;
34 import util.WriterTools;
35 
36 import com.sun.star.awt.XCheckBox;
37 import com.sun.star.awt.XControl;
38 import com.sun.star.awt.XControlModel;
39 import com.sun.star.awt.XDevice;
40 import com.sun.star.awt.XGraphics;
41 import com.sun.star.awt.XToolkit;
42 import com.sun.star.awt.XWindow;
43 import com.sun.star.awt.XWindowPeer;
44 import com.sun.star.drawing.XControlShape;
45 import com.sun.star.drawing.XShape;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.text.XTextDocument;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 import com.sun.star.util.XCloseable;
51 import com.sun.star.view.XControlAccess;
52 
53 
54 /**
55  * Test for object which is represented by default controller
56  * of the <code>com.sun.star.form.component.CheckBox</code>
57  * component. <p>
58  *
59  * Object implements the following interfaces :
60  * <ul>
61  *  <li> <code>com::sun::star::awt::XView</code></li>
62  *  <li> <code>com::sun::star::form::XBoundControl</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::XItemListener</code></li>
66  *  <li> <code>com::sun::star::awt::XWindow</code></li>
67  *  <li> <code>com::sun::star::awt::XCheckBox</code></li>
68  *  <li> <code>com::sun::star::lang::XComponent</code></li>
69  *  <li> <code>com::sun::star::lang::XEventListener</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.awt.XView
75  * @see com.sun.star.form.XBoundControl
76  * @see com.sun.star.awt.XControl
77  * @see com.sun.star.awt.XLayoutConstrains
78  * @see com.sun.star.awt.XItemListener
79  * @see com.sun.star.awt.XWindow
80  * @see com.sun.star.awt.XCheckBox
81  * @see com.sun.star.lang.XComponent
82  * @see com.sun.star.lang.XEventListener
83  * @see ifc.awt._XView
84  * @see ifc.form._XBoundControl
85  * @see ifc.awt._XControl
86  * @see ifc.awt._XLayoutConstrains
87  * @see ifc.awt._XItemListener
88  * @see ifc.awt._XWindow
89  * @see ifc.awt._XCheckBox
90  * @see ifc.lang._XComponent
91  * @see ifc.lang._XEventListener
92  */
93 public class OCheckBoxControl extends TestCase {
94 
95     XTextDocument xTextDoc;
96 
97     /**
98      * Creates a new text document.
99      */
initialize( TestParameters Param, PrintWriter log)100     protected void initialize ( TestParameters Param, PrintWriter log) {
101         SOfficeFactory SOF = SOfficeFactory.getFactory( ((XMultiServiceFactory)Param.getMSF()) );
102 
103         try {
104             log.println( "creating a textdocument" );
105             xTextDoc = SOF.createTextDoc( null );
106         } catch ( com.sun.star.uno.Exception e ) {
107             // Some exception occures.FAILED
108             e.printStackTrace( log );
109             throw new StatusException( "Couldn't create document", e );
110         }
111     }
112 
113     /**
114      * Disposes the text document created before
115      */
cleanup(TestParameters tParam, PrintWriter log)116     protected void cleanup(TestParameters tParam, PrintWriter log) {
117         log.println("    disposing xTextDoc ");
118 
119         try {
120             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
121                                         XCloseable.class, xTextDoc);
122             closer.close(true);
123         } catch (com.sun.star.util.CloseVetoException e) {
124             log.println("couldn't close document");
125         } catch (com.sun.star.lang.DisposedException e) {
126             log.println("couldn't close document");
127         }
128     }
129 
130     /**
131      * Creates two components and inserts them to the form of
132      * text document. One component
133      * (<code>com.sun.star.form.component.CheckBox</code>) is created
134      * for testing, another to be passed as relation. Using a controller
135      * of the text document the controller of the first component is
136      * obtained and returned in environment as a test object. <p>
137      *
138      *     Object relations created :
139      * <ul>
140      *  <li> <code>'GRAPHICS'</code> for
141      *      {@link ifc.awt._XView} : a graphics component
142      *      created using screen device of the window peer of
143      *      the controller tested. </li>
144      *  <li> <code>'CONTEXT'</code> for
145      *      {@link ifc.awt._XControl} : the text document
146      *      where the component is inserted. </li>
147      *  <li> <code>'WINPEER'</code> for
148      *      {@link ifc.awt._XControl} : Window peer of the
149      *      controller tested. </li>
150      *  <li> <code>'TOOLKIT'</code> for
151      *      {@link ifc.awt._XControl} : toolkit of the component.</li>
152      *  <li> <code>'MODEL'</code> for
153      *      {@link ifc.awt._XControl} : the model of the controller.</li>
154      *  <li> <code>'XWindow.AnotherWindow'</code> for
155      *      {@link ifc.awt._XWindow} : the controller of another
156      *      component. </li>
157      *  <li> <code>'TestItemListener'</code> for
158      *      {@link ifc.awt._XItemListener} : listener implementation is
159      *      registered here and passed as relation. </li>
160      * </ul>
161      */
createTestEnvironment(TestParameters Param, PrintWriter log)162     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
163         XInterface oObj = null;
164         XWindowPeer the_win = null;
165         XToolkit the_kit = null;
166         XDevice aDevice = null;
167         XGraphics aGraphic = null;
168         XControl aControl = null;
169         //Insert a ControlShape and get the ControlModel
170         XControlShape aShape = FormTools.createControlShape(
171                                 xTextDoc,3000,4500,15000,10000,"CheckBox");
172 
173         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
174 
175         XControlModel the_Model = aShape.getControl();
176 
177         XControlShape aShape2 = FormTools.createControlShape(
178                                 xTextDoc,3000,4500,5000,10000,"TextField");
179 
180         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape2);
181 
182         XControlModel the_Model2 = aShape2.getControl();
183 
184         //Try to query XControlAccess
185         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
186                         XControlAccess.class,xTextDoc.getCurrentController());
187 
188         //now get the OButtonControl
189         try {
190             oObj = the_access.getControl(the_Model);
191             aControl = the_access.getControl(the_Model2);
192             the_win = the_access.getControl(the_Model).getPeer();
193             the_kit = the_win.getToolkit();
194             aDevice = the_kit.createScreenCompatibleDevice(200,200);
195             aGraphic = aDevice.createGraphics();
196         } catch (com.sun.star.container.NoSuchElementException e) {
197             log.println("Couldn't get OCheckBoxControl");
198             e.printStackTrace(log);
199             throw new StatusException("Couldn't get OCheckBoxControl", e );
200         }
201 
202         log.println( "creating a new environment for OCheckBoxControl object" );
203         TestEnvironment tEnv = new TestEnvironment( oObj );
204 
205         //Adding ObjRelation for XView
206         tEnv.addObjRelation("GRAPHICS",aGraphic);
207 
208         //Adding ObjRelation for XControl
209         tEnv.addObjRelation("CONTEXT",xTextDoc);
210         tEnv.addObjRelation("WINPEER",the_win);
211         tEnv.addObjRelation("TOOLKIT",the_kit);
212         tEnv.addObjRelation("MODEL",the_Model);
213 
214         // Adding relation for XItemListener
215         ifc.awt._XItemListener.TestItemListener listener =
216             new ifc.awt._XItemListener.TestItemListener() ;
217         XCheckBox box = (XCheckBox) UnoRuntime.queryInterface
218             (XCheckBox.class, oObj) ;
219         box.addItemListener(listener) ;
220         tEnv.addObjRelation("TestItemListener", listener) ;
221 
222         // Adding relation for XWindow
223         XWindow forObjRel = (XWindow)
224                             UnoRuntime.queryInterface(XWindow.class,aControl);
225 
226         tEnv.addObjRelation("XWindow.AnotherWindow",forObjRel);
227 
228         return tEnv;
229     } // finish method getTestEnvironment
230 
231 }    // finish class OButtonControl
232 
233 
234