1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XDispatchRecorderSupplier.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import com
.sun
.star
.beans
.PropertyValue
;
34 import com
.sun
.star
.frame
.XDesktop
;
35 import com
.sun
.star
.frame
.XDispatch
;
36 import com
.sun
.star
.frame
.XDispatchProvider
;
37 import com
.sun
.star
.frame
.XDispatchRecorder
;
38 import com
.sun
.star
.frame
.XModel
;
39 import com
.sun
.star
.lang
.XMultiServiceFactory
;
40 import com
.sun
.star
.frame
.XDispatchRecorderSupplier
;
41 import com
.sun
.star
.frame
.XFrame
;
42 import com
.sun
.star
.lang
.XComponent
;
43 import com
.sun
.star
.uno
.UnoRuntime
;
44 import com
.sun
.star
.uno
.XInterface
;
45 import com
.sun
.star
.util
.URL
;
46 import lib
.MultiMethodTest
;
47 import lib
.StatusException
;
48 import util
.SOfficeFactory
;
52 * Testing <code>com.sun.star.frame.XDispatchRecorderSupplier</code>
55 * <li><code> setDispatchRecorder() </code></li>
56 * <li><code> getDispatchRecorder() </code></li>
57 * <li><code> dispatchAndRecord() </code></li>
59 * Test is <b> NOT </b> multithread compilant. <p>
60 * @see com.sun.star.frame.XDispatchRecorderSupplier
62 public class _XDispatchRecorderSupplier
extends MultiMethodTest
{
63 public static XDispatchRecorderSupplier oObj
= null;
65 XComponent xTextDoc
= null;
66 XDispatchRecorder recorder
= null;
67 XDesktop desktop
= null;
70 * Simple <code>XDispatchRecorder</code> implementation
71 * which method <code>getRecordedMacro</code> returns a fixed
74 private static class MyRecorder
implements XDispatchRecorder
{
75 public void startRecording(XFrame p0
) {}
76 public void recordDispatch(URL p0
, PropertyValue
[] p1
) {}
77 public void recordDispatchAsComment(URL p0
, PropertyValue
[] p1
) {}
78 public void endRecording(){}
79 public String
getRecordedMacro() {
80 return "MyRecorder implementation";
85 * Creates a new document which supplies a frame.
86 * Also a <code>com.sun.star.frame.Desktop</code>
87 * service created for obtaining document's frame.
89 protected void before() {
90 SOfficeFactory SOF
= SOfficeFactory
.getFactory((XMultiServiceFactory
) tParam
.getMSF());
93 log
.println( "creating a text document" );
94 xTextDoc
= SOF
.createTextDoc(null);
96 Object inst
= (XInterface
)((XMultiServiceFactory
)tParam
.getMSF()).createInstance
97 ("com.sun.star.frame.Desktop");
98 desktop
= (XDesktop
) UnoRuntime
.queryInterface
99 (XDesktop
.class, inst
);
100 } catch ( com
.sun
.star
.uno
.Exception e
) {
101 // Some exception occures.FAILED
102 e
.printStackTrace( log
);
103 throw new StatusException( "Couldn't create document", e
);
109 * Creates an instance of <code>MyRecorder</code> and set if,
110 * then get the current recorder. Second case is setting
111 * recorder to null. Finally restores the old macro recorder. <p>
113 * Has <b>OK</b> status if in the first case custom recorder
114 * was successfully returned, and in second case current
117 public void _setDispatchRecorder() {
118 requiredMethod("getDispatchRecorder()");
122 log
.print("Setting custom macro recorder ...");
123 oObj
.setDispatchRecorder(new MyRecorder());
124 XDispatchRecorder rec
= oObj
.getDispatchRecorder();
126 locRes
= rec
!= null &&
127 "MyRecorder implementation".equals(rec
.getRecordedMacro());
128 if (locRes
) log
.println("OK");
129 else log
.println("FAILED");
132 log
.print("Setting null dispatch recorder ...");
133 oObj
.setDispatchRecorder(null);
134 locRes
= oObj
.getDispatchRecorder() == null;
135 if (locRes
) log
.println("OK");
136 else log
.println("FAILED");
139 log
.println("Setting old macro recorder ...");
140 oObj
.setDispatchRecorder(recorder
);
142 tRes
.tested("setDispatchRecorder()", res
);
146 * Just gets the current recorder and stores it.
148 * Has <b>OK</b> status.
150 public void _getDispatchRecorder() {
151 recorder
= oObj
.getDispatchRecorder();
152 tRes
.tested("getDispatchRecorder()", true);
156 * First sets the current dispatch recorder to new
157 * <code>DispatchRecorder</code> instance if the current one
158 * is null. The a <code>Dispatch</code> instance is created
159 * which inserts some text into text document.
160 * A number of cases is checked :
162 * <li> A valid call : here the recorded macro must contain
163 * inserted string and URL </li>
164 * <li> Call with invalid URL : the macro recorded must not
165 * contain this URL </li>
166 * <li> Call with null dispatcher : checking for GPF </li>
167 * <li> Call with the current recorder set to null :
168 * checking for GPF </li>
171 * Has <b>OK</b> status if all cases are OK.
173 public void _dispatchAndRecord() {
174 requiredMethod("getDispatchRecorder()");
177 if (recorder
== null) {
179 Object inst
= ((XMultiServiceFactory
) tParam
.getMSF()).createInstance
180 ("com.sun.star.comp.framework.DispatchRecorder");
181 recorder
= (XDispatchRecorder
) UnoRuntime
.queryInterface
182 (XDispatchRecorder
.class, inst
);
183 oObj
.setDispatchRecorder(recorder
);
184 } catch (com
.sun
.star
.uno
.Exception e
) {
185 throw new StatusException("Couldn't create recorder", e
);
191 } catch (InterruptedException ex
) {}
193 XModel model
= (XModel
) UnoRuntime
.queryInterface(XModel
.class, xTextDoc
);
194 XFrame fr
= model
.getCurrentController().getFrame();
196 XDispatchProvider xDispProv
= (XDispatchProvider
)
197 UnoRuntime
.queryInterface(XDispatchProvider
.class, fr
);
199 URL dispURL
= utils
.parseURL((XMultiServiceFactory
) tParam
.getMSF(), ".uno:InsertText");
201 XDispatch xDisp
= xDispProv
.queryDispatch(dispURL
,"",0);
203 PropertyValue
[] args
= new PropertyValue
[1];
204 args
[0] = new PropertyValue();
205 args
[0].Name
= "Text";
206 args
[0].Value
= "XDispatchRecorderSupplier";
208 log
.print("Dispatching and recording ...");
209 oObj
.dispatchAndRecord(dispURL
, args
, xDisp
);
211 String macro
= recorder
.getRecordedMacro();
212 boolean locRes
= macro
!= null &&
213 macro
.indexOf("XDispatchRecorderSupplier")>-1 &&
214 macro
.indexOf(".uno:InsertText")>-1;
215 if (locRes
) log
.println("OK");
216 else log
.println("FAILED");
218 log
.println("Recorder macro :\n" + macro
);
220 log
.print("Trying to set dispatch with null Dispatcher ...");
222 oObj
.dispatchAndRecord(dispURL
, args
, null);
224 } catch (java
.lang
.Exception e
){
225 log
.println("Exception is OK: " + e
);
228 log
.print("Trying to set dispatch recorder to null and record ...");
229 oObj
.setDispatchRecorder(null);
231 oObj
.dispatchAndRecord(dispURL
, args
, xDisp
);
233 } catch (java
.lang
.Exception e
){
234 log
.println("Exception is OK: " + e
);
237 oObj
.setDispatchRecorder(recorder
);
239 tRes
.tested("dispatchAndRecord()", res
);
243 * Disposes the document created in <code>before()</code>
245 protected void after() {