Skip to content

Commit 98ab0ad

Browse files
committed
Find/replace UI tests: unify focus validation
The find/replace UI tests currently validate for proper focus only in specific situations, usually restricted to when running on GTK. On the one hand, this makes debugging more difficult in case the focus was not properly set in the beginning, e.g., because the workbench window was not active when the test started. On the other hand, it makes the test execution more prone to be indeterministic and platform-specific, as GTK-specific code is involved. This change provides three improvements to mitigate these issues: - It ensures that the workbench window is active when test execution starts - It validates that the find/replace UI (overlay/dialog) has focus every time it is opened during test execution - It gets rid of GTK-specific focus validation
1 parent ef8188e commit 98ab0ad

File tree

5 files changed

+40
-25
lines changed

5 files changed

+40
-25
lines changed

tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceTestUtil.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.internal.findandreplace;
1515

16+
import static org.junit.Assert.fail;
17+
18+
import java.util.function.Supplier;
19+
1620
import org.eclipse.swt.widgets.Display;
1721

1822
import org.eclipse.ui.PlatformUI;
1923

24+
import org.eclipse.ui.workbench.texteditor.tests.ScreenshotTest;
25+
2026
public final class FindReplaceTestUtil {
2127

2228
private FindReplaceTestUtil() {
@@ -36,4 +42,22 @@ public static void runEventQueue() {
3642
}
3743
}
3844

45+
public static void waitForFocus(Supplier<Boolean> hasFocusValidator, String testName) {
46+
int focusAttempts= 0;
47+
while (!hasFocusValidator.get() && focusAttempts < 10) {
48+
focusAttempts++;
49+
PlatformUI.getWorkbench().getDisplay().readAndDispatch();
50+
if (!hasFocusValidator.get()) {
51+
try {
52+
Thread.sleep(50);
53+
} catch (InterruptedException e) {
54+
}
55+
}
56+
}
57+
if (!hasFocusValidator.get()) {
58+
String screenshotPath= ScreenshotTest.takeScreenshot(FindReplaceUITest.class, testName, System.out);
59+
fail("The find/replace UI did not receive focus. Screenshot: " + screenshotPath);
60+
}
61+
}
62+
3963
}

tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceUITest.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,27 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.internal.findandreplace;
1515

16-
import static org.eclipse.ui.internal.findandreplace.FindReplaceTestUtil.runEventQueue;
1716
import static org.hamcrest.MatcherAssert.assertThat;
1817
import static org.hamcrest.Matchers.is;
1918
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.fail;
2119

2220
import java.util.ResourceBundle;
2321

2422
import org.junit.After;
23+
import org.junit.Before;
2524
import org.junit.Rule;
2625
import org.junit.Test;
2726
import org.junit.rules.TestName;
2827

2928
import org.eclipse.swt.SWT;
3029

31-
import org.eclipse.jface.util.Util;
32-
3330
import org.eclipse.jface.text.Document;
3431
import org.eclipse.jface.text.IFindReplaceTarget;
3532
import org.eclipse.jface.text.TextSelection;
3633
import org.eclipse.jface.text.TextViewer;
3734

3835
import org.eclipse.ui.PlatformUI;
3936

40-
import org.eclipse.ui.workbench.texteditor.tests.ScreenshotTest;
41-
4237
import org.eclipse.ui.texteditor.FindReplaceAction;
4338

