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