Skip to content

Commit 686baa6

Browse files
committed
Move static utility functionality from UITestCase to UITestUtil
The UITestCase currently contains a bunch of static utility functionality. Since UITestCase is a leftover from JUnit 3 times where test cases were defined in inheritance hierarchies, this class is about to be removed. In order to prepare for that, this moves the utility functions to a separate UITestUtil class. This also includes making some methods static that were effectively static before.
1 parent 0539498 commit 686baa6

File tree

148 files changed

+650
-395
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+650
-395
lines changed

tests/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/AbstratGenericEditorTest.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
package org.eclipse.ui.genericeditor.tests;
1515

1616
import static org.eclipse.ui.tests.harness.util.DisplayHelper.runEventLoop;
17+
import static org.eclipse.ui.tests.harness.util.UITestUtil.forceActive;
18+
import static org.eclipse.ui.tests.harness.util.UITestUtil.processEvents;
19+
import static org.eclipse.ui.tests.harness.util.UITestUtil.waitForJobs;
1720

1821
import java.io.ByteArrayInputStream;
1922
import java.nio.charset.StandardCharsets;
@@ -34,7 +37,6 @@
3437
import org.eclipse.ui.internal.genericeditor.ExtensionBasedTextEditor;
3538
import org.eclipse.ui.intro.IIntroPart;
3639
import org.eclipse.ui.part.FileEditorInput;
37-
import org.eclipse.ui.tests.harness.util.UITestCase;
3840

3941
/**
4042
* Closes intro, create {@link #project}, create {@link #file} and open {@link #editor}; and clean up.
@@ -59,9 +61,9 @@ public void setUp() throws Exception {
5961
project.create(null);
6062
project.open(null);
6163
project.setDefaultCharset(StandardCharsets.UTF_8.name(), null);
62-
UITestCase.waitForJobs(100, 5000);
64+
waitForJobs(100, 5000);
6365
window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
64-
UITestCase.forceActive(window.getShell());
66+
forceActive(window.getShell());
6567
createAndOpenFile();
6668
}
6769

@@ -128,4 +130,13 @@ private static void closeIntro() {
128130
runEventLoop(PlatformUI.getWorkbench().getDisplay(),0);
129131
}
130132
}
133+
134+
public static void waitAndDispatch(long milliseconds) {
135+
long timeout = milliseconds; //ms
136+
long start = System.currentTimeMillis();
137+
while (start + timeout > System.currentTimeMillis()) {
138+
processEvents();
139+
}
140+
}
141+
131142
}

tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/CloseTestWindowsRule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package org.eclipse.ui.tests.harness.util;
1717

18+
import static org.eclipse.ui.tests.harness.util.UITestUtil.processEvents;
19+
1820
import java.util.ArrayList;
1921
import java.util.List;
2022

@@ -43,9 +45,9 @@ protected void before() throws Exception {
4345
@Override
4446
protected void after() {
4547
removeWindowListener();
46-
UITestCase.processEvents();
48+
processEvents();
4749
closeAllTestWindows();
48-
UITestCase.processEvents();
50+
processEvents();
4951
}
5052

5153
/**

tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/DialogCheck.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*******************************************************************************/
1515
package org.eclipse.ui.tests.harness.util;
1616

17+
import static org.eclipse.ui.tests.harness.util.UITestUtil.processEvents;
1718
import static org.junit.Assert.assertNotNull;
1819
import static org.junit.Assert.fail;
1920

@@ -83,7 +84,7 @@ public static void assertDialogTexts(Dialog dialog) {
8384
dialog.setBlockOnOpen(false);
8485
dialog.open();
8586
Shell shell = dialog.getShell();
86-
UITestCase.processEvents();
87+
processEvents();
8788
try {
8889
verifyCompositeText(shell);
8990
} finally {

tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/UITestCase.java

Lines changed: 0 additions & 269 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,14 @@
1616
*******************************************************************************/
1717
package org.eclipse.ui.tests.harness.util;
1818

19-
import java.io.IOException;
20-
import java.io.OutputStream;
21-
import java.io.PrintStream;
2219
import java.util.ArrayList;
2320
import java.util.List;
2421
import java.util.Set;
2522

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;
3123
import org.eclipse.jface.preference.IPreferenceStore;
3224
import org.eclipse.jface.preference.PreferenceMemento;
33-
import org.eclipse.swt.widgets.Display;
3425
import org.eclipse.swt.widgets.Shell;
35-
import org.eclipse.ui.IWorkbenchPage;
36-
import org.eclipse.ui.IWorkbenchWindow;
3726
import org.eclipse.ui.PlatformUI;
38-
import org.eclipse.ui.WorkbenchException;
3927
import org.junit.After;
4028
import org.junit.Before;
4129
import org.junit.Rule;
@@ -54,16 +42,6 @@
5442
*/
5543
public abstract class UITestCase extends TestCase {
5644

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-
6745
/**
6846
* Rule to close windows opened during the test case, manually called to remain
6947
* compatible with JUnit3
@@ -100,57 +78,6 @@ public UITestCase(String testName) {
10078
super(testName);
10179
}
10280

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-
15481
/**
15582
* Outputs a trace message to the trace output device, if enabled.
15683
* By default, trace messages are sent to <code>System.out</code>.
@@ -233,209 +160,13 @@ protected void doTearDown() throws Exception {
233160
closeTestWindows.after();
234161
}
235162

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-
393163
/**
394164
* Close all test windows.
395165
*/
396166
public void closeAllTestWindows() {
397167
closeTestWindows.closeAllTestWindows();
398168
}
399169

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-
439170
/**
440171
* Set whether the window listener will manage opening and closing of created windows.
441172
*/

0 commit comments

Comments
 (0)