Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / frame / _XDispatchRecorderSupplier.java
blob00e8f6ba4e904502bd38245f496fbfb023ea6b3d
1 /*
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 .
19 package ifc.frame;
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;
35 import util.utils;
37 /**
38 * Testing <code>com.sun.star.frame.XDispatchRecorderSupplier</code>
39 * interface methods:
40 * <ul>
41 * <li><code> setDispatchRecorder() </code></li>
42 * <li><code> getDispatchRecorder() </code></li>
43 * <li><code> dispatchAndRecord() </code></li>
44 * </ul><p>
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;
54 /**
55 * Simple <code>XDispatchRecorder</code> implementation
56 * which method <code>getRecordedMacro</code> returns a fixed
57 * string.
59 private static class MyRecorder implements XDispatchRecorder {
60 public void startRecording(XFrame p0) {}
61 public void recordDispatch(URL p0, PropertyValue[] p1) {}
62 public void recordDispatchAsComment(URL p0, PropertyValue[] p1) {}
63 public void endRecording(){}
64 public String getRecordedMacro() {
65 return "MyRecorder implementation";
69 /**
70 * Creates a new document which supplies a frame.
71 * Also a <code>com.sun.star.frame.Desktop</code>
72 * service created for obtaining document's frame.
74 @Override
75 protected void before() throws Exception {
76 SOfficeFactory SOF = SOfficeFactory.getFactory(tParam.getMSF());
78 log.println( "creating a text document" );
79 xTextDoc = SOF.createTextDoc(null);
81 Object inst = tParam.getMSF().createInstance
82 ("com.sun.star.frame.Desktop");
83 UnoRuntime.queryInterface(XDesktop.class, inst);
86 /**
87 * Creates an instance of <code>MyRecorder</code> and set if,
88 * then get the current recorder. Second case is setting
89 * recorder to null. Finally restores the old macro recorder. <p>
91 * Has <b>OK</b> status if in the first case custom recorder
92 * was successfully returned, and in second case current
93 * recorder is null.
95 public void _setDispatchRecorder() {
96 requiredMethod("getDispatchRecorder()");
98 boolean res = true,
99 locRes = true;
100 log.print("Setting custom macro recorder ...");
101 oObj.setDispatchRecorder(new MyRecorder());
102 XDispatchRecorder rec = oObj.getDispatchRecorder();
104 locRes = rec != null &&
105 "MyRecorder implementation".equals(rec.getRecordedMacro());
106 if (locRes) log.println("OK");
107 else log.println("FAILED");
108 res &= locRes;
110 log.print("Setting null dispatch recorder ...");
111 oObj.setDispatchRecorder(null);
112 locRes = oObj.getDispatchRecorder() == null;
113 if (locRes) log.println("OK");
114 else log.println("FAILED");
115 res &= locRes;
117 log.println("Setting old macro recorder ...");
118 oObj.setDispatchRecorder(recorder);
120 tRes.tested("setDispatchRecorder()", res);
124 * Just gets the current recorder and stores it.
126 * Has <b>OK</b> status.
128 public void _getDispatchRecorder() {
129 recorder = oObj.getDispatchRecorder();
130 tRes.tested("getDispatchRecorder()", true);
134 * First sets the current dispatch recorder to new
135 * <code>DispatchRecorder</code> instance if the current one
136 * is null. The a <code>Dispatch</code> instance is created
137 * which inserts some text into text document.
138 * A number of cases is checked :
139 * <ul>
140 * <li> A valid call : here the recorded macro must contain
141 * inserted string and URL </li>
142 * <li> Call with invalid URL : the macro recorded must not
143 * contain this URL </li>
144 * <li> Call with null dispatcher : checking for GPF </li>
145 * <li> Call with the current recorder set to null :
146 * checking for GPF </li>
147 * </ul>
149 * Has <b>OK</b> status if all cases are OK.
151 public void _dispatchAndRecord() throws Exception {
152 requiredMethod("getDispatchRecorder()");
154 boolean res = true;
155 if (recorder == null) {
156 try {
157 Object inst = tParam.getMSF().createInstance
158 ("com.sun.star.comp.framework.DispatchRecorder");
159 recorder = UnoRuntime.queryInterface
160 (XDispatchRecorder.class, inst);
161 oObj.setDispatchRecorder(recorder);
162 } catch (com.sun.star.uno.Exception e) {
163 throw new StatusException("Couldn't create recorder", e);
167 waitForEventIdle();
169 XModel model = UnoRuntime.queryInterface(XModel.class, xTextDoc);
170 XFrame fr = model.getCurrentController().getFrame();
172 XDispatchProvider xDispProv = UnoRuntime.queryInterface(XDispatchProvider.class, fr);
174 URL dispURL = utils.parseURL(tParam.getMSF(), ".uno:InsertText");
176 XDispatch xDisp = xDispProv.queryDispatch(dispURL,"",0);
178 PropertyValue[] args = new PropertyValue[1];
179 args[0] = new PropertyValue();
180 args[0].Name = "Text";
181 args[0].Value = "XDispatchRecorderSupplier";
183 log.print("Dispatching and recording ...");
184 oObj.dispatchAndRecord(dispURL, args, xDisp);
186 String macro = recorder.getRecordedMacro();
187 boolean locRes = macro != null &&
188 macro.indexOf("XDispatchRecorderSupplier")>-1 &&
189 macro.indexOf(".uno:InsertText")>-1;
190 if (locRes) log.println("OK");
191 else log.println("FAILED");
192 res &= locRes;
193 log.println("Recorder macro :\n" + macro);
195 log.print("Trying to set dispatch with null Dispatcher ...");
196 try {
197 oObj.dispatchAndRecord(dispURL, args, null);
198 log.println("OK");
199 } catch (java.lang.Exception e){
200 log.println("Exception is OK: " + e);
203 log.print("Trying to set dispatch recorder to null and record ...");
204 oObj.setDispatchRecorder(null);
205 try {
206 oObj.dispatchAndRecord(dispURL, args, xDisp);
207 log.println("OK");
208 } catch (java.lang.Exception e){
209 log.println("Exception is OK: " + e);
212 oObj.setDispatchRecorder(recorder);
214 tRes.tested("dispatchAndRecord()", res);
218 * Disposes the document created in <code>before()</code>
220 @Override
221 protected void after() {
222 xTextDoc.dispose();