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 com.sun.star.drawing.XDrawPages;
27 import java.io.PrintWriter;
28 
29 import lib.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 import util.DesktopTools;
34 import util.DrawTools;
35 import util.SOfficeFactory;
36 import util.utils;
37 
38 import com.sun.star.awt.XWindow;
39 import com.sun.star.frame.XController;
40 import com.sun.star.frame.XDesktop;
41 import com.sun.star.frame.XDispatch;
42 import com.sun.star.frame.XDispatchProvider;
43 import com.sun.star.frame.XFrame;
44 import com.sun.star.frame.XModel;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 import com.sun.star.util.URL;
50 import com.sun.star.util.XURLTransformer;
51 
52 public class SdUnoSlideView extends TestCase {
53     XDesktop the_Desk;
54     XComponent xImpressDoc;
55     XComponent xSecondDrawDoc;
56 
57     /**
58     * Creates the instance of the service <code>com.sun.star.frame.Desktop</code>.
59     * @see com.sun.star.frame.Desktop
60     */
61     protected void initialize(TestParameters Param, PrintWriter log) {
62         the_Desk = (XDesktop)
63             UnoRuntime.queryInterface(
64                 XDesktop.class, DesktopTools.createDesktop(
65                                     (XMultiServiceFactory)Param.getMSF()) );
66     }
67 
68     /**
69     * Called while disposing a <code>TestEnvironment</code>.
70     * Disposes Impress documents.
71     * @param tParam test parameters
72     * @param tEnv the environment to cleanup
73     * @param log writer to log information while testing
74     */
75     protected void cleanup( TestParameters Param, PrintWriter log) {
76         log.println("disposing impress documents");
77         util.DesktopTools.closeDoc(xImpressDoc);
78         util.DesktopTools.closeDoc(xSecondDrawDoc);
79     }
80 
81     /**
82     * Creating a Testenvironment for the interfaces to be tested.
83     * Creates two impress documents. After creating of the documents makes short
84     * wait to allow frames to be loaded. Retrieves the collection of the draw pages
85     * from the first document and takes one of them. Inserts some shapes to the
86     * retrieved draw page. Obtains a current controller from the first document
87     * using the interface <code>XModel</code>. The obtained controller is the
88     * instance of the service <code>com.sun.star.presentation.OutlineView</code>.
89     * Object relations created :
90     * <ul>
91     *  <li> <code>'FirstModel'</code> for
92     *      {@link ifc.frame._XController}(the interface <code>XModel</code> of
93     *      the first created document) </li>
94     *  <li> <code>'Frame'</code> for
95     *      {@link ifc.frame._XController}(the frame of the created
96     *      document) </li>
97     *  <li> <code>'SecondModel'</code> for
98     *      {@link ifc.frame._XController}(the interface <code>XModel</code> of
99     *      the second created document) </li>
100     *  <li> <code>'SecondController'</code> for
101     *      {@link ifc.frame._XController}(the current controller of the second
102     *      created document) </li>
103     * </ul>
104     * @see com.sun.star.frame.XModel
105     */
106     protected synchronized TestEnvironment createTestEnvironment
107             (TestParameters Param, PrintWriter log) {
108 
109         log.println( "creating a test environment" );
110 
111         // get a soffice factory object
112         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
113         XDrawPages xDP = null;
114         try {
115             log.println( "creating a impress document" );
116             xImpressDoc = SOF.createImpressDoc(null);
117             shortWait();
118             xDP = DrawTools.getDrawPages(xImpressDoc);
119             xDP.insertNewByIndex(0);
120             xDP.insertNewByIndex(0);
121         } catch (com.sun.star.uno.Exception e) {
122             e.printStackTrace( log );
123             throw new StatusException("Couldn't create document", e);
124         }
125 
126         XModel aModel = (XModel)
127             UnoRuntime.queryInterface(XModel.class, xImpressDoc);
128 
129         XInterface oObj = aModel.getCurrentController();
130 
131         //Change to Slide view
132         try {
133             String aSlotID = "slot:27011";
134             XDispatchProvider xDispProv = (XDispatchProvider)
135                 UnoRuntime.queryInterface( XDispatchProvider.class, oObj );
136             XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
137                 UnoRuntime.queryInterface(XURLTransformer.class,
138         ((XMultiServiceFactory)Param.getMSF()).createInstance("com.sun.star.util.URLTransformer"));
139             // Because it's an in/out parameter we must use an array of URL objects.
140             URL[] aParseURL = new URL[1];
141             aParseURL[0] = new URL();
142             aParseURL[0].Complete = aSlotID;
143             xParser.parseStrict(aParseURL);
144             URL aURL = aParseURL[0];
145             XDispatch xDispatcher = xDispProv.queryDispatch( aURL,"",0);
146             if( xDispatcher != null )
147                     xDispatcher.dispatch( aURL, null );
148         } catch (com.sun.star.uno.Exception e) {
149             log.println("Couldn't change to slide view");
150         }
151 
152         try {
153             log.println( "creating a second impress document" );
154             xSecondDrawDoc = SOF.createImpressDoc(null);
155             shortWait();
156         } catch (com.sun.star.uno.Exception e) {
157             e.printStackTrace( log );
158             throw new StatusException("Couldn't create document", e);
159         }
160 
161         XModel aModel2 = (XModel)
162             UnoRuntime.queryInterface(XModel.class, xSecondDrawDoc);
163 
164         XWindow anotherWindow = (XWindow) UnoRuntime.queryInterface(
165                                 XWindow.class,aModel2.getCurrentController());
166 
167         oObj = aModel.getCurrentController();
168 
169         log.println( "creating a new environment for slide view object" );
170         TestEnvironment tEnv = new TestEnvironment( oObj );
171 
172         if (anotherWindow != null) {
173             tEnv.addObjRelation("XWindow.AnotherWindow", anotherWindow);
174         }
175 
176          //Adding ObjRelations for XController
177         tEnv.addObjRelation("FirstModel", aModel);
178 
179         XFrame the_frame = the_Desk.getCurrentFrame();
180         tEnv.addObjRelation("Frame", the_frame);
181 
182         //Adding ObjRelations for XController
183         tEnv.addObjRelation("SecondModel", aModel2);
184 
185         XController secondController = aModel2.getCurrentController();
186         tEnv.addObjRelation("SecondController", secondController);
187         tEnv.addObjRelation("XDispatchProvider.URL",
188                                     "slot:27069");
189 
190         tEnv.addObjRelation("XUserInputInterception.XModel", aModel);
191 
192         //creating obj-relation for the XSelectionSupplier
193         try {
194             Object[] selections =
195                     new Object[]{xDP.getByIndex(0),xDP.getByIndex(1),xDP.getByIndex(2)};
196             tEnv.addObjRelation("Selections", selections);
197         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
198             e.printStackTrace(log);
199         }  catch (com.sun.star.lang.WrappedTargetException e) {
200             e.printStackTrace(log);
201         }
202         log.println("Implementation Name: " + utils.getImplName(oObj));
203 
204         return tEnv;
205 
206     } // finish method getTestEnvironment
207 
208     private void shortWait() {
209         try {
210             Thread.sleep(1000) ;
211         } catch (InterruptedException e) {
212             System.out.println("While waiting :" + e) ;
213         }
214     }
215 
216 
217 } // finish class SdUnoOutlineView
218 
219