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+
26+ import org .eclipse .core .runtime .CoreException ;
27+
28+ import org .eclipse .core .resources .IFile ;
29+ import org .eclipse .core .resources .IFolder ;
30+
31+ import org .eclipse .core .filebuffers .tests .ResourceHelper ;
32+
33+ import org .eclipse .jface .viewers .ISelectionProvider ;
34+
35+ import org .eclipse .jface .text .Document ;
36+ import org .eclipse .jface .text .TextSelection ;
37+
38+ import org .eclipse .ui .IEditorPart ;
39+ import org .eclipse .ui .IWorkbench ;
40+ import org .eclipse .ui .IWorkbenchPage ;
41+ import org .eclipse .ui .IWorkbenchPartSite ;
42+ import org .eclipse .ui .PartInitException ;
43+ import org .eclipse .ui .PlatformUI ;
44+ import org .eclipse .ui .ide .IDE ;
45+
46+ import org .eclipse .ui .texteditor .AbstractTextEditor ;
47+ import org .eclipse .ui .texteditor .FindNextAction ;
48+
49+ import org .eclipse .ui .editors .tests .TestUtil ;
50+
51+
52+ public class FindNextActionTest {
53+ private IFile fFile ;
54+
55+ private IEditorPart fEditor ;
56+
57+ private int fCount ;
58+
59+ private FindNextAction action ;
60+
61+ private static final String BUNDLE_FOR_CONSTRUCTED_KEYS = "org.eclipse.ui.texteditor.ConstructedEditorMessages" ;//$NON-NLS-1$
62+
63+ private static ResourceBundle fgBundleForConstructedKeys = ResourceBundle .getBundle (BUNDLE_FOR_CONSTRUCTED_KEYS );
64+
65+ public void openEditorAndFindNextAction (String content , boolean direction ) {
66+ try {
67+ IFolder folder = ResourceHelper .createFolder ("EncodingChangeTestProject/EncodingChangeTests/" );
68+ fFile = ResourceHelper .createFile (folder , "file" + fCount + ".txt" , content );
69+ fFile .setCharset (null , null );
70+ fCount ++;
71+ } catch (CoreException e ) {
72+ System .out .println (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+ closeEditor (fEditor );
97+ fEditor = null ;
98+ fFile = null ;
99+ ResourceHelper .deleteProject ("EncodingChangeTestProject" );
100+ TestUtil .cleanUp ();
101+ }
102+
103+ public void setEditorSelection (int offset , int length ) {
104+ if (fEditor instanceof AbstractTextEditor ) {
105+ AbstractTextEditor textEditor = (AbstractTextEditor ) fEditor ;
106+ Document document = (Document ) textEditor .getDocumentProvider ().getDocument (textEditor .getEditorInput ());
107+ TextSelection selection = new TextSelection (document , offset , length );
108+ ISelectionProvider selectionProvider = textEditor .getSelectionProvider ();
109+ selectionProvider .setSelection (selection );
110+ }
111+ }
112+
113+ public TextSelection getEditorSelection () {
114+ if (fEditor instanceof AbstractTextEditor ) {
115+ AbstractTextEditor textEditor = (AbstractTextEditor ) fEditor ;
116+ ISelectionProvider selectionProvider = textEditor .getSelectionProvider ();
117+ if (selectionProvider .getSelection () instanceof TextSelection ) {
118+ return (TextSelection ) selectionProvider .getSelection ();
119+ }
120+ }
121+ return null ;
122+ }
123+
124+ @ Test
125+ public void testFindNextForward () {
126+ openEditorAndFindNextAction ("testtesttest" , true );
127+ setEditorSelection (0 , 4 );
128+ action .run ();
129+ assertThat (getEditorSelection ().getRegions ()[0 ].getOffset (), is (4 ));
130+ assertThat (getEditorSelection ().getRegions ()[0 ].getLength (), is (4 ));
131+ action .run ();
132+ assertThat (getEditorSelection ().getRegions ()[0 ].getOffset (), is (8 ));
133+ assertThat (getEditorSelection ().getRegions ()[0 ].getLength (), is (4 ));
134+ action .run ();
135+ assertThat (getEditorSelection ().getRegions ()[0 ].getOffset (), is (0 ));
136+ assertThat (getEditorSelection ().getRegions ()[0 ].getLength (), is (4 ));
137+ }
138+
139+ @ Test
140+ public void testFindNextBackwards () {
141+ openEditorAndFindNextAction ("testtesttest" , false );
142+ setEditorSelection (4 , 4 );
143+ action .run ();
144+ assertThat (getEditorSelection ().getRegions ()[0 ].getOffset (), is (0 ));
145+ assertThat (getEditorSelection ().getRegions ()[0 ].getLength (), is (4 ));
146+
147+ action .run ();
148+ assertThat (getEditorSelection ().getRegions ()[0 ].getOffset (), is (8 ));
149+ assertThat (getEditorSelection ().getRegions ()[0 ].getLength (), is (4 ));
150+ }
151+
152+ }
0 commit comments