1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sd;
29 
30 import com.sun.star.drawing.XDrawPages;
31 import java.io.PrintWriter;
32 
33 import lib.StatusException;
34 import lib.TestCase;
35 import lib.TestEnvironment;
36 import lib.TestParameters;
37 import util.DesktopTools;
38 import util.DrawTools;
39 import util.SOfficeFactory;
40 import util.utils;
41 
42 import com.sun.star.awt.XWindow;
43 import com.sun.star.frame.XController;
44 import com.sun.star.frame.XDesktop;
45 import com.sun.star.frame.XDispatch;
46 import com.sun.star.frame.XDispatchProvider;
47 import com.sun.star.frame.XFrame;
48 import com.sun.star.frame.XModel;
49 import com.sun.star.lang.XComponent;
50 import com.sun.star.lang.XMultiServiceFactory;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XInterface;
53 import com.sun.star.util.URL;
54 import com.sun.star.util.XURLTransformer;
55 
56 public class SdUnoSlideView extends TestCase {
57     XDesktop the_Desk;
58     XComponent xImpressDoc;
59     XComponent xSecondDrawDoc;
60 
61     /**
62     * Creates the instance of the service <code>com.sun.star.frame.Desktop</code>.
63     * @see com.sun.star.frame.Desktop
64     */
65     protected void initialize(TestParameters Param, PrintWriter log) {
66         the_Desk = (XDesktop)
67             UnoRuntime.queryInterface(
68                 XDesktop.class, DesktopTools.createDesktop(
69                                     (XMultiServiceFactory)Param.getMSF()) );
70     }
71 
72     /**
73     * Called while disposing a <code>TestEnvironment</code>.
74     * Disposes Impress documents.
75     * @param tParam test parameters
76     * @param tEnv the environment to cleanup
77     * @param log writer to log information while testing
78     */
79     protected void cleanup( TestParameters Param, PrintWriter log) {
80         log.println("disposing impress documents");
81         util.DesktopTools.closeDoc(xImpressDoc);;
82         util.DesktopTools.closeDoc(xSecondDrawDoc);;
83     }
84 
85     /**
86     * Creating a Testenvironment for the interfaces to be tested.
87     * Creates two impress documents. After creating of the documents makes short
88     * wait to allow frames to be loaded. Retrieves the collection of the draw pages
89     * from the first document and takes one of them. Inserts some shapes to the
90     * retrieved draw page. Obtains a current controller from the first document
91     * using the interface <code>XModel</code>. The obtained controller is the
92     * instance of the service <code>com.sun.star.presentation.OutlineView</code>.
93     * Object relations created :
94     * <ul>
95     *  <li> <code>'FirstModel'</code> for
96     *      {@link ifc.frame._XController}(the interface <code>XModel</code> of
97     *      the first created document) </li>
98     *  <li> <code>'Frame'</code> for
99     *      {@link ifc.frame._XController}(the frame of the created
100     *      document) </li>
101     *  <li> <code>'SecondModel'</code> for
102     *      {@link ifc.frame._XController}(the interface <code>XModel</code> of
103     *      the second created document) </li>
104     *  <li> <code>'SecondController'</code> for
105     *      {@link ifc.frame._XController}(the current controller of the second
106     *      created document) </li>
107     * </ul>
108     * @see com.sun.star.frame.XModel
109     */
110     protected synchronized TestEnvironment createTestEnvironment
111             (TestParameters Param, PrintWriter log) {
112 
113         log.println( "creating a test environment" );
114 
115         // get a soffice factory object
116         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
117         XDrawPages xDP = null;
118         try {
119             log.println( "creating a impress document" );
120             xImpressDoc = SOF.createImpressDoc(null);
121             shortWait();
122             xDP = DrawTools.getDrawPages(xImpressDoc);
123             xDP.insertNewByIndex(0);
124             xDP.insertNewByIndex(0);
125         } catch (com.sun.star.uno.Exception e) {
126             e.printStackTrace( log );
127             throw new StatusException("Couldn't create document", e);
128         }
129 
130         XModel aModel = (XModel)
131             UnoRuntime.queryInterface(XModel.class, xImpressDoc);
132 
133         XInterface oObj = aModel.getCurrentController();
134 
135         //Change to Slide view
136         try {
137             String aSlotID = "slot:27011";
138             XDispatchProvider xDispProv = (XDispatchProvider)
139                 UnoRuntime.queryInterface( XDispatchProvider.class, oObj );
140             XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
141                 UnoRuntime.queryInterface(XURLTransformer.class,
142         ((XMultiServiceFactory)Param.getMSF()).createInstance("com.sun.star.util.URLTransformer"));
143             // Because it's an in/out parameter we must use an array of URL objects.
144             URL[] aParseURL = new URL[1];
145             aParseURL[0] = new URL();
146             aParseURL[0].Complete = aSlotID;
147             xParser.parseStrict(aParseURL);
148             URL aURL = aParseURL[0];
149             XDispatch xDispatcher = xDispProv.queryDispatch( aURL,"",0);
150             if( xDispatcher != null )
151                     xDispatcher.dispatch( aURL, null );
152         } catch (com.sun.star.uno.Exception e) {
153             log.println("Couldn't change to slide view");
154         }
155 
156         try {
157             log.println( "creating a second impress document" );
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         XModel aModel2 = (XModel)
166             UnoRuntime.queryInterface(XModel.class, xSecondDrawDoc);
167 
168         XWindow anotherWindow = (XWindow) UnoRuntime.queryInterface(
169                                 XWindow.class,aModel2.getCurrentController());
170 
171         oObj = aModel.getCurrentController();
172 
173         log.println( "creating a new environment for slide view object" );
174         TestEnvironment tEnv = new TestEnvironment( oObj );
175 
176         if (anotherWindow != null) {
177             tEnv.addObjRelation("XWindow.AnotherWindow", anotherWindow);
178         }
179 
180          //Adding ObjRelations for XController
181         tEnv.addObjRelation("FirstModel", aModel);
182 
183         XFrame the_frame = the_Desk.getCurrentFrame();
184         tEnv.addObjRelation("Frame", the_frame);
185 
186         //Adding ObjRelations for XController
187         tEnv.addObjRelation("SecondModel", aModel2);
188 
189         XController secondController = aModel2.getCurrentController();
190         tEnv.addObjRelation("SecondController", secondController);
191         tEnv.addObjRelation("XDispatchProvider.URL",
192                                     "slot:27069");
193 
194         tEnv.addObjRelation("XUserInputInterception.XModel", aModel);
195 
196         //creating obj-relation for the XSelectionSupplier
197         try {
198             Object[] selections =
199                     new Object[]{xDP.getByIndex(0),xDP.getByIndex(1),xDP.getByIndex(2)};
200             tEnv.addObjRelation("Selections", selections);
201         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
202             e.printStackTrace(log);
203         }  catch (com.sun.star.lang.WrappedTargetException e) {
204             e.printStackTrace(log);
205         }
206         log.println("Implementation Name: " + utils.getImplName(oObj));
207 
208         return tEnv;
209 
210     } // finish method getTestEnvironment
211 
212     private void shortWait() {
213         try {
214             Thread.sleep(1000) ;
215         } catch (InterruptedException e) {
216             System.out.println("While waiting :" + e) ;
217         }
218     }
219 
220 
221 } // finish class SdUnoOutlineView
222 
223