Skip to content

Commit 494178f

Browse files
committed
Improve error messages in CompletionTest #906
Provide more meaningful error message in case any of the test cases in CompletionTest fails. Particularly also explicitly validate whether the proposal list widget has been disposed concurrently. Contributes to #906
1 parent a858614 commit 494178f

File tree

1 file changed

+46
-40
lines changed
  • tests/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests

1 file changed

+46
-40
lines changed

tests/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,24 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.genericeditor.tests;
1515

16+
import static org.hamcrest.CoreMatchers.endsWith;
17+
import static org.hamcrest.MatcherAssert.assertThat;
1618
import static org.junit.Assert.assertEquals;
1719
import static org.junit.Assert.assertFalse;
1820
import static org.junit.Assert.assertNotNull;
1921
import static org.junit.Assert.assertNull;
2022
import static org.junit.Assert.assertTrue;
23+
import static org.junit.Assert.fail;
2124

2225
import java.util.ArrayList;
2326
import java.util.Arrays;
2427
import java.util.Collections;
2528
import java.util.Hashtable;
29+
import java.util.LinkedList;
2630
import java.util.List;
31+
import java.util.Queue;
2732
import java.util.Set;
33+
import java.util.function.BooleanSupplier;
2834
import java.util.stream.Collectors;
2935

