1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
/*******************************************************************************
* Copyright (c) 2021 Red Hat Inc.
*
* 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
********************************************************************************/
package org.eclipse.ui.editors.tests;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Control;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
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.ITextEditorActionDefinitionIds;
/*
* Note: this test would better fit in the org.eclipse.ui.workbench.texteditor bundle, but initializing
* and editor from this bundle is quite tricky without the IDE and EFS utils.
*/
public class TextNavigationTest {
private static File file;
private static AbstractTextEditor editor;
private static StyledText widget;
@BeforeClass
public static void setUpBeforeClass() throws IOException, PartInitException, CoreException {
file = File.createTempFile(TextNavigationTest.class.getName(), ".txt");
Files.write(file.toPath(), " abc".getBytes());
editor = (AbstractTextEditor)IDE.openEditorOnFileStore(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), EFS.getStore(file.toURI()));
widget = (StyledText) editor.getAdapter(Control.class);
}
@AfterClass
public static void tearDownAfterClass() {
editor.dispose();
file.delete();
}
@Test
public void testShiftHome() {
editor.selectAndReveal(5, 0);
IAction action= editor.getAction(ITextEditorActionDefinitionIds.SELECT_LINE_START);
action.run();
ITextSelection selection= (ITextSelection) editor.getSelectionProvider().getSelection();
assertEquals(2, selection.getOffset());
assertEquals(3, selection.getLength());
assertEquals(2, widget.getCaretOffset());
action.run();
selection = (ITextSelection) editor.getSelectionProvider().getSelection();
assertEquals(0, selection.getOffset());
assertEquals(5, selection.getLength());
assertEquals(0, widget.getCaretOffset());
}
@Test
public void testShiftEnd() {
editor.getSelectionProvider().setSelection(new TextSelection(0, 0));
IAction action= editor.getAction(ITextEditorActionDefinitionIds.SELECT_LINE_END);
action.run();
ITextSelection selection= (ITextSelection) editor.getSelectionProvider().getSelection();
assertEquals(0, selection.getOffset());
assertEquals(5, selection.getLength());
assertEquals(5, widget.getCaretOffset());
}
}
|