Index: modules/swing/src/test/api/java/common/javax/swing/text/View_VisualPosition_PartTest.java
===================================================================
--- modules/swing/src/test/api/java/common/javax/swing/text/View_VisualPosition_PartTest.java (revision 0)
+++ modules/swing/src/test/api/java/common/javax/swing/text/View_VisualPosition_PartTest.java (revision 0)
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package javax.swing.text;
+
+import static javax.swing.text.VisualPositionHelper.assertNextPosition;
+
+import java.awt.Rectangle;
+import java.awt.Shape;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests View.getNextVisualPositionFrom method on
+ * PlainView which doesn't overrides this method.
+ *
+ *
The view is constructed on the element representing
+ * the second paragraph
+ * in document (PlainDocument).
+ *
+ *
Only View.EAST (right) and View.WEST (left)
+ * directions are tested here.
+ */
+public class View_VisualPosition_PartTest extends TestCase {
+ private Document doc;
+ private Element root;
+ private Element element;
+ private int startOffset;
+ private int endOffset;
+ private int length;
+
+ private View view;
+ private Shape alloc;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ doc = new PlainDocument();
+ doc.insertString(0, "line 1\nthe second line is rather long\n"
+ + "the third line", null);
+ length = doc.getLength();
+
+ root = doc.getDefaultRootElement();
+ element = root.getElement(1);
+ startOffset = element.getStartOffset();
+ endOffset = element.getEndOffset();
+
+ view = new PlainView(element);
+
+ alloc = new Rectangle(10, 15, 100, 18);
+ }
+
+ public void testGetNextVisualPositionFrom_Right()
+ throws BadLocationException {
+
+ for (int i = 0; i < length; i++) {
+ assertNextPosition(i + 1, i, View.EAST, view, alloc);
+ };
+ }
+
+ public void testGetNextVisualPositionFrom_RightAtBeginning()
+ throws BadLocationException {
+
+ assertNextPosition(startOffset, -1, View.EAST, view, alloc);
+
+ assertNextPosition(1, 0, View.EAST, view, alloc);
+ assertNextPosition(-1, -2, View.EAST, view, alloc);
+ assertNextPosition(-2, -3, View.EAST, view, alloc);
+ assertNextPosition(-9, -10, View.EAST, view, alloc);
+
+ for (int i = startOffset - 2; i <= startOffset + 2; i++) {
+ assertNextPosition(i + 1, i, View.EAST, view, alloc);
+ }
+ }
+
+ public void testGetNextVisualPositionFrom_RightAtEnd()
+ throws BadLocationException {
+
+ for (int i = endOffset - 2; i <= endOffset + 2; i++) {
+ assertNextPosition(i + 1, i, View.EAST, view, alloc);
+ }
+
+ assertNextPosition(length, length - 1, View.EAST, view, alloc);
+ assertNextPosition(length, length, View.EAST, view, alloc);
+ assertNextPosition(length, length + 1, View.EAST, view, alloc);
+ assertNextPosition(length, length + 2, View.EAST, view, alloc);
+
+ assertNextPosition(length, length + 10, View.EAST, view, alloc);
+ }
+
+ public void testGetNextVisualPositionFrom_Left()
+ throws BadLocationException {
+
+ for (int i = 1; i <= length; i++) {
+ assertNextPosition(i - 1, i, View.WEST, view, alloc);
+ };
+ }
+
+ public void testGetNextVisualPositionFrom_LeftAtBeginning()
+ throws BadLocationException {
+
+ assertNextPosition(endOffset - 1, -1, View.WEST, view, alloc);
+
+ assertNextPosition(0, 0, View.WEST, view, alloc);
+ assertNextPosition(0, -2, View.WEST, view, alloc);
+ assertNextPosition(0, -3, View.WEST, view, alloc);
+ assertNextPosition(0, -10, View.WEST, view, alloc);
+
+ for (int i = startOffset - 2; i <= startOffset + 2; i++) {
+ assertNextPosition(i - 1, i, View.WEST, view, alloc);
+ }
+ }
+
+ public void testGetNextVisualPositionFrom_LeftAtEnd()
+ throws BadLocationException {
+
+ for (int i = endOffset - 2; i <= endOffset + 2; i++) {
+ assertNextPosition(i - 1, i, View.WEST, view, alloc);
+ }
+
+ assertNextPosition(length - 2, length - 1, View.WEST, view, alloc);
+ assertNextPosition(length - 1, length, View.WEST, view, alloc);
+ assertNextPosition(length, length + 1, View.WEST, view, alloc);
+ assertNextPosition(length + 1, length + 2, View.WEST, view, alloc);
+ assertNextPosition(length + 9, length + 10, View.WEST, view, alloc);
+ }
+}
Index: modules/swing/src/test/api/java/common/javax/swing/text/View_VisualPositionTest.java
===================================================================
--- modules/swing/src/test/api/java/common/javax/swing/text/View_VisualPositionTest.java (revision 0)
+++ modules/swing/src/test/api/java/common/javax/swing/text/View_VisualPositionTest.java (revision 0)
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package javax.swing.text;
+
+import static javax.swing.text.VisualPositionHelper.assertNextPosition;
+import junit.framework.TestCase;
+
+/**
+ * Tests View.getNextVisualPositionFrom method on
+ * PlainView which doesn't overrides this method.
+ *
+ *
The view is constructed on the root element of
+ * document (PlainDocument).
+ *
+ *
Only View.EAST (right) and View.WEST (left)
+ * directions are tested here.
+ */
+public class View_VisualPositionTest extends TestCase {
+ private Document doc;
+ private View view;
+ private Element root;
+ private int length;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ doc = new PlainDocument();
+ doc.insertString(0, "line 1\nthe second line is rather long\n"
+ + "the third line", null);
+ length = doc.getLength();
+
+ root = doc.getDefaultRootElement();
+ view = new PlainView(root);
+ }
+
+ public void testGetNextVisualPositionFrom_Right() throws BadLocationException {
+ for (int i = 0; i < length; i++) {
+ assertNextPosition(i + 1, i, View.EAST, view, null);
+ };
+ }
+
+ public void testGetNextVisualPositionFrom_RightAtBeginning()
+ throws BadLocationException {
+
+ assertNextPosition(0, -1, View.EAST, view, null);
+
+ assertNextPosition(-1, -2, View.EAST, view, null);
+ assertNextPosition(-2, -3, View.EAST, view, null);
+ assertNextPosition(-3, -4, View.EAST, view, null);
+
+ assertNextPosition(-9, -10, View.EAST, view, null);
+ }
+
+ public void testGetNextVisualPositionFrom_RightAtEnd()
+ throws BadLocationException {
+
+ assertNextPosition(length, length - 1, View.EAST, view, null);
+ assertNextPosition(length, length, View.EAST, view, null);
+
+ assertNextPosition(length, length + 1, View.EAST, view, null);
+ assertNextPosition(length, length + 2, View.EAST, view, null);
+ assertNextPosition(length, length + 3, View.EAST, view, null);
+
+ assertNextPosition(length, length + 10, View.EAST, view, null);
+ }
+
+ public void testGetNextVisualPositionFrom_Left() throws BadLocationException {
+ for (int i = 1; i <= length; i++) {
+ assertNextPosition(i - 1, i, View.WEST, view, null);
+ };
+ }
+
+ public void testGetNextVisualPositionFrom_LeftAtBeginning()
+ throws BadLocationException {
+
+ assertNextPosition(0, 1, View.WEST, view, null);
+ assertNextPosition(0, 0, View.WEST, view, null);
+
+ assertNextPosition(length, -1, View.WEST, view, null);
+
+ assertNextPosition(0, -2, View.WEST, view, null);
+ assertNextPosition(0, -3, View.WEST, view, null);
+
+ assertNextPosition(0, -10, View.WEST, view, null);
+ }
+
+ public void testGetNextVisualPositionFrom_LeftAtEnd()
+ throws BadLocationException {
+
+ assertNextPosition(length - 1, length, View.WEST, view, null);
+ assertNextPosition(length, length + 1, View.WEST, view, null);
+
+ assertNextPosition(length + 1, length + 2, View.WEST, view, null);
+ assertNextPosition(length + 2, length + 3, View.WEST, view, null);
+
+ assertNextPosition(length + 9, length + 10, View.WEST, view, null);
+ }
+}
Index: modules/swing/src/test/api/java/common/javax/swing/text/VisualPositionHelper.java
===================================================================
--- modules/swing/src/test/api/java/common/javax/swing/text/VisualPositionHelper.java (revision 0)
+++ modules/swing/src/test/api/java/common/javax/swing/text/VisualPositionHelper.java (revision 0)
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package javax.swing.text;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertSame;
+
+import java.awt.Shape;
+
+import javax.swing.text.Position.Bias;
+
+/**
+ * Utility class to support testing of
+ * View.getNextVisualPositionFrom method.
+ */
+public final class VisualPositionHelper {
+ private static final Bias[] biasRet = new Bias[1];
+ private static final Bias[] biases = new Bias[] {Bias.Forward,
+ Bias.Backward};
+
+ private VisualPositionHelper() {
+ }
+
+ public static void assertNextPosition(final int expectedPosition,
+ final int offset,
+ final int direction,
+ final View view,
+ final Shape allocation)
+ throws BadLocationException {
+
+ for (Bias bias : biases) {
+ biasRet[0] = null;
+ assertEquals(bias + " from " + offset,
+ expectedPosition,
+ view.getNextVisualPositionFrom(offset, bias,
+ allocation, direction,
+ biasRet));
+ assertSame(Bias.Forward, biasRet[0]);
+ }
+ }
+
+ public static void assertNextBiasedPosition(final int expectedPosition,
+ final int offset,
+ final int direction,
+ final View view,
+ final Shape allocation,
+ final Bias bias)
+ throws BadLocationException {
+
+ biasRet[0] = null;
+ assertEquals(bias + " from " + offset,
+ expectedPosition,
+ view.getNextVisualPositionFrom(offset, bias,
+ allocation, direction,
+ biasRet));
+ assertSame(Bias.Forward, biasRet[0]);
+ }
+
+ public static void assertNextForwardPosition(final int expectedPosition,
+ final int offset,
+ final int direction,
+ final View view,
+ final Shape allocation)
+ throws BadLocationException {
+
+ assertNextBiasedPosition(expectedPosition,
+ offset, direction, view,
+ allocation, Bias.Forward);
+ }
+
+ public static void assertNextBackwardPosition(final int expectedPosition,
+ final int offset,
+ final int direction,
+ final View view,
+ final Shape allocation)
+ throws BadLocationException {
+
+ assertNextBiasedPosition(expectedPosition,
+ offset, direction, view,
+ allocation, Bias.Backward);
+ }
+}
Index: modules/swing/src/test/api/java/common/javax/swing/text/CompositeView_VisualPositionTest.java
===================================================================
--- modules/swing/src/test/api/java/common/javax/swing/text/CompositeView_VisualPositionTest.java (revision 0)
+++ modules/swing/src/test/api/java/common/javax/swing/text/CompositeView_VisualPositionTest.java (revision 0)
@@ -0,0 +1,822 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package javax.swing.text;
+
+import java.awt.Shape;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.text.Position.Bias;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests how CompositeView traverses view hierarchy to calculate
+ * next visual position.
+ *
+ *
The behaviour is tested using BoxView
+ * (instead of CompositeView),
+ * and GlyphView instances are used as child views.
+ *
+ *
Only View.EAST (right) and View.WEST (left)
+ * directions are tested here.
+ */
+public class CompositeView_VisualPositionTest extends TestCase {
+ private static class FlipCallResult {
+ private final View view;
+ private final int offset;
+ private final Bias bias;
+ private final boolean result;
+
+ FlipCallResult(final View view,
+ final int offset, final Bias bias,
+ final boolean result) {
+ this.view = view;
+ this.offset = offset;
+ this.bias = bias;
+ this.result = result;
+ }
+
+ void assertValues(final View view,
+ final int offset, final Bias bias,
+ final boolean result) {
+ assertSame("Flip.view", view, this.view);
+ assertEquals("Flip.offset", offset, this.offset);
+ assertSame("Flip.bias", bias, this.bias);
+ assertEquals("Flip.result", result, this.result);
+ }
+ }
+
+ private static class VisPosCallResult {
+ private final View view;
+ private final int offset;
+ private final Bias bias;
+ private final int result;
+ private final Bias resultBias;
+
+ VisPosCallResult(final View view,
+ final int offset, final Bias bias,
+ final int result, final Bias resultBias) {
+ this.view = view;
+ this.offset = offset;
+ this.bias = bias;
+ this.result = result;
+ this.resultBias = resultBias;
+ }
+
+ void assertValues(final View view,
+ final int offset, final Bias bias,
+ final int result, final Bias resultBias) {
+ assertSame("VisPos.view", view, this.view);
+ assertEquals("VisPos.offset", offset, this.offset);
+ assertSame("VisPos.bias", bias, this.bias);
+ assertEquals("VisPos.result", result, this.result);
+ assertSame("VisPos.resultBias", resultBias, this.resultBias);
+ }
+ }
+
+ private static final Bias Forward = Bias.Forward;
+ private static final Bias Backward = Bias.Backward;
+
+ private Document doc;
+ private View view;
+ private Element root;
+ private int length;
+ private Bias[] biasRet;
+ private ViewFactory factory;
+
+ private List