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