Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fdce2563cf738b5d8e28a3696c499ce33f119be1 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*******************************************************************************
 * Copyright (c) 2012 Florian Thienel 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:
 * 		Florian Thienel - initial API and implementation
 *******************************************************************************/
package org.eclipse.vex.core.internal.widget;

import static org.eclipse.vex.core.internal.widget.VexWidgetTest.PARA;
import static org.eclipse.vex.core.internal.widget.VexWidgetTest.TITLE;
import static org.eclipse.vex.core.internal.widget.VexWidgetTest.createDocumentWithDTD;
import static org.eclipse.vex.core.tests.TestResources.TEST_DTD;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.eclipse.vex.core.dom.IComment;
import org.eclipse.vex.core.dom.IElement;
import org.eclipse.vex.core.internal.css.StyleSheet;
import org.junit.Before;
import org.junit.Test;

/**
 * @author Florian Thienel
 */
public class L2SelectionTest {

	private IVexWidget widget;

	@Before
	public void setUp() throws Exception {
		widget = new VexWidgetImpl(new MockHostComponent());
		widget.setDocument(createDocumentWithDTD(TEST_DTD, "section"), StyleSheet.NULL);
	}

	@Test
	public void givenCaretInElement_whenSelectionIncludesStartOffset_shouldExpandSelectionToEndOffset() throws Exception {
		final IElement titleElement = widget.insertElement(TITLE);
		widget.moveBy(-1, true);
		assertTrue(widget.hasSelection());
		assertEquals(titleElement.getRange(), widget.getSelectedRange());
		assertEquals(titleElement.getStartOffset(), widget.getCaretOffset());
	}

	@Test
	public void givenCaretInElementWith_whenSelectionIncludesStartOffset_shouldExpandSelectionToEndOffset() throws Exception {
		final IElement titleElement = widget.insertElement(TITLE);
		widget.insertText("Hello World");
		widget.moveBy(-5, false);
		widget.moveTo(titleElement.getStartOffset(), true);
		assertTrue(widget.hasSelection());
		assertEquals(titleElement.getRange(), widget.getSelectedRange());
		assertEquals(titleElement.getStartOffset(), widget.getCaretOffset());
	}

	@Test
	public void givenCaretInElementAtEndOffset_whenMovedByOneBehindEndOffset_shouldExpandSelectionToStartOffset() throws Exception {
		final IElement titleElement = widget.insertElement(TITLE);
		widget.insertText("Hello World");
		widget.moveBy(1, true);
		assertTrue(widget.hasSelection());
		assertEquals(titleElement.getRange(), widget.getSelectedRange());
		assertEquals(titleElement.getEndOffset() + 1, widget.getCaretOffset());
	}

	@Test
	public void givenCaretInElementAtEndOffset_whenMovedOneBehindStartOffset_shouldNotIncludeEndOffsetInSelectedRange() throws Exception {
		final IElement titleElement = widget.insertElement(TITLE);
		widget.insertText("Hello World");
		widget.moveTo(titleElement.getStartOffset() + 1, true);
		assertEquals(titleElement.getRange().resizeBy(1, -1), widget.getSelectedRange());
		assertEquals(titleElement.getStartOffset() + 1, widget.getCaretOffset());
	}

	@Test
	public void givenCaretAtStartOffsetOfElementWithText_whenMovedByOneForward_shouldExpandSelectionBehindEndOffset() throws Exception {
		final IElement titleElement = widget.insertElement(TITLE);
		widget.insertText("Hello World");
		widget.moveTo(titleElement.getStartOffset(), false);
		widget.moveBy(1, true);
		assertEquals(titleElement.getRange(), widget.getSelectedRange());
		assertEquals(titleElement.getEndOffset() + 1, widget.getCaretOffset());
	}

