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