Skip to content

Commit 74a5090

Browse files
HeikoKlareclaude
andcommitted
Find/replace overlay: Add tests for select-all and empty-search guard
The overlay's select-all operation (multi-selection of all occurrences, storing the search term in history) and its refusal to replace while the search string is empty were not covered by any test, so regressions in these behaviors would go unnoticed. This adds according tests. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent eb77047 commit 74a5090

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import static org.eclipse.ui.internal.findandreplace.FindReplaceTestUtil.waitForFocus;
1717
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.hamcrest.Matchers.instanceOf;
1819
import static org.hamcrest.Matchers.is;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -29,7 +30,10 @@
2930

3031
import org.eclipse.text.tests.Accessor;
3132

33+
import org.eclipse.jface.viewers.ISelection;
34+
3235
import org.eclipse.jface.text.IFindReplaceTarget;
36+
import org.eclipse.jface.text.IMultiTextSelection;
3337
import org.eclipse.jface.text.TextViewer;
3438

3539
import org.eclipse.ui.internal.findandreplace.FindReplaceUITest;
@@ -227,6 +231,49 @@ public void testSearchTermStoredInHistoryAfterSearchBackward() {
227231
assertEquals("foo", dialog.getFindText());
228232
}
229233

234+
@Test
235+
public void testReplaceDoesNothingIfSearchStringIsEmpty() {
236+
// The overlay refuses replace operations as long as the search string is
237+
// empty; the document must remain unchanged.
238+
initializeTextViewerWithFindReplaceUI("text text");
239+
OverlayAccess dialog= getDialog();
240+
dialog.setReplaceText("replacement");
241+
242+
dialog.performReplace();
243+
assertThat(getTextViewer().getDocument().get(), is("text text"));
244+
245+
dialog.performReplaceAll();
246+
assertThat(getTextViewer().getDocument().get(), is("text text"));
247+
}
248+
249+
@Test
250+
public void testSelectAllSelectsAllOccurrences() {
251+
initializeTextViewerWithFindReplaceUI("foo bar foo bar foo");
252+
OverlayAccess dialog= getDialog();
253+
dialog.setFindText("foo");
254+
255+
dialog.pressSelectAll();
256+
257+
ISelection selection= getTextViewer().getSelection();
258+
assertThat(selection, is(instanceOf(IMultiTextSelection.class)));
259+
assertEquals(3, ((IMultiTextSelection) selection).getRegions().length);
260+
}
261+
262+
@Test
263+
public void testSearchTermStoredInHistoryAfterSelectAll() {
264+
// Selecting all occurrences must persist the search term to history just
265+
// like the other search operations.
266+
initializeTextViewerWithFindReplaceUI("foo bar foo");
267+
OverlayAccess dialog= getDialog();
268+
dialog.setFindText("foo");
269+
dialog.pressSelectAll();
270+
271+
dialog.setFindText("");
272+
dialog.simulateKeyboardInteractionInFindInputField(SWT.ARROW_DOWN, false);
273+
274+
assertEquals("foo", dialog.getFindText());
275+
}
276+
230277
@Test
231278
public void testSearchInSelectionButtonIsInverseOfGlobalOption() {
232279
// The searchInSelection button is the inverse of the GLOBAL option:

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class OverlayAccess implements IFindReplaceUIAccess {
5353

5454
private final ToolItem searchBackward;
5555

56+
private final ToolItem selectAll;
57+
5658
private final ToolItem openReplaceDialog;
5759

5860
private HistoryTextWrapper replace;
@@ -74,6 +76,7 @@ class OverlayAccess implements IFindReplaceUIAccess {
7476
inSelection= widgetExtractor.findToolItem("searchInSelection");
7577
searchForward= widgetExtractor.findToolItem("searchForward");
7678
searchBackward= widgetExtractor.findToolItem("searchBackward");
79+
selectAll= widgetExtractor.findToolItem("selectAll");
7780
openReplaceDialog= widgetExtractor.findToolItem("replaceToggle");
7881
extractReplaceWidgets();
7982
}
@@ -227,6 +230,10 @@ public void selectFindHistoryEntry(int index) {
227230
}
228231
}
229232

233+
public void pressSelectAll() {
234+
selectAll.notifyListeners(SWT.Selection, null);
235+
}
236+
230237
public void pressSearch(boolean forward) {
231238
if (forward) {
232239
searchForward.notifyListeners(SWT.Selection, null);

0 commit comments

Comments
 (0)