4439
public abstract class FindReplaceUITest<AccessType extends IFindReplaceUIAccess> {
@@ -51,6 +46,11 @@ public abstract class FindReplaceUITest<AccessType extends IFindReplaceUIAccess>
5146

5247
private AccessType dialog;
5348

49+
@Before
50+
public final void ensureWorkbenchWindowIsActive() {
51+
PlatformUI.getWorkbench().getWorkbenchWindows()[0].getShell().forceActive();
52+
}
53+
5454
protected FindReplaceAction getFindReplaceAction() {
5555
return findReplaceAction;
5656
}
@@ -78,16 +78,6 @@ protected void reopenFindReplaceUIForTextViewer() {
7878
dialog= openUIFromTextViewer(fTextViewer);
7979
}
8080

81-
protected final void ensureHasFocusOnGTK() {
82-
if (Util.isGtk()) {
83-
runEventQueue();
84-
if (!dialog.hasFocus()) {
85-
String screenshotPath= ScreenshotTest.takeScreenshot(FindReplaceUITest.class, testName.getMethodName(), System.out);
86-
fail("this test does not work on GTK unless the runtime workbench has focus. Screenshot: " + screenshotPath);
87-
}
88-
}
89-
}
90-
9181
protected abstract AccessType openUIFromTextViewer(TextViewer viewer);
9282

9383
@After
@@ -159,7 +149,6 @@ public void testShiftEnterReversesSearchDirection() {
159149

160150
dialog.select(SearchOptions.INCREMENTAL);
161151
dialog.setFindText("line");
162-
ensureHasFocusOnGTK();
163152
IFindReplaceTarget target= getFindReplaceTarget();
164153

165154
assertEquals(0, (target.getSelection()).x);

tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlayTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.internal.findandreplace.overlay;
1515

16+
import static org.eclipse.ui.internal.findandreplace.FindReplaceTestUtil.waitForFocus;
1617
import static org.hamcrest.MatcherAssert.assertThat;
1718
import static org.hamcrest.Matchers.is;
1819
import static org.junit.Assert.assertEquals;
@@ -46,7 +47,9 @@ public OverlayAccess openUIFromTextViewer(TextViewer viewer) {
4647
Accessor actionAccessor= new Accessor(getFindReplaceAction(), FindReplaceAction.class);
4748
actionAccessor.invoke("showOverlayInEditor", null);
4849
FindReplaceOverlay overlay= (FindReplaceOverlay) actionAccessor.get("overlay");
49-
return new OverlayAccess(getFindReplaceTarget(), overlay);
50+
OverlayAccess uiAccess= new OverlayAccess(getFindReplaceTarget(), overlay);
51+
waitForFocus(uiAccess::hasFocus, testName.getMethodName());
52+
return uiAccess;
5053
}
5154

5255
@Test

tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/overlay/OverlayAccess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private void restoreInitialConfiguration() {
102102
public void closeAndRestore() {
103103
restoreInitialConfiguration();
104104
assertInitialConfiguration();
105-
overlay.close();
105+
close();
106106
}
107107

108108
@Override

tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindReplaceDialogTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.ui.workbench.texteditor.tests;
1515

1616
import static org.eclipse.ui.internal.findandreplace.FindReplaceTestUtil.runEventQueue;
17+
import static org.eclipse.ui.internal.findandreplace.FindReplaceTestUtil.waitForFocus;
1718
import static org.hamcrest.MatcherAssert.assertThat;
1819
import static org.hamcrest.Matchers.is;
1920
import static org.junit.Assert.assertEquals;
@@ -52,7 +53,9 @@ public DialogAccess openUIFromTextViewer(TextViewer viewer) {
5253
Accessor fFindReplaceDialogStubAccessor= new Accessor(fFindReplaceDialogStub, "org.eclipse.ui.texteditor.FindReplaceAction$FindReplaceDialogStub", getClass().getClassLoader());
5354

5455
Dialog dialog= (Dialog) fFindReplaceDialogStubAccessor.invoke("getDialog", null);
55-
return new DialogAccess(getFindReplaceTarget(), dialog);
56+
DialogAccess uiAccess= new DialogAccess(getFindReplaceTarget(), dialog);
57+
waitForFocus(uiAccess::hasFocus, testName.getMethodName());
58+
return uiAccess;
5659
}
5760

5861
@Test
@@ -65,8 +68,6 @@ public void testFocusNotChangedWhenEnterPressed() {
6568
dialog.getFindCombo().setFocus();
6669
dialog.setFindText("line");
6770
dialog.simulateKeyboardInteractionInFindInputField(SWT.CR, false);
68-
ensureHasFocusOnGTK();
69-
7071
assertTrue(dialog.getFindCombo().isFocusControl());
7172

7273
Button wrapCheckBox= dialog.getButtonForSearchOption(SearchOptions.WRAP);
@@ -86,9 +87,8 @@ public void testFocusNotChangedWhenButtonMnemonicPressed() {
8687

8788
initializeTextViewerWithFindReplaceUI("");
8889
DialogAccess dialog= getDialog();
89-
9090
dialog.setFindText("line");
91-
ensureHasFocusOnGTK();
91+
runEventQueue();
9292

9393
Button wrapCheckBox= dialog.getButtonForSearchOption(SearchOptions.WRAP);
9494
wrapCheckBox.setFocus();
@@ -122,7 +122,6 @@ public void testShiftEnterReversesSearchDirectionDialogSpecific() {
122122
DialogAccess dialog= getDialog();
123123

124124
dialog.setFindText("line");
125-
ensureHasFocusOnGTK();
126125
IFindReplaceTarget target= getFindReplaceTarget();
127126

128127
dialog.simulateKeyboardInteractionInFindInputField(SWT.CR, false);

0 commit comments

Comments
 (0)