Skip to content

Commit

Permalink
try to fix AsyncContentAssistTest.testCompleteActivationChar() #35
Browse files Browse the repository at this point in the history
forceFocus() seem to be not reliable that the control receives the key
event.
  • Loading branch information
EcljpseB0T authored and jukzi committed Oct 4, 2022
1 parent 48ef40e commit 22d9da2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import org.junit.After;
Expand Down Expand Up @@ -153,39 +154,50 @@ public void testCompleteActivationChar() {
final Set<Shell> beforeShells= Arrays.stream(display.getShells()).filter(Shell::isVisible).collect(Collectors.toSet());
Event keyEvent= new Event();
Control control= viewer.getTextWidget();
display.timerExec(200, new Runnable() {
@Override
public void run() {
if (control.isDisposed()) {
// https://github.com/eclipse-platform/eclipse.platform.text/issues/75#issuecomment-1263429480
return; // do not fail other unit tests
}
control.forceFocus();
keyEvent.widget= control;
keyEvent.type= SWT.KeyDown;
keyEvent.character= 'b';
keyEvent.keyCode= 'b';
control.getDisplay().post(keyEvent);
keyEvent.type= SWT.KeyUp;
control.getDisplay().post(keyEvent);
DisplayHelper.driveEventQueue(control.getDisplay());
if (!document.get().startsWith("bb")) {
display.timerExec(200, this);
AtomicBoolean testEnded= new AtomicBoolean();
try {
display.timerExec(0, new Runnable() {
@Override
public void run() {
if (control.isDisposed() || testEnded.get()) {
// https://github.com/eclipse-platform/eclipse.platform.text/issues/75#issuecomment-1263429480
return; // do not fail other unit tests
}
control.getShell().forceActive();
if (!control.forceFocus()) {
display.timerExec(200, this);
System.out.println("no focus");
return;
}
keyEvent.widget= control;
keyEvent.type= SWT.KeyDown;
keyEvent.character= 'b';
keyEvent.keyCode= 'b';
control.getDisplay().post(keyEvent);
keyEvent.type= SWT.KeyUp;
control.getDisplay().post(keyEvent);
DisplayHelper.driveEventQueue(control.getDisplay());
if (!document.get().startsWith("bb")) {
System.out.println("character b not added to control");
display.timerExec(200, this);
}
}
}
});
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())));
});
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;
}
return false;
}
}.waitForCondition(display, 4000));
}.waitForCondition(display, 4000));
} finally {
testEnded.set(true);
}
}

private static Table findCompletionSelectionControl(Widget control) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
throw new RuntimeException();
throw new RuntimeException("Expected Exception for junit test");
}

@Override
Expand Down

0 comments on commit 22d9da2

Please sign in to comment.