1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._sd;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir import java.util.Comparator;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import lib.StatusException;
30cdf0e10cSrcweir import lib.TestCase;
31cdf0e10cSrcweir import lib.TestEnvironment;
32cdf0e10cSrcweir import lib.TestParameters;
33cdf0e10cSrcweir import util.DesktopTools;
34cdf0e10cSrcweir import util.SOfficeFactory;
35cdf0e10cSrcweir import util.utils;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import com.sun.star.awt.XWindow;
38cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
39cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
40cdf0e10cSrcweir import com.sun.star.drawing.XDrawPages;
41cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier;
42cdf0e10cSrcweir import com.sun.star.drawing.XShape;
43cdf0e10cSrcweir import com.sun.star.drawing.XShapes;
44cdf0e10cSrcweir import com.sun.star.frame.XController;
45cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
46cdf0e10cSrcweir import com.sun.star.frame.XFrame;
47cdf0e10cSrcweir import com.sun.star.frame.XModel;
48cdf0e10cSrcweir import com.sun.star.lang.XComponent;
49cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
50cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
51cdf0e10cSrcweir import com.sun.star.uno.Type;
52cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
53cdf0e10cSrcweir import com.sun.star.uno.XInterface;
54cdf0e10cSrcweir import com.sun.star.util.XModifiable;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir /**
57cdf0e10cSrcweir * Test for object which is represented by service
58cdf0e10cSrcweir * <code>com.sun.star.drawing.DrawingDocumentDrawView</code>. <p>
59cdf0e10cSrcweir * Object implements the following interfaces :
60cdf0e10cSrcweir * <ul>
61cdf0e10cSrcweir *  <li> <code>com::sun::star::drawing::DrawingDocumentDrawView</code></li>
62cdf0e10cSrcweir *  <li> <code>com::sun::star::lang::XComponent</code></li>
63cdf0e10cSrcweir *  <li> <code>com::sun::star::lang::XServiceInfo</code></li>
64cdf0e10cSrcweir *  <li> <code>com::sun::star::frame::XController</code></li>
65cdf0e10cSrcweir *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
66cdf0e10cSrcweir *  <li> <code>com::sun::star::view::XSelectionSupplier</code></li>
67cdf0e10cSrcweir *  <li> <code>com::sun::star::drawing::XDrawView</code></li>
68cdf0e10cSrcweir * </ul>
69cdf0e10cSrcweir * @see com.sun.star.drawing.DrawingDocumentDrawView
70cdf0e10cSrcweir * @see com.sun.star.lang.XComponent
71cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo
72cdf0e10cSrcweir * @see com.sun.star.frame.XController
73cdf0e10cSrcweir * @see com.sun.star.beans.XPropertySet
74cdf0e10cSrcweir * @see com.sun.star.view.XSelectionSupplier
75cdf0e10cSrcweir * @see com.sun.star.drawing.XDrawView
76cdf0e10cSrcweir * @see ifc.drawing._DrawingDocumentDrawView
77cdf0e10cSrcweir * @see ifc.lang._XComponent
78cdf0e10cSrcweir * @see ifc.lang._XServiceInfo
79cdf0e10cSrcweir * @see ifc.frame._XController
80cdf0e10cSrcweir * @see ifc.beans._XPropertySet
81cdf0e10cSrcweir * @see ifc.view._XSelectionSupplier
82cdf0e10cSrcweir * @see ifc.drawing._XDrawView
83cdf0e10cSrcweir */
84cdf0e10cSrcweir public class SdUnoPresView extends TestCase {
85cdf0e10cSrcweir     static XDesktop the_Desk;
86cdf0e10cSrcweir     static XComponent xImpressDoc;
87cdf0e10cSrcweir     static XComponent xSecondDrawDoc;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     /**
90cdf0e10cSrcweir     * Creates the instance of the service <code>com.sun.star.frame.Desktop</code>.
91cdf0e10cSrcweir     * @see com.sun.star.frame.Desktop
92cdf0e10cSrcweir     */
93cdf0e10cSrcweir     protected void initialize(TestParameters Param, PrintWriter log) {
94cdf0e10cSrcweir         the_Desk = (XDesktop)
95cdf0e10cSrcweir             UnoRuntime.queryInterface(
96cdf0e10cSrcweir                 XDesktop.class, DesktopTools.createDesktop(
97cdf0e10cSrcweir                                     (XMultiServiceFactory)Param.getMSF()) );
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     /**
101cdf0e10cSrcweir     * Called while disposing a <code>TestEnvironment</code>.
102cdf0e10cSrcweir     * Disposes Impress documents.
103cdf0e10cSrcweir     * @param tParam test parameters
104cdf0e10cSrcweir     * @param tEnv the environment to cleanup
105cdf0e10cSrcweir     * @param log writer to log information while testing
106cdf0e10cSrcweir     */
107cdf0e10cSrcweir     protected void cleanup( TestParameters Param, PrintWriter log) {
108cdf0e10cSrcweir         log.println("disposing impress documents");
109cdf0e10cSrcweir         util.DesktopTools.closeDoc(xImpressDoc);;
110cdf0e10cSrcweir         util.DesktopTools.closeDoc(xSecondDrawDoc);;
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     /**
114cdf0e10cSrcweir     * Creating a Testenvironment for the interfaces to be tested.
115cdf0e10cSrcweir     * Creates two impress documents. After creating of the documents makes short
116cdf0e10cSrcweir     * wait to allow frames to be loaded. Retrieves the collection of the draw pages
117cdf0e10cSrcweir     * from the first document and takes one of them. Inserts some shapes to the
118cdf0e10cSrcweir     * retrieved draw page. Obtains a current controller from the first document
119cdf0e10cSrcweir     * using the interface <code>XModel</code>. The obtained controller is the
120cdf0e10cSrcweir     * instance of the service
121cdf0e10cSrcweir     * <code>com.sun.star.drawing.DrawingDocumentDrawView</code>.
122cdf0e10cSrcweir     * Object relations created :
123cdf0e10cSrcweir     * <ul>
124cdf0e10cSrcweir     *  <li> <code>'First'</code> for
125cdf0e10cSrcweir     *      {@link ifc.view._XSelectionSupplier}(the first created shape) </li>
126cdf0e10cSrcweir     *  <li> <code>'Second'</code> for
127cdf0e10cSrcweir     *      {@link ifc.view._XSelectionSupplier}(the second created shape) </li>
128cdf0e10cSrcweir     *  <li> <code>'Pages'</code> for
129cdf0e10cSrcweir     *      {@link ifc.drawing._XDrawView}(the retrieved collection of the draw
130cdf0e10cSrcweir     *      pages) </li>
131cdf0e10cSrcweir     *  <li> <code>'FirstModel'</code> for
132cdf0e10cSrcweir     *      {@link ifc.frame._XController}(the interface <code>XModel</code> of
133cdf0e10cSrcweir     *      the first created document) </li>
134cdf0e10cSrcweir     *  <li> <code>'Frame'</code> for
135cdf0e10cSrcweir     *      {@link ifc.frame._XController}(the frame of the created
136cdf0e10cSrcweir     *      document) </li>
137cdf0e10cSrcweir     *  <li> <code>'SecondModel'</code> for
138cdf0e10cSrcweir     *      {@link ifc.frame._XController}(the interface <code>XModel</code> of
139cdf0e10cSrcweir     *      the second created document) </li>
140cdf0e10cSrcweir     *  <li> <code>'SecondController'</code> for
141cdf0e10cSrcweir     *      {@link ifc.frame._XController}(the current controller of the second
142cdf0e10cSrcweir     *      created document) </li>
143cdf0e10cSrcweir     * </ul>
144cdf0e10cSrcweir     * @see com.sun.star.frame.XModel
145cdf0e10cSrcweir     * @see com.sun.star.drawing.DrawingDocumentDrawView
146cdf0e10cSrcweir     */
147cdf0e10cSrcweir     protected synchronized TestEnvironment createTestEnvironment
148cdf0e10cSrcweir             (TestParameters Param, PrintWriter log) {
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         log.println( "creating a test environment" );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         // get a soffice factory object
153cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(
154cdf0e10cSrcweir                                         (XMultiServiceFactory)Param.getMSF());
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         try {
157cdf0e10cSrcweir             log.println( "creating two impress documents" );
158cdf0e10cSrcweir             xImpressDoc = SOF.createImpressDoc(null);
159cdf0e10cSrcweir             xSecondDrawDoc = SOF.createImpressDoc(null);
160cdf0e10cSrcweir             shortWait();
161cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
162cdf0e10cSrcweir             e.printStackTrace( log );
163cdf0e10cSrcweir             throw new StatusException("Couldn't create document", e);
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         // get the drawpage of drawing here
167cdf0e10cSrcweir         log.println( "getting Drawpage" );
168cdf0e10cSrcweir         XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
169cdf0e10cSrcweir             UnoRuntime.queryInterface(XDrawPagesSupplier.class, xImpressDoc);
170cdf0e10cSrcweir         XDrawPages the_pages = oDPS.getDrawPages();
171cdf0e10cSrcweir         XIndexAccess oDPi = (XIndexAccess)
172cdf0e10cSrcweir             UnoRuntime.queryInterface(XIndexAccess.class,the_pages);
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         XDrawPage oDrawPage = null;
175cdf0e10cSrcweir         XDrawPage secondDrawPage = null;
176cdf0e10cSrcweir         try {
177cdf0e10cSrcweir             oDrawPage = (XDrawPage) AnyConverter.toObject(
178cdf0e10cSrcweir                     new Type(XDrawPage.class),oDPi.getByIndex(0));
179cdf0e10cSrcweir             the_pages.insertNewByIndex(0);
180cdf0e10cSrcweir             the_pages.insertNewByIndex(0);
181cdf0e10cSrcweir             the_pages.insertNewByIndex(0);
182cdf0e10cSrcweir             the_pages.insertNewByIndex(0);
183cdf0e10cSrcweir            secondDrawPage = (XDrawPage) AnyConverter.toObject(
184cdf0e10cSrcweir                     new Type(XDrawPage.class),oDPi.getByIndex(3));
185cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
186cdf0e10cSrcweir             e.printStackTrace( log );
187cdf0e10cSrcweir             throw new StatusException("Couldn't get DrawPage", e);
188cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
189cdf0e10cSrcweir             e.printStackTrace( log );
190cdf0e10cSrcweir             throw new StatusException("Couldn't get DrawPage", e);
191cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
192cdf0e10cSrcweir             e.printStackTrace( log );
193cdf0e10cSrcweir             throw new StatusException("Couldn't get DrawPage", e);
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         //put something on the drawpage
197cdf0e10cSrcweir         log.println( "inserting some Shapes" );
198cdf0e10cSrcweir         XShapes oShapes = (XShapes)
199cdf0e10cSrcweir             UnoRuntime.queryInterface(XShapes.class, oDrawPage);
200cdf0e10cSrcweir         XShape shape1 = SOF.createShape(
201cdf0e10cSrcweir             xImpressDoc, 5000, 3500, 7500, 5000, "Rectangle");
202cdf0e10cSrcweir         oShapes.add(shape1);
203cdf0e10cSrcweir 
204cdf0e10cSrcweir         oShapes = (XShapes)
205cdf0e10cSrcweir             UnoRuntime.queryInterface(XShapes.class, secondDrawPage);
206cdf0e10cSrcweir         shape1 = SOF.createShape(
207cdf0e10cSrcweir             xImpressDoc, 3000, 4500, 15000, 1000, "Ellipse");
208cdf0e10cSrcweir         oShapes.add(shape1);
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         XModel aModel = (XModel)
211cdf0e10cSrcweir             UnoRuntime.queryInterface(XModel.class, xImpressDoc);
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         XInterface oObj = aModel.getCurrentController();
214cdf0e10cSrcweir 
215cdf0e10cSrcweir         XModel aModel2 = (XModel)
216cdf0e10cSrcweir             UnoRuntime.queryInterface(XModel.class, xSecondDrawDoc);
217cdf0e10cSrcweir 
218cdf0e10cSrcweir         XWindow anotherWindow = (XWindow) UnoRuntime.queryInterface(
219cdf0e10cSrcweir                                 XWindow.class,aModel2.getCurrentController());
220cdf0e10cSrcweir 
221cdf0e10cSrcweir         log.println( "creating a new environment for impress view object" );
222cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment( oObj );
223cdf0e10cSrcweir 
224cdf0e10cSrcweir         if (anotherWindow != null) {
225cdf0e10cSrcweir             tEnv.addObjRelation("XWindow.AnotherWindow",anotherWindow);
226cdf0e10cSrcweir         }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir         tEnv.addObjRelation("Selections", new Object[] {
229cdf0e10cSrcweir             oDrawPage, secondDrawPage});
230cdf0e10cSrcweir 
231cdf0e10cSrcweir         tEnv.addObjRelation("Comparer", new Comparator() {
232cdf0e10cSrcweir             public int compare(Object o1, Object o2) {
233cdf0e10cSrcweir                 XIndexAccess indAc1 = (XIndexAccess)
234cdf0e10cSrcweir                     UnoRuntime.queryInterface(XIndexAccess.class, o1);
235cdf0e10cSrcweir                 XIndexAccess indAc2 = (XIndexAccess)
236cdf0e10cSrcweir                     UnoRuntime.queryInterface(XIndexAccess.class, o2);
237cdf0e10cSrcweir                 if (indAc1 == null || indAc2 == null) return -1;
238cdf0e10cSrcweir                 if (indAc1.getCount() == indAc2.getCount()) {
239cdf0e10cSrcweir                     return 0;
240cdf0e10cSrcweir                 }
241cdf0e10cSrcweir                 return 1;
242cdf0e10cSrcweir             }
243cdf0e10cSrcweir             public boolean equals(Object obj) {
244cdf0e10cSrcweir                 return compare(this, obj) == 0;
245cdf0e10cSrcweir             } });
246cdf0e10cSrcweir 
247cdf0e10cSrcweir         tEnv.addObjRelation("FirstPage", oDrawPage);
248cdf0e10cSrcweir         tEnv.addObjRelation("SecondPage", secondDrawPage);
249cdf0e10cSrcweir 
250cdf0e10cSrcweir         //tEnv.addObjRelation("First", shape1);
251cdf0e10cSrcweir         //tEnv.addObjRelation("Second", shape2);
252cdf0e10cSrcweir 
253cdf0e10cSrcweir         tEnv.addObjRelation("Pages", the_pages);
254cdf0e10cSrcweir 
255cdf0e10cSrcweir         //Adding ObjRelations for XController
256cdf0e10cSrcweir         tEnv.addObjRelation("FirstModel", aModel);
257cdf0e10cSrcweir 
258cdf0e10cSrcweir         tEnv.addObjRelation("XUserInputInterception.XModel", aModel);
259cdf0e10cSrcweir 
260cdf0e10cSrcweir         XFrame the_frame = the_Desk.getCurrentFrame();
261cdf0e10cSrcweir         tEnv.addObjRelation("Frame", the_frame);
262cdf0e10cSrcweir 
263cdf0e10cSrcweir          aModel = (XModel)
264cdf0e10cSrcweir             UnoRuntime.queryInterface(XModel.class, xSecondDrawDoc);
265cdf0e10cSrcweir         //Adding ObjRelations for XController
266cdf0e10cSrcweir         tEnv.addObjRelation("SecondModel", aModel);
267cdf0e10cSrcweir 
268cdf0e10cSrcweir         XController secondController = aModel.getCurrentController();
269cdf0e10cSrcweir         tEnv.addObjRelation("SecondController", secondController);
270cdf0e10cSrcweir         tEnv.addObjRelation("XDispatchProvider.URL",
271cdf0e10cSrcweir                                     "slot:27010");
272cdf0e10cSrcweir 
273cdf0e10cSrcweir         log.println("Implementation Name: "+utils.getImplName(oObj));
274cdf0e10cSrcweir 
275cdf0e10cSrcweir         XModifiable modify = (XModifiable)
276cdf0e10cSrcweir                               UnoRuntime.queryInterface(XModifiable.class,xImpressDoc);
277cdf0e10cSrcweir 
278cdf0e10cSrcweir         tEnv.addObjRelation("Modifiable",modify);
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 
281cdf0e10cSrcweir         tEnv.addObjRelation("XComponent.DisposeThis", xImpressDoc);
282cdf0e10cSrcweir 
283cdf0e10cSrcweir         return tEnv;
284cdf0e10cSrcweir 
285cdf0e10cSrcweir     } // finish method getTestEnvironment
286cdf0e10cSrcweir 
287cdf0e10cSrcweir     private void shortWait() {
288cdf0e10cSrcweir         try {
289cdf0e10cSrcweir             Thread.sleep(5000) ;
290cdf0e10cSrcweir         } catch (InterruptedException e) {
291cdf0e10cSrcweir             System.out.println("While waiting :" + e) ;
292cdf0e10cSrcweir         }
293cdf0e10cSrcweir     }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 
296cdf0e10cSrcweir } // finish class SdUnoDrawView
297cdf0e10cSrcweir 
298