Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / tests / java / ifc / frame / _XDispatchRecorderSupplier.java
blobb6c1f06c5a7ee7f2e2fa1f7bb30945bc4c4a2a15
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 package ifc.frame;
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.frame.XDesktop;
32 import com.sun.star.frame.XDispatch;
33 import com.sun.star.frame.XDispatchProvider;
34 import com.sun.star.frame.XDispatchRecorder;
35 import com.sun.star.frame.XModel;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.frame.XDispatchRecorderSupplier;
38 import com.sun.star.frame.XFrame;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 import com.sun.star.util.URL;
43 import lib.MultiMethodTest;
44 import lib.StatusException;
45 import util.SOfficeFactory;
46 import util.utils;
48 /**
49 * Testing <code>com.sun.star.frame.XDispatchRecorderSupplier</code>
50 * interface methods:
51 * <ul>
52 * <li><code> setDispatchRecorder() </code></li>
53 * <li><code> getDispatchRecorder() </code></li>
54 * <li><code> dispatchAndRecord() </code></li>
55 * </ul><p>
56 * Test is <b> NOT </b> multithread compilant. <p>
57 * @see com.sun.star.frame.XDispatchRecorderSupplier
59 public class _XDispatchRecorderSupplier extends MultiMethodTest {
60 public static XDispatchRecorderSupplier oObj = null;
62 XComponent xTextDoc = null;
63 XDispatchRecorder recorder = null;
64 XDesktop desktop = null;
66 /**
67 * Simple <code>XDispatchRecorder</code> implementation
68 * which method <code>getRecordedMacro</code> returns a fixed
69 * string.
71 private static class MyRecorder implements XDispatchRecorder {
72 public void startRecording(XFrame p0) {}
73 public void recordDispatch(URL p0, PropertyValue[] p1) {}
74 public void recordDispatchAsComment(URL p0, PropertyValue[] p1) {}
75 public void endRecording(){}
76 public String getRecordedMacro() {
77 return "MyRecorder implementation";
81 /**
82 * Creates a new document which supplies a frame.
83 * Also a <code>com.sun.star.frame.Desktop</code>
84 * service created for obtaining document's frame.
86 protected void before() {
87 SOfficeFactory SOF = SOfficeFactory.getFactory((XMultiServiceFactory) tParam.getMSF());
89 try {
90 log.println( "creating a text document" );
91 xTextDoc = SOF.createTextDoc(null);
93 Object inst = (XInterface)((XMultiServiceFactory)tParam.getMSF()).createInstance
94 ("com.sun.star.frame.Desktop");
95 desktop = (XDesktop) UnoRuntime.queryInterface
96 (XDesktop.class, inst);
97 } catch ( com.sun.star.uno.Exception e ) {
98 // Some exception occures.FAILED
99 e.printStackTrace( log );
100 throw new StatusException( "Couldn't create document", e );
106 * Creates an instance of <code>MyRecorder</code> and set if,
107 * then get the current recorder. Second case is setting
108 * recorder to null. Finally restores the old macro recorder. <p>
110 * Has <b>OK</b> status if in the first case custom recorder
111 * was successfully returned, and in second case current
112 * recorder is null.
114 public void _setDispatchRecorder() {
115 requiredMethod("getDispatchRecorder()");
117 boolean res = true,
118 locRes = true;
119 log.print("Setting custom macro recorder ...");
120 oObj.setDispatchRecorder(new MyRecorder());
121 XDispatchRecorder rec = oObj.getDispatchRecorder();
123 locRes = rec != null &&
124 "MyRecorder implementation".equals(rec.getRecordedMacro());
125 if (locRes) log.println("OK");
126 else log.println("FAILED");
127 res &= locRes;
129 log.print("Setting null dispatch recorder ...");
130 oObj.setDispatchRecorder(null);
131 locRes = oObj.getDispatchRecorder() == null;
132 if (locRes) log.println("OK");
133 else log.println("FAILED");
134 res &= locRes;
136 log.println("Setting old macro recorder ...");
137 oObj.setDispatchRecorder(recorder);
139 tRes.tested("setDispatchRecorder()", res);
143 * Just gets the current recorder and stores it.
145 * Has <b>OK</b> status.
147 public void _getDispatchRecorder() {
148 recorder = oObj.getDispatchRecorder();
149 tRes.tested("getDispatchRecorder()", true);
153 * First sets the current dispatch recorder to new
154 * <code>DispatchRecorder</code> instance if the current one
155 * is null. The a <code>Dispatch</code> instance is created
156 * which inserts some text into text document.
157 * A number of cases is checked :
158 * <ul>
159 * <li> A valid call : here the recorded macro must contain
160 * inserted string and URL </li>
161 * <li> Call with invalid URL : the macro recorded must not
162 * contain this URL </li>
163 * <li> Call with null dispatcher : checking for GPF </li>
164 * <li> Call with the current recorder set to null :
165 * checking for GPF </li>
166 * </ul>
168 * Has <b>OK</b> status if all cases are OK.
170 public void _dispatchAndRecord() {
171 requiredMethod("getDispatchRecorder()");
173 boolean res = true;
174 if (recorder == null) {
175 try {
176 Object inst = ((XMultiServiceFactory) tParam.getMSF()).createInstance
177 ("com.sun.star.comp.framework.DispatchRecorder");
178 recorder = (XDispatchRecorder) UnoRuntime.queryInterface
179 (XDispatchRecorder.class, inst);
180 oObj.setDispatchRecorder(recorder);
181 } catch (com.sun.star.uno.Exception e) {
182 throw new StatusException("Couldn't create recorder", e);
186 try {
187 Thread.sleep(500);
188 } catch (InterruptedException ex) {}
190 XModel model = (XModel) UnoRuntime.queryInterface(XModel.class, xTextDoc);
191 XFrame fr = model.getCurrentController().getFrame();
193 XDispatchProvider xDispProv = (XDispatchProvider)
194 UnoRuntime.queryInterface(XDispatchProvider.class, fr);
196 URL dispURL = utils.parseURL((XMultiServiceFactory) tParam.getMSF(), ".uno:InsertText");
198 XDispatch xDisp = xDispProv.queryDispatch(dispURL,"",0);
200 PropertyValue[] args = new PropertyValue[1];
201 args[0] = new PropertyValue();
202 args[0].Name = "Text";
203 args[0].Value = "XDispatchRecorderSupplier";
205 log.print("Dispatching and recording ...");
206 oObj.dispatchAndRecord(dispURL, args, xDisp);
208 String macro = recorder.getRecordedMacro();
209 boolean locRes = macro != null &&
210 macro.indexOf("XDispatchRecorderSupplier")>-1 &&
211 macro.indexOf(".uno:InsertText")>-1;
212 if (locRes) log.println("OK");
213 else log.println("FAILED");
214 res &= locRes;
215 log.println("Recorder macro :\n" + macro);
217 log.print("Trying to set dispatch with null Dispatcher ...");
218 try {
219 oObj.dispatchAndRecord(dispURL, args, null);
220 log.println("OK");
221 } catch (java.lang.Exception e){
222 log.println("Exception is OK: " + e);
225 log.print("Trying to set dispatch recorder to null and record ...");
226 oObj.setDispatchRecorder(null);
227 try {
228 oObj.dispatchAndRecord(dispURL, args, xDisp);
229 log.println("OK");
230 } catch (java.lang.Exception e){
231 log.println("Exception is OK: " + e);
234 oObj.setDispatchRecorder(recorder);
236 tRes.tested("dispatchAndRecord()", res);
240 * Disposes the document created in <code>before()</code>
242 protected void after() {
243 xTextDoc.dispose();