	@Test
	public void givenCaretAtStartOffsetOfElementWithText_whenMovedOneForwardAndOneBackward_shouldSelectNothing() throws Exception {
		final IElement titleElement = widget.insertElement(TITLE);
		widget.insertText("Hello World");
		widget.moveTo(titleElement.getStartOffset(), false);
		widget.moveBy(1, true);
		widget.moveBy(-1, true);
		assertEquals(titleElement.getStartOffset(), widget.getCaretOffset());
	}

	@Test
	public void givenCaretInElementWithText_whenMovedBehindFollowingElementAndMovedBackOnce_shouldSelectOnlyFirstElement() throws Exception {
		final IElement titleElement = widget.insertElement(TITLE);
		widget.insertText("Hello World");
		widget.moveBy(1);
		final IElement paraElement = widget.insertElement(PARA);
		widget.insertText("Hello Again");
		widget.moveTo(titleElement.getStartOffset() + 3);
		widget.moveTo(paraElement.getEndOffset() + 1, true);
		widget.moveBy(-1, true);
		assertEquals(titleElement.getRange(), widget.getSelectedRange());
		assertEquals(titleElement.getEndOffset() + 1, widget.getCaretOffset());
	}

	@Test
	public void givenCaretInElementWithText_whenMovedBehindFollowingElementAndMovedBackTwice_shouldSelectOnlyTextFragementOfFirstElement() throws Exception {
		final IElement titleElement = widget.insertElement(TITLE);
		widget.insertText("Hello World");
		widget.moveBy(1);
		final IElement paraElement = widget.insertElement(PARA);
		widget.insertText("Hello Again");
		widget.moveTo(titleElement.getStartOffset() + 3);
		widget.moveTo(paraElement.getEndOffset() + 1, true);
		widget.moveBy(-1, true);
		widget.moveBy(-1, true);
		assertEquals(titleElement.getRange().resizeBy(3, -1), widget.getSelectedRange());
		assertEquals(titleElement.getEndOffset(), widget.getCaretOffset());
	}

	@Test
	public void givenCaretInElementWithText_whenMovedBeforePrecedingElementAndMovedForwardOnce_shouldSelectOnlySecondElement() throws Exception {
		final IElement titleElement = widget.insertElement(TITLE);
		widget.insertText("Hello World");
		widget.moveBy(1);
		final IElement paraElement = widget.insertElement(PARA);
		widget.insertText("Hello Again");
		widget.moveTo(paraElement.getEndOffset() - 3);
		widget.moveTo(titleElement.getStartOffset(), true);
		widget.moveBy(1, true);
		assertEquals(paraElement.getRange(), widget.getSelectedRange());
		assertEquals(paraElement.getStartOffset(), widget.getCaretOffset());
	}

	@Test
	public void givenCaretInElementWithText_whenMovedBeforePrecedingElementAndMovedForwardTwice_shouldSelectOnlyTextFragementOfSecondElement() throws Exception {
		final IElement titleElement = widget.insertElement(TITLE);
		widget.insertText("Hello World");
		widget.moveBy(1);
		final IElement paraElement = widget.insertElement(PARA);
		widget.insertText("Hello Again");
		widget.moveTo(paraElement.getEndOffset() - 3);
		widget.moveTo(titleElement.getStartOffset(), true);
		widget.moveBy(1, true);
		widget.moveBy(1, true);
		assertEquals(paraElement.getRange().resizeBy(1, -4), widget.getSelectedRange());
		assertEquals(paraElement.getStartOffset() + 1, widget.getCaretOffset());
	}

	@Test
	public void givenCarentInEmptyComment_whenMovedBeforeComment_shouldExpandSelectionToIncludeEndOffset() throws Exception {
		final IComment comment = widget.insertComment();
		widget.moveBy(-1, true);
		assertTrue(widget.hasSelection());
		assertEquals(comment.getRange(), widget.getSelectedRange());
		assertEquals(comment.getStartOffset(), widget.getCaretOffset());
	}
}

Back to the top