Skip to content

Commit 1127427

Browse files
committed
Streamline genericeditor tests
* Use DisplayHelper from harness bundle not jface.text one. * Use Screenshot tool from o.e.test rather than org.eclipse.ui.workbench.texteditor.tests * Use lambda methods from it * Use pattern matching * Use static imports
1 parent 7118bc8 commit 1127427

File tree

8 files changed

+96
-148
lines changed

8 files changed

+96
-148
lines changed

tests/org.eclipse.ui.genericeditor.tests/META-INF/MANIFEST.MF

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
1717
org.eclipse.ui;bundle-version="3.108.0",
1818
org.eclipse.ui.workbench.texteditor;bundle-version="3.10.0",
1919
org.eclipse.ui.ide;bundle-version="3.11.0",
20-
org.eclipse.jface.text.tests;bundle-version="3.11.100",
2120
org.eclipse.text.tests;bundle-version="3.11.0",
22-
org.eclipse.ui.workbench.texteditor.tests;bundle-version="3.11.100",
2321
org.eclipse.ui.editors;bundle-version="3.11.0",
2422
org.eclipse.core.expressions,
25-
org.eclipse.ui.tests.harness;bundle-version="1.4.500"
23+
org.eclipse.ui.tests.harness;bundle-version="1.4.500",
24+
org.eclipse.test
2625
Bundle-RequiredExecutionEnvironment: JavaSE-17
2726
Eclipse-BundleShape: dir
2827
Bundle-ActivationPolicy: lazy

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016-2017 Red Hat Inc. and others
2+
* Copyright (c) 2016, 2025 Red Hat Inc. and others
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -68,10 +68,10 @@
6868
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
6969
import org.eclipse.jface.text.contentassist.IContextInformation;
7070
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
71-
import org.eclipse.jface.text.tests.util.DisplayHelper;
7271

7372
import org.eclipse.ui.genericeditor.tests.contributions.EnabledPropertyTester;
7473
import org.eclipse.ui.genericeditor.tests.contributions.LongRunningBarContentAssistProcessor;
74+
import org.eclipse.ui.tests.harness.util.DisplayHelper;
7575

