Skip to content

Commit d2c7a71

Browse files
committed
Rewrite "New Wizard" Swing tests to no longer require the SWTBot runner
Newer Eclipse versions no longer launch a workspace when running a test outside the UI thread. Because this is a requirement for all of our SWTBot tests, we can no longer run them locally. The tests were originally written like that because they open a modal dialog and would thus lead to a deadlock if the test is also executed within the UI thread. But thanks to the changes to the `UiContext`, we have a way to make this work regardless.
1 parent 7ac46a4 commit d2c7a71

File tree

5 files changed

+130
-18
lines changed

5 files changed

+130
-18
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023, 2026 Patrick Ziegler and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* https://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Patrick Ziegler - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.wb.tests.designer.rcp;
14+
15+
import org.eclipse.wb.tests.gef.UiContext;
16+
17+
import static org.eclipse.swtbot.swt.finder.waits.Conditions.shellCloses;
18+
19+
import org.eclipse.core.resources.IFile;
20+
import org.eclipse.core.runtime.CoreException;
21+
import org.eclipse.core.runtime.Platform;
22+
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
23+
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
24+
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
25+
import org.eclipse.swtbot.swt.finder.SWTBot;
26+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
27+
import org.eclipse.ui.IViewReference;
28+
import org.eclipse.ui.PlatformUI;
29+
30+
import org.junit.jupiter.api.AfterEach;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.osgi.framework.Version;
33+
34+
import java.util.Arrays;
35+
36+
/**
37+
* Abstract base class for all tests that create Java files using the
38+
* {@code New Wizard}.
39+
*/
40+
public abstract class AbstractWizardTest extends RcpModelTest {
41+
private static Version ECLIPSE_VERSION = Platform.getBundle("org.eclipse.ui.workbench").getVersion();
42+
private static Version VERSION = new Version(3, 135, 0);
43+
// https://github.com/eclipse-platform/eclipse.platform/issues/1749
44+
private static String WIZARD_NAME = ECLIPSE_VERSION.compareTo(VERSION) > 0 ? "New" : "Select a wizard";
45+
private SWTWorkbenchBot workbench;
46+
private SWTBotView projectExplorer;
47+
private IFile editorFile;
48+
protected SWTBot editor;
49+
50+
@Override
51+
@BeforeEach
52+
public void setUp() throws Exception {
53+
super.setUp();
54+
workbench = new SWTWorkbenchBot();
55+
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
56+
.showView("org.eclipse.ui.navigator.ProjectExplorer");
57+
IViewReference projectExplorerView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
58+
.findViewReference("org.eclipse.ui.navigator.ProjectExplorer");
59+
projectExplorer = new SWTBotView(projectExplorerView, workbench);
60+
}
61+
62+
@Override
63+
@AfterEach
64+
public void tearDown() throws Exception {
65+
projectExplorer.close();
66+
if (editorFile != null) {
67+
forceDeleteResource(editorFile);
68+
editorFile = null;
69+
}
70+
super.tearDown();
71+
}
72+
73+
protected final void testTemplateViaProjectExplorer(String... fullPath) throws Exception {
74+
new UiContext().executeAndCheck(() -> {
75+
projectExplorer.bot().tree().getTreeItem("TestProject").contextMenu().menu("New", "Other...").click();
76+
}, bot -> createTemplate(bot, fullPath));
77+
}
78+
79+
protected final void testTemplateViaMenu(String... fullPath) throws Exception {
80+
new UiContext().executeAndCheck(() -> {
81+
workbench.shell().menu().menu("File").menu("New", "Other...").click();
82+
}, bot -> createTemplate(bot, fullPath));
83+
}
84+
85+
private void createTemplate(SWTBot activeShell, String... fullPath) throws CoreException {
86+
assertTrue(fullPath.length > 1, "path requires at least one argument (template name)");
87+
String[] path = Arrays.copyOf(fullPath, fullPath.length - 1);
88+
String name = fullPath[fullPath.length - 1];
89+
90+
SWTBotShell shell = activeShell.shell(WIZARD_NAME);
91+
SWTBot bot = shell.bot();
92+
bot.tree().expandNode(path).getNode(name).select();
93+
bot.button("Next >").click();
94+
bot.text(1).setText("test");
95+
bot.text(2).setText("Test");
96+
bot.button("Finish").click();
97+
// Wait for file creation
98+
bot.waitUntil(shellCloses(shell));
99+
// Open design page
100+
SWTBotEditor activeEditor = new SWTWorkbenchBot().activeEditor();
101+
editorFile = activeEditor.getReference().getEditorInput().getAdapter(IFile.class);
102+
editor = activeEditor.bot();
103+
editor.cTabItem("Design").activate();
104+
}
105+
}

org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/swing/SwingTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011, 2025 Google, Inc. and others.
2+
* Copyright (c) 2011, 2026 Google, Inc. and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -15,6 +15,7 @@
1515
import org.eclipse.wb.tests.designer.swing.laf.LookAndFeelTest;
1616
import org.eclipse.wb.tests.designer.swing.model.ModelTests;
1717
import org.eclipse.wb.tests.designer.swing.swingx.SwingXTests;
18+
import org.eclipse.wb.tests.designer.swing.wizard.WizardTests;
1819

