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