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