1920
import org.junit.platform.suite.api.SelectClasses;
2021
import org.junit.platform.suite.api.Suite;
@@ -29,6 +30,7 @@
2930
CustomizeTest.class,
3031
ModelTests.class,
3132
SwingXTests.class,
33+
WizardTests.class
3234
// WaitForMemoryProfilerTest.class,
3335
})
3436
public class SwingTests {

org.eclipse.wb.tests/src/org/eclipse/wb/tests/swtbot/designer/swing/wizard/SwingNewWizardTest.java renamed to org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/swing/wizard/SwingNewWizardTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2023, 2024 Patrick Ziegler and others.
2+
* Copyright (c) 2023, 2026 Patrick Ziegler and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -10,11 +10,9 @@
1010
* Contributors:
1111
* Patrick Ziegler - initial API and implementation
1212
*******************************************************************************/
13-
package org.eclipse.wb.tests.swtbot.designer.swing.wizard;
13+
package org.eclipse.wb.tests.designer.swing.wizard;
1414

15-
import org.eclipse.wb.tests.swtbot.designer.AbstractWizardTest;
16-
17-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
15+
import org.eclipse.wb.tests.designer.rcp.AbstractWizardTest;
1816

1917
import org.junit.jupiter.api.Test;
2018

@@ -37,7 +35,7 @@ public void testCreateNewJDialog() throws Exception {
3735

3836
@Test
3937
public void testCreateNewJApplet() throws Exception {
40-
testTemplateViaProjectExplorer("WindowBuilder", "Swing Designer", "JApplet");
38+
testTemplateViaProjectExplorer("WindowBuilder", "Swing Designer", "(Deprecated) JApplet");
4139
}
4240

4341
@Test
@@ -52,13 +50,13 @@ public void testCreateNewApplicationWindow() throws Exception {
5250

5351
@Test
5452
public void testCreateWithJavaModules() throws Exception {
55-
bot.setFileContent("module-info.java", """
53+
setFileContentSrc("module-info.java", """
5654
module test {
5755
}""");
5856
//
5957
testTemplateViaProjectExplorer("WindowBuilder", "Swing Designer", "JFrame");
6058
// We can't use code blocks as they don't consider carriage-returns
61-
assertArrayEquals(bot.getFileContent("module-info.java").split(System.lineSeparator()),
59+
assertArrayEquals(getFileContentSrc("module-info.java").split(System.lineSeparator()),
6260
new String[] {
6361
"module test {",
6462
" requires java.desktop;",
@@ -83,7 +81,7 @@ public void testCreateNewJDialogNoSelection() throws Exception {
8381

8482
@Test
8583
public void testCreateNewJAppletNoSelection() throws Exception {
86-
testTemplateViaMenu("WindowBuilder", "Swing Designer", "JApplet");
84+
testTemplateViaMenu("WindowBuilder", "Swing Designer", "(Deprecated) JApplet");
8785
}
8886

8987
@Test
@@ -98,13 +96,13 @@ public void testCreateNewApplicationWindowNoSelection() throws Exception {
9896

9997
@Test
10098
public void testCreateWithJavaModulesNoSelection() throws Exception {
101-
bot.setFileContent("module-info.java", """
99+
setFileContentSrc("module-info.java", """
102100
module test {
103101
}""");
104102
//
105103
testTemplateViaMenu("WindowBuilder", "Swing Designer", "JFrame");
106104
// We can't use code blocks as they don't consider carriage-returns
107-
assertArrayEquals(bot.getFileContent("module-info.java").split(System.lineSeparator()),
105+
assertArrayEquals(getFileContentSrc("module-info.java").split(System.lineSeparator()),
108106
new String[] {
109107
"module test {",
110108
" requires java.desktop;",

org.eclipse.wb.tests/src/org/eclipse/wb/tests/swtbot/designer/swing/SwingTests.java renamed to org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/swing/wizard/WizardTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2024 Patrick Ziegler and others.
2+
* Copyright (c) 2024, 2026 Patrick Ziegler and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -10,9 +10,7 @@
1010
* Contributors:
1111
* Patrick Ziegler - initial API and implementation
1212
*******************************************************************************/
13-
package org.eclipse.wb.tests.swtbot.designer.swing;
14-
15-
import org.eclipse.wb.tests.swtbot.designer.swing.wizard.SwingNewWizardTest;
13+
package org.eclipse.wb.tests.designer.swing.wizard;
1614

1715
import org.junit.platform.suite.api.SelectClasses;
1816
import org.junit.platform.suite.api.Suite;
@@ -22,6 +20,6 @@
2220
*/
2321
@Suite
2422
@SelectClasses(SwingNewWizardTest.class)
25-
public class SwingTests {
23+
public class WizardTests {
2624

2725
}

org.eclipse.wb.tests/src/org/eclipse/wb/tests/gef/UiContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011, 2025 Google, Inc. and others.
2+
* Copyright (c) 2011, 2026 Google, Inc. and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -69,6 +69,15 @@ public void executeAndCheck(final FailableRunnable<Exception> uiRunnable,
6969
Shell activeShell = UIThreadRunnable.syncExec(m_display::getActiveShell);
7070
checkRunnable.accept(new SWTBot(activeShell));
7171
} catch (Throwable e) {
72+
// For debugging: Show all active shells
73+
UIThreadRunnable.syncExec(() -> {
74+
System.err.println("Active Shell:");
75+
System.err.println(Display.getCurrent().getActiveShell());
76+
System.err.println("Shells:");
77+
for (Shell shell : Display.getCurrent().getShells()) {
78+
System.err.println(shell);
79+
}
80+
});
7281
e.printStackTrace();
7382
checkException[0] = e;
7483
} finally {

0 commit comments

Comments
 (0)