Skip to content

Commit

Permalink
Address "Device is disposed" error after test execution #50
Browse files Browse the repository at this point in the history
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 eclipse-platform/eclipse.platform.ui#50
  • Loading branch information
iloveeclipse committed May 6, 2022
1 parent db6a3b4 commit 7c47884
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 31 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.jface.text.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jface.text.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</parent>
<groupId>org.eclipse.jface</groupId>
<artifactId>org.eclipse.jface.text.tests</artifactId>
<version>3.12.400-SNAPSHOT</version>
<version>3.12.500-SNAPSHOT</version>
<packaging>eclipse-test-plugin</packaging>
<properties>
<testSuite>${project.artifactId}</testSuite>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -118,6 +125,30 @@ public void tearDown() {
fParent.dispose();
}

protected List<Shell> getCurrentShells() {
return Arrays.stream(fParent.getDisplay().getShells())
.filter(Shell::isVisible)
.collect(Collectors.toList());
}

protected List<Shell> findNewShells(Collection<Shell> beforeShells) {
return Arrays.stream(fParent.getDisplay().getShells())
.filter(Shell::isVisible)
.filter(shell -> !beforeShells.contains(shell))
.collect(Collectors.toList());
}

protected Shell findNewShell(Collection<Shell> beforeShells) {
DisplayHelper.sleep(fParent.getDisplay(), 100);
List<Shell> 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[] {
Expand Down Expand Up @@ -163,13 +194,15 @@ public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
return contentAssistant;
}
});
final List<Shell> beforeShells= getCurrentShells();
fViewer.setCodeMiningProviders(new ICodeMiningProvider[] {
new DelayedEchoCodeMiningProvider()
});
fViewer.getDocument().set("1a\n2a\n3a\n4a\n5a\n6a\n");
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);
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,13 +23,15 @@

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ST;
import org.eclipse.swt.custom.StyledText;
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;

Expand Down Expand Up @@ -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();
}
}


Expand All @@ -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() {
Expand Down Expand Up @@ -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<Shell> getCurrentShells() {
return Arrays.stream(shell.getDisplay().getShells())
return Arrays.stream(getDisplay().getShells())
.filter(Shell::isVisible)
.collect(Collectors.toList());
}

protected List<Shell> findNewShells(Collection<Shell> 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<Shell> beforeShells) {
DisplayHelper.sleep(shell.getDisplay(), 100);
DisplayHelper.sleep(getDisplay(), 100);
List<Shell> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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<Shell> beforeShells = Arrays.stream(display.getShells()).filter(Shell::isVisible).collect(Collectors.toSet());
contentAssistant.showPossibleCompletions();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ public IContextInformationValidator getContextInformationValidator() {

@Test
public void testContextInfo_withStyledTextPresentation() throws Exception {
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);

final List<Shell> beforeShells= getCurrentShells();
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);

postSourceViewerKeyEvent(SWT.ARROW_RIGHT, 0, SWT.KeyDown);
selectAndReveal(4, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ private ContentAssistant createBarContentAssist() {

@Test
public void testContextInfo() throws Exception {
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);

final List<Shell> beforeShells= getCurrentShells();
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);

selectAndReveal(4, 0);
processEvents();
Expand All @@ -70,9 +69,8 @@ public void testContextInfo() throws Exception {

@Test
public void testContextInfo_hide_Bug512251() throws Exception {
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);

final List<Shell> beforeShells= getCurrentShells();
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);

selectAndReveal(4, 0);
processEvents();
Expand All @@ -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<Shell> beforeShells= getCurrentShells();
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);

final List<Shell> beforeShells = getCurrentShells();

selectAndReveal(4, 0);
processEvents();

Expand All @@ -122,10 +119,9 @@ public void testContextInfo_hide_focusOut() throws Exception {

@Test
public void testContextInfo_hide_keyEsc() throws Exception {
final List<Shell> beforeShells= getCurrentShells();
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);

final List<Shell> beforeShells = getCurrentShells();

selectAndReveal(4, 0);
processEvents();

Expand All @@ -152,10 +148,9 @@ public void testContextInfo_hide_keyEsc() throws Exception {

@Test
public void testContextInfo_hide_validRange() throws Exception {
final List<Shell> beforeShells= getCurrentShells();
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL + '\n');

final List<Shell> beforeShells = getCurrentShells();

selectAndReveal(4, 0);
processEvents();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7c47884

Please sign in to comment.