From 478b69f5c7832c074fe08e04703570bad653140b Mon Sep 17 00:00:00 2001 From: Joerg Kubitz Date: Fri, 26 May 2023 08:32:02 +0200 Subject: [PATCH] fix ContextInformationTest, AsyncContentAssistTest #35 Identify correct shell instead of taking random one. --- .../AbstractContentAssistTest.java | 22 ++++++++-------- .../contentassist/AsyncContentAssistTest.java | 26 +++++++------------ .../ContextInformationPresenterTest.java | 2 +- .../contentassist/ContextInformationTest.java | 20 +++++++------- 4 files changed, 32 insertions(+), 38 deletions(-) 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 a5aa006fd..038105fa5 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,6 +14,7 @@ package org.eclipse.jface.text.tests.contentassist; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.util.Arrays; @@ -204,36 +205,35 @@ public Button getButton() { } - protected void processEvents() { + protected static void processEvents() { DisplayHelper.driveEventQueue(getDisplay()); } - private Display getDisplay() { + private static Display getDisplay() { return Display.getDefault(); } - protected List getCurrentShells() { + protected static List getCurrentShells() { return Arrays.stream(getDisplay().getShells()) .filter(Shell::isVisible) .toList(); } - protected List findNewShells(Collection beforeShells) { + protected static List findNewShells(Collection beforeShells) { return Arrays.stream(getDisplay().getShells()) .filter(Shell::isVisible) .filter(s -> !beforeShells.contains(s)) .toList(); } - protected Shell findNewShell(Collection beforeShells) { - DisplayHelper.sleep(getDisplay(), 100); + protected static Shell findNewShell(Collection beforeShells) { List afterShells= findNewShells(beforeShells); - if (afterShells.isEmpty()) { - DisplayHelper.sleep(getDisplay(), 1000); + for (int attempt= 0; afterShells.size() != 1 && attempt < 10; attempt++) { + DisplayHelper.sleep(getDisplay(), 100); + afterShells= findNewShells(beforeShells); } - afterShells= findNewShells(beforeShells); - assertTrue("No new shell found, existing: " + beforeShells, afterShells.size() > beforeShells.size()); - return afterShells.get(afterShells.size() - 1); + assertEquals("Not unique new shell found", 1, afterShells.size()); + return afterShells.get(0); } } 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 f82d598ef..617d59601 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 @@ -19,9 +19,8 @@ import static org.junit.Assert.assertTrue; import java.util.Arrays; -import java.util.Set; +import java.util.Collection; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.stream.Collectors; import org.junit.After; import org.junit.Before; @@ -117,18 +116,14 @@ public void testCompletePrefix() { shell.open(); DisplayHelper.driveEventQueue(shell.getDisplay()); Display display = shell.getDisplay(); - final Set beforeShells = Arrays.stream(display.getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); + final Collection beforeShells= AbstractContentAssistTest.getCurrentShells(); contentAssistant.showPossibleCompletions(); + Shell newShell= AbstractContentAssistTest.findNewShell(beforeShells); assertTrue("Completion item not shown", new DisplayHelper() { @Override protected boolean condition() { - Set newShells = Arrays.stream(display.getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); - newShells.removeAll(beforeShells); - if (!newShells.isEmpty()) { - Table completionTable = findCompletionSelectionControl(newShells.iterator().next()); + Table completionTable= findCompletionSelectionControl(newShell); return Arrays.stream(completionTable.getItems()).map(TableItem::getText).anyMatch(item -> item.contains(BarContentAssistProcessor.PROPOSAL.substring(document.getLength()))); - } - return false; } }.waitForCondition(display, 2000)); } @@ -151,7 +146,6 @@ public void testCompleteActivationChar() { contentAssistant.install(viewer); shell.open(); Display display= shell.getDisplay(); - final Set beforeShells= Arrays.stream(display.getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); Event keyEvent= new Event(); Control control= viewer.getTextWidget(); AtomicBoolean testEnded= new AtomicBoolean(); @@ -183,16 +177,14 @@ public void run() { } } }); + final Collection beforeShells= AbstractContentAssistTest.getCurrentShells(); + AbstractContentAssistTest.processEvents(); + Shell newShell= AbstractContentAssistTest.findNewShell(beforeShells); assertTrue("Completion item not shown", new DisplayHelper() { @Override protected boolean condition() { - Set newShells= Arrays.stream(display.getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); - newShells.removeAll(beforeShells); - if (!newShells.isEmpty()) { - Table completionTable= findCompletionSelectionControl(newShells.iterator().next()); - return Arrays.stream(completionTable.getItems()).map(TableItem::getText).anyMatch(item -> item.contains(BarContentAssistProcessor.PROPOSAL.substring(document.getLength()))); - } - return false; + Table completionTable= findCompletionSelectionControl(newShell); + return Arrays.stream(completionTable.getItems()).map(TableItem::getText).anyMatch(item -> item.contains(BarContentAssistProcessor.PROPOSAL.substring(document.getLength()))); } }.waitForCondition(display, 4000)); } finally { 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 ecf95d72e..5f2ab4788 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,13 +87,13 @@ public IContextInformationValidator getContextInformationValidator() { @Test public void testContextInfo_withStyledTextPresentation() throws Exception { - final List beforeShells= getCurrentShells(); setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); postSourceViewerKeyEvent(SWT.ARROW_RIGHT, 0, SWT.KeyDown); selectAndReveal(4, 0); processEvents(); + final List beforeShells= getCurrentShells(); triggerContextInformation(); this.infoShell= findNewShell(beforeShells); assertEquals("idx= 0", getInfoText(this.infoShell)); 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 6830339a6..dcaaa4444 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,12 +49,12 @@ private ContentAssistant createBarContentAssist() { @Test public void testContextInfo() throws Exception { - final List beforeShells= getCurrentShells(); setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); selectAndReveal(4, 0); processEvents(); + final List beforeShells= getCurrentShells(); triggerContextInformation(); this.infoShell= findNewShell(beforeShells); assertEquals("idx= 0", getInfoText(this.infoShell)); @@ -69,12 +69,12 @@ public void testContextInfo() throws Exception { @Test public void testContextInfo_hide_Bug512251() throws Exception { - final List beforeShells= getCurrentShells(); setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); selectAndReveal(4, 0); processEvents(); + final List beforeShells= getCurrentShells(); triggerContextInformation(); this.infoShell= findNewShell(beforeShells); @@ -92,15 +92,14 @@ public void testContextInfo_hide_Bug512251() throws Exception { @Test public void testContextInfo_hide_focusOut() throws Exception { - assumeFalse("Test fails on Mac: Bug 558989", Platform.OS_MACOSX.equals(Platform.getOS())); - assumeFalse("Test fails on CentOS 8: See https://github.com/eclipse-platform/eclipse.platform.text/pull/162", Platform.OS_LINUX.equals(Platform.getOS())); - - final List beforeShells= getCurrentShells(); + // opens source viewer shell: setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); selectAndReveal(4, 0); processEvents(); + final List beforeShells= getCurrentShells(); + // opens content assist shell: triggerContextInformation(); this.infoShell= findNewShell(beforeShells); assertEquals("idx= 0", getInfoText(this.infoShell)); @@ -114,18 +113,21 @@ public void testContextInfo_hide_focusOut() throws Exception { // Hide all getButton().setFocus(); + // hides and disposes Shell (by org.eclipse.jface.text.contentassist.ContentAssistant.hide()): processEvents(); - assertTrue(this.infoShell.isDisposed() || !this.infoShell.isVisible()); + assumeFalse("Test fails on Mac: Bug 558989", Platform.OS_MACOSX.equals(Platform.getOS())); + assumeFalse("Test fails on CentOS 8: See https://github.com/eclipse-platform/eclipse.platform.text/pull/162", Platform.OS_LINUX.equals(Platform.getOS())); + assertTrue("Shell not disposed:" + this.infoShell, this.infoShell.isDisposed()); } @Test public void testContextInfo_hide_keyEsc() throws Exception { - final List beforeShells= getCurrentShells(); setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL); selectAndReveal(4, 0); processEvents(); + final List beforeShells= getCurrentShells(); triggerContextInformation(); this.infoShell= findNewShell(beforeShells); assertEquals("idx= 0", getInfoText(this.infoShell)); @@ -149,12 +151,12 @@ public void testContextInfo_hide_keyEsc() throws Exception { @Test public void testContextInfo_hide_validRange() throws Exception { - final List beforeShells= getCurrentShells(); setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL + '\n'); selectAndReveal(4, 0); processEvents(); + final List beforeShells= getCurrentShells(); triggerContextInformation(); this.infoShell= findNewShell(beforeShells); assertEquals("idx= 0", getInfoText(this.infoShell));