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