Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / frame / _XDispatchRecorder.java
blobb48d11ff8781e739f30c57cf4ac71c1cd51fa1c1
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 lib.MultiMethodTest;
22 import lib.Status;
23 import lib.StatusException;
24 import util.utils;
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.frame.XDispatch;
28 import com.sun.star.frame.XDispatchProvider;
29 import com.sun.star.frame.XDispatchRecorder;
30 import com.sun.star.frame.XFrame;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.util.URL;
34 /**
35 * Testing <code>com.sun.star.frame.XDispatchRecorder</code>
36 * interface methods:
37 * <ul>
38 * <li><code> startRecording() </code></li>
39 * <li><code> recordDispatch() </code></li>
40 * <li><code> recordDispatchAsComment() </code></li>
41 * <li><code> endRecording() </code></li>
42 * <li><code> getRecordedMacro() </code></li>
43 * </ul><p>
44 * Test is <b> NOT </b> multithread compliant. <p>
45 * @see com.sun.star.frame.XDispatchRecorder
47 public class _XDispatchRecorder extends MultiMethodTest {
48 public static XDispatchRecorder oObj = null;
50 XFrame xFrame = null;
51 URL dispURL = null;
52 PropertyValue[] dispArgs = new PropertyValue[0];
54 @Override
55 protected void before() {
56 xFrame = (XFrame) tEnv.getObjRelation("XDispatchRecorder.Frame");
58 if (xFrame == null) {
59 throw new StatusException(Status.failed("Relation not found"));
63 public void _startRecording() {
65 oObj.startRecording(xFrame);
66 oObj.endRecording();
67 tRes.tested("startRecording()", true);
70 public void _getRecordedMacro() throws Exception {
72 clearBuffer();
74 oObj.startRecording(xFrame);
76 log.println("dispatch calls via API must be recorded");
77 dispURL = utils.parseURL(tParam.getMSF(), ".uno:InsertText");
79 PropertyValue prop = new PropertyValue();
80 prop.Name = "Text";
81 prop.Value = "XDispatchRecorder.endRecording()";
82 dispArgs = new PropertyValue[] {prop};
84 log.println("Dispatching event for frame ...");
85 XDispatchProvider xDispProv = UnoRuntime.queryInterface(XDispatchProvider.class, xFrame);
86 XDispatch xDisp = xDispProv.queryDispatch(dispURL, "", 0);
87 xDisp.dispatch(dispURL, dispArgs);
89 waitForEventIdle();
91 log.println("Getting macro ... :");
92 String macro = oObj.getRecordedMacro();
93 log.println(macro);
95 log.println("Ending record ...");
96 oObj.endRecording();
98 boolean res = macro != null
99 && macro.indexOf(dispURL.Complete) > -1;
100 if (!res) log.println("Dispatch URL '" + dispURL.Complete
101 + "' was NOT found in macro - FAILED");
103 tRes.tested("getRecordedMacro()", res);
106 public void _endRecording() {
108 oObj.startRecording(xFrame);
109 oObj.endRecording();
110 String macro = oObj.getRecordedMacro();
111 boolean res = true;
112 if (macro.length() != 0){
113 log.println("'endRecording()' was called but macro buffer is not cleared: FALSE");
114 log.println(macro);
115 res = false;
118 tRes.tested("endRecording()", res);
121 public void _recordDispatch() {
122 clearBuffer();
124 oObj.startRecording(xFrame);
126 // positive test
127 dispURL = utils.parseURL(tParam.getMSF(), ".uno:InsertText");
129 PropertyValue prop = new PropertyValue();
130 prop.Name = "Text";
131 prop.Value = "XDispatchRecorder.recordDispatch()";
132 dispArgs = new PropertyValue[] {prop};
134 log.println("Recording dispatch ...");
135 oObj.recordDispatch(dispURL, dispArgs);
137 log.println("Getting macro ... :");
138 String macro = oObj.getRecordedMacro();
139 log.println(macro);
141 oObj.endRecording();
143 boolean res = macro != null
144 && macro.indexOf(dispURL.Complete) > -1
145 && macro.indexOf((String)dispArgs[0].Value) > -1;
146 if (!res) log.println("Dispatch URL '" + dispURL.Complete
147 + "' or its argument '" + dispArgs[0].Value
148 + "' was not found in macro returned - FAILED");
150 tRes.tested("recordDispatch()", res);
153 public void _recordDispatchAsComment() {
154 clearBuffer();
156 oObj.startRecording(xFrame);
158 dispURL = utils.parseURL(tParam.getMSF(), ".uno:InsertText");
160 PropertyValue prop = new PropertyValue();
161 prop.Name = "Text";
162 prop.Value = "XDispatchRecorder.recordDispatchAsComment()";
163 dispArgs = new PropertyValue[] {prop};
165 log.println("Recording dispatch ...");
166 oObj.recordDispatchAsComment(dispURL, dispArgs);
168 log.println("Getting macro ... :");
169 String macro = oObj.getRecordedMacro();
170 log.println(macro);
172 oObj.endRecording();
174 boolean res = macro != null
175 && macro.indexOf(dispURL.Complete) > -1
176 && macro.indexOf((String)dispArgs[0].Value) > -1;
177 if (!res) log.println("Dispatch URL '" + dispURL.Complete
178 + "' or its argument '" + dispArgs[0].Value
179 + "' was not found in macro returned - FAILED");
181 log.println("Checking if macro is commented ...");
182 int idx = macro.indexOf((String)dispArgs[0].Value);
183 int lineStartIdx = macro.lastIndexOf("\n", idx);
184 String lineStart = macro.substring(lineStartIdx + 1, idx).toLowerCase();
185 log.println("Line start is '" + lineStart + "'");
187 if (lineStart.startsWith("'") || lineStart.startsWith("rem")) {
188 log.println("Line is commented : OK");
189 } else {
190 log.println("Line is not commented : FAILED");
191 res =false;
194 tRes.tested("recordDispatchAsComment()", res);
198 private void clearBuffer() {
199 oObj.startRecording(xFrame);
200 oObj.endRecording();
201 String macro = oObj.getRecordedMacro();
202 if (macro.length() != 0){
203 log.println("ERROR: 'endRecording()' was called but macro buffer is not cleared!");
204 log.println(macro);