3036
import org.junit.After;
@@ -119,14 +125,20 @@ public void testCompletionUsingViewerSelection() throws Exception {
119125
editor.selectAndReveal(0, 3);
120126
this.completionShell= openConentAssist();
121127
final Table completionProposalList = findCompletionSelectionControl(completionShell);
122-
assertTrue(new DisplayHelper() {
128+
waitForProposalRelatedCondition("Proposal list did not contain expected item: ABC", completionProposalList,
129+
() -> Arrays.stream(completionProposalList.getItems()).map(TableItem::getText).anyMatch("ABC"::equals), 5_000);
130+
}
131+
132+
private static void waitForProposalRelatedCondition(String errorMessage, Table completionProposalList, BooleanSupplier condition, int timeoutInMsec) {
133+
assertTrue(errorMessage, new DisplayHelper() {
123134
@Override
124135
protected boolean condition() {
125-
return Arrays.stream(completionProposalList.getItems()).map(TableItem::getText).anyMatch("ABC"::equals);
136+
assertFalse("Completion proposal list was unexpectedly disposed", completionProposalList.isDisposed());
137+
return condition.getAsBoolean();
126138
}
127-
}.waitForCondition(completionProposalList.getDisplay(), 200));
139+
}.waitForCondition(completionProposalList.getDisplay(), timeoutInMsec));
128140
}
129-
141+
130142
@Test
131143
public void testEnabledWhenCompletion() throws Exception {
132144
// Confirm that when disabled, a completion shell is present
@@ -165,29 +177,27 @@ private Shell openConentAssist(boolean expectShell) {
165177
*/
166178
private void checkCompletionContent(final Table completionProposalList) {
167179
// should be instantaneous, but happens to go asynchronous on CI so let's allow a wait
168-
assertTrue(new DisplayHelper() {
169-
@Override
170-
protected boolean condition() {
171-
return completionProposalList.getItemCount() == 2;
172-
}
173-
}.waitForCondition(completionProposalList.getDisplay(), 200));
180+
waitForProposalRelatedCondition("Proposal list did not show two initial items", completionProposalList,
181+
() -> completionProposalList.getItemCount() == 2, 200);
174182
assertTrue("Missing computing info entry", isComputingInfoEntry(completionProposalList.getItem(0)));
175-
TableItem completionProposalItem = completionProposalList.getItem(1);
176-
final ICompletionProposal selectedProposal = (ICompletionProposal)completionProposalItem.getData();
177-
assertTrue("Incorrect proposal content", BarContentAssistProcessor.PROPOSAL.endsWith(selectedProposal .getDisplayString()));
178-
completionProposalList.setSelection(completionProposalItem);
183+
assertTrue("Missing computing info entry in proposal list", isComputingInfoEntry(completionProposalList.getItem(0)));
184+
final TableItem initialProposalItem = completionProposalList.getItem(1);
185+
final String initialProposalString = ((ICompletionProposal)initialProposalItem.getData()).getDisplayString();
186+
assertThat("Unexpected initial proposal item",
187+
BarContentAssistProcessor.PROPOSAL, endsWith(initialProposalString));
188+
completionProposalList.setSelection(initialProposalItem);
179189
// asynchronous
180-
new DisplayHelper() {
181-
@Override
182-
protected boolean condition() {
183-
return !isComputingInfoEntry(completionProposalList.getItem(0)) && completionProposalList.getItemCount() == 2;
184-
}
185-
}.waitForCondition(completionProposalList.getDisplay(), LongRunningBarContentAssistProcessor.DELAY + 200);
186-
completionProposalItem = completionProposalList.getItem(0);
187-
assertTrue("Proposal content seems incorrect", BarContentAssistProcessor.PROPOSAL.endsWith(((ICompletionProposal)completionProposalItem.getData()).getDisplayString()));
188-
TableItem otherProposalItem = completionProposalList.getItem(1);
189-
assertTrue("Proposal content seems incorrect", LongRunningBarContentAssistProcessor.PROPOSAL.endsWith(((ICompletionProposal)otherProposalItem.getData()).getDisplayString()));
190-
assertEquals("Addition of completion proposal should keep selection", selectedProposal, completionProposalList.getSelection()[0].getData());
190+
waitForProposalRelatedCondition("Proposal list did not show two items after finishing computing", completionProposalList,
191+
() -> !isComputingInfoEntry(completionProposalList.getItem(0)) && completionProposalList.getItemCount() == 2,
192+
LongRunningBarContentAssistProcessor.DELAY + 200);
193+
final TableItem firstCompletionProposalItem = completionProposalList.getItem(0);
194+
final TableItem secondCompletionProposalItem = completionProposalList.getItem(1);
195+
String firstCompletionProposalText = ((ICompletionProposal)firstCompletionProposalItem.getData()).getDisplayString();
196+
String secondCOmpletionProposalText = ((ICompletionProposal)secondCompletionProposalItem.getData()).getDisplayString();
197+
assertThat("Unexpected first proposal item", BarContentAssistProcessor.PROPOSAL, endsWith(firstCompletionProposalText));
198+
assertThat("Unexpected second proposal item", LongRunningBarContentAssistProcessor.PROPOSAL, endsWith(secondCOmpletionProposalText));
199+
String selectedProposalString = ((ICompletionProposal)completionProposalList.getSelection()[0].getData()).getDisplayString();
200+
assertEquals("Addition of completion proposal should keep selection", initialProposalString, selectedProposalString);
191201
}
192202

193203
private static boolean isComputingInfoEntry(TableItem item) {
@@ -211,13 +221,8 @@ public void testCompletionFreeze_bug521484() throws Exception {
211221
this.completionShell=openConentAssist();
212222
final Table completionProposalList = findCompletionSelectionControl(this.completionShell);
213223
// should be instantaneous, but happens to go asynchronous on CI so let's allow a wait
214-
new DisplayHelper() {
215-
@Override
216-
protected boolean condition() {
217-
return completionProposalList.getItemCount() == 2;
218-
}
219-
}.waitForCondition(completionShell.getDisplay(), 200);
220-
assertEquals(2, completionProposalList.getItemCount());
224+
waitForProposalRelatedCondition("Proposal list did not show two items", completionProposalList,
225+
() -> completionProposalList.getItemCount() == 2, 200);
221226
assertTrue("Missing computing info entry", isComputingInfoEntry(completionProposalList.getItem(0)));
222227
// Some processors are long running, moving cursor can cause freeze (bug 521484)
223228
// asynchronous
@@ -251,16 +256,17 @@ private void emulatePressLeftArrowKey() {
251256
}
252257

253258
public static Table findCompletionSelectionControl(Widget control) {
254-
if (control instanceof Table) {
255-
return (Table)control;
256-
} else if (control instanceof Composite) {
257-
for (Widget child : ((Composite)control).getChildren()) {
258-
Table res = findCompletionSelectionControl(child);
259-
if (res != null) {
260-
return res;
261-
}
259+
Queue<Widget> widgetsToProcess = new LinkedList<>();
260+
widgetsToProcess.add(control);
261+
while (!widgetsToProcess.isEmpty()) {
262+
Widget child = widgetsToProcess.poll();
263+
if (child instanceof Table table) {
264+
return table;
265+
} else if (child instanceof Composite composite) {
266+
widgetsToProcess.addAll(Arrays.asList(composite.getChildren()));
262267
}
263268
}
269+
fail("No completion selection control found in widget: " + control);
264270
return null;
265271
}
266272

0 commit comments

Comments
 (0)