1 /*
2  * This file is part of the LibreOffice project.
3  *
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/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
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 .
17  */
18 
19 package ifc.frame;
20 
21 import lib.MultiMethodTest;
22 import lib.Status;
23 import lib.StatusException;
24 import util.utils;
25 
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;
33 
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
46 */
47 public class _XDispatchRecorder extends MultiMethodTest {
48     public static XDispatchRecorder oObj = null;
49 
50     XFrame xFrame = null;
51     URL dispURL = null;
52     PropertyValue[] dispArgs = new PropertyValue[0];
53 
54     @Override
before()55     protected void before() {
56         xFrame = (XFrame) tEnv.getObjRelation("XDispatchRecorder.Frame");
57 
58         if (xFrame == null) {
59             throw new StatusException(Status.failed("Relation not found"));
60         }
61     }
62 
_startRecording()63     public void _startRecording() {
64 
65         oObj.startRecording(xFrame);
66         oObj.endRecording();
67         tRes.tested("startRecording()", true);
68     }
69 
_getRecordedMacro()70     public void _getRecordedMacro() throws Exception {
71 
72         clearBuffer();
73 
74         oObj.startRecording(xFrame);
75 
76         log.println("dispatch calls via API must be recorded");
77         dispURL = utils.parseURL(tParam.getMSF(), ".uno:InsertText");
78 
79         PropertyValue prop = new PropertyValue();
80         prop.Name = "Text";
81         prop.Value = "XDispatchRecorder.endRecording()";
82         dispArgs = new PropertyValue[] {prop};
83 
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);
88 
89         waitForEventIdle();
90 
91         log.println("Getting macro ... :");
92         String macro = oObj.getRecordedMacro();
93         log.println(macro);
94 
95         log.println("Ending record ...");
96         oObj.endRecording();
97 
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");
102 
103         tRes.tested("getRecordedMacro()", res);
104     }
105 
_endRecording()106     public void _endRecording() {
107 
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;
116         }
117 
118         tRes.tested("endRecording()", res);
119     }
120 
_recordDispatch()121     public void _recordDispatch() {
122         clearBuffer();
123 
124         oObj.startRecording(xFrame);
125 
126         // positive test
127         dispURL = utils.parseURL(tParam.getMSF(), ".uno:InsertText");
128 
129         PropertyValue prop = new PropertyValue();
130         prop.Name = "Text";
131         prop.Value = "XDispatchRecorder.recordDispatch()";
132         dispArgs = new PropertyValue[] {prop};
133 
134         log.println("Recording dispatch ...");
135         oObj.recordDispatch(dispURL, dispArgs);
136 
137         log.println("Getting macro ... :");
138         String macro = oObj.getRecordedMacro();
139         log.println(macro);
140 
141         oObj.endRecording();
142 
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");
149 
150         tRes.tested("recordDispatch()", res);
151     }
152 
_recordDispatchAsComment()153     public void _recordDispatchAsComment() {
154         clearBuffer();
155 
156         oObj.startRecording(xFrame);
157 
158         dispURL = utils.parseURL(tParam.getMSF(), ".uno:InsertText");
159 
160         PropertyValue prop = new PropertyValue();
161         prop.Name = "Text";
162         prop.Value = "XDispatchRecorder.recordDispatchAsComment()";
163         dispArgs = new PropertyValue[] {prop};
164 
165         log.println("Recording dispatch ...");
166         oObj.recordDispatchAsComment(dispURL, dispArgs);
167 
168         log.println("Getting macro ... :");
169         String macro = oObj.getRecordedMacro();
170         log.println(macro);
171 
172         oObj.endRecording();
173 
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");
180 
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 + "'");
186 
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;
192         }
193 
194         tRes.tested("recordDispatchAsComment()", res);
195     }
196 
197 
clearBuffer()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);
205         }
206     }
207 
208 }
209