Skip to content

Commit f44acf3

Browse files
committed
Fix faulty test name access in FindReplaceDialogTest utilities
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.
1 parent 51d6f45 commit f44acf3

File tree

5 files changed

+65
-40
lines changed

5 files changed

+65
-40
lines changed

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,18 @@
1010
*******************************************************************************/
1111
package org.eclipse.ui.workbench.texteditor.tests;
1212

13+
import static org.eclipse.ui.workbench.texteditor.tests.FindReplaceTestUtil.runEventQueue;
1314
import static org.hamcrest.MatcherAssert.assertThat;
1415
import static org.hamcrest.Matchers.hasItems;
1516
import static org.hamcrest.Matchers.not;
1617
import static org.junit.Assert.assertFalse;
1718
import static org.junit.Assert.assertTrue;
18-
import static org.junit.Assert.fail;
1919

2020
import java.util.Arrays;
2121
import java.util.Set;
2222
import java.util.function.Supplier;
2323
import java.util.stream.Collectors;
2424

25-
import org.junit.Rule;
26-
import org.junit.rules.TestName;
27-
2825
import org.eclipse.swt.SWT;
2926
import org.eclipse.swt.widgets.Button;
3027
import org.eclipse.swt.widgets.Combo;
@@ -33,8 +30,6 @@
3330

3431
import org.eclipse.text.tests.Accessor;
3532

36-
import org.eclipse.jface.util.Util;
37-
3833
import org.eclipse.jface.text.IFindReplaceTarget;
3934
import org.eclipse.jface.text.IFindReplaceTargetExtension;
4035

@@ -167,19 +162,9 @@ public void close() {
167162
closeOperation.run();
168163
}
169164

170-
@Rule
171-
public TestName name= new TestName();
172-
173165
@Override
174-
public void ensureHasFocusOnGTK() {
175-
if (Util.isGtk()) {
176-
// Ensure workbench has focus on GTK
177-
FindReplaceUITest.runEventQueue();
178-
if (shellRetriever.get() == null) {
179-
String screenshotPath= ScreenshotTest.takeScreenshot(FindReplaceUITest.class, name.getMethodName(), System.out);
180-
fail("this test does not work on GTK unless the runtime workbench has focus. Screenshot: " + screenshotPath);
181-
}
182-
}
166+
public Shell getActiveShell() {
167+
return shellRetriever.get();
183168
}
184169

185170
@Override
@@ -197,7 +182,7 @@ public void simulateKeyPressInFindInputField(int keyCode, boolean shiftPressed)
197182
event.stateMask= SWT.SHIFT;
198183
}
199184
findCombo.traverse(SWT.TRAVERSE_RETURN, event);
200-
FindReplaceUITest.runEventQueue();
185+
runEventQueue();
201186
}
202187

203188

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.workbench.texteditor.tests;
1515

