Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bdb242143ff395b8fbeaa2b2f637b794ba5b0813 (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Jonah Graham (Kichwa Coders) - Bug 465684 - Fix initial setVisibleRegion of 0, 0
 *******************************************************************************/
package org.eclipse.ui.editors.tests;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;

import org.eclipse.core.filebuffers.tests.ResourceHelper;

import org.eclipse.jface.text.IDocument;

import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

import org.eclipse.ui.texteditor.ITextEditor;


public class SegmentedModeTest {

	private static final String ORIGINAL_CONTENT= "this\nis\nthe\ncontent\nof\nthe\nfile";

	private IFile fFile;

	private String getOriginalContent() {
		return ORIGINAL_CONTENT;
	}

	@Before
	public void setUp() throws Exception {
		IFolder folder= ResourceHelper.createFolder("project/folderA/folderB/");
		fFile= ResourceHelper.createFile(folder, "file.txt", getOriginalContent());
	}

	@After
	public void tearDown() throws Exception {
		ResourceHelper.deleteProject("project");
	}

	/*
	 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=70934
	 */
	@Test
	public void testSegmentation() {
		IWorkbench workbench= PlatformUI.getWorkbench();
		IWorkbenchPage page= workbench.getActiveWorkbenchWindow().getActivePage();
		try {
			IEditorPart part= IDE.openEditor(page, fFile);

			try {
				if (part instanceof ITextEditor) {
					ITextEditor editor= (ITextEditor) part;

					editor.showHighlightRangeOnly(true);
					editor.setHighlightRange(5, 0, true);

					Control control= part.getAdapter(Control.class);
					if (control instanceof StyledText) {
						StyledText styledText= (StyledText) control;
						int caret= styledText.getCaretOffset();
						styledText.replaceTextRange(caret, 0, "really ");

						StringBuffer buffer= new StringBuffer(getOriginalContent());
						buffer.insert(5, "really ");
						IDocument document= editor.getDocumentProvider().getDocument(editor.getEditorInput());
						assertEquals(buffer.toString(), document.get());
					}
				}
			} finally {
				page.saveEditor(part, false);
			}

		} catch (PartInitException e) {
			assertTrue(false);
		}
	}

	/*
	 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=465684
	 */
	@Test
	public void testShowNothing() {
		IWorkbench workbench= PlatformUI.getWorkbench();
		IWorkbenchPage page= workbench.getActiveWorkbenchWindow().getActivePage();
		try {
			while (Display.getDefault().readAndDispatch()) {
			}
			IEditorPart part= IDE.openEditor(page, fFile);

			try {
				if (part instanceof ITextEditor) {
					ITextEditor editor= (ITextEditor)part;

					editor.showHighlightRangeOnly(true);
					editor.setHighlightRange(0, 0, true);

					Control control= part.getAdapter(Control.class);
					if (control instanceof StyledText) {
						StyledText styledText= (StyledText)control;
						String text= styledText.getText();
						assertEquals("", text);
					}
				}
			} finally {
				page.saveEditor(part, false);
			}

		} catch (PartInitException e) {
			assertTrue(false);
		}
	}
}

Back to the top