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