Skip to content

Commit d41f3cb

Browse files
committed
Add a SWTTest Junit extension to allow tests executed in the UI thread
Currently we require SWT-UI based tests to be executed by special runners known as the 'UIHarness' to be run in the UI thread, what has several problems: 1) is requires usually to fire up a whole workbench (and a workspace) 2) the whole testsuite has to run in the UI thread 3) no way to customize this further 4) actually this is nothing that belongs into the test framework This now adds a new SWTTest JUnit extension that allows to mark a test class with an extension and get your methods executed in the UI thread.
1 parent 84f0345 commit d41f3cb

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

binaries/.classpath_cocoa

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@
3030
<classpathentry kind="src" path="Eclipse SWT OpenGL/cocoa"/>
3131
<classpathentry kind="src" path="Eclipse SWT OpenGL/common"/>
3232
<classpathentry kind="output" path="bin"/>
33+
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
3334
</classpath>

binaries/.classpath_gtk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@
3232
<classpathentry kind="src" path="Eclipse SWT OpenGL/common"/>
3333
<classpathentry kind="src" path="Eclipse SWT WebKit/gtk"/>
3434
<classpathentry kind="output" path="bin"/>
35+
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
3536
</classpath>

binaries/.classpath_win32

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@
3232
</classpathentry>
3333
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
3434
<classpathentry kind="output" path="bin"/>
35+
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
3536
</classpath>

binaries/org.eclipse.swt.gtk.linux.x86_64/build.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ source.. = \
4343
../../bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk
4444
output.. = bin/
4545

46+
jars.extra.classpath = platform:/plugin/junit-jupiter-api
47+
4648
pom.model.property.os=linux
4749
pom.model.property.ws=gtk
4850
pom.model.property.arch=x86_64
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Christoph Läubrich and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Christoph Läubrich - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.test;
15+
16+
import java.lang.reflect.*;
17+
18+
import org.eclipse.pde.api.tools.annotations.*;
19+
import org.eclipse.swt.widgets.*;
20+
import org.junit.jupiter.api.extension.*;
21+
22+
/**
23+
* The {@link SWTTest} is an extension that can be using in JUnit Platform
24+
* Runners as an extension to execute the test code in the default display
25+
* thread.
26+
* <p>
27+
* <b>Usage: </b>You would never use this in any client code but when writing a
28+
* test for the JUnit Platform like in this example:
29+
* </p>
30+
* <p>
31+
*
32+
* <pre>
33+
* @ExtendWith(SWTTest.class)
34+
* public class MyTestThaRequireUI {
35+
*
36+
* }
37+
* </pre>
38+
* </p>
39+
*/
40+
@NoInstantiate
41+
public final class SWTTest implements InvocationInterceptor {
42+
43+
@Override
44+
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
45+
ExtensionContext extensionContext) throws Throwable {
46+
47+
if (Display.getCurrent() == null) {
48+
invocation.proceed();
49+
} else {
50+
Throwable[] throwable = new Throwable[1];
51+
Display.getDefault().syncExec(() -> {
52+
try {
53+
invocation.proceed();
54+
} catch (Throwable t) {
55+
throwable[0] = t;
56+
}
57+
58+
});
59+
Throwable t = throwable[0];
60+
if (t != null) {
61+
throw t;
62+
}
63+
}
64+
}
65+
}

bundles/org.eclipse.swt/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Bundle-SymbolicName: org.eclipse.swt; singleton:=true
55
Bundle-Version: 3.132.0.qualifier
66
Bundle-ManifestVersion: 2
77
Bundle-Localization: plugin
8-
DynamicImport-Package: org.eclipse.swt.accessibility2
8+
DynamicImport-Package: org.eclipse.swt.accessibility2,org.junit.jupiter.api.extension.*
99
Export-Package:
1010
org.eclipse.swt,
1111
org.eclipse.swt.accessibility,

0 commit comments

Comments
 (0)