Skip to content

Commit bd45ba0

Browse files
committed
Ensure focus after each opening of a find/replace UI
1 parent c9bd720 commit bd45ba0

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ResourceBundle;
2323

2424
import org.junit.After;
25+
import org.junit.Before;
2526
import org.junit.Rule;
2627
import org.junit.Test;
2728
import org.junit.rules.TestName;
@@ -51,6 +52,11 @@ public abstract class FindReplaceUITest<AccessType extends IFindReplaceUIAccess>
5152

5253
private AccessType dialog;
5354

55+
@Before
56+
public final void ensureWorkbenchWindowIsActive() {
57+
PlatformUI.getWorkbench().getWorkbenchWindows()[0].getShell().forceActive();
58+
}
59+
5460
protected FindReplaceAction getFindReplaceAction() {
5561
return findReplaceAction;
5662
}
@@ -159,7 +165,6 @@ public void testShiftEnterReversesSearchDirection() {
159165

160166
dialog.select(SearchOptions.INCREMENTAL);
161167
dialog.setFindText("line");
162-
ensureHasFocusOnGTK();
163168
IFindReplaceTarget target= getFindReplaceTarget();
164169

165170
assertEquals(0, (target.getSelection()).x);
@@ -308,7 +313,6 @@ public void testSearchTextSelectedWhenOpeningDialog() {
308313

309314
fTextViewer.setSelection(new TextSelection(0, 4));
310315
initializeFindReplaceUIForTextViewer();
311-
ensureHasFocusOnGTK();
312316

313317
assertEquals("test", dialog.getFindText());
314318
assertEquals(dialog.getSelectedFindText(), dialog.getFindText());
@@ -321,7 +325,6 @@ public void testSearchTextSelectedWhenSwitchingFocusToDialog() {
321325

322326
dialog.setFindText("text");
323327
initializeFindReplaceUIForTextViewer();
324-
ensureHasFocusOnGTK();
325328

326329
assertEquals("text", dialog.getFindText());
327330
assertEquals(dialog.getSelectedFindText(), dialog.getFindText());

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
@@ -103,7 +103,7 @@ private void restoreInitialConfiguration() {
103103
public void closeAndRestore() {
104104
restoreInitialConfiguration();
105105
assertInitialConfiguration();
106-
overlay.close();
106+
close();
107107
}
108108

109109
@Override

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

Lines changed: 4 additions & 1 deletion
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

0 commit comments

Comments
 (0)