Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find/replace UI tests: unify focus validation #2356

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
*******************************************************************************/
package org.eclipse.ui.internal.findandreplace;

import static org.junit.Assert.fail;

import java.util.function.Supplier;

import org.eclipse.swt.widgets.Display;

import org.eclipse.ui.PlatformUI;

import org.eclipse.ui.workbench.texteditor.tests.ScreenshotTest;

public final class FindReplaceTestUtil {

private FindReplaceTestUtil() {
Expand All @@ -36,4 +42,22 @@ public static void runEventQueue() {
}
}

public static void waitForFocus(Supplier<Boolean> hasFocusValidator, String testName) {
int focusAttempts= 0;
while (!hasFocusValidator.get() && focusAttempts < 10) {
focusAttempts++;
PlatformUI.getWorkbench().getDisplay().readAndDispatch();
if (!hasFocusValidator.get()) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
if (!hasFocusValidator.get()) {
String screenshotPath= ScreenshotTest.takeScreenshot(FindReplaceUITest.class, testName, System.out);
fail("The find/replace UI did not receive focus. Screenshot: " + screenshotPath);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,27 @@
*******************************************************************************/
package org.eclipse.ui.internal.findandreplace;

import static org.eclipse.ui.internal.findandreplace.FindReplaceTestUtil.runEventQueue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.ResourceBundle;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import org.eclipse.swt.SWT;

import org.eclipse.jface.util.Util;

import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IFindReplaceTarget;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.text.TextViewer;

import org.eclipse.ui.PlatformUI;

import org.eclipse.ui.workbench.texteditor.tests.ScreenshotTest;

import org.eclipse.ui.texteditor.FindReplaceAction;

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

private AccessType dialog;

@Before
public final void ensureWorkbenchWindowIsActive() {
PlatformUI.getWorkbench().getWorkbenchWindows()[0].getShell().forceActive();
}

protected FindReplaceAction getFindReplaceAction() {
return findReplaceAction;
}
Expand Down Expand Up @@ -78,16 +78,6 @@ protected void reopenFindReplaceUIForTextViewer() {
dialog= openUIFromTextViewer(fTextViewer);
}

protected final void ensureHasFocusOnGTK() {
if (Util.isGtk()) {
runEventQueue();
if (!dialog.hasFocus()) {
String screenshotPath= ScreenshotTest.takeScreenshot(FindReplaceUITest.class, testName.getMethodName(), System.out);
fail("this test does not work on GTK unless the runtime workbench has focus. Screenshot: " + screenshotPath);
}
}
}

protected abstract AccessType openUIFromTextViewer(TextViewer viewer);

@After
Expand Down Expand Up @@ -159,7 +149,6 @@ public void testShiftEnterReversesSearchDirection() {

dialog.select(SearchOptions.INCREMENTAL);
dialog.setFindText("line");
ensureHasFocusOnGTK();
IFindReplaceTarget target= getFindReplaceTarget();

assertEquals(0, (target.getSelection()).x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*******************************************************************************/
package org.eclipse.ui.internal.findandreplace.overlay;

import static org.eclipse.ui.internal.findandreplace.FindReplaceTestUtil.waitForFocus;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -46,7 +47,9 @@ public OverlayAccess openUIFromTextViewer(TextViewer viewer) {
Accessor actionAccessor= new Accessor(getFindReplaceAction(), FindReplaceAction.class);
actionAccessor.invoke("showOverlayInEditor", null);
FindReplaceOverlay overlay= (FindReplaceOverlay) actionAccessor.get("overlay");
return new OverlayAccess(getFindReplaceTarget(), overlay);
OverlayAccess uiAccess= new OverlayAccess(getFindReplaceTarget(), overlay);
waitForFocus(uiAccess::hasFocus, testName.getMethodName());
return uiAccess;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void restoreInitialConfiguration() {
public void closeAndRestore() {
restoreInitialConfiguration();
assertInitialConfiguration();
overlay.close();
close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.ui.workbench.texteditor.tests;

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

Dialog dialog= (Dialog) fFindReplaceDialogStubAccessor.invoke("getDialog", null);
return new DialogAccess(getFindReplaceTarget(), dialog);
DialogAccess uiAccess= new DialogAccess(getFindReplaceTarget(), dialog);
waitForFocus(uiAccess::hasFocus, testName.getMethodName());
return uiAccess;
}

@Test
Expand All @@ -65,8 +68,6 @@ public void testFocusNotChangedWhenEnterPressed() {
dialog.getFindCombo().setFocus();
dialog.setFindText("line");
dialog.simulateKeyboardInteractionInFindInputField(SWT.CR, false);
ensureHasFocusOnGTK();

assertTrue(dialog.getFindCombo().isFocusControl());

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

initializeTextViewerWithFindReplaceUI("");
DialogAccess dialog= getDialog();

dialog.setFindText("line");
ensureHasFocusOnGTK();
runEventQueue();

Button wrapCheckBox= dialog.getButtonForSearchOption(SearchOptions.WRAP);
wrapCheckBox.setFocus();
Expand Down Expand Up @@ -122,7 +122,6 @@ public void testShiftEnterReversesSearchDirectionDialogSpecific() {
DialogAccess dialog= getDialog();

dialog.setFindText("line");
ensureHasFocusOnGTK();
IFindReplaceTarget target= getFindReplaceTarget();

dialog.simulateKeyboardInteractionInFindInputField(SWT.CR, false);
Expand Down
Loading