1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.openoffice.test.vcl.widgets;
23 
24 import java.io.IOException;
25 
26 import org.openoffice.test.OpenOffice;
27 import org.openoffice.test.common.Condition;
28 import org.openoffice.test.vcl.Tester;
29 import org.openoffice.test.vcl.client.CommandCaller;
30 import org.openoffice.test.vcl.client.CommunicationManager;
31 import org.openoffice.test.vcl.client.Constant;
32 import org.openoffice.test.vcl.client.Handshaker;
33 import org.openoffice.test.vcl.client.WinInfoReceiver;
34 
35 /**
36  * This class provides a proxy to interact with OpenOffice application.
37  *
38  */
39 public class VclApp {
40 
41 	protected CommunicationManager communicationManager = null;
42 
43 	protected CommandCaller caller = null;
44 
45 	protected OpenOffice openOffice = null;
46 
47 	/**
48 	 *
49 	 * @param openOffice
50 	 */
VclApp(OpenOffice openOffice)51 	public VclApp(OpenOffice openOffice) {
52 		this.openOffice = openOffice;
53 		this.communicationManager = new CommunicationManager("127.0.0.1", openOffice.getAutomationPort());
54 		this.caller = new CommandCaller(communicationManager);
55 		new Handshaker(communicationManager);
56 	}
57 
setWinInfoReceiver(WinInfoReceiver receiver)58 	public void setWinInfoReceiver(WinInfoReceiver receiver) {
59 		caller.setWinInfoReceiver(receiver);
60 	}
61 
clean()62 	public void clean() {
63 		openOffice.kill();
64 		openOffice.cleanUserInstallation();
65 	}
66 
start()67 	public void start() {
68 		openOffice.start();
69 		//wait for UI to be ready
70 		new Condition() {
71 
72 			@Override
73 			public boolean value() {
74 				try {
75 					String tree = (String) caller.callCommand(Constant.RC_WinTree);
76 					return tree.length() > 0;
77 				} catch (Exception e) {
78 					return false;
79 				}
80 
81 			}
82 
83 		}.waitForTrue("Can't connect to automation server!", 20, 1);
84 	}
85 
start(boolean clean)86 	public void start(boolean clean) {
87 		if (clean)
88 			clean();
89 		start();
90 	}
91 
92 	/**
93 	 * Quit the application, if failed, one exception will be thrown
94 	 */
quit()95 	public void quit() {
96 		dispatch(".uno:Quit");
97 		if (openOffice != null) {
98 			new Condition() {
99 				@Override
100 				public boolean value() {
101 					return !openOffice.isRunning();
102 				}
103 
104 			}.waitForTrue("OpenOffice can't quit!", 5, 1);
105 		}
106 	}
107 
stop()108 	public void stop() {
109 		if (openOffice != null) {
110 			if (!openOffice.isRunning())
111 				return;
112 		}
113 
114 		try {
115 			quit();
116 		} catch(Exception e) {
117 			openOffice.kill();
118 		}
119 
120 		communicationManager.stop();
121 	}
122 
123 	/**
124 	 * @deprecated use stop
125 	 */
close()126 	public void close() {
127 		stop();
128 	}
129 
130 
131 	/**
132 	 * Activate the document window at the given index
133 	 * Note: this method requires automation enabled.
134 	 * @param i
135 	 * @return
136 	 */
activateDoc(int i)137 	public void activateDoc(int i) {
138 		caller.callCommand(Constant.RC_ActivateDocument, new Object[] { i + 1 });
139 	}
140 
141 	/**
142 	 * Try to close all OpenOffice windows, until startcenter window appears.
143 	 * The method does not always succeed.
144 	 * Note: this method requires automation enabled.
145 	 */
reset()146 	public void reset() {
147 		caller.callCommand(Constant.RC_ResetApplication);
148 	}
149 
150 	/**
151 	 * Note: this method requires automation enabled.
152 	 * @return
153 	 */
existsSysDialog()154 	public boolean existsSysDialog() {
155 		return (Boolean) caller.callCommand(Constant.RC_ExistsSysDialog);
156 	}
157 
158 	/**
159 	 * Note: this method requires automation enabled.
160 	 */
closeSysDialog()161 	public void closeSysDialog() {
162 		caller.callCommand(Constant.RC_CloseSysDialog);
163 	}
164 
165 	/**
166 	 * Note: this method requires automation enabled.
167 	 * @return
168 	 */
getClipboard()169 	public String getClipboard() {
170 		return (String) caller.callCommand(Constant.RC_GetClipboard);
171 	}
172 
173 	/**
174 	 * Note: this method requires automation enabled.
175 	 * @param content
176 	 */
setClipboard(String content)177 	public void setClipboard(String content) {
178 		caller.callCommand(Constant.RC_SetClipboard, content);
179 	}
180 
181 	/**
182 	 * Check if OpenOffice exists.
183 	 * Note: this method requires automation enabled.
184 	 * @return
185 	 */
exists()186 	public boolean exists() {
187 		try {
188 			communicationManager.connect();
189 			return true;
190 		} catch (IOException e) {
191 			return false;
192 		}
193 	}
194 
195 	/**
196 	 * Check if the control exists in a period of time
197 	 * Note: this method requires automation enabled.
198 	 */
exists(double iTimeout)199 	public boolean exists(double iTimeout) {
200 		return exists(iTimeout, 1);
201 	}
202 
203 	/**
204 	 * Check if the control exists in a period of time
205 	 * Note: this method requires automation enabled.
206 	 */
exists(double iTimeout, double interval)207 	public boolean exists(double iTimeout, double interval) {
208 		long startTime = System.currentTimeMillis();
209 		while (System.currentTimeMillis() - startTime < iTimeout * 1000) {
210 			if (exists())
211 				return true;
212 			Tester.sleep(interval);
213 		}
214 
215 		return exists();
216 	}
217 
218 	/**
219 	 * Wait OpenOffice for existence in the given period.
220 	 * When time is out, an runtime exception will be thrown.
221 	 * Note: this method requires automation enabled.
222 	 * @param iTimeout
223 	 * @param interval
224 	 */
waitForExistence(double iTimeout, double interval)225 	public void waitForExistence(double iTimeout, double interval) {
226 		if (!exists(iTimeout, interval))
227 			throw new RuntimeException("OpenOffice is not found!");
228 	}
229 
230 	/**
231 	 * Get document window count
232 	 * Note: this method requires automation enabled.
233 	 * @return
234 	 */
getDocCount()235 	public int getDocCount() {
236 		return (Integer) caller.callCommand(Constant.RC_GetDocumentCount);
237 	}
238 
239 	/**
240 	 * Run a dispatch
241 	 * Note: this method requires automation enabled.
242 	 * @param url
243 	 */
dispatch(String url)244 	public void dispatch(String url) {
245 		caller.callUNOSlot(url);
246 	}
247 
248 	private static final int CONST_WSTimeout = 701;
249 
250 	// private static final int CONST_WSAborted = 702; // Not used now!
251 	// private static final int CONST_WSFinished = 703; //
252 
253 	/**
254 	 * Run a dispatch and then wait some time.
255 	 * If time is out, an runtime exception will be thrown
256 	 * Note: this method requires automation enabled.
257 	 * @param url
258 	 * @param time timeout
259 	 */
dispatch(String url, double time)260 	public void dispatch(String url, double time) {
261 		caller.callUNOSlot(url);
262 		waitSlot(time);
263 	}
264 
waitSlot(double time)265 	public void waitSlot(double time) {
266 		int result = (Integer) caller.callCommand(Constant.RC_WaitSlot, (int) time * 1000);
267 		if (result == CONST_WSTimeout)
268 			throw new RuntimeException("Timeout to execute the dispatch!");
269 	}
270 }
271