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 
28 package mod._sd;
29 
30 import java.io.PrintWriter;
31 import java.util.Comparator;
32 
33 import lib.StatusException;
34 import lib.TestCase;
35 import lib.TestEnvironment;
36 import lib.TestParameters;
37 import util.DesktopTools;
38 import util.SOfficeFactory;
39 import util.utils;
40 
41 import com.sun.star.awt.XWindow;
42 import com.sun.star.container.XIndexAccess;
43 import com.sun.star.drawing.XDrawPage;
44 import com.sun.star.drawing.XDrawPages;
45 import com.sun.star.drawing.XDrawPagesSupplier;
46 import com.sun.star.drawing.XShape;
47 import com.sun.star.drawing.XShapes;
48 import com.sun.star.frame.XController;
49 import com.sun.star.frame.XDesktop;
50 import com.sun.star.frame.XFrame;
51 import com.sun.star.frame.XModel;
52 import com.sun.star.lang.XComponent;
53 import com.sun.star.lang.XMultiServiceFactory;
54 import com.sun.star.uno.AnyConverter;
55 import com.sun.star.uno.Type;
56 import com.sun.star.uno.UnoRuntime;
57 import com.sun.star.uno.XInterface;
58 import com.sun.star.util.XModifiable;
59 
60 /**
61 * Test for object which is represented by service
62 * <code>com.sun.star.drawing.DrawingDocumentDrawView</code>. <p>
63 * Object implements the following interfaces :
64 * <ul>
65 *  <li> <code>com::sun::star::drawing::DrawingDocumentDrawView</code></li>
66 *  <li> <code>com::sun::star::lang::XComponent</code></li>
67 *  <li> <code>com::sun::star::lang::XServiceInfo</code></li>
68 *  <li> <code>com::sun::star::frame::XController</code></li>
69 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
70 *  <li> <code>com::sun::star::view::XSelectionSupplier</code></li>
71 *  <li> <code>com::sun::star::drawing::XDrawView</code></li>
72 * </ul>
73 * @see com.sun.star.drawing.DrawingDocumentDrawView
74 * @see com.sun.star.lang.XComponent
75 * @see com.sun.star.lang.XServiceInfo
76 * @see com.sun.star.frame.XController
77 * @see com.sun.star.beans.XPropertySet
78 * @see com.sun.star.view.XSelectionSupplier
79 * @see com.sun.star.drawing.XDrawView
80 * @see ifc.drawing._DrawingDocumentDrawView
81 * @see ifc.lang._XComponent
82 * @see ifc.lang._XServiceInfo
83 * @see ifc.frame._XController
84 * @see ifc.beans._XPropertySet
85 * @see ifc.view._XSelectionSupplier
86 * @see ifc.drawing._XDrawView
87 */
88 public class SdUnoPresView extends TestCase {
89     static XDesktop the_Desk;
90     static XComponent xImpressDoc;
91     static XComponent xSecondDrawDoc;
92 
93     /**
94     * Creates the instance of the service <code>com.sun.star.frame.Desktop</code>.
95     * @see com.sun.star.frame.Desktop
96     */
97     protected void initialize(TestParameters Param, PrintWriter log) {
98         the_Desk = (XDesktop)
99             UnoRuntime.queryInterface(
100                 XDesktop.class, DesktopTools.createDesktop(
101                                     (XMultiServiceFactory)Param.getMSF()) );
102     }
103 
104     /**
105     * Called while disposing a <code>TestEnvironment</code>.
106     * Disposes Impress documents.
107     * @param tParam test parameters
108     * @param tEnv the environment to cleanup
109     * @param log writer to log information while testing
110     */
111     protected void cleanup( TestParameters Param, PrintWriter log) {
112         log.println("disposing impress documents");
113         util.DesktopTools.closeDoc(xImpressDoc);;
114         util.DesktopTools.closeDoc(xSecondDrawDoc);;
115     }
116 
117     /**
118     * Creating a Testenvironment for the interfaces to be tested.
119     * Creates two impress documents. After creating of the documents makes short
120     * wait to allow frames to be loaded. Retrieves the collection of the draw pages
121     * from the first document and takes one of them. Inserts some shapes to the
122     * retrieved draw page. Obtains a current controller from the first document
123     * using the interface <code>XModel</code>. The obtained controller is the
124     * instance of the service
125     * <code>com.sun.star.drawing.DrawingDocumentDrawView</code>.
126     * Object relations created :
127     * <ul>
128     *  <li> <code>'First'</code> for
129     *      {@link ifc.view._XSelectionSupplier}(the first created shape) </li>
130     *  <li> <code>'Second'</code> for
131     *      {@link ifc.view._XSelectionSupplier}(the second created shape) </li>
132     *  <li> <code>'Pages'</code> for
133     *      {@link ifc.drawing._XDrawView}(the retrieved collection of the draw
134     *      pages) </li>
135     *  <li> <code>'FirstModel'</code> for
136     *      {@link ifc.frame._XController}(the interface <code>XModel</code> of
137     *      the first created document) </li>
138     *  <li> <code>'Frame'</code> for
139     *      {@link ifc.frame._XController}(the frame of the created
140     *      document) </li>
141     *  <li> <code>'SecondModel'</code> for
142     *      {@link ifc.frame._XController}(the interface <code>XModel</code> of
143     *      the second created document) </li>
144     *  <li> <code>'SecondController'</code> for
145     *      {@link ifc.frame._XController}(the current controller of the second
146     *      created document) </li>
147     * </ul>
148     * @see com.sun.star.frame.XModel
149     * @see com.sun.star.drawing.DrawingDocumentDrawView
150     */
151     protected synchronized TestEnvironment createTestEnvironment
152             (TestParameters Param, PrintWriter log) {
153 
154         log.println( "creating a test environment" );
155 
156         // get a soffice factory object
157         SOfficeFactory SOF = SOfficeFactory.getFactory(
158                                         (XMultiServiceFactory)Param.getMSF());
159 
160         try {
161             log.println( "creating two impress documents" );
162             xImpressDoc = SOF.createImpressDoc(null);
163             xSecondDrawDoc = SOF.createImpressDoc(null);
164             shortWait();
165         } catch (com.sun.star.uno.Exception e) {
166             e.printStackTrace( log );
167             throw new StatusException("Couldn't create document", e);
168         }
169 
170         // get the drawpage of drawing here
171         log.println( "getting Drawpage" );
172         XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
173             UnoRuntime.queryInterface(XDrawPagesSupplier.class, xImpressDoc);
174         XDrawPages the_pages = oDPS.getDrawPages();
175         XIndexAccess oDPi = (XIndexAccess)
176             UnoRuntime.queryInterface(XIndexAccess.class,the_pages);
177 
178         XDrawPage oDrawPage = null;
179         XDrawPage secondDrawPage = null;
180         try {
181             oDrawPage = (XDrawPage) AnyConverter.toObject(
182                     new Type(XDrawPage.class),oDPi.getByIndex(0));
183             the_pages.insertNewByIndex(0);
184             the_pages.insertNewByIndex(0);
185             the_pages.insertNewByIndex(0);
186             the_pages.insertNewByIndex(0);
187            secondDrawPage = (XDrawPage) AnyConverter.toObject(
188                     new Type(XDrawPage.class),oDPi.getByIndex(3));
189         } catch (com.sun.star.lang.WrappedTargetException e) {
190             e.printStackTrace( log );
191             throw new StatusException("Couldn't get DrawPage", e);
192         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
193             e.printStackTrace( log );
194             throw new StatusException("Couldn't get DrawPage", e);
195         } catch (com.sun.star.lang.IllegalArgumentException e) {
196             e.printStackTrace( log );
197             throw new StatusException("Couldn't get DrawPage", e);
198         }
199 
200         //put something on the drawpage
201         log.println( "inserting some Shapes" );
202         XShapes oShapes = (XShapes)
203             UnoRuntime.queryInterface(XShapes.class, oDrawPage);
204         XShape shape1 = SOF.createShape(
205             xImpressDoc, 5000, 3500, 7500, 5000, "Rectangle");
206         oShapes.add(shape1);
207 
208         oShapes = (XShapes)
209             UnoRuntime.queryInterface(XShapes.class, secondDrawPage);
210         shape1 = SOF.createShape(
211             xImpressDoc, 3000, 4500, 15000, 1000, "Ellipse");
212         oShapes.add(shape1);
213 
214         XModel aModel = (XModel)
215             UnoRuntime.queryInterface(XModel.class, xImpressDoc);
216 
217         XInterface oObj = aModel.getCurrentController();
218 
219         XModel aModel2 = (XModel)
220             UnoRuntime.queryInterface(XModel.class, xSecondDrawDoc);
221 
222         XWindow anotherWindow = (XWindow) UnoRuntime.queryInterface(
223                                 XWindow.class,aModel2.getCurrentController());
224 
225         log.println( "creating a new environment for impress view object" );
226         TestEnvironment tEnv = new TestEnvironment( oObj );
227 
228         if (anotherWindow != null) {
229             tEnv.addObjRelation("XWindow.AnotherWindow",anotherWindow);
230         }
231 
232         tEnv.addObjRelation("Selections", new Object[] {
233             oDrawPage, secondDrawPage});
234 
235         tEnv.addObjRelation("Comparer", new Comparator() {
236             public int compare(Object o1, Object o2) {
237                 XIndexAccess indAc1 = (XIndexAccess)
238                     UnoRuntime.queryInterface(XIndexAccess.class, o1);
239                 XIndexAccess indAc2 = (XIndexAccess)
240                     UnoRuntime.queryInterface(XIndexAccess.class, o2);
241                 if (indAc1 == null || indAc2 == null) return -1;
242                 if (indAc1.getCount() == indAc2.getCount()) {
243                     return 0;
244                 }
245                 return 1;
246             }
247             public boolean equals(Object obj) {
248                 return compare(this, obj) == 0;
249             } });
250 
251         tEnv.addObjRelation("FirstPage", oDrawPage);
252         tEnv.addObjRelation("SecondPage", secondDrawPage);
253 
254         //tEnv.addObjRelation("First", shape1);
255         //tEnv.addObjRelation("Second", shape2);
256 
257         tEnv.addObjRelation("Pages", the_pages);
258 
259         //Adding ObjRelations for XController
260         tEnv.addObjRelation("FirstModel", aModel);
261 
262         tEnv.addObjRelation("XUserInputInterception.XModel", aModel);
263 
264         XFrame the_frame = the_Desk.getCurrentFrame();
265         tEnv.addObjRelation("Frame", the_frame);
266 
267          aModel = (XModel)
268             UnoRuntime.queryInterface(XModel.class, xSecondDrawDoc);
269         //Adding ObjRelations for XController
270         tEnv.addObjRelation("SecondModel", aModel);
271 
272         XController secondController = aModel.getCurrentController();
273         tEnv.addObjRelation("SecondController", secondController);
274         tEnv.addObjRelation("XDispatchProvider.URL",
275                                     "slot:27010");
276 
277         log.println("Implementation Name: "+utils.getImplName(oObj));
278 
279         XModifiable modify = (XModifiable)
280                               UnoRuntime.queryInterface(XModifiable.class,xImpressDoc);
281 
282         tEnv.addObjRelation("Modifiable",modify);
283 
284 
285         tEnv.addObjRelation("XComponent.DisposeThis", xImpressDoc);
286 
287         return tEnv;
288 
289     } // finish method getTestEnvironment
290 
291     private void shortWait() {
292         try {
293             Thread.sleep(5000) ;
294         } catch (InterruptedException e) {
295             System.out.println("While waiting :" + e) ;
296         }
297     }
298 
299 
300 } // finish class SdUnoDrawView
301 
302