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