Skip to content

Commit d7e6c30

Browse files
committed
Move check for leaked shells to CloseTestWindowsRule
The UITestCase contains a leaked shell check that would be better places in a test rule and fits to the already existing CloseTestWindowsRule. This change integrates the leaked shell check into the CloseTestWindowsRule.
1 parent b9d8b69 commit d7e6c30

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

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

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,40 @@
1616
package org.eclipse.ui.tests.harness.util;
1717

1818
import static org.eclipse.ui.tests.harness.util.UITestUtil.processEvents;
19+
import static org.junit.Assert.assertEquals;
1920

2021
import java.util.ArrayList;
2122
import java.util.List;
23+
import java.util.Set;
2224

25+
import org.eclipse.swt.widgets.Shell;
2326
import org.eclipse.ui.IWindowListener;
2427
import org.eclipse.ui.IWorkbenchWindow;
2528
import org.eclipse.ui.PlatformUI;
2629
import org.junit.rules.ExternalResource;
2730

31+
/**
32+
* Rule that close windows opened during the test case and checks for shells
33+
* unintentionally leaked from the test case.
34+
*/
2835
public class CloseTestWindowsRule extends ExternalResource {
2936

30-
private boolean enabled = true;
31-
3237
private final List<IWorkbenchWindow> testWindows;
3338

3439
private TestWindowListener windowListener;
3540

41+
private Set<Shell> initialShells;
42+
43+
private boolean leakChecksDisabled;
44+
3645
public CloseTestWindowsRule() {
3746
testWindows = new ArrayList<>(3);
3847
}
3948

4049
@Override
4150
protected void before() throws Exception {
4251
addWindowListener();
52+
storeInitialShells();
4353
}
4454

4555
@Override
@@ -48,6 +58,7 @@ protected void after() {
4858
processEvents();
4959
closeAllTestWindows();
5060
processEvents();
61+
checkForLeakedShells();
5162
}
5263

5364
/**
@@ -78,11 +89,7 @@ private void closeAllTestWindows() {
7889
testWindows.clear();
7990
}
8091

81-
public void setEnabled(boolean enabled) {
82-
this.enabled = enabled;
83-
}
84-
85-
class TestWindowListener implements IWindowListener {
92+
private class TestWindowListener implements IWindowListener {
8693
@Override
8794
public void windowActivated(IWorkbenchWindow window) {
8895
// do nothing
@@ -95,16 +102,36 @@ public void windowDeactivated(IWorkbenchWindow window) {
95102

96103
@Override
97104
public void windowClosed(IWorkbenchWindow window) {
98-
if (enabled) {
99-
testWindows.remove(window);
100-
}
105+
testWindows.remove(window);
101106
}
102107

103108
@Override
104109
public void windowOpened(IWorkbenchWindow window) {
105-
if (enabled) {
106-
testWindows.add(window);
110+
testWindows.add(window);
111+
}
112+
}
113+
114+
private void storeInitialShells() {
115+
this.initialShells = Set.of(PlatformUI.getWorkbench().getDisplay().getShells());
116+
}
117+
118+
private void checkForLeakedShells() {
119+
List<String> leakedModalShellTitles = new ArrayList<>();
120+
Shell[] shells = PlatformUI.getWorkbench().getDisplay().getShells();
121+
for (Shell shell : shells) {
122+
if (!shell.isDisposed() && !initialShells.contains(shell)) {
123+
leakedModalShellTitles.add(shell.getText());
124+
shell.close();
107125
}
108126
}
127+
if (!leakChecksDisabled) {
128+
assertEquals("Test leaked modal shell: [" + String.join(", ", leakedModalShellTitles) + "]", 0,
129+
leakedModalShellTitles.size());
130+
}
109131
}
132+
133+
public void disableLeakChecks() {
134+
this.leakChecksDisabled = true;
135+
}
136+
110137
}

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

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616
*******************************************************************************/
1717
package org.eclipse.ui.tests.harness.util;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
21-
import java.util.Set;
22-
23-
import org.eclipse.swt.widgets.Shell;
24-
import org.eclipse.ui.PlatformUI;
2519
import org.junit.After;
2620
import org.junit.Before;
2721
import org.junit.Rule;
@@ -46,8 +40,6 @@ public abstract class UITestCase extends TestCase {
4640
*/
4741
private final CloseTestWindowsRule closeTestWindows = new CloseTestWindowsRule();
4842

49-
private Set<Shell> preExistingShells;
50-
5143
/**
5244
* Required to preserve the existing logging output when running tests with
5345
* {@link BlockJUnit4ClassRunner}.
@@ -97,11 +89,9 @@ protected void trace(String msg) {
9789
public final void setUp() throws Exception {
9890
super.setUp();
9991
closeTestWindows.before();
100-
this.preExistingShells = Set.of(PlatformUI.getWorkbench().getDisplay().getShells());
10192
String name = runningTest != null ? runningTest : this.getName();
10293
trace(TestRunLogUtil.formatTestStartMessage(name));
10394
doSetUp();
104-
10595
}
10696

10797
/**
@@ -129,18 +119,7 @@ public final void tearDown() throws Exception {
129119
String name = runningTest != null ? runningTest : this.getName();
130120
trace(TestRunLogUtil.formatTestFinishedMessage(name));
131121
doTearDown();
132-
133-
// Check for shell leak.
134-
List<String> leakedModalShellTitles = new ArrayList<>();
135-
Shell[] shells = PlatformUI.getWorkbench().getDisplay().getShells();
136-
for (Shell shell : shells) {
137-
if (!shell.isDisposed() && !preExistingShells.contains(shell)) {
138-
leakedModalShellTitles.add(shell.getText());
139-
shell.close();
140-
}
141-
}
142-
assertEquals("Test leaked modal shell: [" + String.join(", ", leakedModalShellTitles) + "]", 0,
143-
leakedModalShellTitles.size());
122+
closeTestWindows.after();
144123
}
145124

146125
/**
@@ -151,7 +130,6 @@ public final void tearDown() throws Exception {
151130
* Subclasses may extend.
152131
*/
153132
protected void doTearDown() throws Exception {
154-
closeTestWindows.after();
155133
}
156134

157135
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/stress/OpenCloseTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public void testOpenCloseWorkbenchWindow() throws WorkbenchException {
118118
*/
119119
@Test
120120
public void testOpenClosePerspective() {
121+
// Reopening the perspective will create a new popup shell that would be
122+
// detected as leak
123+
closeTestWindows.disableLeakChecks();
121124
ICommandService commandService = workbench.getService(ICommandService.class);
122125
Command command = commandService.getCommand("org.eclipse.ui.window.closePerspective");
123126

@@ -131,11 +134,11 @@ public void testOpenClosePerspective() {
131134

132135
for (int index = 0; index < numIterations; index++) {
133136
try {
134-
PlatformUI.getWorkbench().showPerspective(ORG_ECLIPSE_RESOURCE_PERSPECTIVE, workbenchWindow);
135137
try {
136138
handlerService.executeCommand(pCommand, null);
137139
} catch (ExecutionException | NotDefinedException | NotEnabledException | NotHandledException e1) {
138140
}
141+
PlatformUI.getWorkbench().showPerspective(ORG_ECLIPSE_RESOURCE_PERSPECTIVE, workbenchWindow);
139142
} catch (WorkbenchException e) {
140143
e.printStackTrace();
141144
}
@@ -148,8 +151,6 @@ public void testOpenClosePerspective() {
148151
@Test
149152
public void testOpenCloseView() throws WorkbenchException {
150153
IViewPart consoleView;
151-
IWorkbenchPage page = PlatformUI.getWorkbench().showPerspective(ORG_ECLIPSE_RESOURCE_PERSPECTIVE,
152-
workbenchWindow);
153154
for (int index = 0; index < numIterations; index++) {
154155
consoleView = page.showView(IPageLayout.ID_MINIMAP_VIEW);
155156
page.hideView(consoleView);

0 commit comments

Comments
 (0)