| 1 | 52635 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
package org.eclipse.swtbot.swt.finder.utils; |
| 13 | |
|
| 14 | |
import java.io.File; |
| 15 | |
import java.lang.reflect.InvocationTargetException; |
| 16 | |
import java.lang.reflect.Method; |
| 17 | |
import java.text.MessageFormat; |
| 18 | |
|
| 19 | |
import org.apache.log4j.Logger; |
| 20 | |
import org.eclipse.swt.SWT; |
| 21 | |
import org.eclipse.swt.graphics.GC; |
| 22 | |
import org.eclipse.swt.graphics.Image; |
| 23 | |
import org.eclipse.swt.graphics.ImageData; |
| 24 | |
import org.eclipse.swt.graphics.ImageLoader; |
| 25 | |
import org.eclipse.swt.graphics.Rectangle; |
| 26 | |
import org.eclipse.swt.widgets.Control; |
| 27 | |
import org.eclipse.swt.widgets.Display; |
| 28 | |
import org.eclipse.swt.widgets.Shell; |
| 29 | |
import org.eclipse.swt.widgets.Text; |
| 30 | |
import org.eclipse.swt.widgets.Widget; |
| 31 | |
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
| 32 | |
import org.eclipse.swtbot.swt.finder.results.BoolResult; |
| 33 | |
import org.eclipse.swtbot.swt.finder.results.Result; |
| 34 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
| 35 | |
import org.eclipse.swtbot.swt.finder.utils.internal.NextWidgetFinder; |
| 36 | |
import org.eclipse.swtbot.swt.finder.utils.internal.PreviousWidgetFinder; |
| 37 | |
import org.eclipse.swtbot.swt.finder.utils.internal.ReflectionInvoker; |
| 38 | |
import org.eclipse.swtbot.swt.finder.utils.internal.SiblingFinder; |
| 39 | |
import org.eclipse.swtbot.swt.finder.utils.internal.WidgetIndexFinder; |
| 40 | |
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; |
| 41 | |
import org.eclipse.swtbot.swt.finder.waits.ICondition; |
| 42 | |
import org.eclipse.swtbot.swt.finder.widgets.TimeoutException; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 1 | public abstract class SWTUtils { |
| 49 | |
|
| 50 | |
|
| 51 | 1 | private static Logger log = Logger.getLogger(SWTUtils.class); |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
private static Display display; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public static Widget[] siblings(final Widget w) { |
| 63 | 17 | if ((w == null) || w.isDisposed()) |
| 64 | 0 | return new Widget[] {}; |
| 65 | 17 | return UIThreadRunnable.syncExec(w.getDisplay(), new SiblingFinder(w)); |
| 66 | |
} |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public static int widgetIndex(final Widget w) { |
| 76 | 1720 | if ((w == null) || w.isDisposed()) |
| 77 | 1 | return -1; |
| 78 | 1719 | return UIThreadRunnable.syncExec(w.getDisplay(), new WidgetIndexFinder(w)); |
| 79 | |
} |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public static Widget previousWidget(final Widget w) { |
| 88 | 668 | if ((w == null) || w.isDisposed()) |
| 89 | 0 | return null; |
| 90 | 668 | return UIThreadRunnable.syncExec(w.getDisplay(), new PreviousWidgetFinder(w)); |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public static Widget nextWidget(Widget w) { |
| 100 | 2 | if ((w == null) || w.isDisposed()) |
| 101 | 0 | return null; |
| 102 | 2 | return UIThreadRunnable.syncExec(w.getDisplay(), new NextWidgetFinder(w)); |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public static String getText(final Object obj) { |
| 112 | 1628 | if ((obj instanceof Widget) && !((Widget) obj).isDisposed()) { |
| 113 | 1628 | Widget widget = (Widget) obj; |
| 114 | 1628 | String text = UIThreadRunnable.syncExec(widget.getDisplay(), new ReflectionInvoker(obj, "getText")); |
| 115 | 1628 | text = text.replaceAll(Text.DELIMITER, "\n"); |
| 116 | 1628 | return text; |
| 117 | |
} |
| 118 | 0 | return ""; |
| 119 | |
} |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
public static String getToolTipText(final Object obj) { |
| 129 | 1 | if ((obj instanceof Widget) && !((Widget) obj).isDisposed()) { |
| 130 | 1 | Widget widget = (Widget) obj; |
| 131 | 1 | return UIThreadRunnable.syncExec(widget.getDisplay(), new ReflectionInvoker(obj, "getToolTipText")); |
| 132 | |
} |
| 133 | 0 | return ""; |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public static String toString(final Widget w) { |
| 143 | 947 | if ((w == null) || w.isDisposed()) |
| 144 | 2 | return ""; |
| 145 | 945 | return toString(w.getDisplay(), w); |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public static String toString(Display display, final Object o) { |
| 156 | 945 | return ClassUtils.simpleClassName(o) + " {" + trimToLength(getText(o), 20) + "}"; |
| 157 | |
|
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
private static String trimToLength(String result, int maxLength) { |
| 168 | 945 | if (result.length() > maxLength) |
| 169 | 29 | result = result.substring(0, maxLength) + "..."; |
| 170 | 945 | return result; |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public static boolean hasStyle(final Widget w, final int style) { |
| 181 | 2617 | if ((w == null) || w.isDisposed()) |
| 182 | 1 | return false; |
| 183 | 2616 | if (style == SWT.NONE) |
| 184 | 1 | return true; |
| 185 | 2615 | return UIThreadRunnable.syncExec(w.getDisplay(), new BoolResult() { |
| 186 | |
public Boolean run() { |
| 187 | 2615 | return (w.getStyle() & style) != 0; |
| 188 | |
} |
| 189 | |
}); |
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
public static void sleep(long millis) { |
| 198 | |
try { |
| 199 | 260 | Thread.sleep(millis); |
| 200 | 0 | } catch (InterruptedException e) { |
| 201 | 0 | throw new RuntimeException("Could not sleep", e); |
| 202 | |
} |
| 203 | 260 | } |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
public static Thread[] allThreads() { |
| 211 | 1 | ThreadGroup threadGroup = primaryThreadGroup(); |
| 212 | |
|
| 213 | 1 | Thread[] threads = new Thread[64]; |
| 214 | 1 | int enumerate = threadGroup.enumerate(threads, true); |
| 215 | |
|
| 216 | 1 | Thread[] result = new Thread[enumerate]; |
| 217 | 1 | System.arraycopy(threads, 0, result, 0, enumerate); |
| 218 | |
|
| 219 | 1 | return result; |
| 220 | |
} |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
public static ThreadGroup primaryThreadGroup() { |
| 228 | 1 | ThreadGroup threadGroup = Thread.currentThread().getThreadGroup(); |
| 229 | 3 | while (threadGroup.getParent() != null) |
| 230 | 1 | threadGroup = threadGroup.getParent(); |
| 231 | 1 | return threadGroup; |
| 232 | |
} |
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
public static Display display() { |
| 240 | 18675 | if ((display == null) || display.isDisposed()) { |
| 241 | 1 | display = null; |
| 242 | 1 | Thread[] allThreads = allThreads(); |
| 243 | 8 | for (Thread thread : allThreads) { |
| 244 | 7 | Display d = Display.findDisplay(thread); |
| 245 | 7 | if (d != null) |
| 246 | 1 | display = d; |
| 247 | |
} |
| 248 | 1 | if (display == null) |
| 249 | 0 | throw new IllegalStateException("Could not find a display"); |
| 250 | |
} |
| 251 | 18675 | return display; |
| 252 | |
} |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
public static boolean isEmptyOrNullText(Widget w) { |
| 263 | 0 | return getText(w).trim().equals(""); |
| 264 | |
} |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
public static Object invokeMethod(final Object object, String methodName) throws NoSuchMethodException, IllegalAccessException, |
| 280 | |
InvocationTargetException { |
| 281 | 53293 | final Method method = object.getClass().getMethod(methodName, new Class[0]); |
| 282 | 52649 | Widget widget = null; |
| 283 | |
final Object result; |
| 284 | 52649 | if (object instanceof Widget) { |
| 285 | 52635 | widget = (Widget) object; |
| 286 | 52635 | result = UIThreadRunnable.syncExec(widget.getDisplay(), new Result<Object>() { |
| 287 | |
public Object run() { |
| 288 | |
try { |
| 289 | 52635 | return method.invoke(object, new Object[0]); |
| 290 | 0 | } catch (Exception niceTry) { |
| 291 | |
} |
| 292 | 0 | return null; |
| 293 | |
} |
| 294 | |
}); |
| 295 | |
} else |
| 296 | 14 | result = method.invoke(object, new Object[0]); |
| 297 | |
|
| 298 | 52649 | return result; |
| 299 | |
} |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
public static boolean captureScreenshot(final String fileName) { |
| 309 | 1 | new ImageFormatConverter().imageTypeOf(fileName.substring(fileName.lastIndexOf('.') + 1)); |
| 310 | 1 | return UIThreadRunnable.syncExec(new BoolResult() { |
| 311 | |
public Boolean run() { |
| 312 | 1 | return captureScreenshotInternal(fileName); |
| 313 | |
} |
| 314 | |
}); |
| 315 | |
} |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
public static boolean captureScreenshot(final String fileName, final Control control) { |
| 326 | 0 | new ImageFormatConverter().imageTypeOf(fileName.substring(fileName.lastIndexOf('.') + 1)); |
| 327 | 0 | return UIThreadRunnable.syncExec(new BoolResult() { |
| 328 | |
public Boolean run() { |
| 329 | 0 | if (control instanceof Shell) |
| 330 | 0 | return captureScreenshotInternal(fileName, control.getBounds()); |
| 331 | 0 | Display display = control.getDisplay(); |
| 332 | 0 | Rectangle bounds = control.getBounds(); |
| 333 | 0 | Rectangle mappedToDisplay = display.map(control.getParent(), null, bounds); |
| 334 | |
|
| 335 | 0 | return captureScreenshotInternal(fileName, mappedToDisplay); |
| 336 | |
} |
| 337 | |
}); |
| 338 | |
} |
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
public static boolean captureScreenshot(final String fileName, final Rectangle bounds) { |
| 349 | 0 | new ImageFormatConverter().imageTypeOf(fileName.substring(fileName.lastIndexOf('.') + 1)); |
| 350 | 0 | return UIThreadRunnable.syncExec(new BoolResult() { |
| 351 | |
public Boolean run() { |
| 352 | 0 | return captureScreenshotInternal(fileName, bounds); |
| 353 | |
} |
| 354 | |
}); |
| 355 | |
} |
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | 1 | private static boolean captureScreenshotInternal(final String fileName) { |
| 367 | 1 | return captureScreenshotInternal(fileName, display.getBounds()); |
| 368 | |
} |
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | 0 | private static boolean captureScreenshotInternal(final String fileName, Rectangle bounds) { |
| 381 | 1 | GC gc = new GC(display); |
| 382 | 1 | Image image = null; |
| 383 | 1 | File file = new File(fileName); |
| 384 | 1 | File parentDir = file.getParentFile(); |
| 385 | 1 | if (parentDir != null) |
| 386 | 1 | parentDir.mkdirs(); |
| 387 | |
try { |
| 388 | 1 | log.debug(MessageFormat.format("Capturing screenshot ''{0}''", fileName)); |
| 389 | |
|
| 390 | 1 | image = new Image(display, bounds.width, bounds.height); |
| 391 | 1 | gc.copyArea(image, bounds.x, bounds.y); |
| 392 | 1 | ImageLoader imageLoader = new ImageLoader(); |
| 393 | 1 | imageLoader.data = new ImageData[] { image.getImageData() }; |
| 394 | 1 | imageLoader.save(fileName, new ImageFormatConverter().imageTypeOf(fileName.substring(fileName.lastIndexOf('.') + 1))); |
| 395 | 1 | return true; |
| 396 | 0 | } catch (Exception e) { |
| 397 | 0 | log.warn("Could not capture screenshot: " + fileName + "'", e); |
| 398 | 0 | File brokenImage = file.getAbsoluteFile(); |
| 399 | 0 | if (brokenImage.exists()) { |
| 400 | |
try { |
| 401 | 0 | log.trace(MessageFormat.format("Broken screenshot set to be deleted on exit: {0}", fileName)); |
| 402 | 0 | brokenImage.deleteOnExit(); |
| 403 | 0 | } catch (Exception ex) { |
| 404 | 0 | log.info(MessageFormat.format("Could not set broken screenshot to be deleted on exit: {0}", fileName), ex); |
| 405 | |
} |
| 406 | |
} |
| 407 | 0 | return false; |
| 408 | 0 | } finally { |
| 409 | 1 | gc.dispose(); |
| 410 | 1 | if (image != null) { |
| 411 | 1 | image.dispose(); |
| 412 | |
} |
| 413 | 0 | } |
| 414 | |
} |
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
public static void waitForDisplayToAppear() { |
| 423 | 0 | waitForDisplayToAppear(SWTBotPreferences.TIMEOUT); |
| 424 | 0 | } |
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
public static void waitForDisplayToAppear(long timeout) { |
| 433 | 0 | waitUntil(new DefaultCondition() { |
| 434 | |
|
| 435 | |
public String getFailureMessage() { |
| 436 | 0 | return "Could not find a display"; |
| 437 | |
} |
| 438 | |
|
| 439 | |
public boolean test() throws Exception { |
| 440 | 0 | return SWTUtils.display() != null; |
| 441 | |
} |
| 442 | |
|
| 443 | 0 | }, timeout, 500); |
| 444 | 0 | } |
| 445 | |
|
| 446 | |
private static void waitUntil(ICondition condition, long timeout, long interval) throws TimeoutException { |
| 447 | 0 | Assert.isTrue(interval >= 0, "interval value is negative"); |
| 448 | 0 | Assert.isTrue(timeout >= 0, "timeout value is negative"); |
| 449 | 0 | long limit = System.currentTimeMillis() + timeout; |
| 450 | |
while (true) { |
| 451 | |
try { |
| 452 | 0 | if (condition.test()) |
| 453 | 0 | return; |
| 454 | 0 | } catch (Throwable e) { |
| 455 | |
|
| 456 | |
} |
| 457 | 0 | sleep(interval); |
| 458 | 0 | if (System.currentTimeMillis() > limit) |
| 459 | 0 | throw new TimeoutException("Timeout after: " + timeout + " ms.: " + condition.getFailureMessage()); |
| 460 | |
} |
| 461 | |
} |
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
public static boolean isUIThread(Display display) { |
| 470 | 94262 | return display.getThread() == Thread.currentThread(); |
| 471 | |
} |
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
public static boolean isUIThread() { |
| 479 | 0 | return isUIThread(display); |
| 480 | |
} |
| 481 | |
} |