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>NotesMode</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_NotesView 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>NotesMode</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         XIndexAccess oDPi = (XIndexAccess)
177             UnoRuntime.queryInterface(XIndexAccess.class,the_pages);
178 
179         XDrawPage oDrawPage = null;
180         try {
181             oDrawPage = (XDrawPage) AnyConverter.toObject(
182                         new Type(XDrawPage.class),oDPi.getByIndex(0));
183         } catch (com.sun.star.lang.WrappedTargetException e) {
184             e.printStackTrace( log );
185             throw new StatusException("Couldn't get DrawPage", e);
186         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
187             e.printStackTrace( log );
188             throw new StatusException("Couldn't get DrawPage", e);
189         } catch (com.sun.star.lang.IllegalArgumentException e) {
190             e.printStackTrace( log );
191             throw new StatusException("Couldn't get DrawPage", e);
192         }
193 
194         //put something on the drawpage
195         log.println( "inserting some Shapes" );
196         XShapes oShapes = (XShapes)
197             UnoRuntime.queryInterface(XShapes.class, oDrawPage);
198         XShape shape1 = SOF.createShape(
199             xDrawDoc, 3000, 4500, 15000, 1000, "Ellipse");
200         XShape shape2 = SOF.createShape(
201             xDrawDoc, 5000, 3500, 7500, 5000, "Rectangle");
202         XShape shape3 = SOF.createShape(
203             xDrawDoc, 3000, 500, 5000, 1000, "Line");
204         oShapes.add(shape1);
205         oShapes.add(shape2);
206         oShapes.add(shape3);
207         shortWait();
208 
209         log.println("switch to HandoutView...");
210         try{
211             utils.dispatchURL(xMSF, xDrawDoc, ".uno:NotesMode");
212         } catch (Exception e){
213             e.printStackTrace(log);
214             throw new StatusException(Status.failed(e.toString()));
215         }
216 
217         utils.shortWait(500);
218 
219         XModel aModel = (XModel)
220             UnoRuntime.queryInterface(XModel.class, xDrawDoc);
221 
222         XInterface oObj = aModel.getCurrentController();
223 
224         log.println("bring first document to front...");
225         DesktopTools.bringWindowToFront(aModel);
226 
227         XModel aModel2 = (XModel)
228             UnoRuntime.queryInterface(XModel.class, xSecondDrawDoc);
229 
230         XWindow anotherWindow = (XWindow) UnoRuntime.queryInterface(
231                                 XWindow.class,aModel2.getCurrentController());
232 
233         log.println( "creating a new environment for impress view object" );
234         TestEnvironment tEnv = new TestEnvironment( oObj );
235 
236         if (anotherWindow != null) {
237             tEnv.addObjRelation("XWindow.AnotherWindow",anotherWindow);
238         }
239 
240         Object oShapeCol1 = null;
241         Object oShapeCol2 = null;
242         try {
243             oShapeCol1 = xMSF.
244                 createInstance("com.sun.star.drawing.ShapeCollection");
245             oShapeCol2 = xMSF.
246                 createInstance("com.sun.star.drawing.ShapeCollection");
247         } catch(com.sun.star.uno.Exception e) {
248             e.printStackTrace(log);
249             throw new StatusException(Status.failed("Couldn't create instance"));
250         }
251 
252         XShapes xShapes1 = (XShapes)
253             UnoRuntime.queryInterface(XShapes.class, oShapeCol1);
254         XShapes xShapes2 = (XShapes)
255             UnoRuntime.queryInterface(XShapes.class, oShapeCol2);
256         xShapes1.add(shape2);
257         xShapes1.add(shape3);
258         xShapes2.add(shape1);
259         shortWait();
260 
261         tEnv.addObjRelation("Selections", new Object[] {
262             oDrawPage, oShapeCol1, oShapeCol2});
263         tEnv.addObjRelation("Comparer", new Comparator() {
264             public int compare(Object o1, Object o2) {
265                 XIndexAccess indAc1 = (XIndexAccess)
266                     UnoRuntime.queryInterface(XIndexAccess.class, o1);
267                 XIndexAccess indAc2 = (XIndexAccess)
268                     UnoRuntime.queryInterface(XIndexAccess.class, o2);
269                 if (indAc1 == null || indAc2 == null) return -1;
270                 if (indAc1.getCount() == indAc2.getCount()) {
271                     return 0;
272                 }
273                 return 1;
274             }
275             public boolean equals(Object obj) {
276                 return compare(this, obj) == 0;
277             } });
278 
279 
280 
281         tEnv.addObjRelation("Pages", the_pages);
282 
283         //Adding ObjRelations for XController
284         tEnv.addObjRelation("FirstModel", aModel);
285 
286         tEnv.addObjRelation("XUserInputInterception.XModel", aModel);
287 
288         XFrame the_frame = the_Desk.getCurrentFrame();
289         tEnv.addObjRelation("Frame", the_frame);
290 
291          aModel = (XModel)
292             UnoRuntime.queryInterface(XModel.class, xSecondDrawDoc);
293         //Adding ObjRelations for XController
294         tEnv.addObjRelation("SecondModel", aModel);
295 
296         XController secondController = aModel.getCurrentController();
297         tEnv.addObjRelation("SecondController", secondController);
298         tEnv.addObjRelation("XDispatchProvider.URL",
299                                     "slot:27009");
300 
301         //Adding relations for DrawingDocumentDrawView
302         XDrawPage new_page = the_pages.insertNewByIndex(1);
303         tEnv.addObjRelation("DrawPage", new_page);
304 
305         log.println("Implementation Name: "+utils.getImplName(oObj));
306 
307         XModifiable modify = (XModifiable)
308             UnoRuntime.queryInterface(XModifiable.class,xDrawDoc);
309 
310         tEnv.addObjRelation("Modifiable",modify);
311 
312         tEnv.addObjRelation("XComponent.DisposeThis", xDrawDoc);
313 
314 
315         return tEnv;
316 
317     } // finish method getTestEnvironment
318 
shortWait()319     private void shortWait() {
320         try {
321             Thread.sleep(1000) ;
322         } catch (InterruptedException e) {
323             System.out.println("While waiting :" + e) ;
324         }
325     }
326 
327 
328 }
329 
330