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