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 static org.openoffice.test.common.SystemUtil.*; 25 26 import java.io.IOException; 27 28 import org.openoffice.test.OpenOffice; 29 import org.openoffice.test.common.Condition; 30 import org.openoffice.test.vcl.Tester; 31 import org.openoffice.test.vcl.client.CommandCaller; 32 import org.openoffice.test.vcl.client.CommunicationManager; 33 import org.openoffice.test.vcl.client.Constant; 34 import org.openoffice.test.vcl.client.Handshaker; 35 import org.openoffice.test.vcl.client.WinInfoReceiver; 36 37 /** 38 * This class provides a proxy to interact with OpenOffice application. 39 * 40 */ 41 public class VclApp { 42 43 private static VclApp defaultInstance = null; 44 45 protected CommunicationManager communicationManager = null; 46 47 protected CommandCaller caller = null; 48 49 protected OpenOffice openOffice = null; 50 51 public VclApp() { 52 this(OpenOffice.getDefault()); 53 } 54 55 public VclApp(OpenOffice openOffice) { 56 // this("localhost", openOffice.getAutomationPort()); 57 this("127.0.0.1", openOffice.getAutomationPort()); // In case "localhost" is modified incorrectly 58 this.openOffice = openOffice; 59 } 60 61 public VclApp(String host, int port) { 62 communicationManager = new CommunicationManager(host, port); 63 caller = new CommandCaller(communicationManager); 64 new Handshaker(communicationManager); 65 } 66 67 public static VclApp getDefault() { 68 if (defaultInstance == null) { 69 defaultInstance = new VclApp(); 70 } 71 72 return defaultInstance; 73 } 74 75 public void setWinInfoReceiver(WinInfoReceiver receiver) { 76 caller.setWinInfoReceiver(receiver); 77 } 78 79 public void clean() { 80 OpenOffice.killAll(); 81 if (openOffice != null) 82 openOffice.cleanUserInstallation(); 83 } 84 85 public void start() { 86 if (openOffice != null) { 87 // workaround for crash when connect to automation server too early 88 double sleep = openOffice.getUserInstallation().exists() ? 3 : 6; 89 if (openOffice.start()) 90 sleep(sleep); 91 } 92 93 communicationManager.start(); 94 } 95 96 public void start(boolean isCleanUserInstallation) { 97 if (isCleanUserInstallation) 98 clean(); 99 start(); 100 } 101 102 public void loadDocument(String file) { 103 dispatch(".uno:Open"); 104 VclComboBox FilePicker_Path = new VclComboBox("SVT_HID_FILEDLG_AUTOCOMPLETEBOX"); 105 FilePicker_Path.setText(file); 106 VclButton FilePicker_Open = new VclButton("fpicker:PushButton:DLG_SVT_EXPLORERFILE:BTN_EXPLORERFILE_OPEN"); 107 FilePicker_Open.click(); 108 VclWindow writer = new VclWindow("SW_HID_EDIT_WIN"); 109 writer.waitForExistence(10, 2); 110 } 111 112 public void newDocument(String type) { 113 dispatch(type); 114 } 115 116 public OpenOffice getOpenOffice() { 117 return this.openOffice; 118 } 119 120 public void close() { 121 if (openOffice != null) { 122 if (!openOffice.isRunning()) 123 return; 124 } 125 126 try { 127 dispatch(".uno:Quit"); 128 } catch(Exception e) { 129 130 } 131 132 if (openOffice != null) { 133 if (!new Condition() { 134 @Override 135 public boolean value() { 136 return !openOffice.isRunning(); 137 } 138 139 }.test(5, 1)) { 140 openOffice.kill(); 141 } 142 } 143 144 communicationManager.stop(); 145 } 146 147 148 /** 149 * Activate the document window at the given index 150 * Note: this method requires automation enabled. 151 * @param i 152 * @return 153 */ 154 public void activateDoc(int i) { 155 caller.callCommand(Constant.RC_ActivateDocument, new Object[] { i + 1 }); 156 } 157 158 /** 159 * Try to close all OpenOffice windows, until startcenter window appears. 160 * The method does not always succeed. 161 * Note: this method requires automation enabled. 162 */ 163 public void reset() { 164 caller.callCommand(Constant.RC_ResetApplication); 165 } 166 167 /** 168 * Note: this method requires automation enabled. 169 * @return 170 */ 171 public boolean existsSysDialog() { 172 return (Boolean) caller.callCommand(Constant.RC_ExistsSysDialog); 173 } 174 175 /** 176 * Note: this method requires automation enabled. 177 */ 178 public void closeSysDialog() { 179 caller.callCommand(Constant.RC_CloseSysDialog); 180 } 181 182 /** 183 * Note: this method requires automation enabled. 184 * @return 185 */ 186 public String getClipboard() { 187 return (String) caller.callCommand(Constant.RC_GetClipboard); 188 } 189 190 /** 191 * Note: this method requires automation enabled. 192 * @param content 193 */ 194 public void setClipboard(String content) { 195 caller.callCommand(Constant.RC_SetClipboard, content); 196 } 197 198 /** 199 * Check if OpenOffice exists. 200 * Note: this method requires automation enabled. 201 * @return 202 */ 203 public boolean exists() { 204 try { 205 communicationManager.connect(); 206 return true; 207 } catch (IOException e) { 208 return false; 209 } 210 } 211 212 /** 213 * Check if the control exists in a period of time 214 * Note: this method requires automation enabled. 215 */ 216 public boolean exists(double iTimeout) { 217 return exists(iTimeout, 1); 218 } 219 220 /** 221 * Check if the control exists in a period of time 222 * Note: this method requires automation enabled. 223 */ 224 public boolean exists(double iTimeout, double interval) { 225 long startTime = System.currentTimeMillis(); 226 while (System.currentTimeMillis() - startTime < iTimeout * 1000) { 227 if (exists()) 228 return true; 229 Tester.sleep(interval); 230 } 231 232 return exists(); 233 } 234 235 /** 236 * Wait OpenOffice for existence in the given period. 237 * When time is out, an runtime exception will be thrown. 238 * Note: this method requires automation enabled. 239 * @param iTimeout 240 * @param interval 241 */ 242 public void waitForExistence(double iTimeout, double interval) { 243 if (!exists(iTimeout, interval)) 244 throw new RuntimeException("OpenOffice is not found!"); 245 } 246 247 /** 248 * Get document window count 249 * Note: this method requires automation enabled. 250 * @return 251 */ 252 public int getDocCount() { 253 return (Integer) caller.callCommand(Constant.RC_GetDocumentCount); 254 } 255 256 /** 257 * Run a dispatch 258 * Note: this method requires automation enabled. 259 * @param url 260 */ 261 public void dispatch(String url) { 262 caller.callUNOSlot(url); 263 } 264 265 private static final int CONST_WSTimeout = 701; 266 267 // private static final int CONST_WSAborted = 702; // Not used now! 268 // private static final int CONST_WSFinished = 703; // 269 270 /** 271 * Run a dispatch and then wait some time. 272 * If time is out, an runtime exception will be thrown 273 * Note: this method requires automation enabled. 274 * @param url 275 * @param time timeout 276 */ 277 public void dispatch(String url, double time) { 278 caller.callUNOSlot(url); 279 waitSlot(time); 280 } 281 282 public void waitSlot(double time) { 283 int result = (Integer) caller.callCommand(Constant.RC_WaitSlot, (int) time * 1000); 284 if (result == CONST_WSTimeout) 285 throw new RuntimeException("Timeout to execute the dispatch!"); 286 } 287 } 288