Skip to content

Commit acf9958

Browse files
Maximilian WittmerMaximilian Wittmer
authored andcommitted
[FindNextAction] Introduce unit tests
Introduces unit tests for the FindNextAction.
1 parent 7587ea1 commit acf9958

File tree

3 files changed

+255
-1
lines changed

3 files changed

+255
-1
lines changed

tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/EditorsTestSuite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
ZoomTest.class,
4242
FileDocumentProviderTest.class,
4343
TextFileDocumentProviderTest.class,
44+
FindNextActionTest.class,
4445
StatusEditorTest.class,
4546
TextNavigationTest.class,
4647
LargeFileTest.class, CaseActionTest.class,
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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+
* Vector Informatik GmbH - initial API and implementation
13+
*******************************************************************************/
14+
15+
package org.eclipse.ui.editors.tests;
16+
17+
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.hamcrest.Matchers.is;
19+
import static org.junit.Assert.fail;
20+
21+
import java.util.ResourceBundle;
22+
23+
import org.junit.After;
24+
import org.junit.Test;
25+
import org.osgi.framework.FrameworkUtil;
26+
27+
import org.eclipse.core.runtime.CoreException;
28+
29+
import org.eclipse.core.resources.IFile;
30+
import org.eclipse.core.resources.IFolder;
31+
32+
import org.eclipse.core.filebuffers.tests.ResourceHelper;
33+
34+
import org.eclipse.jface.dialogs.IDialogSettings;
35+
import org.eclipse.jface.viewers.ISelectionProvider;
36+
37+
import org.eclipse.jface.text.Document;
38+
import org.eclipse.jface.text.TextSelection;
39+
40+
import org.eclipse.ui.IEditorPart;
41+
import org.eclipse.ui.IWorkbench;
42+
import org.eclipse.ui.IWorkbenchPage;
43+
import org.eclipse.ui.IWorkbenchPartSite;
44+
import org.eclipse.ui.PartInitException;
45+
import org.eclipse.ui.PlatformUI;
46+
import org.eclipse.ui.ide.IDE;
47+
import org.eclipse.ui.internal.findandreplace.HistoryStore;
48+
49+
import org.eclipse.ui.texteditor.AbstractTextEditor;
50+
import org.eclipse.ui.texteditor.FindNextAction;
51+
52+
53+
public class FindNextActionTest {
54+
private IFile fFile;
55+
56+
private IEditorPart fEditor;
57+
58+
private int fCount;
59+
60+
private FindNextAction action;
61+
62+
private static final String BUNDLE_FOR_CONSTRUCTED_KEYS= "org.eclipse.ui.texteditor.ConstructedEditorMessages";//$NON-NLS-1$
63+
64+
private static ResourceBundle fgBundleForConstructedKeys= ResourceBundle.getBundle(BUNDLE_FOR_CONSTRUCTED_KEYS);
65+
66+
public void openEditorAndFindNextAction(String content, boolean direction) {
67+
try {
68+
IFolder folder= ResourceHelper.createFolder("EncodingChangeTestProject/EncodingChangeTests/");
69+
fFile= ResourceHelper.createFile(folder, "file" + fCount + ".txt", content);
70+
fFile.setCharset(null, null);
71+
fCount++;
72+
} catch (CoreException e) {
73+
fail();
74+
}
75+
IWorkbench workbench= PlatformUI.getWorkbench();
76+
IWorkbenchPage page= workbench.getActiveWorkbenchWindow().getActivePage();
77+
try {
78+
fEditor= IDE.openEditor(page, fFile);
79+
} catch (PartInitException e) {
80+
fail();
81+
}
82+
83+
action= new FindNextAction(fgBundleForConstructedKeys, "findNext", fEditor, direction);
84+
}
85+
86+
public static void closeEditor(IEditorPart editor) {
87+
IWorkbenchPartSite site;
88+
IWorkbenchPage page;
89+
if (editor != null && (site= editor.getSite()) != null && (page= site.getPage()) != null) {
90+
page.closeEditor(editor, false);
91+
}
92+
}
93+
94+
@After
95+
public void tearDown() throws Exception {
96+
resetInitialSearchSettings();
97+
closeEditor(fEditor);
98+
fEditor= null;
99+
fFile= null;
100+
ResourceHelper.deleteProject("EncodingChangeTestProject");
101+
TestUtil.cleanUp();
102+
}
103+
104+
private void resetInitialSearchSettings() {
105+
IDialogSettings settings= getActionSettings();
106+
settings.put("isRegEx", false);
107+
settings.put("casesensitive", false);
108+
settings.put("wrap", true);
109+
settings.put("wholeword", false);
110+
}
111+
112+
public void setEditorSelection(int offset, int length) {
113+
if (fEditor instanceof AbstractTextEditor textEditor) {
114+
Document document= (Document) textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
115+
TextSelection selection= new TextSelection(document, offset, length);
116+
ISelectionProvider selectionProvider= textEditor.getSelectionProvider();
117+
selectionProvider.setSelection(selection);
118+
}
119+
}
120+
121+
public TextSelection getEditorSelection() {
122+
if (fEditor instanceof AbstractTextEditor textEditor) {
123+
ISelectionProvider selectionProvider= textEditor.getSelectionProvider();
124+
if (selectionProvider.getSelection() instanceof TextSelection) {
125+
return (TextSelection) selectionProvider.getSelection();
126+
}
127+
}
128+
return null;
129+
}
130+
131+
public void assertSelectionIs(int offset, int length) {
132+
assertThat(getEditorSelection().getRegions()[0].getOffset(), is(offset));
133+
assertThat(getEditorSelection().getRegions()[0].getLength(), is(length));
134+
}
135+
136+
private IDialogSettings getActionSettings() {
137+
IDialogSettings settings= PlatformUI.getDialogSettingsProvider(FrameworkUtil.getBundle(FindNextAction.class))
138+
.getDialogSettings();
139+
IDialogSettings fDialogSettings= settings.getSection("org.eclipse.ui.texteditor.FindReplaceDialog");
140+
if (fDialogSettings == null)
141+
fDialogSettings= settings.addNewSection("org.eclipse.ui.texteditor.FindReplaceDialog");
142+
return fDialogSettings;
143+
}
144+
145+
@Test
146+
public void testFindNextForward() {
147+
openEditorAndFindNextAction("testtesttest", true);
148+
setEditorSelection(0, 4);
149+
action.run();
150+
assertSelectionIs(4, 4);
151+
action.run();
152+
assertSelectionIs(8, 4);
153+
action.run();
154+
assertSelectionIs(0, 4);
155+
}
156+
157+
@Test
158+
public void testFindNextBackwards() {
159+
openEditorAndFindNextAction("testtesttest", false);
160+
setEditorSelection(4, 4);
161+
action.run();
162+
assertSelectionIs(0, 4);
163+
action.run();
164+
assertSelectionIs(8, 4);
165+
}
166+
167+
@Test
168+
public void testFindNextFromHistory() {
169+
openEditorAndFindNextAction("word-abcd-text", true);
170+
IDialogSettings settings= getActionSettings();
171+
HistoryStore historyStore= new HistoryStore(settings, "findhistory", 15);
172+
historyStore.add("abcd");
173+
setEditorSelection(0, 0);
174+
action.run();
175+
assertSelectionIs(5, 4);
176+
setEditorSelection(3, 0);
177+
action.run();
178+
assertSelectionIs(5, 4);
179+
}
180+
181+
@Test
182+
public void testFindNextStoresCorrectHistory() {
183+
openEditorAndFindNextAction("history", true);
184+
setEditorSelection(0, "history".length());
185+
action.run();
186+
IDialogSettings settings= getActionSettings();
187+
HistoryStore historyStore= new HistoryStore(settings, "findhistory", 15);
188+
assertThat(historyStore.get(0), is("history"));
189+
}
190+
191+
@Test
192+
public void testFindNextWithRegExEscapedCorrectly() {
193+
openEditorAndFindNextAction("wo+rd-woord", true);
194+
IDialogSettings settings= getActionSettings();
195+
setEditorSelection(0, 5);
196+
settings.put("isRegEx", true);
197+
action.run();
198+
assertSelectionIs(0, 5);
199+
}
200+
201+
@Test
202+
public void testCaseSensitiveFindNext() {
203+
openEditorAndFindNextAction("wordWORD", true);
204+
IDialogSettings settings= getActionSettings();
205+
settings.put("casesensitive", true);
206+
setEditorSelection(0, 4);
207+
action.run();
208+
assertSelectionIs(0, 4);
209+
}
210+
211+
@Test
212+
public void testFindNextMultilineSelection() {
213+
openEditorAndFindNextAction("line\n\rnext\n\rnext\r\nline", true);
214+
// we expect the search string to only contain the first line
215+
setEditorSelection(0, 10);
216+
action.run();
217+
assertSelectionIs(18, 4);
218+
}
219+
220+
@Test
221+
public void testFindNextNoWrap() {
222+
openEditorAndFindNextAction("wordword", true);
223+
IDialogSettings settings= getActionSettings();
224+
settings.put("wrap", false);
225+
setEditorSelection(0, 4);
226+
action.run();
227+
assertSelectionIs(4, 4);
228+
action.run();
229+
assertSelectionIs(4, 4);
230+
}
231+
232+
@Test
233+
public void testFindWholeWords() {
234+
openEditorAndFindNextAction("word longerword word", true);
235+
IDialogSettings settings= getActionSettings();
236+
settings.put("wholeword", true);
237+
setEditorSelection(0, 4);
238+
action.run();
239+
assertSelectionIs(16, 4);
240+
}
241+
242+
@Test
243+
public void testFindWholeWordsIsNotWord() {
244+
openEditorAndFindNextAction("w ord longerw ordinner w ord", true);
245+
IDialogSettings settings= getActionSettings();
246+
settings.put("wholeword", true);
247+
setEditorSelection(0, 5);
248+
action.run();
249+
assertSelectionIs(12, 5);
250+
action.run();
251+
assertSelectionIs(23, 5);
252+
}
253+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
TextViewerDeleteLineTargetTest.class,
5050
FindReplaceDialogTest.class,
5151
FindReplaceOverlayTest.class,
52-
FindReplaceLogicTest.class,
52+
FindReplaceLogicTest.class
5353
})
5454
public class WorkbenchTextEditorTestSuite {
5555
// see @SuiteClasses

0 commit comments

Comments
 (0)