001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package javax.swing.text;
018:
019: import static javax.swing.text.VisualPositionHelper.assertNextPosition;
020:
021: import java.awt.Rectangle;
022: import java.awt.Shape;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Tests <code>View.getNextVisualPositionFrom</code> method on
028: * <code>PlainView</code> which doesn't overrides this method.
029: *
030: * <p>The view is constructed on the <em>element</em> representing
031: * the <em>second paragraph</em>
032: * in document (<code>PlainDocument</code>).
033: *
034: * <p>Only <code>View.EAST</code> (right) and <code>View.WEST</code> (left)
035: * directions are tested here.
036: */
037: public class View_VisualPosition_PartTest extends TestCase {
038: private Document doc;
039: private Element root;
040: private Element element;
041: private int startOffset;
042: private int endOffset;
043: private int length;
044:
045: private View view;
046: private Shape alloc;
047:
048: @Override
049: protected void setUp() throws Exception {
050: super .setUp();
051: doc = new PlainDocument();
052: doc.insertString(0, "line 1\nthe second line is rather long\n"
053: + "the third line", null);
054: length = doc.getLength();
055:
056: root = doc.getDefaultRootElement();
057: element = root.getElement(1);
058: startOffset = element.getStartOffset();
059: endOffset = element.getEndOffset();
060:
061: view = new PlainView(element);
062:
063: alloc = new Rectangle(10, 15, 100, 18);
064: }
065:
066: public void testGetNextVisualPositionFrom_Right()
067: throws BadLocationException {
068:
069: for (int i = 0; i < length; i++) {
070: assertNextPosition(i + 1, i, View.EAST, view, alloc);
071: }
072: ;
073: }
074:
075: public void testGetNextVisualPositionFrom_RightAtBeginning()
076: throws BadLocationException {
077:
078: assertNextPosition(startOffset, -1, View.EAST, view, alloc);
079:
080: assertNextPosition(1, 0, View.EAST, view, alloc);
081: assertNextPosition(-1, -2, View.EAST, view, alloc);
082: assertNextPosition(-2, -3, View.EAST, view, alloc);
083: assertNextPosition(-9, -10, View.EAST, view, alloc);
084:
085: for (int i = startOffset - 2; i <= startOffset + 2; i++) {
086: assertNextPosition(i + 1, i, View.EAST, view, alloc);
087: }
088: }
089:
090: public void testGetNextVisualPositionFrom_RightAtEnd()
091: throws BadLocationException {
092:
093: for (int i = endOffset - 2; i <= endOffset + 2; i++) {
094: assertNextPosition(i + 1, i, View.EAST, view, alloc);
095: }
096:
097: assertNextPosition(length, length - 1, View.EAST, view, alloc);
098: assertNextPosition(length, length, View.EAST, view, alloc);
099: assertNextPosition(length, length + 1, View.EAST, view, alloc);
100: assertNextPosition(length, length + 2, View.EAST, view, alloc);
101:
102: assertNextPosition(length, length + 10, View.EAST, view, alloc);
103: }
104:
105: public void testGetNextVisualPositionFrom_Left()
106: throws BadLocationException {
107:
108: for (int i = 1; i <= length; i++) {
109: assertNextPosition(i - 1, i, View.WEST, view, alloc);
110: }
111: ;
112: }
113:
114: public void testGetNextVisualPositionFrom_LeftAtBeginning()
115: throws BadLocationException {
116:
117: assertNextPosition(endOffset - 1, -1, View.WEST, view, alloc);
118:
119: assertNextPosition(0, 0, View.WEST, view, alloc);
120: assertNextPosition(0, -2, View.WEST, view, alloc);
121: assertNextPosition(0, -3, View.WEST, view, alloc);
122: assertNextPosition(0, -10, View.WEST, view, alloc);
123:
124: for (int i = startOffset - 2; i <= startOffset + 2; i++) {
125: assertNextPosition(i - 1, i, View.WEST, view, alloc);
126: }
127: }
128:
129: public void testGetNextVisualPositionFrom_LeftAtEnd()
130: throws BadLocationException {
131:
132: for (int i = endOffset - 2; i <= endOffset + 2; i++) {
133: assertNextPosition(i - 1, i, View.WEST, view, alloc);
134: }
135:
136: assertNextPosition(length - 2, length - 1, View.WEST, view,
137: alloc);
138: assertNextPosition(length - 1, length, View.WEST, view, alloc);
139: assertNextPosition(length, length + 1, View.WEST, view, alloc);
140: assertNextPosition(length + 1, length + 2, View.WEST, view,
141: alloc);
142: assertNextPosition(length + 9, length + 10, View.WEST, view,
143: alloc);
144: }
145: }
|