From 7c478845b7882edc0ead6a1350f2fea8ac9a4451 Mon Sep 17 00:00:00 2001 From: Andrey Loskutov Date: Tue, 3 May 2022 18:56:41 +0200 Subject: [PATCH] Address "Device is disposed" error after test execution #50 Fixed test that didn't created Display but disposed it. Fixed tests that didn't closed created shells or assumed wrong number of shells after creation of new. See https://github.com/eclipse-platform/eclipse.platform.ui/issues/50 --- .../META-INF/MANIFEST.MF | 2 +- org.eclipse.jface.text.tests/pom.xml | 2 +- .../text/tests/TextPresentationTest.java | 6 ++- .../CodeMiningProjectionViewerTest.java | 37 +++++++++++++++++- .../AbstractContentAssistTest.java | 38 ++++++++++++++----- .../contentassist/AsyncContentAssistTest.java | 9 +++-- .../ContextInformationPresenterTest.java | 3 +- .../contentassist/ContextInformationTest.java | 15 +++----- .../FilteringAsyncContentAssistTests.java | 1 + .../IncrementalAsyncContentAssistTests.java | 1 + 10 files changed, 83 insertions(+), 31 deletions(-) diff --git a/org.eclipse.jface.text.tests/META-INF/MANIFEST.MF b/org.eclipse.jface.text.tests/META-INF/MANIFEST.MF index 36a3e53c7..17944d5fa 100644 --- a/org.eclipse.jface.text.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.jface.text.tests/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Plugin.name Bundle-SymbolicName: org.eclipse.jface.text.tests -Bundle-Version: 3.12.400.qualifier +Bundle-Version: 3.12.500.qualifier Bundle-Vendor: %Plugin.providerName Bundle-Localization: plugin Export-Package: diff --git a/org.eclipse.jface.text.tests/pom.xml b/org.eclipse.jface.text.tests/pom.xml index 7b4b21743..8601e828d 100644 --- a/org.eclipse.jface.text.tests/pom.xml +++ b/org.eclipse.jface.text.tests/pom.xml @@ -19,7 +19,7 @@ org.eclipse.jface org.eclipse.jface.text.tests - 3.12.400-SNAPSHOT + 3.12.500-SNAPSHOT eclipse-test-plugin ${project.artifactId} diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextPresentationTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextPresentationTest.java index b749d85d0..eddcaa0a2 100644 --- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextPresentationTest.java +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextPresentationTest.java @@ -89,7 +89,11 @@ private void setUpTextPresentation() { @After public void tearDown() { fColors.clear(); - fDisplay.dispose(); + if (!fDisplay.isDisposed()) { + for (Shell shell : fDisplay.getShells()) { + shell.dispose(); + } + } } private StyleRange createStyleRange(int start, int end, int style) { diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/codemining/CodeMiningProjectionViewerTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/codemining/CodeMiningProjectionViewerTest.java index a94662629..493bf13ff 100644 --- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/codemining/CodeMiningProjectionViewerTest.java +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/codemining/CodeMiningProjectionViewerTest.java @@ -16,9 +16,12 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; import org.junit.After; import org.junit.Assert; @@ -92,6 +95,10 @@ public void dispose() { @Before public void setUp() { + Shell[] shells= Display.getDefault().getShells(); + for (Shell shell : shells) { + shell.dispose(); + } fParent= new Shell(SWT.ON_TOP); fParent.setSize(500, 200); fParent.setLayout(new FillLayout()); @@ -118,6 +125,30 @@ public void tearDown() { fParent.dispose(); } + protected List getCurrentShells() { + return Arrays.stream(fParent.getDisplay().getShells()) + .filter(Shell::isVisible) + .collect(Collectors.toList()); + } + + protected List findNewShells(Collection beforeShells) { + return Arrays.stream(fParent.getDisplay().getShells()) + .filter(Shell::isVisible) + .filter(shell -> !beforeShells.contains(shell)) + .collect(Collectors.toList()); + } + + protected Shell findNewShell(Collection beforeShells) { + DisplayHelper.sleep(fParent.getDisplay(), 100); + List afterShells= findNewShells(beforeShells); + if (afterShells.isEmpty()) { + DisplayHelper.sleep(fParent.getDisplay(), 1000); + } + afterShells= findNewShells(beforeShells); + assertTrue("No new shell found, existing: " + beforeShells, afterShells.size() > beforeShells.size()); + return afterShells.get(0); + } + @Test public void testCollapse() throws Exception { fViewer.setCodeMiningProviders(new ICodeMiningProvider[] { @@ -163,6 +194,7 @@ public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { return contentAssistant; } }); + final List beforeShells= getCurrentShells(); fViewer.setCodeMiningProviders(new ICodeMiningProvider[] { new DelayedEchoCodeMiningProvider() }); @@ -170,6 +202,7 @@ public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { fParent.setSize(200, 4 * fViewer.getTextWidget().getLineHeight()); //fParent.pack(true); fParent.open(); + DisplayHelper.driveEventQueue(fParent.getDisplay()); // ensure ViewportGuard is initialized fViewer.getControl().notifyListeners(SWT.KeyUp, new Event()); fViewer.setSelectedRange(1, 0); @@ -178,10 +211,10 @@ public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { assertTrue(new DisplayHelper() { @Override protected boolean condition() { - return display.getShells().length > 1; + return display.getShells().length > beforeShells.size(); } }.waitForCondition(display, 1000)); - Shell completionShell= display.getShells()[1]; + Shell completionShell= findNewShell(beforeShells); // ↓ showing codeminings changing viewport while completion popup is open fViewer.updateCodeMinings(); assertTrue(new DisplayHelper() { diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/AbstractContentAssistTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/AbstractContentAssistTest.java index 797553414..b32bfd764 100644 --- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/AbstractContentAssistTest.java +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/AbstractContentAssistTest.java @@ -14,7 +14,6 @@ package org.eclipse.jface.text.tests.contentassist; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.util.Arrays; @@ -24,6 +23,7 @@ import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ST; @@ -31,6 +31,7 @@ import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; @@ -58,12 +59,24 @@ public class AbstractContentAssistTest { public AbstractContentAssistTest() { } + @Before + public void setUp() { + Shell[] shells= Display.getDefault().getShells(); + for (Shell s : shells) { + s.dispose(); + } + DisplayHelper.driveEventQueue(Display.getDefault()); + } @After public void close() { if (shell != null && !shell.isDisposed()) { shell.close(); } + Shell[] shells= Display.getDefault().getShells(); + for (Shell s : shells) { + s.dispose(); + } } @@ -87,12 +100,13 @@ protected void setupSourceViewer(ContentAssistant contentAssistant, String initi button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); shell.open(); + processEvents(); Assert.assertTrue(new DisplayHelper() { @Override protected boolean condition() { return viewer.getTextWidget().isVisible(); } - }.waitForCondition(shell.getDisplay(), 3000)); + }.waitForCondition(getDisplay(), 3000)); } protected SourceViewerConfiguration createSourceViewerConfiguration() { @@ -192,31 +206,35 @@ public Button getButton() { protected void processEvents() { - DisplayHelper.driveEventQueue(shell.getDisplay()); + DisplayHelper.driveEventQueue(getDisplay()); + } + + private Display getDisplay() { + return Display.getDefault(); } protected List getCurrentShells() { - return Arrays.stream(shell.getDisplay().getShells()) + return Arrays.stream(getDisplay().getShells()) .filter(Shell::isVisible) .collect(Collectors.toList()); } protected List findNewShells(Collection beforeShells) { - return Arrays.stream(shell.getDisplay().getShells()) + return Arrays.stream(getDisplay().getShells()) .filter(Shell::isVisible) - .filter(shell -> !beforeShells.contains(shell)) + .filter(s -> !beforeShells.contains(s)) .collect(Collectors.toList()); } protected Shell findNewShell(Collection beforeShells) { - DisplayHelper.sleep(shell.getDisplay(), 100); + DisplayHelper.sleep(getDisplay(), 100); List afterShells= findNewShells(beforeShells); if (afterShells.isEmpty()) { - DisplayHelper.sleep(shell.getDisplay(), 1000); + DisplayHelper.sleep(getDisplay(), 1000); } afterShells= findNewShells(beforeShells); - assertEquals("No new shell found", 1, afterShells.size()); - return afterShells.get(0); + assertTrue("No new shell found, existing: " + beforeShells, afterShells.size() > beforeShells.size()); + return afterShells.get(afterShells.size() - 1); } } diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/AsyncContentAssistTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/AsyncContentAssistTest.java index c96e18226..302a70a0a 100644 --- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/AsyncContentAssistTest.java +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/AsyncContentAssistTest.java @@ -52,8 +52,11 @@ public class AsyncContentAssistTest { private ILogListener listener; private IStatus errorStatus; + private Shell shell; + @Before public void setUp() { + shell= new Shell(); listener= (status, plugin) -> { if(status.getSeverity() == IStatus.ERROR && "org.eclipse.jface.text".equals(status.getPlugin())) { errorStatus = status; @@ -64,12 +67,12 @@ public void setUp() { @After public void tearDown() { + shell.dispose(); Platform.removeLogListener(listener); } @Test public void testAsyncFailureStackOverflow() { - Shell shell = new Shell(); SourceViewer viewer = new SourceViewer(shell, null, SWT.NONE); Document document = new Document("a"); viewer.setDocument(document); @@ -85,7 +88,6 @@ public void testAsyncFailureStackOverflow() { @Test public void testSyncFailureNPE() { - Shell shell = new Shell(); SourceViewer viewer = new SourceViewer(shell, null, SWT.NONE); Document document = new Document("a"); viewer.setDocument(document); @@ -101,7 +103,6 @@ public void testSyncFailureNPE() { @Test public void testCompletePrefix() { - Shell shell = new Shell(); shell.setLayout(new FillLayout()); shell.setSize(500, 300); SourceViewer viewer = new SourceViewer(shell, null, SWT.NONE); @@ -113,6 +114,7 @@ public void testCompletePrefix() { contentAssistant.enablePrefixCompletion(true); contentAssistant.install(viewer); shell.open(); + DisplayHelper.driveEventQueue(shell.getDisplay()); Display display = shell.getDisplay(); final Set beforeShells = Arrays.stream(display.getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); contentAssistant.showPossibleCompletions(); @@ -132,7 +134,6 @@ protected boolean condition() { @Test public void testCompleteActivationChar() { - Shell shell= new Shell(); shell.setLayout(new FillLayout()); shell.setSize(500, 300); SourceViewer viewer= new SourceViewer(shell, null, SWT.NONE); diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/ContextInformationPresenterTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/ContextInformationPresenterTest.java index 3d69a7ba6..ecf95d72e 100644 --- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/ContextInformationPresenterTest.java +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/ContextInformationPresenterTest.java @@ -87,9 +87,8 @@ public IContextInformationValidator getContextInformationValidator() { @Test public void testContextInfo_withStyledTextPresentation() throws Exception { - setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); - final List beforeShells= getCurrentShells(); + setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); postSourceViewerKeyEvent(SWT.ARROW_RIGHT, 0, SWT.KeyDown); selectAndReveal(4, 0); diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/ContextInformationTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/ContextInformationTest.java index f1ce3384a..32793b9f4 100644 --- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/ContextInformationTest.java +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/ContextInformationTest.java @@ -49,9 +49,8 @@ private ContentAssistant createBarContentAssist() { @Test public void testContextInfo() throws Exception { - setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); - final List beforeShells= getCurrentShells(); + setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); selectAndReveal(4, 0); processEvents(); @@ -70,9 +69,8 @@ public void testContextInfo() throws Exception { @Test public void testContextInfo_hide_Bug512251() throws Exception { - setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); - final List beforeShells= getCurrentShells(); + setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); selectAndReveal(4, 0); processEvents(); @@ -96,10 +94,9 @@ public void testContextInfo_hide_Bug512251() throws Exception { public void testContextInfo_hide_focusOut() throws Exception { assumeFalse("Test fails on Mac: Bug 558989", Platform.OS_MACOSX.equals(Platform.getOS())); + final List beforeShells= getCurrentShells(); setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); - final List beforeShells = getCurrentShells(); - selectAndReveal(4, 0); processEvents(); @@ -122,10 +119,9 @@ public void testContextInfo_hide_focusOut() throws Exception { @Test public void testContextInfo_hide_keyEsc() throws Exception { + final List beforeShells= getCurrentShells(); setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); - final List beforeShells = getCurrentShells(); - selectAndReveal(4, 0); processEvents(); @@ -152,10 +148,9 @@ public void testContextInfo_hide_keyEsc() throws Exception { @Test public void testContextInfo_hide_validRange() throws Exception { + final List beforeShells= getCurrentShells(); setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL + '\n'); - final List beforeShells = getCurrentShells(); - selectAndReveal(4, 0); processEvents(); diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/FilteringAsyncContentAssistTests.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/FilteringAsyncContentAssistTests.java index 4ff609594..8dcd16ca4 100644 --- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/FilteringAsyncContentAssistTests.java +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/FilteringAsyncContentAssistTests.java @@ -71,6 +71,7 @@ public void setup() { shell = new Shell(); shell.setSize(300, 300); shell.open(); + DisplayHelper.driveEventQueue(shell.getDisplay()); viewer = new SourceViewer(shell, null, SWT.NONE); Document document = new Document(); diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/IncrementalAsyncContentAssistTests.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/IncrementalAsyncContentAssistTests.java index be82f68ee..8f467a7a7 100644 --- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/IncrementalAsyncContentAssistTests.java +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/contentassist/IncrementalAsyncContentAssistTests.java @@ -46,6 +46,7 @@ public void setup() { shell= new Shell(); shell.setSize(300, 300); shell.open(); + DisplayHelper.driveEventQueue(shell.getDisplay()); viewer= new SourceViewer(shell, null, SWT.NONE); Document document= new Document();