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
.frame
.XDispatchRecorderSupplier
;
28 import com
.sun
.star
.frame
.XFrame
;
29 import com
.sun
.star
.lang
.XComponent
;
30 import com
.sun
.star
.uno
.UnoRuntime
;
31 import com
.sun
.star
.util
.URL
;
32 import lib
.MultiMethodTest
;
33 import lib
.StatusException
;
34 import util
.SOfficeFactory
;
38 * Testing <code>com.sun.star.frame.XDispatchRecorderSupplier</code>
41 * <li><code> setDispatchRecorder() </code></li>
42 * <li><code> getDispatchRecorder() </code></li>
43 * <li><code> dispatchAndRecord() </code></li>
45 * Test is <b> NOT </b> multithread compliant. <p>
46 * @see com.sun.star.frame.XDispatchRecorderSupplier
48 public class _XDispatchRecorderSupplier
extends MultiMethodTest
{
49 public static XDispatchRecorderSupplier oObj
= null;
51 XComponent xTextDoc
= null;
52 XDispatchRecorder recorder
= null;
53 XDesktop desktop
= null;
56 * Simple <code>XDispatchRecorder</code> implementation
57 * which method <code>getRecordedMacro</code> returns a fixed
60 private static class MyRecorder
implements XDispatchRecorder
{
61 public void startRecording(XFrame p0
) {}
62 public void recordDispatch(URL p0
, PropertyValue
[] p1
) {}
63 public void recordDispatchAsComment(URL p0
, PropertyValue
[] p1
) {}
64 public void endRecording(){}
65 public String
getRecordedMacro() {
66 return "MyRecorder implementation";
71 * Creates a new document which supplies a frame.
72 * Also a <code>com.sun.star.frame.Desktop</code>
73 * service created for obtaining document's frame.
76 protected void before() {
77 SOfficeFactory SOF
= SOfficeFactory
.getFactory(tParam
.getMSF());
80 log
.println( "creating a text document" );
81 xTextDoc
= SOF
.createTextDoc(null);
83 Object inst
= 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
= 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
);
176 util
.utils
.shortWait();
178 XModel model
= UnoRuntime
.queryInterface(XModel
.class, xTextDoc
);
179 XFrame fr
= model
.getCurrentController().getFrame();
181 XDispatchProvider xDispProv
= UnoRuntime
.queryInterface(XDispatchProvider
.class, fr
);
183 URL dispURL
= utils
.parseURL(tParam
.getMSF(), ".uno:InsertText");
185 XDispatch xDisp
= xDispProv
.queryDispatch(dispURL
,"",0);
187 PropertyValue
[] args
= new PropertyValue
[1];
188 args
[0] = new PropertyValue();
189 args
[0].Name
= "Text";
190 args
[0].Value
= "XDispatchRecorderSupplier";
192 log
.print("Dispatching and recording ...");
193 oObj
.dispatchAndRecord(dispURL
, args
, xDisp
);
195 String macro
= recorder
.getRecordedMacro();
196 boolean locRes
= macro
!= null &&
197 macro
.indexOf("XDispatchRecorderSupplier")>-1 &&
198 macro
.indexOf(".uno:InsertText")>-1;
199 if (locRes
) log
.println("OK");
200 else log
.println("FAILED");
202 log
.println("Recorder macro :\n" + macro
);
204 log
.print("Trying to set dispatch with null Dispatcher ...");
206 oObj
.dispatchAndRecord(dispURL
, args
, null);
208 } catch (java
.lang
.Exception e
){
209 log
.println("Exception is OK: " + e
);
212 log
.print("Trying to set dispatch recorder to null and record ...");
213 oObj
.setDispatchRecorder(null);
215 oObj
.dispatchAndRecord(dispURL
, args
, xDisp
);
217 } catch (java
.lang
.Exception e
){
218 log
.println("Exception is OK: " + e
);
221 oObj
.setDispatchRecorder(recorder
);
223 tRes
.tested("dispatchAndRecord()", res
);
227 * Disposes the document created in <code>before()</code>
230 protected void after() {