|
16 | 16 | *******************************************************************************/
|
17 | 17 | package org.eclipse.ui.tests.harness.util;
|
18 | 18 |
|
19 |
| -import java.io.IOException; |
20 |
| -import java.io.OutputStream; |
21 |
| -import java.io.PrintStream; |
22 | 19 | import java.util.ArrayList;
|
23 | 20 | import java.util.List;
|
24 | 21 | import java.util.Set;
|
25 | 22 |
|
26 |
| -import org.eclipse.core.resources.ResourcesPlugin; |
27 |
| -import org.eclipse.core.runtime.CoreException; |
28 |
| -import org.eclipse.core.runtime.IAdaptable; |
29 |
| -import org.eclipse.core.runtime.IStatus; |
30 |
| -import org.eclipse.core.runtime.jobs.Job; |
31 | 23 | import org.eclipse.jface.preference.IPreferenceStore;
|
32 | 24 | import org.eclipse.jface.preference.PreferenceMemento;
|
33 |
| -import org.eclipse.swt.widgets.Display; |
34 | 25 | import org.eclipse.swt.widgets.Shell;
|
35 |
| -import org.eclipse.ui.IWorkbenchPage; |
36 |
| -import org.eclipse.ui.IWorkbenchWindow; |
37 | 26 | import org.eclipse.ui.PlatformUI;
|
38 |
| -import org.eclipse.ui.WorkbenchException; |
39 | 27 | import org.junit.After;
|
40 | 28 | import org.junit.Before;
|
41 | 29 | import org.junit.Rule;
|
|
54 | 42 | */
|
55 | 43 | public abstract class UITestCase extends TestCase {
|
56 | 44 |
|
57 |
| - /** |
58 |
| - * Returns the workbench page input to use for newly created windows. |
59 |
| - * |
60 |
| - * @return the page input to use for newly created windows |
61 |
| - * @since 3.1 |
62 |
| - */ |
63 |
| - public static IAdaptable getPageInput() { |
64 |
| - return ResourcesPlugin.getWorkspace().getRoot(); |
65 |
| - } |
66 |
| - |
67 | 45 | /**
|
68 | 46 | * Rule to close windows opened during the test case, manually called to remain
|
69 | 47 | * compatible with JUnit3
|
@@ -100,57 +78,6 @@ public UITestCase(String testName) {
|
100 | 78 | super(testName);
|
101 | 79 | }
|
102 | 80 |
|
103 |
| - /** |
104 |
| - * Fails the test due to the given throwable. |
105 |
| - */ |
106 |
| - public static void fail(String message, Throwable e) { |
107 |
| - // If the exception is a CoreException with a multistatus |
108 |
| - // then print out the multistatus so we can see all the info. |
109 |
| - if (e instanceof CoreException) { |
110 |
| - IStatus status = ((CoreException) e).getStatus(); |
111 |
| - write(status, 0); |
112 |
| - } else |
113 |
| - e.printStackTrace(); |
114 |
| - throw new AssertionError(message, e); |
115 |
| - } |
116 |
| - |
117 |
| - private static void indent(OutputStream output, int indent) { |
118 |
| - for (int i = 0; i < indent; i++) |
119 |
| - try { |
120 |
| - output.write("\t".getBytes()); |
121 |
| - } catch (IOException e) { |
122 |
| - // ignore |
123 |
| - } |
124 |
| - } |
125 |
| - |
126 |
| - private static void write(IStatus status, int indent) { |
127 |
| - PrintStream output = System.out; |
128 |
| - indent(output, indent); |
129 |
| - output.println("Severity: " + status.getSeverity()); |
130 |
| - |
131 |
| - indent(output, indent); |
132 |
| - output.println("Plugin ID: " + status.getPlugin()); |
133 |
| - |
134 |
| - indent(output, indent); |
135 |
| - output.println("Code: " + status.getCode()); |
136 |
| - |
137 |
| - indent(output, indent); |
138 |
| - output.println("Message: " + status.getMessage()); |
139 |
| - |
140 |
| - if (status.getException() != null) { |
141 |
| - indent(output, indent); |
142 |
| - output.print("Exception: "); |
143 |
| - status.getException().printStackTrace(output); |
144 |
| - } |
145 |
| - |
146 |
| - if (status.isMultiStatus()) { |
147 |
| - IStatus[] children = status.getChildren(); |
148 |
| - for (IStatus child : children) { |
149 |
| - write(child, indent + 1); |
150 |
| - } |
151 |
| - } |
152 |
| - } |
153 |
| - |
154 | 81 | /**
|
155 | 82 | * Outputs a trace message to the trace output device, if enabled.
|
156 | 83 | * By default, trace messages are sent to <code>System.out</code>.
|
@@ -233,209 +160,13 @@ protected void doTearDown() throws Exception {
|
233 | 160 | closeTestWindows.after();
|
234 | 161 | }
|
235 | 162 |
|
236 |
| - public static void processEvents() { |
237 |
| - Display display = PlatformUI.getWorkbench().getDisplay(); |
238 |
| - if (display != null) |
239 |
| - while (display.readAndDispatch()) |
240 |
| - ; |
241 |
| - } |
242 |
| - |
243 |
| - /** |
244 |
| - * Utility for waiting until the execution of jobs of any family has |
245 |
| - * finished or timeout is reached. If no jobs are running, the method waits |
246 |
| - * given minimum wait time. While this method is waiting for jobs, UI events |
247 |
| - * are processed. |
248 |
| - * |
249 |
| - * @param minTimeMs |
250 |
| - * minimum wait time in milliseconds |
251 |
| - * @param maxTimeMs |
252 |
| - * maximum wait time in milliseconds |
253 |
| - */ |
254 |
| - public static void waitForJobs(long minTimeMs, long maxTimeMs) { |
255 |
| - if (maxTimeMs < minTimeMs) { |
256 |
| - throw new IllegalArgumentException("Max time is smaller as min time!"); |
257 |
| - } |
258 |
| - final long start = System.currentTimeMillis(); |
259 |
| - while (System.currentTimeMillis() - start < minTimeMs) { |
260 |
| - processEvents(); |
261 |
| - sleep(10); |
262 |
| - } |
263 |
| - while (!Job.getJobManager().isIdle() && System.currentTimeMillis() - start < maxTimeMs) { |
264 |
| - processEvents(); |
265 |
| - sleep(10); |
266 |
| - } |
267 |
| - } |
268 |
| - |
269 |
| - /** |
270 |
| - * Pauses execution of the current thread |
271 |
| - */ |
272 |
| - protected static void sleep(long millis) { |
273 |
| - try { |
274 |
| - Thread.sleep(millis); |
275 |
| - } catch (InterruptedException e) { |
276 |
| - return; |
277 |
| - } |
278 |
| - } |
279 |
| - |
280 |
| - /** |
281 |
| - * Tries to make given shell active. |
282 |
| - * |
283 |
| - * <p> |
284 |
| - * Note: the method runs at least 1000 milliseconds to make sure the active |
285 |
| - * window is really active, see |
286 |
| - * https://bugs.eclipse.org/bugs/show_bug.cgi?id=417258#c27 |
287 |
| - * |
288 |
| - * @param shell |
289 |
| - * non null |
290 |
| - * @return true if the given shell is active for the current display |
291 |
| - */ |
292 |
| - public static boolean forceActive(Shell shell) { |
293 |
| - Display display = PlatformUI.getWorkbench().getDisplay(); |
294 |
| - Shell[] shells = display.getShells(); |
295 |
| - for (Shell s : shells) { |
296 |
| - if (s.isVisible()) { |
297 |
| - s.setMinimized(true); |
298 |
| - } |
299 |
| - processEvents(); |
300 |
| - } |
301 |
| - waitForJobs(200, 3000); |
302 |
| - for (Shell s : shells) { |
303 |
| - if (s.isVisible()) { |
304 |
| - s.setMinimized(false); |
305 |
| - } |
306 |
| - processEvents(); |
307 |
| - } |
308 |
| - waitForJobs(200, 3000); |
309 |
| - shell.setVisible(false); |
310 |
| - processEvents(); |
311 |
| - shell.setMinimized(true); |
312 |
| - processEvents(); |
313 |
| - waitForJobs(200, 3000); |
314 |
| - shell.setVisible(true); |
315 |
| - processEvents(); |
316 |
| - shell.setMinimized(false); |
317 |
| - processEvents(); |
318 |
| - shell.forceActive(); |
319 |
| - processEvents(); |
320 |
| - shell.forceFocus(); |
321 |
| - processEvents(); |
322 |
| - waitForJobs(400, 3000); |
323 |
| - return display.getActiveShell() == shell; |
324 |
| - } |
325 |
| - |
326 |
| - public static interface Condition { |
327 |
| - public boolean compute(); |
328 |
| - } |
329 |
| - |
330 |
| - /** |
331 |
| - * |
332 |
| - * @param condition |
333 |
| - * , or null if this should only wait |
334 |
| - * @param timeout |
335 |
| - * , -1 if forever |
336 |
| - * @return true if successful, false if time out or interrupted |
337 |
| - */ |
338 |
| - public static boolean processEventsUntil(Condition condition, long timeout) { |
339 |
| - long startTime = System.currentTimeMillis(); |
340 |
| - Display display = PlatformUI.getWorkbench().getDisplay(); |
341 |
| - while (condition == null || !condition.compute()) { |
342 |
| - if (timeout != -1 |
343 |
| - && System.currentTimeMillis() - startTime > timeout) { |
344 |
| - return false; |
345 |
| - } |
346 |
| - while (display.readAndDispatch()) |
347 |
| - ; |
348 |
| - try { |
349 |
| - Thread.sleep(20); |
350 |
| - } catch (InterruptedException e) { |
351 |
| - Thread.currentThread().interrupt(); |
352 |
| - return false; |
353 |
| - } |
354 |
| - } |
355 |
| - return true; |
356 |
| - } |
357 |
| - |
358 |
| - /** |
359 |
| - * Open a test window with the empty perspective. |
360 |
| - */ |
361 |
| - public static IWorkbenchWindow openTestWindow() { |
362 |
| - return openTestWindow(EmptyPerspective.PERSP_ID); |
363 |
| - } |
364 |
| - |
365 |
| - /** |
366 |
| - * Open a test window with the provided perspective. |
367 |
| - */ |
368 |
| - public static IWorkbenchWindow openTestWindow(String perspectiveId) { |
369 |
| - try { |
370 |
| - IWorkbenchWindow window = PlatformUI.getWorkbench().openWorkbenchWindow( |
371 |
| - perspectiveId, getPageInput()); |
372 |
| - waitOnShell(window.getShell()); |
373 |
| - return window; |
374 |
| - } catch (WorkbenchException e) { |
375 |
| - fail("Problem opening test window", e); |
376 |
| - return null; |
377 |
| - } |
378 |
| - } |
379 |
| - |
380 |
| - /** |
381 |
| - * Try and process events until the new shell is the active shell. This may |
382 |
| - * never happen, so time out after a suitable period. |
383 |
| - * |
384 |
| - * @param shell |
385 |
| - * the shell to wait on |
386 |
| - * @since 3.2 |
387 |
| - */ |
388 |
| - private static void waitOnShell(Shell shell) { |
389 |
| - processEvents(); |
390 |
| - waitForJobs(100, 5000); |
391 |
| - } |
392 |
| - |
393 | 163 | /**
|
394 | 164 | * Close all test windows.
|
395 | 165 | */
|
396 | 166 | public void closeAllTestWindows() {
|
397 | 167 | closeTestWindows.closeAllTestWindows();
|
398 | 168 | }
|
399 | 169 |
|
400 |
| - /** |
401 |
| - * Open a test page with the empty perspective in a window. |
402 |
| - */ |
403 |
| - public IWorkbenchPage openTestPage(IWorkbenchWindow win) { |
404 |
| - IWorkbenchPage[] pages = openTestPage(win, 1); |
405 |
| - if (pages != null) { |
406 |
| - return pages[0]; |
407 |
| - } |
408 |
| - return null; |
409 |
| - } |
410 |
| - |
411 |
| - /** |
412 |
| - * Open "n" test pages with the empty perspective in a window. |
413 |
| - */ |
414 |
| - public IWorkbenchPage[] openTestPage(IWorkbenchWindow win, int pageTotal) { |
415 |
| - try { |
416 |
| - IWorkbenchPage[] pages = new IWorkbenchPage[pageTotal]; |
417 |
| - IAdaptable input = getPageInput(); |
418 |
| - |
419 |
| - for (int i = 0; i < pageTotal; i++) { |
420 |
| - pages[i] = win.openPage(EmptyPerspective.PERSP_ID, input); |
421 |
| - } |
422 |
| - return pages; |
423 |
| - } catch (WorkbenchException e) { |
424 |
| - fail("Problem opening test page", e); |
425 |
| - return null; |
426 |
| - } |
427 |
| - } |
428 |
| - |
429 |
| - /** |
430 |
| - * Close all pages within a window. |
431 |
| - */ |
432 |
| - public void closeAllPages(IWorkbenchWindow window) { |
433 |
| - IWorkbenchPage[] pages = window.getPages(); |
434 |
| - for (IWorkbenchPage page : pages) { |
435 |
| - page.close(); |
436 |
| - } |
437 |
| - } |
438 |
| - |
439 | 170 | /**
|
440 | 171 | * Set whether the window listener will manage opening and closing of created windows.
|
441 | 172 | */
|
|
0 commit comments