16+
import static org.eclipse.ui.workbench.texteditor.tests.FindReplaceTestUtil.runEventQueue;
1617
import static org.junit.Assert.assertEquals;
1718
import static org.junit.Assert.assertTrue;
1819
import static org.junit.Assume.assumeFalse;
@@ -67,7 +68,7 @@ public void testFocusNotChangedWhenEnterPressed() {
6768
dialog.findCombo.setFocus();
6869
dialog.setFindText("line");
6970
dialog.simulateEnterInFindInputField(false);
70-
dialog.ensureHasFocusOnGTK();
71+
ensureHasFocusOnGTK();
7172

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

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

9293
dialog.setFindText("line");
93-
dialog.ensureHasFocusOnGTK();
94+
ensureHasFocusOnGTK();
9495

9596
Button wrapCheckBox= dialog.getButtonForSearchOption(SearchOptions.WRAP);
9697
wrapCheckBox.setFocus();
@@ -124,7 +125,7 @@ public void testShiftEnterReversesSearchDirectionDialogSpecific() {
124125
DialogAccess dialog= getDialog();
125126

126127
dialog.setFindText("line");
127-
dialog.ensureHasFocusOnGTK();
128+
ensureHasFocusOnGTK();
128129
IFindReplaceTarget target= dialog.getTarget();
129130

130131
dialog.simulateEnterInFindInputField(false);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Vector Informatik GmbH and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.ui.workbench.texteditor.tests;
15+
16+
import org.eclipse.swt.widgets.Display;
17+
18+
import org.eclipse.ui.PlatformUI;
19+
20+
public final class FindReplaceTestUtil {
21+
22+
private FindReplaceTestUtil() {
23+
}
24+
25+
public static void runEventQueue() {
26+
Display display= PlatformUI.getWorkbench().getDisplay();
27+
for (int i= 0; i < 10; i++) { // workaround for https://bugs.eclipse.org/323272
28+
while (display.readAndDispatch()) {
29+
// do nothing
30+
}
31+
try {
32+
Thread.sleep(50);
33+
} catch (InterruptedException e) {
34+
// do nothing
35+
}
36+
}
37+
}
38+
39+
}

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.workbench.texteditor.tests;
1515

16+
import static org.eclipse.ui.workbench.texteditor.tests.FindReplaceTestUtil.runEventQueue;
1617
import static org.hamcrest.MatcherAssert.assertThat;
1718
import static org.hamcrest.Matchers.is;
1819
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.fail;
1921

2022
import org.junit.After;
2123
import org.junit.Rule;
2224
import org.junit.Test;
2325
import org.junit.rules.TestName;
2426

2527
import org.eclipse.swt.SWT;
26-
import org.eclipse.swt.widgets.Display;
28+
29+
import org.eclipse.jface.util.Util;
2730

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

4043
private TextViewer fTextViewer;
4144

42-
static void runEventQueue() {
43-
Display display= PlatformUI.getWorkbench().getDisplay();
44-
for (int i= 0; i < 10; i++) { // workaround for https://bugs.eclipse.org/323272
45-
while (display.readAndDispatch()) {
46-
// do nothing
47-
}
48-
try {
49-
Thread.sleep(50);
50-
} catch (InterruptedException e) {
51-
// do nothing
52-
}
53-
}
54-
}
55-
5645
private AccessType dialog;
5746

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

68+
protected final void ensureHasFocusOnGTK() {
69+
if (Util.isGtk()) {
70+
runEventQueue();
71+
if (dialog.getActiveShell() == null) {
72+
String screenshotPath= ScreenshotTest.takeScreenshot(FindReplaceUITest.class, testName.getMethodName(), System.out);
73+
fail("this test does not work on GTK unless the runtime workbench has focus. Screenshot: " + screenshotPath);
74+
}
75+
}
76+
}
77+
7978
protected abstract AccessType openUIFromTextViewer(TextViewer viewer);
8079

8180
@After
@@ -148,7 +147,7 @@ public void testShiftEnterReversesSearchDirection() {
148147

149148
dialog.select(SearchOptions.INCREMENTAL);
150149
dialog.setFindText("line");
151-
dialog.ensureHasFocusOnGTK();
150+
ensureHasFocusOnGTK();
152151
IFindReplaceTarget target= dialog.getTarget();
153152

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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*******************************************************************************/
1111
package org.eclipse.ui.workbench.texteditor.tests;
1212

13+
import org.eclipse.swt.widgets.Shell;
1314
import org.eclipse.swt.widgets.Widget;
1415

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

2930
void close();
3031

31-
void ensureHasFocusOnGTK();
32-
3332
void unselect(SearchOptions option);
3433

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

4746
void setReplaceText(String text);
4847

48+
Shell getActiveShell();
49+
4950
Widget getButtonForSearchOption(SearchOptions option);
5051

5152
IFindReplaceLogic getFindReplaceLogic();

0 commit comments

Comments
 (0)