7676
import org.eclipse.ui.texteditor.ContentAssistAction;
7777
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
@@ -101,7 +101,7 @@ public void testDefaultContentAssistBug570488() throws Exception {
101101
log.addLogListener(listener);
102102
createAndOpenFile("Bug570488.txt", "bar 'bar'");
103103
openConentAssist(false);
104-
DisplayHelper.driveEventQueue(Display.getCurrent());
104+
DisplayHelper.runEventLoop(Display.getCurrent(), 0);
105105
assertFalse("There are errors in the log", listener.messages.stream().anyMatch(s -> s.matches(IStatus.ERROR)));
106106
log.removeLogListener(listener);
107107
}
@@ -116,7 +116,7 @@ public void testCompletionService() throws Exception {
116116
MockContentAssistProcessor service= new MockContentAssistProcessor();
117117
ServiceRegistration<IContentAssistProcessor> registration= bundleContext.registerService(IContentAssistProcessor.class, service,
118118
new Hashtable<>(Collections.singletonMap("contentType", "org.eclipse.ui.genericeditor.tests.content-type")));
119-
DisplayHelper.driveEventQueue(Display.getCurrent());
119+
DisplayHelper.runEventLoop(Display.getCurrent(), 0);
120120
editor.selectAndReveal(3, 0);
121121
this.completionShell= openConentAssist();
122122
final Table completionProposalList= findCompletionSelectionControl(completionShell);

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2022 Avaloq Group AG (http://www.avaloq.com).
2+
* Copyright (c) 2022, 2025 Avaloq Group AG (http://www.avaloq.com).
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -12,6 +12,9 @@
1212
*******************************************************************************/
1313
package org.eclipse.ui.genericeditor.tests;
1414

15+
import static org.eclipse.ui.tests.harness.util.DisplayHelper.runEventLoop;
16+
import static org.eclipse.ui.tests.harness.util.DisplayHelper.waitForCondition;
17+
1518
import org.junit.Assert;
1619
import org.junit.Test;
1720

@@ -22,14 +25,13 @@
2225
import org.eclipse.swt.widgets.Event;
2326
import org.eclipse.swt.widgets.Listener;
2427

25-
import org.eclipse.jface.text.tests.util.DisplayHelper;
26-
2728
import org.eclipse.ui.genericeditor.tests.contributions.EnabledPropertyTester;
2829

2930
public class DoubleClickTest extends AbstratGenericEditorTest {
30-
private static final String EDITOR_TEXT= "one two three\n" +
31-
"four five six\n" +
32-
"seven eight nine";
31+
private static final String EDITOR_TEXT= """
32+
one two three
33+
four five six
34+
seven eight nine""";
3335

3436
@Override
3537
protected void createAndOpenFile() throws Exception {
@@ -54,13 +56,10 @@ public void testEnabledWhenDoubleClick() throws Exception {
5456
private void checkDoubleClickSelectionForCaretOffset(int pos, String expectedSelection) throws Exception {
5557
editor.selectAndReveal(pos, 0);
5658
final StyledText editorTextWidget= (StyledText) editor.getAdapter(Control.class);
57-
DisplayHelper.driveEventQueue(editorTextWidget.getDisplay());
58-
new DisplayHelper() {
59-
@Override
60-
protected boolean condition() {
61-
return editorTextWidget.isFocusControl() && editorTextWidget.getSelection().x == pos;
62-
}
63-
}.waitForCondition(editorTextWidget.getDisplay(), 3000);
59+
runEventLoop(editorTextWidget.getDisplay(), 0);
60+
waitForCondition(editorTextWidget.getDisplay(), 3000, ()->
61+
editorTextWidget.isFocusControl() && editorTextWidget.getSelection().x == pos
62+
);
6463
editorTextWidget.getShell().forceActive();
6564
editorTextWidget.getShell().setActive();
6665
editorTextWidget.getShell().setFocus();
@@ -72,7 +71,7 @@ protected boolean condition() {
7271

7372
private void doubleClick(StyledText widget, int x, int y) {
7473
widget.getDisplay().setCursorLocation(widget.toDisplay(x, y));
75-
DisplayHelper.driveEventQueue(widget.getDisplay());
74+
runEventLoop(widget.getDisplay(), 0);
7675

7776
Event mouseDownEvent= new Event();
7877
mouseDownEvent.button = 1;
@@ -104,6 +103,6 @@ private void postEvent(StyledText widget, Event event) {
104103
for (Listener listener : listeners) {
105104
listener.handleEvent(event);
106105
}
107-
DisplayHelper.driveEventQueue(widget.getDisplay());
106+
runEventLoop(widget.getDisplay(), 0);
108107
}
109108
}

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017, 2018 Red Hat Inc. and others.
2+
* Copyright (c) 2017, 2025 Red Hat Inc. and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -30,10 +30,10 @@
3030
import org.eclipse.jface.text.Position;
3131
import org.eclipse.jface.text.source.Annotation;
3232
import org.eclipse.jface.text.source.IAnnotationModel;
33-
import org.eclipse.jface.text.tests.util.DisplayHelper;
3433

3534
import org.eclipse.ui.genericeditor.tests.contributions.EnabledPropertyTester;
3635
import org.eclipse.ui.part.FileEditorInput;
36+
import org.eclipse.ui.tests.harness.util.DisplayHelper;
3737

3838
import org.eclipse.ui.texteditor.IDocumentProvider;
3939

@@ -134,7 +134,8 @@ private void checkHighlightForCaretOffset(int pos, String expectedHighlight, int
134134
clearAnnotations();
135135

136136
editor.selectAndReveal(pos, 0);
137-
waitForAnnotations(expectedHighlightCount);
137+
DisplayHelper.waitForCondition(Display.getDefault(), 2000,
138+
() -> getAnnotationsFromAnnotationModel().size() == expectedHighlightCount);
138139

139140
List<Annotation> annotations= getAnnotationsFromAnnotationModel();
140141

@@ -164,15 +165,6 @@ private IAnnotationModel getAnnotationModel() {
164165
return am;
165166
}
166167

167-
private void waitForAnnotations(int count) {
168-
new DisplayHelper() {
169-
@Override
170-
protected boolean condition() {
171-
return getAnnotationsFromAnnotationModel().size() == count;
172-
}
173-
}.waitForCondition(Display.getDefault(), 2000);
174-
}
175-
176168
private List<Annotation> getAnnotationsFromAnnotationModel() {
177169
List<Annotation> annotationList= new ArrayList<>();
178170
Iterator<Annotation> annotationIterator= getAnnotationModel().getAnnotationIterator();

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

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Map;
2424

2525
import org.junit.Assume;
26-
import org.junit.Before;
26+
import org.junit.BeforeClass;
2727
import org.junit.Rule;
2828
import org.junit.Test;
2929
import org.junit.rules.TestName;
@@ -37,7 +37,7 @@
3737
import org.eclipse.swt.widgets.Link;
3838
import org.eclipse.swt.widgets.Shell;
3939
import org.eclipse.swt.widgets.Text;
40-
40+
import org.eclipse.test.Screenshots;
4141
import org.eclipse.core.runtime.Platform;
4242

4343
import org.eclipse.core.resources.IMarker;
@@ -48,15 +48,13 @@
4848
import org.eclipse.jface.text.AbstractInformationControlManager;
4949
import org.eclipse.jface.text.ITextViewer;
5050
import org.eclipse.jface.text.TextViewer;
51-
import org.eclipse.jface.text.tests.util.DisplayHelper;
5251

5352
import org.eclipse.ui.genericeditor.tests.contributions.AlrightyHoverProvider;
5453
import org.eclipse.ui.genericeditor.tests.contributions.EnabledPropertyTester;
5554
import org.eclipse.ui.genericeditor.tests.contributions.HelloHoverProvider;
5655
import org.eclipse.ui.genericeditor.tests.contributions.MarkerResolutionGenerator;
5756
import org.eclipse.ui.genericeditor.tests.contributions.WorldHoverProvider;
58-
59-
import org.eclipse.ui.workbench.texteditor.tests.ScreenshotTest;
57+
import org.eclipse.ui.tests.harness.util.DisplayHelper;
6058

6159
/**
6260
* @since 1.0
@@ -68,8 +66,8 @@ public class HoverTest extends AbstratGenericEditorTest {
6866
@Rule
6967
public TestName testName= new TestName();
7068

71-
@Before
72-
public void skipOnNonLinux() {
69+
@BeforeClass
70+
public static void skipOnNonLinux() {
7371
Assume.assumeFalse("This test currently always fail on Windows (bug 505842), skipping", Platform.OS_WIN32.equals(Platform.getOS()));
7472
Assume.assumeFalse("This test currently always fail on macOS (bug 505842), skipping", Platform.OS_MACOSX.equals(Platform.getOS()));
7573
}
@@ -146,12 +144,7 @@ public void testProblemHover() throws Exception {
146144
event.type= SWT.Selection;
147145
link.notifyListeners(SWT.Selection, event);
148146
final IMarker m= marker;
149-
new DisplayHelper() {
150-
@Override
151-
protected boolean condition() {
152-
return !m.exists();
153-
}
154-
}.waitForCondition(event.display, 1000);
147+
DisplayHelper.waitForCondition(event.display, 1000, () -> !m.exists());
155148
assertFalse(marker.exists());
156149
} finally {
157150
if (marker != null && marker.exists()) {
@@ -162,28 +155,21 @@ protected boolean condition() {
162155

163156
private Shell getHoverShell(AbstractInformationControlManager manager, boolean failOnError) {
164157
AbstractInformationControl[] control= { null };
165-
new DisplayHelper() {
166-
@Override
167-
protected boolean condition() {
168-
control[0]= (AbstractInformationControl) new Accessor(manager, AbstractInformationControlManager.class).get("fInformationControl");
169-
return control[0] != null;
170-
}
171-
}.waitForCondition(this.editor.getSite().getShell().getDisplay(), 5000);
158+
DisplayHelper.waitForCondition(this.editor.getSite().getShell().getDisplay(), 5000, () -> {
159+
control[0] = (AbstractInformationControl) new Accessor(manager, AbstractInformationControlManager.class)
160+
.get("fInformationControl");
161+
return control[0] != null;
162+
});
172163
if (control[0] == null) {
173164
if (failOnError) {
174-
ScreenshotTest.takeScreenshot(getClass(), testName.getMethodName(), System.out);
165+
Screenshots.takeScreenshot(getClass(), testName.getMethodName());
175166
fail();
176167
} else {
177168
return null;
178169
}
179170
}
180171
Shell shell= (Shell) new Accessor(control[0], AbstractInformationControl.class).get("fShell");
181-
new DisplayHelper() {
182-
@Override
183-
protected boolean condition() {
184-
return shell.isVisible();
185-
}
186-
}.waitForCondition(this.editor.getSite().getShell().getDisplay(), 2000);
172+
DisplayHelper.waitForCondition(this.editor.getSite().getShell().getDisplay(), 2000, () -> shell.isVisible());
187173
if (failOnError) {
188174
assertTrue(shell.isVisible());
189175
}
@@ -198,20 +184,20 @@ private <T extends Control> T findControl(Control control, Class<T> controlType,
198184
return res;
199185
}
200186
String controlLabel= null;
201-
if (control instanceof Label) {
202-
controlLabel= ((Label) control).getText();
203-
} else if (control instanceof Link) {
204-
controlLabel= ((Link) control).getText();
205-
} else if (control instanceof Text) {
206-
controlLabel= ((Text) control).getText();
207-
} else if (control instanceof StyledText) {
208-
controlLabel= ((StyledText) control).getText();
187+
if (control instanceof Label l) {
188+
controlLabel= l.getText();
189+
} else if (control instanceof Link link) {
190+
controlLabel= link.getText();
191+
} else if (control instanceof Text text) {
192+
controlLabel= text.getText();
193+
} else if (control instanceof StyledText styled) {
194+
controlLabel= styled.getText();
209195
}
210196
if (controlLabel != null && controlLabel.contains(label)) {
211197
return res;
212198
}
213-
} else if (control instanceof Composite) {
214-
for (Control child : ((Composite) control).getChildren()) {
199+
} else if (control instanceof Composite comp) {
200+
for (Control child : comp.getChildren()) {
215201
T res= findControl(child, controlType, label);
216202
if (res != null) {
217203
return res;
@@ -238,12 +224,8 @@ private AbstractInformationControlManager triggerCompletionAndRetrieveInformatio
238224
editor.setFocus();
239225
this.editor.selectAndReveal(caretLocation, 0);
240226
final StyledText editorTextWidget= (StyledText) this.editor.getAdapter(Control.class);
241-
new DisplayHelper() {
242-
@Override
243-
protected boolean condition() {
244-
return editorTextWidget.isFocusControl() && editorTextWidget.getSelection().x == caretLocation;
245-
}
246-
}.waitForCondition(editorTextWidget.getDisplay(), 3000);
227+
DisplayHelper.waitForCondition(editorTextWidget.getDisplay(), 3000, ()->
228+
editorTextWidget.isFocusControl() && editorTextWidget.getSelection().x == caretLocation);
247229
assertTrue("editor does not have focus", editorTextWidget.isFocusControl());
248230
// sending event to trigger hover computation
249231
Event hoverEvent= new Event();
@@ -256,12 +238,8 @@ protected boolean condition() {
256238
editorTextWidget.getDisplay().setCursorLocation(editorTextWidget.toDisplay(hoverEvent.x, hoverEvent.y));
257239
editorTextWidget.notifyListeners(SWT.MouseHover, hoverEvent);
258240
// retrieving hover content
259-
foundHoverData = new DisplayHelper() {
260-
@Override
261-
protected boolean condition() {
262-
return getHoverData(textHoverManager) != null;
263-
}
264-
}.waitForCondition(hoverEvent.display, 6000);
241+
foundHoverData = DisplayHelper.waitForCondition(hoverEvent.display, 6000,
242+
() -> getHoverData(textHoverManager) != null);
265243
}
266244
assertTrue("hover data not found", foundHoverData);
267245
return textHoverManager;

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017 Red Hat Inc. and others.
2+
* Copyright (c) 2017, 2025 Red Hat Inc. and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -13,9 +13,10 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.genericeditor.tests;
1515

16+
import static org.junit.Assert.assertTrue;
17+
1618
import java.io.ByteArrayInputStream;
1719

18-
import org.junit.Assert;
1920
import org.junit.Test;
2021

2122
import org.eclipse.core.runtime.NullProgressMonitor;
@@ -26,14 +27,14 @@
2627

2728
import org.eclipse.jface.text.BadLocationException;
2829
import org.eclipse.jface.text.IDocument;
29-
import org.eclipse.jface.text.tests.util.DisplayHelper;
3030

3131
import org.eclipse.ui.PlatformUI;
3232
import org.eclipse.ui.genericeditor.tests.contributions.EnabledPropertyTester;
3333
import org.eclipse.ui.genericeditor.tests.contributions.ReconcilerStrategyFirst;
3434
import org.eclipse.ui.genericeditor.tests.contributions.ReconcilerStrategySecond;
3535
import org.eclipse.ui.internal.genericeditor.ExtensionBasedTextEditor;
3636
import org.eclipse.ui.part.FileEditorInput;
37+
import org.eclipse.ui.tests.harness.util.DisplayHelper;
3738
import org.eclipse.ui.tests.harness.util.UITestCase;
3839

3940
import org.eclipse.ui.texteditor.IDocumentProvider;
@@ -88,17 +89,14 @@ private void performTestOnEditor(String startingText, ExtensionBasedTextEditor t
8889

8990
doc.set(startingText);
9091

91-
new DisplayHelper() {
92-
@Override
93-
protected boolean condition() {
94-
try {
95-
return doc.get(0, doc.getLineLength(0)).contains(expectedText);
96-
} catch (BadLocationException e) {
97-
return false;
98-
}
92+
DisplayHelper.waitForCondition(window.getShell().getDisplay(), 2000, () -> {
93+
try {
94+
return doc.get(0, doc.getLineLength(0)).contains(expectedText);
95+
} catch (BadLocationException e) {
96+
return false;
9997
}
100-
}.waitForCondition(window.getShell().getDisplay(), 2000);
101-
Assert.assertTrue("file was not affected by reconciler", doc.get().contains(expectedText));
98+
});
99+
assertTrue("file was not affected by reconciler", doc.get().contains(expectedText));
102100
}
103101

104102
@Override

0 commit comments

Comments
 (0)