Skip to content

Commit

Permalink
Fix faulty test name access in FindReplaceDialogTest utilities
Browse files Browse the repository at this point in the history
The DialogAccess class used for FindReplaceDialogTest currently tried to
access the test name via a test rule, but since it is not a test class,
that one is not assigned correctly.

This change moves the functionality depending on the test information to
an actual test class and also moves the runEventQueue() utility method
to a proper utility class.
  • Loading branch information
HeikoKlare committed Apr 29, 2024
1 parent 51d6f45 commit f44acf3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@
*******************************************************************************/
package org.eclipse.ui.workbench.texteditor.tests;

import static org.eclipse.ui.workbench.texteditor.tests.FindReplaceTestUtil.runEventQueue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.junit.Rule;
import org.junit.rules.TestName;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
Expand All @@ -33,8 +30,6 @@

import org.eclipse.text.tests.Accessor;

import org.eclipse.jface.util.Util;

import org.eclipse.jface.text.IFindReplaceTarget;
import org.eclipse.jface.text.IFindReplaceTargetExtension;

Expand Down Expand Up @@ -167,19 +162,9 @@ public void close() {
closeOperation.run();
}

@Rule
public TestName name= new TestName();

@Override
public void ensureHasFocusOnGTK() {
if (Util.isGtk()) {
// Ensure workbench has focus on GTK
FindReplaceUITest.runEventQueue();
if (shellRetriever.get() == null) {
String screenshotPath= ScreenshotTest.takeScreenshot(FindReplaceUITest.class, name.getMethodName(), System.out);
fail("this test does not work on GTK unless the runtime workbench has focus. Screenshot: " + screenshotPath);
}
}
public Shell getActiveShell() {
return shellRetriever.get();
}

@Override
Expand All @@ -197,7 +182,7 @@ public void simulateKeyPressInFindInputField(int keyCode, boolean shiftPressed)
event.stateMask= SWT.SHIFT;
}
findCombo.traverse(SWT.TRAVERSE_RETURN, event);
FindReplaceUITest.runEventQueue();
runEventQueue();
}


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

import static org.eclipse.ui.workbench.texteditor.tests.FindReplaceTestUtil.runEventQueue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
Expand Down Expand Up @@ -67,7 +68,7 @@ public void testFocusNotChangedWhenEnterPressed() {
dialog.findCombo.setFocus();
dialog.setFindText("line");
dialog.simulateEnterInFindInputField(false);
dialog.ensureHasFocusOnGTK();
ensureHasFocusOnGTK();

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

Expand All @@ -90,7 +91,7 @@ public void testFocusNotChangedWhenButtonMnemonicPressed() {
DialogAccess dialog= getDialog();

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

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

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

dialog.simulateEnterInFindInputField(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2024 Vector Informatik GmbH and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ui.workbench.texteditor.tests;

import org.eclipse.swt.widgets.Display;

import org.eclipse.ui.PlatformUI;

public final class FindReplaceTestUtil {

private FindReplaceTestUtil() {
}

public static void runEventQueue() {
Display display= PlatformUI.getWorkbench().getDisplay();
for (int i= 0; i < 10; i++) { // workaround for https://bugs.eclipse.org/323272
while (display.readAndDispatch()) {
// do nothing
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// do nothing
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
*******************************************************************************/
package org.eclipse.ui.workbench.texteditor.tests;

import static org.eclipse.ui.workbench.texteditor.tests.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 org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;

import org.eclipse.jface.util.Util;

import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IFindReplaceTarget;
Expand All @@ -39,20 +42,6 @@ public abstract class FindReplaceUITest<AccessType extends IFindReplaceUIAccess>

private TextViewer fTextViewer;

static void runEventQueue() {
Display display= PlatformUI.getWorkbench().getDisplay();
for (int i= 0; i < 10; i++) { // workaround for https://bugs.eclipse.org/323272
while (display.readAndDispatch()) {
// do nothing
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// do nothing
}
}
}

private AccessType dialog;

protected final void initializeTextViewerWithFindReplaceUI(String content) {
Expand All @@ -76,6 +65,16 @@ private void reopenFindReplaceUIForTextViewer() {
dialog= openUIFromTextViewer(fTextViewer);
}

protected final void ensureHasFocusOnGTK() {
if (Util.isGtk()) {
runEventQueue();
if (dialog.getActiveShell() == null) {
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 @@ -148,7 +147,7 @@ public void testShiftEnterReversesSearchDirection() {

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

assertEquals(0, (target.getSelection()).x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.ui.workbench.texteditor.tests;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Widget;

import org.eclipse.jface.text.IFindReplaceTarget;
Expand All @@ -28,8 +29,6 @@ interface IFindReplaceUIAccess {

void close();

void ensureHasFocusOnGTK();

void unselect(SearchOptions option);

void select(SearchOptions option);
Expand All @@ -46,6 +45,8 @@ interface IFindReplaceUIAccess {

void setReplaceText(String text);

Shell getActiveShell();

Widget getButtonForSearchOption(SearchOptions option);

IFindReplaceLogic getFindReplaceLogic();
Expand Down

0 comments on commit f44acf3

Please sign in to comment.