|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2012 BREDEX GmbH. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* BREDEX GmbH - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jubula.rc.common.caps; |
| 12 |
|
| 13 |
import java.awt.AWTException; |
| 14 |
import java.awt.Dimension; |
| 15 |
import java.awt.Graphics2D; |
| 16 |
import java.awt.Rectangle; |
| 17 |
import java.awt.RenderingHints; |
| 18 |
import java.awt.Toolkit; |
| 19 |
import java.awt.datatransfer.StringSelection; |
| 20 |
import java.awt.image.BufferedImage; |
| 21 |
import java.io.File; |
| 22 |
import java.io.IOException; |
| 23 |
|
| 24 |
import javax.imageio.ImageIO; |
| 25 |
|
| 26 |
import org.eclipse.jubula.rc.common.driver.ClickOptions; |
| 27 |
import org.eclipse.jubula.rc.common.driver.IRobot; |
| 28 |
import org.eclipse.jubula.rc.common.driver.KeyTyper; |
| 29 |
import org.eclipse.jubula.rc.common.exception.ExecutionEvent; |
| 30 |
import org.eclipse.jubula.rc.common.exception.OsNotSupportedException; |
| 31 |
import org.eclipse.jubula.rc.common.exception.RobotException; |
| 32 |
import org.eclipse.jubula.rc.common.exception.StepExecutionException; |
| 33 |
import org.eclipse.jubula.rc.common.implclasses.Comparer; |
| 34 |
import org.eclipse.jubula.rc.common.implclasses.IBaseImplementationClass; |
| 35 |
import org.eclipse.jubula.rc.common.implclasses.Verifier; |
| 36 |
import org.eclipse.jubula.rc.common.logger.AutServerLogger; |
| 37 |
import org.eclipse.jubula.rc.common.util.KeyStrokeUtil; |
| 38 |
import org.eclipse.jubula.tools.constants.StringConstants; |
| 39 |
import org.eclipse.jubula.tools.objects.event.EventFactory; |
| 40 |
import org.eclipse.jubula.tools.objects.event.TestErrorEvent; |
| 41 |
import org.eclipse.jubula.tools.utils.ExternalCommandExecutor; |
| 42 |
import org.eclipse.jubula.tools.utils.TimeUtil; |
| 43 |
import org.eclipse.jubula.tools.utils.ExternalCommandExecutor.MonitorTask; |
| 44 |
/** |
| 45 |
* |
| 46 |
* @author BREDEX GmbH |
| 47 |
* |
| 48 |
*/ |
| 49 |
public abstract class AbstractApplicationCAPs |
| 50 |
implements IBaseImplementationClass { |
| 51 |
/** |
| 52 |
* String for sequential numbering for screenshots |
| 53 |
*/ |
| 54 |
public static final String RENAME = "rename"; //$NON-NLS-1$ |
| 55 |
|
| 56 |
/** |
| 57 |
* String for overwriting for screenshots |
| 58 |
*/ |
| 59 |
public static final String OVERWRITE = "overwrite"; //$NON-NLS-1$ |
| 60 |
|
| 61 |
/** |
| 62 |
* The default format to use when writing images to disk. |
| 63 |
*/ |
| 64 |
private static final String DEFAULT_IMAGE_FORMAT = "png"; //$NON-NLS-1$ |
| 65 |
|
| 66 |
/** |
| 67 |
* The string used to separate filename and file extension. |
| 68 |
*/ |
| 69 |
private static final String EXTENSION_SEPARATOR = "."; //$NON-NLS-1$ |
| 70 |
|
| 71 |
/** constants for communication */ |
| 72 |
private static final String POS_UNIT_PIXEL = "Pixel"; //$NON-NLS-1$ |
| 73 |
|
| 74 |
/** constants for communication */ |
| 75 |
private static final String POS_UNI_PERCENT = "Percent"; //$NON-NLS-1$ |
| 76 |
|
| 77 |
/** |
| 78 |
* The logging. |
| 79 |
*/ |
| 80 |
private static AutServerLogger log = |
| 81 |
new AutServerLogger(AbstractApplicationCAPs.class); |
| 82 |
|
| 83 |
/** |
| 84 |
* @param text text to type |
| 85 |
*/ |
| 86 |
public void gdInputText(String text) { |
| 87 |
getRobot().type(getFocusOwner(), text); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Executes the given command and waits for it to finish. If the |
| 92 |
* execution does not finish in good time, a timeout will occur. If the |
| 93 |
* exit code for the execution is not the same as the expected code, the |
| 94 |
* test step fails. |
| 95 |
* |
| 96 |
* @param cmd The command to execute. |
| 97 |
* @param expectedExitCode The expected exit code of the command. |
| 98 |
* @param local <code>true</code> if the command should be executed on the |
| 99 |
* local (client) machine. Otherwise (should run on the server side), |
| 100 |
* this value should be <code>false</code>. |
| 101 |
* @param timeout The amount of time (in milliseconds) to wait for the |
| 102 |
* execution to finish. |
| 103 |
*/ |
| 104 |
public void gdExecuteExternalCommand(String cmd, int expectedExitCode, |
| 105 |
boolean local, int timeout) { |
| 106 |
|
| 107 |
if (!local) { |
| 108 |
MonitorTask mt = new ExternalCommandExecutor().executeCommand( |
| 109 |
null, cmd, timeout); |
| 110 |
|
| 111 |
if (!mt.wasCmdValid()) { |
| 112 |
throw new StepExecutionException( |
| 113 |
"Command not found.", //$NON-NLS-1$ |
| 114 |
EventFactory.createActionError( |
| 115 |
TestErrorEvent.NO_SUCH_COMMAND)); |
| 116 |
} |
| 117 |
|
| 118 |
if (mt.hasTimeoutOccurred()) { |
| 119 |
throw new StepExecutionException( |
| 120 |
"Timeout received before completing execution of script.", //$NON-NLS-1$ |
| 121 |
EventFactory.createActionError( |
| 122 |
TestErrorEvent.CONFIRMATION_TIMEOUT)); |
| 123 |
} |
| 124 |
|
| 125 |
int actualExitValue = mt.getExitCode(); |
| 126 |
if (actualExitValue != expectedExitCode) { |
| 127 |
throw new StepExecutionException( |
| 128 |
"Verification of exit code failed.", //$NON-NLS-1$ |
| 129 |
EventFactory.createVerifyFailed( |
| 130 |
String.valueOf(expectedExitCode), |
| 131 |
String.valueOf(actualExitValue))); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* {@inheritDoc} |
| 139 |
*/ |
| 140 |
public void setComponent(Object graphicsComponent) { |
| 141 |
// Do nothing; Application has no correspaonding component |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Takes a screenshot and saves the image to disk. |
| 146 |
* |
| 147 |
* @param destination |
| 148 |
* Path and filename for the created image. If the extension is not |
| 149 |
* ".jpeg" (case-insensitive), ".jpeg" will be appended to the |
| 150 |
* filename. |
| 151 |
* @param delay |
| 152 |
* Amount of time to wait (in milliseconds) before taking the |
| 153 |
* screenshot. |
| 154 |
* @param fileAccess |
| 155 |
* Determines how the file will be created if a file with the |
| 156 |
* given name and path already exists:<br> |
| 157 |
* <code>SwingApplicationImplClass.RENAME</code> - |
| 158 |
* The screenshot will be saved with a sequential integer appended |
| 159 |
* to the filename.<br> |
| 160 |
* <code>SwingApplicationImplClass.OVERWRITE</code> - |
| 161 |
* The screenshot will overwrite the file. |
| 162 |
* @param scaling |
| 163 |
* Degree to which the image should be scaled, in percent. A |
| 164 |
* <code>scaling</code> value of <code>100</code> produces an |
| 165 |
* unscaled image. This value must be greater than <code>0</code> |
| 166 |
* and less than or equal to <code>200</code>. |
| 167 |
* @param createDirs |
| 168 |
* Determines whether a path will be created if it does not already |
| 169 |
* exist. A value of <code>true</code> means that all necessary |
| 170 |
* directories that do not exist will be created automatically. |
| 171 |
*/ |
| 172 |
public void gdTakeScreenshot(String destination, int delay, |
| 173 |
String fileAccess, int scaling, boolean createDirs) { |
| 174 |
// Determine current screen size |
| 175 |
Toolkit toolkit = Toolkit.getDefaultToolkit(); |
| 176 |
Dimension screenSize = toolkit.getScreenSize(); |
| 177 |
|
| 178 |
// If screen !(resolution%2==0) --> bad scaling |
| 179 |
int screenWidth = (int)screenSize.getWidth(); |
| 180 |
int screenHeight = (int)screenSize.getHeight(); |
| 181 |
if (!(screenWidth % 2 == 0)) { |
| 182 |
screenWidth = screenWidth - 1; |
| 183 |
} |
| 184 |
if (!(screenHeight % 2 == 0)) { |
| 185 |
screenHeight = screenHeight - 1; |
| 186 |
} |
| 187 |
|
| 188 |
screenSize.setSize(screenWidth, screenHeight); |
| 189 |
|
| 190 |
Rectangle screenRect = new Rectangle(screenSize); |
| 191 |
|
| 192 |
takeScreenshot(destination, delay, fileAccess, scaling, createDirs, |
| 193 |
screenRect); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Takes a screenshot and saves the image to disk. |
| 198 |
* |
| 199 |
* @param destination |
| 200 |
* Path and filename for the created image. If the extension is |
| 201 |
* not ".jpeg" (case-insensitive), ".jpeg" will be appended to |
| 202 |
* the filename. |
| 203 |
* @param delay |
| 204 |
* Amount of time to wait (in milliseconds) before taking the |
| 205 |
* screenshot. |
| 206 |
* @param fileAccess |
| 207 |
* Determines how the file will be created if a file with the |
| 208 |
* given name and path already exists:<br> |
| 209 |
* <code>SwingApplicationImplClass.RENAME</code> - The screenshot |
| 210 |
* will be saved with a sequential integer appended to the |
| 211 |
* filename.<br> |
| 212 |
* <code>SwingApplicationImplClass.OVERWRITE</code> - The |
| 213 |
* screenshot will overwrite the file. |
| 214 |
* @param scaling |
| 215 |
* Degree to which the image should be scaled, in percent. A |
| 216 |
* <code>scaling</code> value of <code>100</code> produces an |
| 217 |
* unscaled image. This value must be greater than <code>0</code> |
| 218 |
* and less than or equal to <code>200</code>. |
| 219 |
* @param createDirs |
| 220 |
* Determines whether a path will be created if it does not |
| 221 |
* already exist. A value of <code>true</code> means that all |
| 222 |
* necessary directories that do not exist will be created |
| 223 |
* automatically. |
| 224 |
* @param marginTop |
| 225 |
* the extra top margin |
| 226 |
* @param marginRight |
| 227 |
* the extra right margin |
| 228 |
* @param marginBottom |
| 229 |
* the extra bottom margin |
| 230 |
* @param marginLeft |
| 231 |
* the extra left margin |
| 232 |
*/ |
| 233 |
public void gdTakeScreenshotOfActiveWindow(String destination, int delay, |
| 234 |
String fileAccess, int scaling, boolean createDirs, int marginTop, |
| 235 |
int marginRight, int marginBottom, int marginLeft) { |
| 236 |
Rectangle activeWindowBounds = getActiveWindowBounds(); |
| 237 |
|
| 238 |
if (activeWindowBounds == null) { |
| 239 |
throw new StepExecutionException("No active window found", //$NON-NLS-1$ |
| 240 |
EventFactory |
| 241 |
.createActionError(TestErrorEvent.NO_ACTIVE_WINDOW)); |
| 242 |
} |
| 243 |
|
| 244 |
int x = activeWindowBounds.x - marginLeft; |
| 245 |
int y = activeWindowBounds.y - marginTop; |
| 246 |
int width = activeWindowBounds.width + marginLeft + marginRight; |
| 247 |
int height = activeWindowBounds.height + marginTop + marginBottom; |
| 248 |
|
| 249 |
if (width < 1 || height < 1) { |
| 250 |
throw new StepExecutionException("Margin parameter lead to negative height or width", //$NON-NLS-1$ |
| 251 |
EventFactory |
| 252 |
.createActionError(TestErrorEvent.INVALID_INPUT)); |
| 253 |
} |
| 254 |
|
| 255 |
Rectangle screenRect = new Rectangle(x, y, width, height); |
| 256 |
|
| 257 |
takeScreenshot(destination, delay, fileAccess, scaling, createDirs, |
| 258 |
screenRect); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* @return an awt rectangle which represents the absolute active window |
| 263 |
* bounds; may return null e.g. if no active window could be found |
| 264 |
*/ |
| 265 |
public abstract Rectangle getActiveWindowBounds(); |
| 266 |
|
| 267 |
|
| 268 |
/** |
| 269 |
* Takes a screenshot and saves the image to disk, in JPEG format. |
| 270 |
* |
| 271 |
* @param destination |
| 272 |
* Path and filename for the created image. If the extension is not |
| 273 |
* ".jpeg" (case-insensitive), ".jpeg" will be appended to the |
| 274 |
* filename. |
| 275 |
* @param delay |
| 276 |
* Amount of time to wait (in milliseconds) before taking the |
| 277 |
* screenshot. |
| 278 |
* @param fileAccess |
| 279 |
* Determines how the file will be created if a file with the |
| 280 |
* given name and path already exists:<br> |
| 281 |
* <code>SwingApplicationImplClass.RENAME</code> - |
| 282 |
* The screenshot will be saved with a sequential integer appended |
| 283 |
* to the filename.<br> |
| 284 |
* <code>SwingApplicationImplClass.OVERWRITE</code> - |
| 285 |
* The screenshot will overwrite the file. |
| 286 |
* @param scaling |
| 287 |
* Degree to which the image should be scaled, in percent. A |
| 288 |
* <code>scaling</code> value of <code>100</code> produces an |
| 289 |
* unscaled image. This value must be greater than <code>0</code> |
| 290 |
* and less than or equal to <code>200</code>. |
| 291 |
* @param createDirs |
| 292 |
* Determines whether a path will be created if it does not already |
| 293 |
* exist. A value of <code>true</code> means that all necessary |
| 294 |
* directories that do not exist will be created automatically. |
| 295 |
* @param screenShotRect |
| 296 |
* the rectangle to take the screenshot of |
| 297 |
*/ |
| 298 |
private void takeScreenshot(String destination, int delay, |
| 299 |
String fileAccess, int scaling, boolean createDirs, |
| 300 |
Rectangle screenShotRect) { |
| 301 |
if (scaling <= 0 || scaling > 200) { |
| 302 |
throw new StepExecutionException( |
| 303 |
"Invalid scaling factor: Must be between 1 and 200", //$NON-NLS-1$ |
| 304 |
EventFactory.createActionError( |
| 305 |
TestErrorEvent.INVALID_PARAM_VALUE)); |
| 306 |
} |
| 307 |
|
| 308 |
double scaleFactor = scaling * 0.01; |
| 309 |
|
| 310 |
// Check if file name is valid |
| 311 |
String outFileName = destination; |
| 312 |
String imageExtension = getExtension(outFileName); |
| 313 |
if (imageExtension.length() == 0) { |
| 314 |
// If not, then we simply append the default extension |
| 315 |
imageExtension = DEFAULT_IMAGE_FORMAT; |
| 316 |
outFileName += EXTENSION_SEPARATOR + imageExtension; |
| 317 |
} |
| 318 |
|
| 319 |
// Wait for a user-specified time |
| 320 |
if (delay > 0) { |
| 321 |
TimeUtil.delay(delay); |
| 322 |
} |
| 323 |
|
| 324 |
// Create path, if necessary |
| 325 |
File pic = new File(outFileName); |
| 326 |
if (pic.getParent() == null) { |
| 327 |
throw new StepExecutionException( |
| 328 |
"Invalid file name: specify a file name", //$NON-NLS-1$ |
| 329 |
EventFactory.createActionError( |
| 330 |
TestErrorEvent.INVALID_PARAM_VALUE)); |
| 331 |
} |
| 332 |
|
| 333 |
File path = new File(pic.getParent()); |
| 334 |
if (createDirs && !path.exists() && !path.mkdirs()) { |
| 335 |
throw new StepExecutionException( |
| 336 |
"Directory path does not exist and could not be created", //$NON-NLS-1$ |
| 337 |
EventFactory.createActionError( |
| 338 |
TestErrorEvent.FILE_IO_ERROR)); |
| 339 |
} |
| 340 |
|
| 341 |
// Rename file if file already exists |
| 342 |
// FIXME zeb This naming scheme can lead to sorting problems when |
| 343 |
// filenames have varying numbers of digits (ex. "pic_9" and |
| 344 |
// "pic_10") |
| 345 |
if (fileAccess.equals(RENAME)) { |
| 346 |
String completeExtension = |
| 347 |
EXTENSION_SEPARATOR + imageExtension.toLowerCase(); |
| 348 |
int extensionIndex = |
| 349 |
pic.getName().toLowerCase().lastIndexOf(completeExtension); |
| 350 |
String fileName = pic.getName().substring(0, extensionIndex); |
| 351 |
for (int i = 1; pic.exists(); i++) { |
| 352 |
pic = new File(pic.getParent(), fileName + "_" + i + completeExtension); //$NON-NLS-1$ |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
takeScreenshot(screenShotRect, scaleFactor, pic); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Takes a screenshot and saves the image to disk. This method will attempt |
| 361 |
* to encode the image according to the file extension of the given |
| 362 |
* output file. If this is not possible (because the encoding type |
| 363 |
* is not supported), then the default encoding type will be used. If |
| 364 |
* the default encoding type is used, an appropriate extension will be added |
| 365 |
* to the filename. |
| 366 |
* |
| 367 |
* @param captureRect |
| 368 |
* Rect to capture in screen coordinates. |
| 369 |
* @param scaleFactor |
| 370 |
* Degree to which the image should be scaled, in percent. A |
| 371 |
* <code>scaleFactor</code> of <code>100</code> produces an |
| 372 |
* unscaled image. This value must be greater than <code>0</code> |
| 373 |
* and less than or equal to <code>200</code>. |
| 374 |
* @param outputFile |
| 375 |
* Path and filename for the created image. |
| 376 |
*/ |
| 377 |
public void takeScreenshot( |
| 378 |
Rectangle captureRect, double scaleFactor, File outputFile) { |
| 379 |
// Create screenshot |
| 380 |
java.awt.Robot robot; |
| 381 |
File out = outputFile; |
| 382 |
|
| 383 |
try { |
| 384 |
robot = new java.awt.Robot(); |
| 385 |
BufferedImage image = robot.createScreenCapture(captureRect); |
| 386 |
|
| 387 |
int scaledWidth = (int) Math.floor(image.getWidth() * scaleFactor); |
| 388 |
int scaledHeight = |
| 389 |
(int) Math.floor(image.getHeight() * scaleFactor); |
| 390 |
BufferedImage imageOut = |
| 391 |
new BufferedImage(scaledWidth, |
| 392 |
scaledHeight, BufferedImage.TYPE_INT_RGB); |
| 393 |
// Scale it to the new size on-the-fly |
| 394 |
Graphics2D graphics2D = imageOut.createGraphics(); |
| 395 |
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, |
| 396 |
RenderingHints.VALUE_RENDER_QUALITY); |
| 397 |
graphics2D.drawImage(image, 0, 0, scaledWidth, scaledHeight, null); |
| 398 |
|
| 399 |
// Save captured image using given format, if supported. |
| 400 |
String extension = getExtension(out.getName()); |
| 401 |
if (extension.length() == 0 |
| 402 |
|| !ImageIO.getImageWritersBySuffix(extension).hasNext() |
| 403 |
|| !ImageIO.write(imageOut, extension, out)) { |
| 404 |
|
| 405 |
// Otherwise, save using default format |
| 406 |
out = new File(outputFile.getPath() |
| 407 |
+ EXTENSION_SEPARATOR + DEFAULT_IMAGE_FORMAT); |
| 408 |
if (!ImageIO.write(imageOut, DEFAULT_IMAGE_FORMAT, out)) { |
| 409 |
|
| 410 |
// This should never happen, so log as error if it does. |
| 411 |
// In this situation, the screenshot will not be saved, but |
| 412 |
// the test step will still be marked as successful. |
| 413 |
log.error("Screenshot could not be saved. " + //$NON-NLS-1$ |
| 414 |
"Default image format (" + DEFAULT_IMAGE_FORMAT //$NON-NLS-1$ |
| 415 |
+ ") is not supported."); //$NON-NLS-1$ |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
} catch (AWTException e) { |
| 420 |
throw new RobotException(e); |
| 421 |
} catch (IOException e) { |
| 422 |
throw new StepExecutionException( |
| 423 |
"Screenshot could not be saved", //$NON-NLS-1$ |
| 424 |
EventFactory.createActionError( |
| 425 |
TestErrorEvent.FILE_IO_ERROR)); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Waits a specified time. |
| 431 |
* @param timeMilliSec the time to wait in MilliSec |
| 432 |
*/ |
| 433 |
public void gdWait(int timeMilliSec) { |
| 434 |
TimeUtil.delay(timeMilliSec); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* shows a ConfirmDialog and Pause the Execution of the Test until Window |
| 439 |
* is closed |
| 440 |
*/ |
| 441 |
public void gdPause() { |
| 442 |
throw new ExecutionEvent(ExecutionEvent.PAUSE_EXECUTION); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Does nothing! The restart is implemented in the client but the server |
| 447 |
* must have an action to execute. |
| 448 |
*/ |
| 449 |
public void gdRestart() { |
| 450 |
// nothing |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Types the given text without checking location or event confirmation. |
| 455 |
* |
| 456 |
* @param text The text to type. |
| 457 |
*/ |
| 458 |
public void gdNativeInputText(String text) { |
| 459 |
try { |
| 460 |
KeyTyper.getInstance().nativeTypeString(text); |
| 461 |
} catch (AWTException e) { |
| 462 |
throw new RobotException(e); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Action to perform a manual test step on server side; opens a window and |
| 468 |
* wait's for real user interaction |
| 469 |
* |
| 470 |
* @param actionToPerform |
| 471 |
* a textual description of the action to perform in the AUT |
| 472 |
* @param expectedBehavior |
| 473 |
* a textual description of the expected behaviour |
| 474 |
* @param timeout |
| 475 |
* the timeout |
| 476 |
*/ |
| 477 |
public void gdManualTestStep(String actionToPerform, |
| 478 |
String expectedBehavior, int timeout) { |
| 479 |
// empty implementation: implementation can be found in the corresponding |
| 480 |
// postExecutionCommand |
| 481 |
} |
| 482 |
|
| 483 |
/** |
| 484 |
* Perform a keystroke specified according <a |
| 485 |
* href=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/KeyStroke.html#getKeyStroke(java.lang.String)> |
| 486 |
* string representation of a keystroke </a>. |
| 487 |
* This method does not wait for event confirmation, as we have no way of |
| 488 |
* confirming events on OS-native widgets. |
| 489 |
* |
| 490 |
* @param modifierSpec the string representation of the modifiers |
| 491 |
* @param keySpec the string representation of the key |
| 492 |
*/ |
| 493 |
public void gdNativeKeyStroke(String modifierSpec, String keySpec) { |
| 494 |
if (keySpec == null || keySpec.trim().length() == 0) { |
| 495 |
throw new StepExecutionException("The base key of the key stroke " //$NON-NLS-1$ |
| 496 |
+ "must not be null or empty", //$NON-NLS-1$ |
| 497 |
EventFactory.createActionError()); |
| 498 |
} |
| 499 |
|
| 500 |
try { |
| 501 |
|
| 502 |
KeyTyper typer = KeyTyper.getInstance(); |
| 503 |
String keyStrokeSpec = keySpec.trim().toUpperCase(); |
| 504 |
String mod = KeyStrokeUtil.getModifierString(modifierSpec); |
| 505 |
if (mod.length() > 0) { |
| 506 |
keyStrokeSpec = mod + " " + keyStrokeSpec; //$NON-NLS-1$ |
| 507 |
} |
| 508 |
|
| 509 |
typer.type(keyStrokeSpec, null, null, null); |
| 510 |
|
| 511 |
} catch (AWTException e) { |
| 512 |
throw new RobotException(e); |
| 513 |
} |
| 514 |
|
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* Action to set the value of a variable in the Client. |
| 519 |
* |
| 520 |
* @param variable The name of the variable. |
| 521 |
* @param value The new value for the variable. |
| 522 |
* @return the new value for the variable. |
| 523 |
*/ |
| 524 |
public String gdSetValue(String variable, String value) { |
| 525 |
return value; |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* @return The Robot instance |
| 530 |
*/ |
| 531 |
protected abstract IRobot getRobot(); |
| 532 |
|
| 533 |
/** |
| 534 |
* |
| 535 |
* @param filename A filename for which to find the extension. |
| 536 |
* @return the file extension for the given filename. For example, |
| 537 |
* "png" for "example.png". Returns an empty string if the given |
| 538 |
* name has no extension. This is the case if the given name does |
| 539 |
* not contain an instance of the extension separator or ends with |
| 540 |
* the extension separator. For example, "example" or "example.". |
| 541 |
*/ |
| 542 |
private String getExtension(String filename) { |
| 543 |
File file = new File(filename); |
| 544 |
int extensionIndex = file.getName().lastIndexOf(EXTENSION_SEPARATOR); |
| 545 |
return extensionIndex == -1 |
| 546 |
|| extensionIndex == file.getName().length() - 1 |
| 547 |
? StringConstants.EMPTY |
| 548 |
: file.getName().substring(extensionIndex + 1); |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* method to copy a string to the system clipboard |
| 553 |
* |
| 554 |
* @param text The text to copy |
| 555 |
*/ |
| 556 |
public void gdCopyToClipboard(final String text) { |
| 557 |
StringSelection strSel = new StringSelection(text); |
| 558 |
try { |
| 559 |
Toolkit.getDefaultToolkit().getSystemClipboard() |
| 560 |
.setContents(strSel, null); |
| 561 |
} catch (IllegalStateException ise) { |
| 562 |
throw new StepExecutionException( |
| 563 |
"Clipboard not available.", //$NON-NLS-1$ |
| 564 |
EventFactory.createActionError( |
| 565 |
TestErrorEvent.CLIPBOARD_NOT_AVAILABLE)); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* method to compare to values |
| 571 |
* |
| 572 |
* @param value1 |
| 573 |
* the first value for comparison |
| 574 |
* @param comparisonMethod |
| 575 |
* the comparison method |
| 576 |
* @param value2 |
| 577 |
* the second value for comparison |
| 578 |
*/ |
| 579 |
public void gdCheckValues(final String value1, |
| 580 |
final String comparisonMethod, final String value2) { |
| 581 |
Comparer.compare(value1, value2, comparisonMethod); |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* method to compare to strings |
| 586 |
* |
| 587 |
* @param value1 |
| 588 |
* the first value for comparison |
| 589 |
* @param operator |
| 590 |
* the comparison method |
| 591 |
* @param value2 |
| 592 |
* the second value for comparison |
| 593 |
*/ |
| 594 |
public void gdCheckStringValues(final String value1, |
| 595 |
final String operator, final String value2) { |
| 596 |
Verifier.match(value1, value2, operator); |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* Does nothing! The start timer is implemented in the client but the server |
| 601 |
* must have an action to execute. |
| 602 |
* @param timerName the name for the timer |
| 603 |
* @param variableName the variable name to store the current time in millisecs in |
| 604 |
*/ |
| 605 |
public void gdStartTimer(String timerName, String variableName) { |
| 606 |
// empty |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* Does nothing! The read timer is implemented in the client but the server |
| 611 |
* must have an action to execute. |
| 612 |
* @param timerName the name for the timer |
| 613 |
* @param variableName the variable name to store the current time delta in millisecs in |
| 614 |
*/ |
| 615 |
public void gdReadTimer(String timerName, String variableName) { |
| 616 |
// empty |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* activate the AUT |
| 621 |
* |
| 622 |
* @param method activation method |
| 623 |
*/ |
| 624 |
public void gdActivate(String method) { |
| 625 |
getRobot().activateApplication(method); |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* clicks into the active window. |
| 630 |
* |
| 631 |
* @param count amount of clicks |
| 632 |
* @param button what mouse button should be used |
| 633 |
* @param xPos what x position |
| 634 |
* @param xUnits should x position be pixel or percent values |
| 635 |
* @param yPos what y position |
| 636 |
* @param yUnits should y position be pixel or percent values |
| 637 |
* @throws StepExecutionException error |
| 638 |
*/ |
| 639 |
public void gdClickDirect(int count, int button, |
| 640 |
int xPos, String xUnits, int yPos, String yUnits) |
| 641 |
throws StepExecutionException { |
| 642 |
|
| 643 |
Object activeWindow = getActiveWindow(); |
| 644 |
if (activeWindow != null) { |
| 645 |
getRobot().click(activeWindow, null, |
| 646 |
ClickOptions.create() |
| 647 |
.setClickCount(count) |
| 648 |
.setConfirmClick(false) |
| 649 |
.setMouseButton(button), |
| 650 |
xPos, |
| 651 |
xUnits.equalsIgnoreCase(POS_UNIT_PIXEL), |
| 652 |
yPos, |
| 653 |
yUnits.equalsIgnoreCase(POS_UNIT_PIXEL)); |
| 654 |
} else { |
| 655 |
throw new StepExecutionException("No active window.", //$NON-NLS-1$ |
| 656 |
EventFactory.createActionError( |
| 657 |
TestErrorEvent.NO_ACTIVE_WINDOW)); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
/** |
| 662 |
* Checks for the existence of a window with the given title |
| 663 |
* |
| 664 |
* @param title |
| 665 |
* the title |
| 666 |
* @param operator |
| 667 |
* the comparing operator |
| 668 |
* @param exists |
| 669 |
* <code>True</code> if the window is expected to exist and be |
| 670 |
* visible, otherwise <code>false</code>. |
| 671 |
*/ |
| 672 |
// public void gdCheckExistenceOfWindow(final String title, String operator, |
| 673 |
// boolean exists) { |
| 674 |
// Verifier.equals(exists, isWindowOpen(title, operator)); |
| 675 |
// } |
| 676 |
|
| 677 |
/** |
| 678 |
* Just a server side method, not useable as action. |
| 679 |
* |
| 680 |
* @param keyCode The key code |
| 681 |
*/ |
| 682 |
public void gdKeyType(int keyCode) { |
| 683 |
getRobot().keyType(null, keyCode); |
| 684 |
} |
| 685 |
|
| 686 |
/** |
| 687 |
* Just a server side method, not useable as action. |
| 688 |
* |
| 689 |
* note : this action only works if application got focus, |
| 690 |
* because using defaultToolkit does not work. You have to |
| 691 |
* use component.getToolKit()s |
| 692 |
* @param key to set |
| 693 |
* numlock Num Lock 1 |
| 694 |
* caplock Caps Lock 2 |
| 695 |
* scolllock Scroll 3 |
| 696 |
* @param activated |
| 697 |
* boolean |
| 698 |
*/ |
| 699 |
public void gdToggle(int key, boolean activated) { |
| 700 |
|
| 701 |
int event = getEventCode(key); |
| 702 |
if (event != 0) { |
| 703 |
try { |
| 704 |
getRobot().keyToggle(getFocusOwner(), |
| 705 |
event, activated); |
| 706 |
} catch (UnsupportedOperationException usoe) { |
| 707 |
throw new StepExecutionException( |
| 708 |
TestErrorEvent.UNSUPPORTED_OPERATION_ERROR, |
| 709 |
EventFactory.createActionError( |
| 710 |
TestErrorEvent.UNSUPPORTED_OPERATION_ERROR)); |
| 711 |
} catch (OsNotSupportedException e) { |
| 712 |
throw new StepExecutionException( |
| 713 |
TestErrorEvent.UNSUPPORTED_OPERATION_ERROR, |
| 714 |
EventFactory.createActionError( |
| 715 |
TestErrorEvent.UNSUPPORTED_OPERATION_ERROR)); |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
* perform a keystroke |
| 722 |
* @param modifierSpec the string representation of the modifiers |
| 723 |
* @param keySpec the string representation of the key |
| 724 |
*/ |
| 725 |
public abstract void gdKeyStroke(String modifierSpec, String keySpec); |
| 726 |
/** |
| 727 |
* |
| 728 |
* @return the Focus Owner |
| 729 |
*/ |
| 730 |
protected abstract Object getFocusOwner(); |
| 731 |
/** |
| 732 |
* |
| 733 |
* @param key to set |
| 734 |
* numlock Num Lock 1 |
| 735 |
* caplock Caps Lock 2 |
| 736 |
* scolllock Scroll 3 |
| 737 |
* @return the toolkit specific eventcode |
| 738 |
*/ |
| 739 |
protected abstract int getEventCode(int key); |
| 740 |
|
| 741 |
/** |
| 742 |
* |
| 743 |
* @return The active application window, or <code>null</code> if no |
| 744 |
* application window is currently active. |
| 745 |
*/ |
| 746 |
protected abstract Object getActiveWindow(); |
| 747 |
|
| 748 |
|
| 749 |
} |