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