Skip to content

Commit

Permalink
Move specific FindReplaceDialog Tests into the separated test-class
Browse files Browse the repository at this point in the history
Some tests in FindReplaceUITest.java are not general to all
find/replace interfaces and are specific for the find/replace dialog.
This commit moves these tests into the dedicated test class and provides
the necessary interface in FindReplaceUITest
  • Loading branch information
Wittmaxi authored and HeikoKlare committed May 13, 2024
1 parent 8ba3829 commit deb75d4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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.assertTrue;
import static org.junit.Assume.assumeFalse;
Expand All @@ -37,7 +39,6 @@
import org.eclipse.ui.internal.findandreplace.SearchOptions;

public class FindReplaceDialogTest extends FindReplaceUITest<DialogAccess> {

@Override
public DialogAccess openUIFromTextViewer(TextViewer viewer) {
TextViewer textViewer= getTextViewer();
Expand Down Expand Up @@ -146,4 +147,87 @@ public void testShiftEnterReversesSearchDirectionDialogSpecific() {
dialog.simulateEnterInFindInputField(true);
assertEquals(5, (target.getSelection()).x);
}

@Test
public void testReplaceAndFindAfterInitializingFindWithSelectedString() {
openTextViewer("text text text");
getTextViewer().setSelectedRange(0, 4);
initializeFindReplaceUIForTextViewer();
DialogAccess dialog= getDialog();

assertThat(dialog.getFindText(), is("text"));

IFindReplaceTarget target= dialog.getTarget();
assertEquals(0, (target.getSelection()).x);
assertEquals(4, (target.getSelection()).y);

dialog.performReplaceAndFind();

assertEquals(" text text", getTextViewer().getDocument().get());
assertEquals(1, (target.getSelection()).x);
assertEquals(4, (target.getSelection()).y);
}

@Test
public void testIncrementalSearchOnlyEnabledWhenAllowed() {
initializeTextViewerWithFindReplaceUI("text text text");
DialogAccess dialog= getDialog();

dialog.select(SearchOptions.INCREMENTAL);
dialog.select(SearchOptions.REGEX);

dialog.assertSelected(SearchOptions.INCREMENTAL);
dialog.assertDisabled(SearchOptions.INCREMENTAL);
}

/*
* Test for https://github.com/eclipse-platform/eclipse.platform.ui/pull/1805#pullrequestreview-1993772378
*/
@Test
public void testIncrementalSearchOptionRecoveredCorrectly() {
initializeTextViewerWithFindReplaceUI("text text text");
DialogAccess dialog= getDialog();

dialog.select(SearchOptions.INCREMENTAL);
dialog.assertSelected(SearchOptions.INCREMENTAL);
dialog.assertEnabled(SearchOptions.INCREMENTAL);

reopenFindReplaceUIForTextViewer();
dialog= getDialog();
dialog.assertSelected(SearchOptions.INCREMENTAL);
dialog.assertEnabled(SearchOptions.INCREMENTAL);

dialog.select(SearchOptions.REGEX);
dialog.assertSelected(SearchOptions.INCREMENTAL);
dialog.assertDisabled(SearchOptions.INCREMENTAL);

reopenFindReplaceUIForTextViewer();
dialog= getDialog();
dialog.assertSelected(SearchOptions.INCREMENTAL);
dialog.assertDisabled(SearchOptions.INCREMENTAL);
}

@Test
public void testFindWithWholeWordEnabledWithMultipleWordsNotIncremental() {
initializeTextViewerWithFindReplaceUI("two words two");
DialogAccess dialog = getDialog();
dialog.setFindText("two");
dialog.select(SearchOptions.WHOLE_WORD);
dialog.select(SearchOptions.WRAP);
IFindReplaceTarget target= dialog.getTarget();

dialog.simulateEnterInFindInputField(false);
assertEquals(0, (target.getSelection()).x);
assertEquals(3, (target.getSelection()).y);

dialog.setFindText("two wo");
dialog.assertDisabled(SearchOptions.WHOLE_WORD);
dialog.assertSelected(SearchOptions.WHOLE_WORD);

dialog.simulateEnterInFindInputField(false);
assertThat(target.getSelectionText(), is(dialog.getFindText()));

assertEquals(0, (target.getSelection()).x);
assertEquals(dialog.getFindText().length(), (target.getSelection()).y);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ protected final void initializeTextViewerWithFindReplaceUI(String content) {
initializeFindReplaceUIForTextViewer();
}

private void openTextViewer(String content) {
protected void openTextViewer(String content) {
fTextViewer= new TextViewer(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
fTextViewer.setDocument(new Document(content));
fTextViewer.getControl().setFocus();
}

private void initializeFindReplaceUIForTextViewer() {
protected void initializeFindReplaceUIForTextViewer() {
dialog= openUIFromTextViewer(fTextViewer);
dialog.assertInitialConfiguration();
}

private void reopenFindReplaceUIForTextViewer() {
protected void reopenFindReplaceUIForTextViewer() {
dialog.close();
dialog= openUIFromTextViewer(fTextViewer);
}
Expand Down Expand Up @@ -189,45 +189,31 @@ public void testChangeInputForIncrementalSearch() {
@Test
public void testFindWithWholeWordEnabledWithMultipleWords() {
initializeTextViewerWithFindReplaceUI("two words two");
dialog.setFindText("two");
dialog.select(SearchOptions.INCREMENTAL);
dialog.select(SearchOptions.WHOLE_WORD);
dialog.select(SearchOptions.WRAP);
dialog.setFindText("two");
IFindReplaceTarget target= dialog.getTarget();
assertEquals(0, (target.getSelection()).x);
assertEquals(0, (target.getSelection()).y);
assertEquals(3, (target.getSelection()).y);

dialog.setFindText("two wo");
dialog.assertDisabled(SearchOptions.WHOLE_WORD);
dialog.assertSelected(SearchOptions.WHOLE_WORD);

dialog.simulateEnterInFindInputField(false);
assertThat(target.getSelectionText(), is(dialog.getFindText()));
assertEquals(0, (target.getSelection()).x);
assertEquals(dialog.getFindText().length(), (target.getSelection()).y);
}

@Test
public void testReplaceAndFindAfterInitializingFindWithSelectedString() {
openTextViewer("text text text");
fTextViewer.setSelectedRange(0, 4);
initializeFindReplaceUIForTextViewer();

IFindReplaceTarget target= dialog.getTarget();
assertEquals(0, (target.getSelection()).x);
assertEquals(4, (target.getSelection()).y);

dialog.performReplaceAndFind();

assertEquals(" text text", fTextViewer.getDocument().get());
assertEquals(1, (target.getSelection()).x);
assertEquals(4, (target.getSelection()).y);
assertEquals(dialog.getFindText().length(), (target.getSelection()).y);
}

@Test
public void testRegExSearch() {
initializeTextViewerWithFindReplaceUI("abc");
dialog.select(SearchOptions.REGEX);
dialog.setFindText("(a|bc)");

IFindReplaceTarget target= dialog.getTarget();
dialog.simulateEnterInFindInputField(false);
assertEquals(0, (target.getSelection()).x);
Expand Down Expand Up @@ -304,41 +290,6 @@ public void testActivateDialogWithSelectionActive() {
assertThat(fTextViewer.getDocument().get(), is("text" + System.lineSeparator() + System.lineSeparator()));
}

@Test
public void testIncrementalSearchOnlyEnabledWhenAllowed() {
initializeTextViewerWithFindReplaceUI("text text text");

dialog.select(SearchOptions.INCREMENTAL);
dialog.select(SearchOptions.REGEX);

dialog.assertSelected(SearchOptions.INCREMENTAL);
dialog.assertDisabled(SearchOptions.INCREMENTAL);
}

/*
* Test for https://github.com/eclipse-platform/eclipse.platform.ui/pull/1805#pullrequestreview-1993772378
*/
@Test
public void testIncrementalSearchOptionRecoveredCorrectly() {
initializeTextViewerWithFindReplaceUI("text text text");

dialog.select(SearchOptions.INCREMENTAL);
dialog.assertSelected(SearchOptions.INCREMENTAL);
dialog.assertEnabled(SearchOptions.INCREMENTAL);

reopenFindReplaceUIForTextViewer();
dialog.assertSelected(SearchOptions.INCREMENTAL);
dialog.assertEnabled(SearchOptions.INCREMENTAL);

dialog.select(SearchOptions.REGEX);
dialog.assertSelected(SearchOptions.INCREMENTAL);
dialog.assertDisabled(SearchOptions.INCREMENTAL);

reopenFindReplaceUIForTextViewer();
dialog.assertSelected(SearchOptions.INCREMENTAL);
dialog.assertDisabled(SearchOptions.INCREMENTAL);
}

protected AccessType getDialog() {
return dialog;
}
Expand Down

0 comments on commit deb75d4

Please sign in to comment.