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