Skip to content

Commit

Permalink
fix ContextInformationTest, AsyncContentAssistTest, CompletionTest #35
Browse files Browse the repository at this point in the history
Identify correct shell instead of taking random one.
  • Loading branch information
EcljpseB0T committed May 26, 2023
1 parent e6757c2 commit f6655f7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Shell> getCurrentShells() {
protected static List<Shell> getCurrentShells() {
return Arrays.stream(getDisplay().getShells())
.filter(Shell::isVisible)
.toList();
}

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -117,18 +116,14 @@ public void testCompletePrefix() {
shell.open();
DisplayHelper.driveEventQueue(shell.getDisplay());
Display display = shell.getDisplay();
final Set<Shell> beforeShells = Arrays.stream(display.getShells()).filter(Shell::isVisible).collect(Collectors.toSet());
final Collection<Shell> beforeShells= AbstractContentAssistTest.getCurrentShells();
contentAssistant.showPossibleCompletions();
Shell newShell= AbstractContentAssistTest.findNewShell(beforeShells);
assertTrue("Completion item not shown", new DisplayHelper() {
@Override
protected boolean condition() {
Set<Shell> 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));
}
Expand All @@ -151,7 +146,6 @@ public void testCompleteActivationChar() {
contentAssistant.install(viewer);
shell.open();
Display display= shell.getDisplay();
final Set<Shell> beforeShells= Arrays.stream(display.getShells()).filter(Shell::isVisible).collect(Collectors.toSet());
Event keyEvent= new Event();
Control control= viewer.getTextWidget();
AtomicBoolean testEnded= new AtomicBoolean();
Expand Down Expand Up @@ -183,16 +177,14 @@ public void run() {
}
}
});
final Collection<Shell> beforeShells= AbstractContentAssistTest.getCurrentShells();
AbstractContentAssistTest.processEvents();
Shell newShell= AbstractContentAssistTest.findNewShell(beforeShells);
assertTrue("Completion item not shown", new DisplayHelper() {
@Override
protected boolean condition() {
Set<Shell> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public IContextInformationValidator getContextInformationValidator() {

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

postSourceViewerKeyEvent(SWT.ARROW_RIGHT, 0, SWT.KeyDown);
selectAndReveal(4, 0);
processEvents();

final List<Shell> beforeShells= getCurrentShells();
triggerContextInformation();
this.infoShell= findNewShell(beforeShells);
assertEquals("idx= 0", getInfoText(this.infoShell));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ private ContentAssistant createBarContentAssist() {

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

selectAndReveal(4, 0);
processEvents();

final List<Shell> beforeShells= getCurrentShells();
triggerContextInformation();
this.infoShell= findNewShell(beforeShells);
assertEquals("idx= 0", getInfoText(this.infoShell));
Expand All @@ -69,12 +69,12 @@ public void testContextInfo() throws Exception {

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

selectAndReveal(4, 0);
processEvents();

final List<Shell> beforeShells= getCurrentShells();
triggerContextInformation();
this.infoShell= findNewShell(beforeShells);

Expand All @@ -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<Shell> beforeShells= getCurrentShells();
// opens source viewer shell:
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);

selectAndReveal(4, 0);
processEvents();

final List<Shell> beforeShells= getCurrentShells();
// opens content assist shell:
triggerContextInformation();
this.infoShell= findNewShell(beforeShells);
assertEquals("idx= 0", getInfoText(this.infoShell));
Expand All @@ -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<Shell> beforeShells= getCurrentShells();
setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);

selectAndReveal(4, 0);
processEvents();

final List<Shell> beforeShells= getCurrentShells();
triggerContextInformation();
this.infoShell= findNewShell(beforeShells);
assertEquals("idx= 0", getInfoText(this.infoShell));
Expand All @@ -149,12 +151,12 @@ 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');

selectAndReveal(4, 0);
processEvents();

final List<Shell> beforeShells= getCurrentShells();
triggerContextInformation();
this.infoShell= findNewShell(beforeShells);
assertEquals("idx= 0", getInfoText(this.infoShell));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,10 +75,8 @@ public class CompletionTest extends AbstratGenericEditorTest {

@Test
public void testCompletion() throws Exception {
final Set<Shell> 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
Expand All @@ -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);
Expand All @@ -106,10 +105,8 @@ public void testCompletionService() throws Exception {
ServiceRegistration<IContentAssistProcessor> registration= bundleContext.registerService(IContentAssistProcessor.class, service,
new Hashtable<>(Collections.singletonMap("contentType", "org.eclipse.ui.genericeditor.tests.content-type")));
DisplayHelper.driveEventQueue(Display.getCurrent());
final Set<Shell> 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);
Expand All @@ -118,11 +115,9 @@ public void testCompletionService() throws Exception {

@Test
public void testCompletionUsingViewerSelection() throws Exception {
final Set<Shell> 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
Expand All @@ -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<Shell> 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<Shell> 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<Shell> 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;
}

/**
Expand Down Expand Up @@ -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<Shell> beforeShells, Display display) {
public static Shell findNewShell(Set<Shell> 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<Shell> 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() {
Expand All @@ -235,11 +228,11 @@ protected boolean condition() {

@Test
public void testMoveCaretBackUsesAllProcessors_bug522255() throws Exception {
final Set<Shell> beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet());
testCompletion();
emulatePressLeftArrowKey();
final Set<Shell> 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
Expand Down
Loading

0 comments on commit f6655f7

Please sign in to comment.