2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import com
.sun
.star
.beans
.PropertyValue
;
22 import com
.sun
.star
.frame
.XDesktop
;
23 import com
.sun
.star
.frame
.XDispatch
;
24 import com
.sun
.star
.frame
.XDispatchProvider
;
25 import com
.sun
.star
.frame
.XDispatchRecorder
;
26 import com
.sun
.star
.frame
.XModel
;
27 import com
.sun
.star
.lang
.XMultiServiceFactory
;
28 import com
.sun
.star
.frame
.XDispatchRecorderSupplier
;
29 import com
.sun
.star
.frame
.XFrame
;
30 import com
.sun
.star
.lang
.XComponent
;
31 import com
.sun
.star
.uno
.UnoRuntime
;
32 import com
.sun
.star
.util
.URL
;
33 import lib
.MultiMethodTest
;
34 import lib
.StatusException
;
35 import util
.SOfficeFactory
;
39 * Testing <code>com.sun.star.frame.XDispatchRecorderSupplier</code>
42 * <li><code> setDispatchRecorder() </code></li>
43 * <li><code> getDispatchRecorder() </code></li>
44 * <li><code> dispatchAndRecord() </code></li>
46 * Test is <b> NOT </b> multithread compilant. <p>
47 * @see com.sun.star.frame.XDispatchRecorderSupplier
49 public class _XDispatchRecorderSupplier
extends MultiMethodTest
{
50 public static XDispatchRecorderSupplier oObj
= null;
52 XComponent xTextDoc
= null;
53 XDispatchRecorder recorder
= null;
54 XDesktop desktop
= null;
57 * Simple <code>XDispatchRecorder</code> implementation
58 * which method <code>getRecordedMacro</code> returns a fixed
61 private static class MyRecorder
implements XDispatchRecorder
{
62 public void startRecording(XFrame p0
) {}
63 public void recordDispatch(URL p0
, PropertyValue
[] p1
) {}
64 public void recordDispatchAsComment(URL p0
, PropertyValue
[] p1
) {}
65 public void endRecording(){}
66 public String
getRecordedMacro() {
67 return "MyRecorder implementation";
72 * Creates a new document which supplies a frame.
73 * Also a <code>com.sun.star.frame.Desktop</code>
74 * service created for obtaining document's frame.
76 protected void before() {
77 SOfficeFactory SOF
= SOfficeFactory
.getFactory((XMultiServiceFactory
) tParam
.getMSF());
80 log
.println( "creating a text document" );
81 xTextDoc
= SOF
.createTextDoc(null);
83 Object inst
= ((XMultiServiceFactory
)tParam
.getMSF()).createInstance
84 ("com.sun.star.frame.Desktop");
85 desktop
= UnoRuntime
.queryInterface
86 (XDesktop
.class, inst
);
87 } catch ( com
.sun
.star
.uno
.Exception e
) {
88 // Some exception occurs.FAILED
89 e
.printStackTrace( log
);
90 throw new StatusException( "Couldn't create document", e
);
96 * Creates an instance of <code>MyRecorder</code> and set if,
97 * then get the current recorder. Second case is setting
98 * recorder to null. Finally restores the old macro recorder. <p>
100 * Has <b>OK</b> status if in the first case custom recorder
101 * was successfully returned, and in second case current
104 public void _setDispatchRecorder() {
105 requiredMethod("getDispatchRecorder()");
109 log
.print("Setting custom macro recorder ...");
110 oObj
.setDispatchRecorder(new MyRecorder());
111 XDispatchRecorder rec
= oObj
.getDispatchRecorder();
113 locRes
= rec
!= null &&
114 "MyRecorder implementation".equals(rec
.getRecordedMacro());
115 if (locRes
) log
.println("OK");
116 else log
.println("FAILED");
119 log
.print("Setting null dispatch recorder ...");
120 oObj
.setDispatchRecorder(null);
121 locRes
= oObj
.getDispatchRecorder() == null;
122 if (locRes
) log
.println("OK");
123 else log
.println("FAILED");
126 log
.println("Setting old macro recorder ...");
127 oObj
.setDispatchRecorder(recorder
);
129 tRes
.tested("setDispatchRecorder()", res
);
133 * Just gets the current recorder and stores it.
135 * Has <b>OK</b> status.
137 public void _getDispatchRecorder() {
138 recorder
= oObj
.getDispatchRecorder();
139 tRes
.tested("getDispatchRecorder()", true);
143 * First sets the current dispatch recorder to new
144 * <code>DispatchRecorder</code> instance if the current one
145 * is null. The a <code>Dispatch</code> instance is created
146 * which inserts some text into text document.
147 * A number of cases is checked :
149 * <li> A valid call : here the recorded macro must contain
150 * inserted string and URL </li>
151 * <li> Call with invalid URL : the macro recorded must not
152 * contain this URL </li>
153 * <li> Call with null dispatcher : checking for GPF </li>
154 * <li> Call with the current recorder set to null :
155 * checking for GPF </li>
158 * Has <b>OK</b> status if all cases are OK.
160 public void _dispatchAndRecord() {
161 requiredMethod("getDispatchRecorder()");
164 if (recorder
== null) {
166 Object inst
= ((XMultiServiceFactory
) tParam
.getMSF()).createInstance
167 ("com.sun.star.comp.framework.DispatchRecorder");
168 recorder
= UnoRuntime
.queryInterface
169 (XDispatchRecorder
.class, inst
);
170 oObj
.setDispatchRecorder(recorder
);
171 } catch (com
.sun
.star
.uno
.Exception e
) {
172 throw new StatusException("Couldn't create recorder", e
);
178 } catch (InterruptedException ex
) {}
180 XModel model
= UnoRuntime
.queryInterface(XModel
.class, xTextDoc
);
181 XFrame fr
= model
.getCurrentController().getFrame();
183 XDispatchProvider xDispProv
= UnoRuntime
.queryInterface(XDispatchProvider
.class, fr
);
185 URL dispURL
= utils
.parseURL((XMultiServiceFactory
) tParam
.getMSF(), ".uno:InsertText");
187 XDispatch xDisp
= xDispProv
.queryDispatch(dispURL
,"",0);
189 PropertyValue
[] args
= new PropertyValue
[1];
190 args
[0] = new PropertyValue();
191 args
[0].Name
= "Text";
192 args
[0].Value
= "XDispatchRecorderSupplier";
194 log
.print("Dispatching and recording ...");
195 oObj
.dispatchAndRecord(dispURL
, args
, xDisp
);
197 String macro
= recorder
.getRecordedMacro();
198 boolean locRes
= macro
!= null &&
199 macro
.indexOf("XDispatchRecorderSupplier")>-1 &&
200 macro
.indexOf(".uno:InsertText")>-1;
201 if (locRes
) log
.println("OK");
202 else log
.println("FAILED");
204 log
.println("Recorder macro :\n" + macro
);
206 log
.print("Trying to set dispatch with null Dispatcher ...");
208 oObj
.dispatchAndRecord(dispURL
, args
, null);
210 } catch (java
.lang
.Exception e
){
211 log
.println("Exception is OK: " + e
);
214 log
.print("Trying to set dispatch recorder to null and record ...");
215 oObj
.setDispatchRecorder(null);
217 oObj
.dispatchAndRecord(dispURL
, args
, xDisp
);
219 } catch (java
.lang
.Exception e
){
220 log
.println("Exception is OK: " + e
);
223 oObj
.setDispatchRecorder(recorder
);
225 tRes
.tested("dispatchAndRecord()", res
);
229 * Disposes the document created in <code>before()</code>
231 protected void after() {