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