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