move tests related to selection balancing into BalancingSelectorTest

Signed-off-by: Florian Thienel <florian@thienel.org>
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/BalancingSelectorTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/BalancingSelectorTest.java
index 854b5a6..fafe0b3 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/BalancingSelectorTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/BalancingSelectorTest.java
@@ -7,10 +7,12 @@
  *
  * Contributors:
  * 		Florian Thienel - initial API and implementation
+ *      Carsten Hiesserich - moved additional tests from L2SelectionTest
  *******************************************************************************/
 package org.eclipse.vex.core.internal.widget;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 
 import org.eclipse.vex.core.internal.io.UniversalTestDocument;
 import org.eclipse.vex.core.provisional.dom.ContentRange;
@@ -98,6 +100,32 @@
 		assertBalancedSelectionIs(section.getStartOffset(), section.getEndOffset() + 1, section.getStartOffset());
 	}
 
+	@Test
+	public void givenMarkAtStartOffsetOfParagraphWithText_whenSelectingForward_shouldSelectWholeParagraph() throws Exception {
+		final IElement paragraphWithText = document.getParagraphWithText(0);
+		select(paragraphWithText.getStartOffset(), paragraphWithText.getStartOffset() + 1);
+		assertBalancedSelectionIs(paragraphWithText.getStartOffset(), paragraphWithText.getEndOffset() + 1, paragraphWithText.getEndOffset() + 1);
+	}
+
+	@Test
+	public void givenMarkAtEndOffsetOfParagraphWithText_whenSelectingBackwardOneCharBehindStartOffset_shouldNotIncludeEndOffsetInSelectedRange() throws Exception {
+		final IElement paragraphWithText = document.getParagraphWithText(0);
+		select(paragraphWithText.getEndOffset(), paragraphWithText.getStartOffset() + 1);
+		assertBalancedSelectionIs(paragraphWithText.getStartOffset() + 1, paragraphWithText.getEndOffset(), paragraphWithText.getStartOffset() + 1);
+	}
+
+	@Test
+	public void givenMarkAtStartOffsetOfParagraphWithText_whenSelectingOneForwardAndOneBackward_shouldSelectNothing() throws Exception {
+		final IElement paragraphWithText = document.getParagraphWithText(0);
+		select(paragraphWithText.getStartOffset(), paragraphWithText.getStartOffset() + 1);
+		selector.moveEndTo(paragraphWithText.getStartOffset());
+		assertFalse(selector.isActive());
+	}
+
+	/*
+	 * Utility Methods
+	 */
+
 	private void select(final int mark, final int caretPosition) {
 		selector.setMark(mark);
 		selector.moveEndTo(caretPosition);
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/L2SelectionTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/L2SelectionTest.java
index defcbb0..7cb6e51 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/L2SelectionTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/L2SelectionTest.java
@@ -11,8 +11,6 @@
  *******************************************************************************/
 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.PRE;
 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;
@@ -20,7 +18,7 @@
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-import org.eclipse.vex.core.provisional.dom.IComment;
+import org.eclipse.vex.core.provisional.dom.IDocument;
 import org.eclipse.vex.core.provisional.dom.IElement;
 import org.junit.Before;
 import org.junit.Test;
@@ -34,167 +32,9 @@
 
 	@Before
 	public void setUp() throws Exception {
-		editor = new BaseVexWidget(new MockHostComponent());
-		editor.setDocument(createDocumentWithDTD(TEST_DTD, "section"));
-	}
-
-	@Test
-	public void givenCaretInElement_whenSelectionIncludesStartOffset_shouldExpandSelectionToEndOffset() throws Exception {
-		final IElement titleElement = editor.insertElement(TITLE);
-		editor.moveBy(-1, true);
-		assertTrue(editor.hasSelection());
-		assertEquals(titleElement.getRange(), editor.getSelectedRange());
-		assertEquals(titleElement.getStartPosition(), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretInElementWith_whenSelectionIncludesStartOffset_shouldExpandSelectionToEndOffset() throws Exception {
-		final IElement titleElement = editor.insertElement(TITLE);
-		editor.insertText("Hello World");
-		editor.moveBy(-5, false);
-		editor.moveTo(titleElement.getStartPosition(), true);
-		assertTrue(editor.hasSelection());
-		assertEquals(titleElement.getRange(), editor.getSelectedRange());
-		assertEquals(titleElement.getStartPosition(), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretInElementWith_whenSelectionForwardIncludesStartOffset_shouldExpandSelectionToEndOffset() throws Exception {
-		editor.insertElement(PARA);
-		editor.insertText("before");
-		final IElement innerElement = editor.insertElement(PRE);
-		editor.insertText("Selection");
-		editor.moveTo(innerElement.getEndPosition().moveBy(1));
-		editor.insertText("after");
-
-		editor.moveTo(innerElement.getStartPosition().moveBy(-1));
-		editor.moveTo(innerElement.getStartPosition().moveBy(1), true);
-
-		assertTrue(editor.hasSelection());
-		assertEquals(innerElement.getStartPosition().moveBy(-1), editor.getSelectedPositionRange().getStartPosition());
-		assertEquals(innerElement.getEndPosition().moveBy(1), editor.getSelectedPositionRange().getEndPosition());
-		assertEquals(innerElement.getEndPosition().moveBy(1), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretInElementWith_whenSelectionBackwardIncludesStartOffset_shouldExpandSelectionToEndOffset() throws Exception {
-		editor.insertElement(PARA);
-		final IElement innerElement = editor.insertElement(PRE);
-		editor.insertText("Selection");
-		editor.moveTo(innerElement.getEndPosition().moveBy(1));
-		editor.insertText("after");
-		editor.moveTo(innerElement.getEndPosition().moveBy(-1));
-		editor.moveTo(innerElement.getStartPosition(), true);
-
-		assertTrue(editor.hasSelection());
-		assertEquals(innerElement.getRange(), editor.getSelectedRange());
-		assertEquals(innerElement.getStartPosition(), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretInElementAtEndOffset_whenMovedByOneBehindEndOffset_shouldExpandSelectionToStartOffset() throws Exception {
-		final IElement titleElement = editor.insertElement(TITLE);
-		editor.insertText("Hello World");
-		editor.moveBy(1, true);
-		assertTrue(editor.hasSelection());
-		assertEquals(titleElement.getRange(), editor.getSelectedRange());
-		assertEquals(titleElement.getEndPosition().moveBy(1), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretInElementAtEndOffset_whenMovedOneBehindStartOffset_shouldNotIncludeEndOffsetInSelectedRange() throws Exception {
-		final IElement titleElement = editor.insertElement(TITLE);
-		editor.insertText("Hello World");
-		editor.moveTo(titleElement.getStartPosition().moveBy(1), true);
-		assertEquals(titleElement.getRange().resizeBy(1, -1), editor.getSelectedRange());
-		assertEquals(titleElement.getStartPosition().moveBy(1), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretAtStartOffsetOfElementWithText_whenMovedByOneForward_shouldExpandSelectionBehindEndOffset() throws Exception {
-		final IElement titleElement = editor.insertElement(TITLE);
-		editor.insertText("Hello World");
-		editor.moveTo(titleElement.getStartPosition(), false);
-		editor.moveBy(1, true);
-		assertEquals(titleElement.getRange(), editor.getSelectedRange());
-		assertEquals(titleElement.getEndPosition().moveBy(1), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretAtStartOffsetOfElementWithText_whenMovedOneForwardAndOneBackward_shouldSelectNothing() throws Exception {
-		final IElement titleElement = editor.insertElement(TITLE);
-		editor.insertText("Hello World");
-		editor.moveTo(titleElement.getStartPosition(), false);
-		editor.moveBy(1, true);
-		editor.moveBy(-1, true);
-		assertEquals(titleElement.getStartPosition(), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretInElementWithText_whenMovedBehindFollowingElementAndMovedBackOnce_shouldSelectOnlyFirstElement() throws Exception {
-		final IElement titleElement = editor.insertElement(TITLE);
-		editor.insertText("Hello World");
-		editor.moveBy(1);
-		final IElement paraElement = editor.insertElement(PARA);
-		editor.insertText("Hello Again");
-		editor.moveTo(titleElement.getStartPosition().moveBy(3));
-		editor.moveTo(paraElement.getEndPosition().moveBy(1), true);
-		editor.moveBy(-1, true);
-		assertEquals(titleElement.getRange(), editor.getSelectedRange());
-		assertEquals(titleElement.getEndPosition().moveBy(1), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretInElementWithText_whenMovedBehindFollowingElementAndMovedBackTwice_shouldSelectOnlyTextFragementOfFirstElement() throws Exception {
-		final IElement titleElement = editor.insertElement(TITLE);
-		editor.insertText("Hello World");
-		editor.moveBy(1);
-		final IElement paraElement = editor.insertElement(PARA);
-		editor.insertText("Hello Again");
-		editor.moveTo(titleElement.getStartPosition().moveBy(3));
-		editor.moveTo(paraElement.getEndPosition().moveBy(1), true);
-		editor.moveBy(-1, true);
-		editor.moveBy(-1, true);
-		assertEquals(titleElement.getRange().resizeBy(3, -1), editor.getSelectedRange());
-		assertEquals(titleElement.getEndPosition(), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretInElementWithText_whenMovedBeforePrecedingElementAndMovedForwardOnce_shouldSelectOnlySecondElement() throws Exception {
-		final IElement titleElement = editor.insertElement(TITLE);
-		editor.insertText("Hello World");
-		editor.moveBy(1);
-		final IElement paraElement = editor.insertElement(PARA);
-		editor.insertText("Hello Again");
-		editor.moveTo(paraElement.getEndPosition().moveBy(-3));
-		editor.moveTo(titleElement.getStartPosition(), true);
-		editor.moveBy(1, true);
-		assertEquals(paraElement.getRange(), editor.getSelectedRange());
-		assertEquals(paraElement.getStartPosition(), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCaretInElementWithText_whenMovedBeforePrecedingElementAndMovedForwardTwice_shouldSelectOnlyTextFragementOfSecondElement() throws Exception {
-		final IElement titleElement = editor.insertElement(TITLE);
-		editor.insertText("Hello World");
-		editor.moveBy(1);
-		final IElement paraElement = editor.insertElement(PARA);
-		editor.insertText("Hello Again");
-		editor.moveTo(paraElement.getEndPosition().moveBy(-3));
-		editor.moveTo(titleElement.getStartPosition(), true);
-		editor.moveBy(1, true);
-		editor.moveBy(1, true);
-		assertEquals(paraElement.getRange().resizeBy(1, -4), editor.getSelectedRange());
-		assertEquals(paraElement.getStartPosition().moveBy(+1), editor.getCaretPosition());
-	}
-
-	@Test
-	public void givenCarentInEmptyComment_whenMovedBeforeComment_shouldExpandSelectionToIncludeEndOffset() throws Exception {
-		final IComment comment = editor.insertComment();
-		editor.moveBy(-1, true);
-		assertTrue(editor.hasSelection());
-		assertEquals(comment.getRange(), editor.getSelectedRange());
-		assertEquals(comment.getStartPosition(), editor.getCaretPosition());
+		final IDocument document = createDocumentWithDTD(TEST_DTD, "section");
+		editor = new DocumentEditor(new FakeCursor(document));
+		editor.setDocument(document);
 	}
 
 	@Test
@@ -228,18 +68,4 @@
 		assertEquals(title.getRange(), editor.getSelectedRange());
 		assertTrue(editor.hasSelection());
 	}
-
-	@Test
-	public void givenWorkBlockStarted_whenWorkBlockNotEnded_shouldNotFireSelectionChangedEvent() throws Exception {
-		// TODO should IDocumentEditor implement ISelectionProvider?
-		//		hostComponent.selectionChanged = false;
-		//		editor.beginWork();
-		//		final IElement title = editor.insertElement(TITLE);
-		//		editor.moveTo(title.getStartPosition());
-		//		editor.moveTo(title.getEndPosition(), true);
-		//		final boolean selectionChangedWhileWorking = hostComponent.selectionChanged;
-		//		editor.endWork(true);
-		//
-		//		assertFalse(selectionChangedWhileWorking);
-	}
 }
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DocumentEditor.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DocumentEditor.java
index 613b983..c6c83f2 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DocumentEditor.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DocumentEditor.java
@@ -351,7 +351,7 @@
 			cursor.move(toOffset(node.getEndOffset()));
 		} else {
 			cursor.move(toOffset(node.getStartOffset() + 1));
-			cursor.select(toOffset(node.getEndOffset()));
+			cursor.select(toOffset(node.getEndOffset() - 1));
 		}
 	}