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.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 import util.DesktopTools;
34 import util.SOfficeFactory;
35 import util.utils;
36 
37 import com.sun.star.awt.XWindow;
38 import com.sun.star.container.XIndexAccess;
39 import com.sun.star.drawing.XDrawPage;
40 import com.sun.star.drawing.XDrawPages;
41 import com.sun.star.drawing.XDrawPagesSupplier;
42 import com.sun.star.drawing.XShape;
43 import com.sun.star.drawing.XShapes;
44 import com.sun.star.frame.XController;
45 import com.sun.star.frame.XDesktop;
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 SdUnoPresView extends TestCase {
85     static XDesktop the_Desk;
86     static XComponent xImpressDoc;
87     static XComponent xSecondDrawDoc;
88 
89     /**
90     * Creates the instance of the service <code>com.sun.star.frame.Desktop</code>.
91     * @see com.sun.star.frame.Desktop
92     */
93     protected void initialize(TestParameters Param, PrintWriter log) {
94         the_Desk = (XDesktop)
95             UnoRuntime.queryInterface(
96                 XDesktop.class, DesktopTools.createDesktop(
97                                     (XMultiServiceFactory)Param.getMSF()) );
98     }
99 
100     /**
101     * Called while disposing a <code>TestEnvironment</code>.
102     * Disposes Impress documents.
103     * @param tParam test parameters
104     * @param tEnv the environment to cleanup
105     * @param log writer to log information while testing
106     */
107     protected void cleanup( TestParameters Param, PrintWriter log) {
108         log.println("disposing impress documents");
109         util.DesktopTools.closeDoc(xImpressDoc);
110         util.DesktopTools.closeDoc(xSecondDrawDoc);
111     }
112 
113     /**
114     * Creating a Testenvironment for the interfaces to be tested.
115     * Creates two impress documents. After creating of the documents makes short
116     * wait to allow frames to be loaded. Retrieves the collection of the draw pages
117     * from the first document and takes one of them. Inserts some shapes to the
118     * retrieved draw page. Obtains a current controller from the first document
119     * using the interface <code>XModel</code>. The obtained controller is the
120     * instance of the service
121     * <code>com.sun.star.drawing.DrawingDocumentDrawView</code>.
122     * Object relations created :
123     * <ul>
124     *  <li> <code>'First'</code> for
125     *      {@link ifc.view._XSelectionSupplier}(the first created shape) </li>
126     *  <li> <code>'Second'</code> for
127     *      {@link ifc.view._XSelectionSupplier}(the second created shape) </li>
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     * </ul>
144     * @see com.sun.star.frame.XModel
145     * @see com.sun.star.drawing.DrawingDocumentDrawView
146     */
147     protected synchronized TestEnvironment createTestEnvironment
148             (TestParameters Param, PrintWriter log) {
149 
150         log.println( "creating a test environment" );
151 
152         // get a soffice factory object
153         SOfficeFactory SOF = SOfficeFactory.getFactory(
154                                         (XMultiServiceFactory)Param.getMSF());
155 
156         try {
157             log.println( "creating two impress documents" );
158             xImpressDoc = SOF.createImpressDoc(null);
159             xSecondDrawDoc = SOF.createImpressDoc(null);
160             shortWait();
161         } catch (com.sun.star.uno.Exception e) {
162             e.printStackTrace( log );
163             throw new StatusException("Couldn't create document", e);
164         }
165 
166         // get the drawpage of drawing here
167         log.println( "getting Drawpage" );
168         XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
169             UnoRuntime.queryInterface(XDrawPagesSupplier.class, xImpressDoc);
170         XDrawPages the_pages = oDPS.getDrawPages();
171         XIndexAccess oDPi = (XIndexAccess)
172             UnoRuntime.queryInterface(XIndexAccess.class,the_pages);
173 
174         XDrawPage oDrawPage = null;
175         XDrawPage secondDrawPage = null;
176         try {
177             oDrawPage = (XDrawPage) AnyConverter.toObject(
178                     new Type(XDrawPage.class),oDPi.getByIndex(0));
179             the_pages.insertNewByIndex(0);
180             the_pages.insertNewByIndex(0);
181             the_pages.insertNewByIndex(0);
182             the_pages.insertNewByIndex(0);
183            secondDrawPage = (XDrawPage) AnyConverter.toObject(
184                     new Type(XDrawPage.class),oDPi.getByIndex(3));
185         } catch (com.sun.star.lang.WrappedTargetException e) {
186             e.printStackTrace( log );
187             throw new StatusException("Couldn't get DrawPage", e);
188         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
189             e.printStackTrace( log );
190             throw new StatusException("Couldn't get DrawPage", e);
191         } catch (com.sun.star.lang.IllegalArgumentException e) {
192             e.printStackTrace( log );
193             throw new StatusException("Couldn't get DrawPage", e);
194         }
195 
196         //put something on the drawpage
197         log.println( "inserting some Shapes" );
198         XShapes oShapes = (XShapes)
199             UnoRuntime.queryInterface(XShapes.class, oDrawPage);
200         XShape shape1 = SOF.createShape(
201             xImpressDoc, 5000, 3500, 7500, 5000, "Rectangle");
202         oShapes.add(shape1);
203 
204         oShapes = (XShapes)
205             UnoRuntime.queryInterface(XShapes.class, secondDrawPage);
206         shape1 = SOF.createShape(
207             xImpressDoc, 3000, 4500, 15000, 1000, "Ellipse");
208         oShapes.add(shape1);
209 
210         XModel aModel = (XModel)
211             UnoRuntime.queryInterface(XModel.class, xImpressDoc);
212 
213         XInterface oObj = aModel.getCurrentController();
214 
215         XModel aModel2 = (XModel)
216             UnoRuntime.queryInterface(XModel.class, xSecondDrawDoc);
217 
218         XWindow anotherWindow = (XWindow) UnoRuntime.queryInterface(
219                                 XWindow.class,aModel2.getCurrentController());
220 
221         log.println( "creating a new environment for impress view object" );
222         TestEnvironment tEnv = new TestEnvironment( oObj );
223 
224         if (anotherWindow != null) {
225             tEnv.addObjRelation("XWindow.AnotherWindow",anotherWindow);
226         }
227 
228         tEnv.addObjRelation("Selections", new Object[] {
229             oDrawPage, secondDrawPage});
230 
231         tEnv.addObjRelation("Comparer", new Comparator() {
232             public int compare(Object o1, Object o2) {
233                 XIndexAccess indAc1 = (XIndexAccess)
234                     UnoRuntime.queryInterface(XIndexAccess.class, o1);
235                 XIndexAccess indAc2 = (XIndexAccess)
236                     UnoRuntime.queryInterface(XIndexAccess.class, o2);
237                 if (indAc1 == null || indAc2 == null) return -1;
238                 if (indAc1.getCount() == indAc2.getCount()) {
239                     return 0;
240                 }
241                 return 1;
242             }
243             public boolean equals(Object obj) {
244                 return compare(this, obj) == 0;
245             } });
246 
247         tEnv.addObjRelation("FirstPage", oDrawPage);
248         tEnv.addObjRelation("SecondPage", secondDrawPage);
249 
250         //tEnv.addObjRelation("First", shape1);
251         //tEnv.addObjRelation("Second", shape2);
252 
253         tEnv.addObjRelation("Pages", the_pages);
254 
255         //Adding ObjRelations for XController
256         tEnv.addObjRelation("FirstModel", aModel);
257 
258         tEnv.addObjRelation("XUserInputInterception.XModel", aModel);
259 
260         XFrame the_frame = the_Desk.getCurrentFrame();
261         tEnv.addObjRelation("Frame", the_frame);
262 
263          aModel = (XModel)
264             UnoRuntime.queryInterface(XModel.class, xSecondDrawDoc);
265         //Adding ObjRelations for XController
266         tEnv.addObjRelation("SecondModel", aModel);
267 
268         XController secondController = aModel.getCurrentController();
269         tEnv.addObjRelation("SecondController", secondController);
270         tEnv.addObjRelation("XDispatchProvider.URL",
271                                     "slot:27010");
272 
273         log.println("Implementation Name: "+utils.getImplName(oObj));
274 
275         XModifiable modify = (XModifiable)
276                               UnoRuntime.queryInterface(XModifiable.class,xImpressDoc);
277 
278         tEnv.addObjRelation("Modifiable",modify);
279 
280 
281         tEnv.addObjRelation("XComponent.DisposeThis", xImpressDoc);
282 
283         return tEnv;
284 
285     } // finish method getTestEnvironment
286 
287     private void shortWait() {
288         try {
289             Thread.sleep(5000) ;
290         } catch (InterruptedException e) {
291             System.out.println("While waiting :" + e) ;
292         }
293     }
294 
295 
296 } // finish class SdUnoDrawView
297 
298