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