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