-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FindNextAction] Introduce unit tests
Introduces unit tests for the FindNextAction.
- Loading branch information
Showing
2 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...ch.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindNextActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Vector Informatik GmbH and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Vector Informatik GmbH - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.ui.workbench.texteditor.tests; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.util.ResourceBundle; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import org.eclipse.core.runtime.CoreException; | ||
|
||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IFolder; | ||
|
||
import org.eclipse.core.filebuffers.tests.ResourceHelper; | ||
|
||
import org.eclipse.jface.viewers.ISelectionProvider; | ||
|
||
import org.eclipse.jface.text.Document; | ||
import org.eclipse.jface.text.TextSelection; | ||
|
||
import org.eclipse.ui.IEditorPart; | ||
import org.eclipse.ui.IWorkbench; | ||
import org.eclipse.ui.IWorkbenchPage; | ||
import org.eclipse.ui.IWorkbenchPartSite; | ||
import org.eclipse.ui.PartInitException; | ||
import org.eclipse.ui.PlatformUI; | ||
import org.eclipse.ui.ide.IDE; | ||
|
||
import org.eclipse.ui.texteditor.AbstractTextEditor; | ||
import org.eclipse.ui.texteditor.FindNextAction; | ||
|
||
import org.eclipse.ui.editors.tests.TestUtil; | ||
|
||
|
||
public class FindNextActionTest { | ||
private IFile fFile; | ||
|
||
private IEditorPart fEditor; | ||
|
||
private int fCount; | ||
|
||
private FindNextAction action; | ||
|
||
private static final String BUNDLE_FOR_CONSTRUCTED_KEYS= "org.eclipse.ui.texteditor.ConstructedEditorMessages";//$NON-NLS-1$ | ||
|
||
private static ResourceBundle fgBundleForConstructedKeys= ResourceBundle.getBundle(BUNDLE_FOR_CONSTRUCTED_KEYS); | ||
|
||
public void openEditorAndFindNextAction(String content, boolean direction) { | ||
try { | ||
IFolder folder= ResourceHelper.createFolder("EncodingChangeTestProject/EncodingChangeTests/"); | ||
fFile= ResourceHelper.createFile(folder, "file" + fCount + ".txt", content); | ||
fFile.setCharset(null, null); | ||
fCount++; | ||
} catch (CoreException e) { | ||
System.out.println(e); | ||
fail(); | ||
} | ||
IWorkbench workbench= PlatformUI.getWorkbench(); | ||
IWorkbenchPage page= workbench.getActiveWorkbenchWindow().getActivePage(); | ||
try { | ||
fEditor= IDE.openEditor(page, fFile); | ||
} catch (PartInitException e) { | ||
fail(); | ||
} | ||
|
||
action= new FindNextAction(fgBundleForConstructedKeys, "findNext", fEditor, direction); | ||
} | ||
|
||
public static void closeEditor(IEditorPart editor) { | ||
IWorkbenchPartSite site; | ||
IWorkbenchPage page; | ||
if (editor != null && (site= editor.getSite()) != null && (page= site.getPage()) != null) { | ||
page.closeEditor(editor, false); | ||
} | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
closeEditor(fEditor); | ||
fEditor= null; | ||
fFile= null; | ||
ResourceHelper.deleteProject("EncodingChangeTestProject"); | ||
TestUtil.cleanUp(); | ||
} | ||
|
||
public void setEditorSelection(int offset, int length) { | ||
if (fEditor instanceof AbstractTextEditor) { | ||
AbstractTextEditor textEditor= (AbstractTextEditor) fEditor; | ||
Document document= (Document) textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); | ||
TextSelection selection= new TextSelection(document, offset, length); | ||
ISelectionProvider selectionProvider= textEditor.getSelectionProvider(); | ||
selectionProvider.setSelection(selection); | ||
} | ||
} | ||
|
||
public TextSelection getEditorSelection() { | ||
if (fEditor instanceof AbstractTextEditor) { | ||
AbstractTextEditor textEditor= (AbstractTextEditor) fEditor; | ||
ISelectionProvider selectionProvider= textEditor.getSelectionProvider(); | ||
if (selectionProvider.getSelection() instanceof TextSelection) { | ||
return (TextSelection) selectionProvider.getSelection(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Test | ||
public void testFindNextForward() { | ||
openEditorAndFindNextAction("testtesttest", true); | ||
setEditorSelection(0, 4); | ||
action.run(); | ||
assertThat(getEditorSelection().getRegions()[0].getOffset(), is(4)); | ||
assertThat(getEditorSelection().getRegions()[0].getLength(), is(4)); | ||
action.run(); | ||
assertThat(getEditorSelection().getRegions()[0].getOffset(), is(8)); | ||
assertThat(getEditorSelection().getRegions()[0].getLength(), is(4)); | ||
action.run(); | ||
assertThat(getEditorSelection().getRegions()[0].getOffset(), is(0)); | ||
assertThat(getEditorSelection().getRegions()[0].getLength(), is(4)); | ||
} | ||
|
||
@Test | ||
public void testFindNextBackwards() { | ||
openEditorAndFindNextAction("testtesttest", false); | ||
setEditorSelection(4, 4); | ||
action.run(); | ||
assertThat(getEditorSelection().getRegions()[0].getOffset(), is(0)); | ||
assertThat(getEditorSelection().getRegions()[0].getLength(), is(4)); | ||
|
||
action.run(); | ||
assertThat(getEditorSelection().getRegions()[0].getOffset(), is(8)); | ||
assertThat(getEditorSelection().getRegions()[0].getLength(), is(4)); | ||
} | ||
|
||
} |