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 ifc.frame;
25 
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.frame.XDesktop;
28 import com.sun.star.frame.XDispatch;
29 import com.sun.star.frame.XDispatchProvider;
30 import com.sun.star.frame.XDispatchRecorder;
31 import com.sun.star.frame.XModel;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.frame.XDispatchRecorderSupplier;
34 import com.sun.star.frame.XFrame;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XInterface;
38 import com.sun.star.util.URL;
39 import lib.MultiMethodTest;
40 import lib.StatusException;
41 import util.SOfficeFactory;
42 import util.utils;
43 
44 /**
45 * Testing <code>com.sun.star.frame.XDispatchRecorderSupplier</code>
46 * interface methods:
47 * <ul>
48 *  <li><code> setDispatchRecorder() </code></li>
49 *  <li><code> getDispatchRecorder() </code></li>
50 *  <li><code> dispatchAndRecord() </code></li>
51 * </ul><p>
52 * Test is <b> NOT </b> multithread compilant. <p>
53 * @see com.sun.star.frame.XDispatchRecorderSupplier
54 */
55 public class _XDispatchRecorderSupplier extends MultiMethodTest {
56 	public static XDispatchRecorderSupplier oObj = null;
57 
58     XComponent xTextDoc = null;
59     XDispatchRecorder recorder = null;
60     XDesktop desktop = null;
61 
62     /**
63      * Simple <code>XDispatchRecorder</code> implementation
64      * which method <code>getRecordedMacro</code> returns a fixed
65      * string.
66      */
67     private static class MyRecorder implements XDispatchRecorder {
startRecording(XFrame p0)68         public void startRecording(XFrame p0) {}
recordDispatch(URL p0, PropertyValue[] p1)69         public void recordDispatch(URL p0, PropertyValue[] p1) {}
recordDispatchAsComment(URL p0, PropertyValue[] p1)70         public void recordDispatchAsComment(URL p0, PropertyValue[] p1) {}
endRecording()71         public void endRecording(){}
getRecordedMacro()72         public String getRecordedMacro() {
73             return "MyRecorder implementation";
74         }
75     }
76 
77     /**
78      * Creates a new document which supplies a frame.
79      * Also a <code>com.sun.star.frame.Desktop</code>
80      * service created for obtaining document's frame.
81      */
before()82     protected void before() {
83         SOfficeFactory SOF = SOfficeFactory.getFactory((XMultiServiceFactory) tParam.getMSF());
84 
85         try {
86             log.println( "creating a text document" );
87             xTextDoc = SOF.createTextDoc(null);
88 
89             Object inst = (XInterface)((XMultiServiceFactory)tParam.getMSF()).createInstance
90                 ("com.sun.star.frame.Desktop");
91             desktop = (XDesktop) UnoRuntime.queryInterface
92                 (XDesktop.class, inst);
93         } catch ( com.sun.star.uno.Exception e ) {
94             // Some exception occures.FAILED
95             e.printStackTrace( log );
96             throw new StatusException( "Couldn't create document", e );
97         }
98 
99     }
100 
101     /**
102      * Creates an instance of <code>MyRecorder</code> and set if,
103      * then get the current recorder. Second case is setting
104      * recorder to null. Finally restores the old macro recorder. <p>
105      *
106      * Has <b>OK</b> status if in the first case custom recorder
107      * was successfully returned, and in second case current
108      * recorder is null.
109      */
_setDispatchRecorder()110     public void _setDispatchRecorder() {
111         requiredMethod("getDispatchRecorder()");
112 
113         boolean res = true,
114                 locRes = true;
115         log.print("Setting custom macro recorder ...");
116         oObj.setDispatchRecorder(new MyRecorder());
117         XDispatchRecorder rec = oObj.getDispatchRecorder();
118 
119         locRes = rec != null &&
120             "MyRecorder implementation".equals(rec.getRecordedMacro());
121         if (locRes) log.println("OK");
122         else log.println("FAILED");
123         res &= locRes;
124 
125         log.print("Setting null dispatch recorder ...");
126         oObj.setDispatchRecorder(null);
127         locRes = oObj.getDispatchRecorder() == null;
128         if (locRes) log.println("OK");
129         else log.println("FAILED");
130         res &= locRes;
131 
132         log.println("Setting old macro recorder ...");
133         oObj.setDispatchRecorder(recorder);
134 
135         tRes.tested("setDispatchRecorder()", res);
136     }
137 
138     /**
139      * Just gets the current recorder and stores it.
140      *
141      * Has <b>OK</b> status.
142      */
_getDispatchRecorder()143     public void _getDispatchRecorder() {
144         recorder = oObj.getDispatchRecorder();
145         tRes.tested("getDispatchRecorder()", true);
146     }
147 
148     /**
149      * First sets the current dispatch recorder to new
150      * <code>DispatchRecorder</code> instance if the current one
151      * is null. The a <code>Dispatch</code> instance is created
152      * which inserts some text into text document.
153      * A number of cases is checked :
154      * <ul>
155      *  <li> A valid call : here the recorded macro must contain
156      *      inserted string and URL </li>
157      *  <li> Call with invalid URL : the macro recorded must not
158      *      contain this URL </li>
159      *  <li> Call with null dispatcher : checking for GPF </li>
160      *  <li> Call with the current recorder set to null :
161      *      checking for GPF </li>
162      * </ul>
163      *
164      * Has <b>OK</b> status if all cases are OK.
165      */
_dispatchAndRecord()166     public void _dispatchAndRecord() {
167         requiredMethod("getDispatchRecorder()");
168 
169         boolean res = true;
170         if (recorder == null) {
171             try {
172                 Object inst = ((XMultiServiceFactory) tParam.getMSF()).createInstance
173                     ("com.sun.star.comp.framework.DispatchRecorder");
174                 recorder = (XDispatchRecorder) UnoRuntime.queryInterface
175                     (XDispatchRecorder.class, inst);
176                 oObj.setDispatchRecorder(recorder);
177             } catch (com.sun.star.uno.Exception e) {
178                 throw new StatusException("Couldn't create recorder", e);
179             }
180         }
181 
182         try {
183             Thread.sleep(500);
184         } catch (InterruptedException ex) {}
185 
186         XModel model = (XModel) UnoRuntime.queryInterface(XModel.class, xTextDoc);
187         XFrame fr = model.getCurrentController().getFrame();
188 
189         XDispatchProvider xDispProv = (XDispatchProvider)
190             UnoRuntime.queryInterface(XDispatchProvider.class, fr);
191 
192         URL dispURL = utils.parseURL((XMultiServiceFactory) tParam.getMSF(), ".uno:InsertText");
193 
194         XDispatch xDisp = xDispProv.queryDispatch(dispURL,"",0);
195 
196         PropertyValue[] args = new PropertyValue[1];
197         args[0] = new PropertyValue();
198         args[0].Name = "Text";
199         args[0].Value = "XDispatchRecorderSupplier";
200 
201         log.print("Dispatching and recording ...");
202         oObj.dispatchAndRecord(dispURL, args, xDisp);
203 
204         String macro = recorder.getRecordedMacro();
205         boolean locRes = macro != null &&
206             macro.indexOf("XDispatchRecorderSupplier")>-1 &&
207             macro.indexOf(".uno:InsertText")>-1;
208         if (locRes) log.println("OK");
209         else log.println("FAILED");
210         res &= locRes;
211         log.println("Recorder macro :\n" + macro);
212 
213         log.print("Trying to set dispatch with null Dispatcher ...");
214         try {
215             oObj.dispatchAndRecord(dispURL, args, null);
216             log.println("OK");
217         } catch (java.lang.Exception e){
218             log.println("Exception is OK: " + e);
219         }
220 
221         log.print("Trying to set dispatch recorder to null and record ...");
222         oObj.setDispatchRecorder(null);
223         try {
224             oObj.dispatchAndRecord(dispURL, args, xDisp);
225             log.println("OK");
226         } catch (java.lang.Exception e){
227             log.println("Exception is OK: " + e);
228         }
229 
230         oObj.setDispatchRecorder(recorder);
231 
232         tRes.tested("dispatchAndRecord()", res);
233     }
234 
235     /**
236      * Disposes the document created in <code>before()</code>
237      */
after()238     protected void after() {
239         xTextDoc.dispose();
240     }
241 }