From 8fa5550696c6b0d5dfd519935e8631336f2e3f2c Mon Sep 17 00:00:00 2001 From: Joerg Kubitz Date: Fri, 26 May 2023 08:32:02 +0200 Subject: [PATCH] fix ContextInformationTest, AsyncContentAssistTest, CompletionTest #35 Identify correct shell instead of taking random one. --- .../AbstractContentAssistTest.java | 22 +++---- .../contentassist/AsyncContentAssistTest.java | 26 +++------ .../ContextInformationPresenterTest.java | 2 +- .../contentassist/ContextInformationTest.java | 20 ++++--- .../genericeditor/tests/CompletionTest.java | 57 ++++++++----------- .../genericeditor/tests/TestQuickAssist.java | 17 +++--- 6 files changed, 64 insertions(+), 80 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)); diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java index dcf4a7d27..e397484e6 100644 --- a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java @@ -16,6 +16,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.ArrayList; @@ -74,10 +75,8 @@ public class CompletionTest extends AbstratGenericEditorTest { @Test public void testCompletion() throws Exception { - final Set beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); editor.selectAndReveal(3, 0); - openConentAssist(); - this.completionShell= findNewShell(beforeShells, editor.getSite().getShell().getDisplay()); + this.completionShell= openConentAssist(); final Table completionProposalList = findCompletionSelectionControl(completionShell); checkCompletionContent(completionProposalList); // TODO find a way to actually trigger completion and verify result against Editor content @@ -90,7 +89,7 @@ public void testDefaultContentAssistBug570488() throws Exception { TestLogListener listener= new TestLogListener(); log.addLogListener(listener); createAndOpenFile("Bug570488.txt", "bar 'bar'"); - openConentAssist(); + openConentAssist(false); DisplayHelper.driveEventQueue(Display.getCurrent()); assertFalse("There are errors in the log", listener.messages.stream().anyMatch(s -> s.matches(IStatus.ERROR))); log.removeLogListener(listener); @@ -106,10 +105,8 @@ public void testCompletionService() throws Exception { ServiceRegistration registration= bundleContext.registerService(IContentAssistProcessor.class, service, new Hashtable<>(Collections.singletonMap("contentType", "org.eclipse.ui.genericeditor.tests.content-type"))); DisplayHelper.driveEventQueue(Display.getCurrent()); - final Set beforeShells= Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); editor.selectAndReveal(3, 0); - openConentAssist(); - this.completionShell= findNewShell(beforeShells, editor.getSite().getShell().getDisplay()); + this.completionShell= openConentAssist(); final Table completionProposalList= findCompletionSelectionControl(completionShell); checkCompletionContent(completionProposalList); assertTrue("Service was not called!", service.called); @@ -118,11 +115,9 @@ public void testCompletionService() throws Exception { @Test public void testCompletionUsingViewerSelection() throws Exception { - final Set beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); editor.getDocumentProvider().getDocument(editor.getEditorInput()).set("abc"); editor.selectAndReveal(0, 3); - openConentAssist(); - this.completionShell= findNewShell(beforeShells, editor.getSite().getShell().getDisplay()); + this.completionShell= openConentAssist(); final Table completionProposalList = findCompletionSelectionControl(completionShell); assertTrue(new DisplayHelper() { @Override @@ -137,30 +132,28 @@ public void testEnabledWhenCompletion() throws Exception { // Confirm that when disabled, a completion shell is present EnabledPropertyTester.setEnabled(false); createAndOpenFile("enabledWhen.txt", "bar 'bar'"); - final Set beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); editor.selectAndReveal(3, 0); - openConentAssist(); - Shell[] afterShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()) - .filter(Shell::isVisible) - .filter(shell -> !beforeShells.contains(shell)) - .toArray(Shell[]::new); - assertEquals("A new shell was found", 0, afterShells.length); + assertNull("A new shell was found", openConentAssist(false)); cleanFileAndEditor(); // Confirm that when enabled, a completion shell is present EnabledPropertyTester.setEnabled(true); createAndOpenFile("enabledWhen.txt", "bar 'bar'"); - final Set beforeEnabledShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); - editor.selectAndReveal(3, 0); - openConentAssist(); - assertNotNull(findNewShell(beforeEnabledShells, editor.getSite().getShell().getDisplay())); + editor.selectAndReveal(3, 0); + assertNotNull(openConentAssist()); } - private void openConentAssist() { + private Shell openConentAssist() { + return openConentAssist(true); + } + private Shell openConentAssist(boolean expectShell) { ContentAssistAction action = (ContentAssistAction) editor.getAction(ITextEditorActionConstants.CONTENT_ASSIST); action.update(); - action.run(); - waitAndDispatch(100); + final Set beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); + action.run(); //opens shell + Shell shell= findNewShell(beforeShells, editor.getSite().getShell().getDisplay(),expectShell); + waitAndDispatch(100); // can dispose shell when focus lost during debugging + return shell; } /** @@ -198,21 +191,21 @@ protected boolean condition() { assertEquals("Addition of completion proposal should keep selection", selectedProposal, completionProposalList.getSelection()[0].getData()); } - public static Shell findNewShell(Set beforeShells, Display display) { + public static Shell findNewShell(Set beforeShells, Display display, boolean expectShell) { Shell[] afterShells = Arrays.stream(display.getShells()) .filter(Shell::isVisible) .filter(shell -> !beforeShells.contains(shell)) .toArray(Shell[]::new); - assertEquals("No new shell found", 1, afterShells.length); - return afterShells[0]; + if (expectShell) { + assertEquals("No new shell found", 1, afterShells.length); + } + return afterShells.length > 0 ? afterShells[0] : null; } @Test public void testCompletionFreeze_bug521484() throws Exception { - final Set beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); editor.selectAndReveal(3, 0); - openConentAssist(); - this.completionShell= findNewShell(beforeShells, editor.getSite().getShell().getDisplay()); + this.completionShell=openConentAssist(); final Table completionProposalList = findCompletionSelectionControl(this.completionShell); // should be instantaneous, but happens to go asynchronous on CI so let's allow a wait new DisplayHelper() { @@ -235,11 +228,11 @@ protected boolean condition() { @Test public void testMoveCaretBackUsesAllProcessors_bug522255() throws Exception { - final Set beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); testCompletion(); emulatePressLeftArrowKey(); + final Set beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); DisplayHelper.sleep(editor.getSite().getShell().getDisplay(), LongRunningBarContentAssistProcessor.DELAY + 500); // adding delay is a workaround for bug521484, use only 100ms without the bug - this.completionShell= findNewShell(beforeShells, editor.getSite().getShell().getDisplay()); + this.completionShell= findNewShell(beforeShells, editor.getSite().getShell().getDisplay(), true); final Table completionProposalList = findCompletionSelectionControl(this.completionShell); assertEquals("Missing proposals from a Processor", 2, completionProposalList.getItemCount()); // replace with line below when #5214894 is done // checkCompletionContent(completionProposalList); // use this instead of assert above when #521484 is done diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/TestQuickAssist.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/TestQuickAssist.java index e0a1ee227..179ff4755 100644 --- a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/TestQuickAssist.java +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/TestQuickAssist.java @@ -50,16 +50,13 @@ public class TestQuickAssist extends AbstratGenericEditorTest { @Test public void testCompletion() throws Exception { - final Set beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); - openQuickAssist(); - this.completionShell= CompletionTest.findNewShell(beforeShells, editor.getSite().getShell().getDisplay()); + this.completionShell=openQuickAssist(); final Table completionProposalList = CompletionTest.findCompletionSelectionControl(completionShell); checkCompletionContent(completionProposalList, new String[] { DEFAULT_PROPOSAL }); } @Test public void testMarkerQuickAssist() throws Exception { - final Set beforeShells= Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); DisplayHelper.driveEventQueue(Display.getDefault()); IMarker marker= null; try { @@ -70,8 +67,7 @@ public void testMarkerQuickAssist() throws Exception { marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); marker.setAttribute(IMarker.MESSAGE, "We have a problem"); marker.setAttribute(MarkerResolutionGenerator.FIXME, true); - openQuickAssist(); - this.completionShell= CompletionTest.findNewShell(beforeShells, editor.getSite().getShell().getDisplay()); + this.completionShell= openQuickAssist(); final Table completionProposalList= CompletionTest.findCompletionSelectionControl(completionShell); checkCompletionContent(completionProposalList, new String[] { DEFAULT_PROPOSAL, FIXME_PROPOSAL }); } finally { @@ -83,7 +79,6 @@ public void testMarkerQuickAssist() throws Exception { @Test public void testMarkerQuickAssistLineOnly() throws Exception { - final Set beforeShells= Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); DisplayHelper.driveEventQueue(Display.getDefault()); IMarker marker= null; try { @@ -92,8 +87,7 @@ public void testMarkerQuickAssistLineOnly() throws Exception { marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); marker.setAttribute(IMarker.MESSAGE, "We have a problem"); marker.setAttribute(MarkerResolutionGenerator.FIXME, true); - openQuickAssist(); - this.completionShell= CompletionTest.findNewShell(beforeShells, editor.getSite().getShell().getDisplay()); + this.completionShell= openQuickAssist(); final Table completionProposalList= CompletionTest.findCompletionSelectionControl(completionShell); checkCompletionContent(completionProposalList, new String[] { DEFAULT_PROPOSAL, FIXME_PROPOSAL }); } finally { @@ -103,12 +97,15 @@ public void testMarkerQuickAssistLineOnly() throws Exception { } } - private void openQuickAssist() { + private Shell openQuickAssist() { editor.selectAndReveal(3, 0); TextOperationAction action = (TextOperationAction) editor.getAction(ITextEditorActionConstants.QUICK_ASSIST); action.update(); + final Set beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); action.run(); + Shell shell= CompletionTest.findNewShell(beforeShells, editor.getSite().getShell().getDisplay(), true); waitAndDispatch(100); + return shell; } /**