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 textEditor ) {
117+ Document document = (Document ) textEditor .getDocumentProvider ().getDocument (textEditor .getEditorInput ());
118+ TextSelection selection = new TextSelection (document , offset , length );
119+ ISelectionProvider selectionProvider = textEditor .getSelectionProvider ();
120+ selectionProvider .setSelection (selection );
121+ }
122+ }
123+
124+ public TextSelection getEditorSelection () {
125+ if (fEditor instanceof AbstractTextEditor textEditor ) {
126+ ISelectionProvider selectionProvider = textEditor .getSelectionProvider ();
127+ if (selectionProvider .getSelection () instanceof TextSelection ) {
128+ return (TextSelection ) selectionProvider .getSelection ();
129+ }
130+ }
131+ return null ;
132+ }
133+
134+ public void assertSelectionIs (int offset , int length ) {
135+ assertThat (getEditorSelection ().getRegions ()[0 ].getOffset (), is (offset ));
136+ assertThat (getEditorSelection ().getRegions ()[0 ].getLength (), is (length ));
137+ }
138+
139+ private IDialogSettings getActionSettings () {
140+ IDialogSettings settings = PlatformUI .getDialogSettingsProvider (FrameworkUtil .getBundle (FindNextAction .class ))
141+ .getDialogSettings ();
142+ IDialogSettings fDialogSettings = settings .getSection ("org.eclipse.ui.texteditor.FindReplaceDialog" );
143+ if (fDialogSettings == null )
144+ fDialogSettings = settings .addNewSection ("org.eclipse.ui.texteditor.FindReplaceDialog" );
145+ return fDialogSettings ;
146+ }
147+
148+ @ Test
149+ public void testFindNextForward () {
150+ openEditorAndFindNextAction ("testtesttest" , true );
151+ setEditorSelection (0 , 4 );
152+ action .run ();
153+ assertSelectionIs (4 , 4 );
154+ action .run ();
155+ assertSelectionIs (8 , 4 );
156+ action .run ();
157+ assertSelectionIs (0 , 4 );
158+ }
159+
160+ @ Test
161+ public void testFindNextBackwards () {
162+ openEditorAndFindNextAction ("testtesttest" , false );
163+ setEditorSelection (4 , 4 );
164+ action .run ();
165+ assertSelectionIs (0 , 4 );
166+ action .run ();
167+ assertSelectionIs (8 , 4 );
168+ }
169+
170+ @ Test
171+ public void testFindNextFromHistory () {
172+ openEditorAndFindNextAction ("word-abcd-text" , true );
173+ IDialogSettings settings = getActionSettings ();
174+ HistoryStore historyStore = new HistoryStore (settings , "findhistory" , 15 );
175+ historyStore .add ("abcd" );
176+ setEditorSelection (0 , 0 );
177+ action .run ();
178+ assertSelectionIs (5 , 4 );
179+ setEditorSelection (3 , 0 );
180+ action .run ();
181+ assertSelectionIs (5 , 4 );
182+ }
183+
184+ @ Test
185+ public void testFindNextStoresCorrectHistory () {
186+ openEditorAndFindNextAction ("history" , true );
187+ setEditorSelection (0 , "history" .length ());
188+ action .run ();
189+ IDialogSettings settings = getActionSettings ();
190+ HistoryStore historyStore = new HistoryStore (settings , "findhistory" , 15 );
191+ assertThat (historyStore .get (0 ), is ("history" ));
192+ }
193+
194+ @ Test
195+ public void testFindNextWithRegExEscapedCorrectly () {
196+ openEditorAndFindNextAction ("wo+rd-woord" , true );
197+ IDialogSettings settings = getActionSettings ();
198+ setEditorSelection (0 , 5 );
199+ settings .put ("isRegEx" , true );
200+ action .run ();
201+ assertSelectionIs (0 , 5 );
202+ }
203+
204+ @ Test
205+ public void testCaseSensitiveFindNext () {
206+ openEditorAndFindNextAction ("wordWORD" , true );
207+ IDialogSettings settings = getActionSettings ();
208+ settings .put ("casesensitive" , true );
209+ setEditorSelection (0 , 4 );
210+ action .run ();
211+ assertSelectionIs (0 , 4 );
212+ }
213+
214+ @ Test
215+ public void testFindNextMultilineSelection () {
216+ openEditorAndFindNextAction ("line\n \r next\n \r next\r \n line" , true );
217+ // we expect the search string to only contain the first line
218+ setEditorSelection (0 , 10 );
219+ action .run ();
220+ assertSelectionIs (18 , 4 );
221+ }
222+
223+ @ Test
224+ public void testFindNextNoWrap () {
225+ openEditorAndFindNextAction ("wordword" , true );
226+ IDialogSettings settings = getActionSettings ();
227+ settings .put ("wrap" , false );
228+ setEditorSelection (0 , 4 );
229+ action .run ();
230+ assertSelectionIs (4 , 4 );
231+ action .run ();
232+ assertSelectionIs (4 , 4 );
233+ }
234+
235+ @ Test
236+ public void testFindWholeWords () {
237+ openEditorAndFindNextAction ("word longerword word" , true );
238+ IDialogSettings settings = getActionSettings ();
239+ settings .put ("wholeword" , true );
240+ setEditorSelection (0 , 4 );
241+ action .run ();
242+ assertSelectionIs (16 , 4 );
243+ }
244+
245+ @ Test
246+ public void testFindWholeWordsIsNotWord () {
247+ openEditorAndFindNextAction ("w ord longerw ordinner w ord" , true );
248+ IDialogSettings settings = getActionSettings ();
249+ settings .put ("wholeword" , true );
250+ setEditorSelection (0 , 5 );
251+ action .run ();
252+ assertSelectionIs (12 , 5 );
253+ action .run ();
254+ assertSelectionIs (23 , 5 );
255+ }
256+ }
0 commit comments