Skip to content

Commit 7021ede

Browse files
Maximilian WittmerMaximilian Wittmer
authored andcommitted
[FindNextAction] Introduce unit tests
Introduces unit tests for the FindNextAction.
1 parent 8df2017 commit 7021ede

File tree

3 files changed

+269
-2
lines changed

3 files changed

+269
-2
lines changed

tests/org.eclipse.ui.workbench.texteditor.tests/META-INF/MANIFEST.MF

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
FindReplaceDialogTest.class,
5151
FindReplaceOverlayTest.class,
5252
FindReplaceLogicTest.class,
53+
FindNextActionTest.class,
5354
})
5455
public class WorkbenchTextEditorTestSuite {
5556
// see @SuiteClasses

0 commit comments

Comments
 (0)