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: /**
018: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Point;
023: import java.awt.Shape;
024: import javax.swing.JFrame;
025: import javax.swing.JTextArea;
026: import javax.swing.SwingConstants;
027: import javax.swing.SwingTestCase;
028: import javax.swing.text.Position.Bias;
029:
030: /**
031: * Tests default implementation of View.getNextVisualPositionFrom method.
032: *
033: */
034: public class View_NextVisualPositionFromTest extends SwingTestCase {
035: /**
036: * Specialized caret to get magic caret position. This ensures
037: * the one has expected value and don't change.
038: */
039: private static class MagicCaret extends DefaultCaret {
040: private static final long serialVersionUID = 1L;
041:
042: public Point magicPoint;
043:
044: @Override
045: public Point getMagicCaretPosition() {
046: return magicPoint;
047: }
048: }
049:
050: private JTextArea area;
051:
052: private MagicCaret caret;
053:
054: private Document doc;
055:
056: private JFrame frame;
057:
058: private Shape shape;
059:
060: private View view;
061:
062: private Bias[] bias;
063:
064: public void testGetNextVisualPositionFromEast()
065: throws BadLocationException {
066: assertEquals(1, view.getNextVisualPositionFrom(0, Bias.Forward,
067: shape, SwingConstants.EAST, bias));
068: assertSame(Bias.Forward, bias[0]);
069: bias[0] = null;
070: assertEquals(1, view.getNextVisualPositionFrom(0,
071: Bias.Backward, shape, SwingConstants.EAST, bias));
072: assertSame(Bias.Forward, bias[0]);
073: bias[0] = null;
074: assertEquals(2, view.getNextVisualPositionFrom(1, Bias.Forward,
075: shape, SwingConstants.EAST, bias));
076: assertSame(Bias.Forward, bias[0]);
077: bias[0] = null;
078: assertEquals(2, view.getNextVisualPositionFrom(1,
079: Bias.Backward, shape, SwingConstants.EAST, bias));
080: assertSame(Bias.Forward, bias[0]);
081: bias[0] = null;
082: // End of document: try to go further
083: assertEquals(doc.getLength(), view.getNextVisualPositionFrom(
084: doc.getLength(), Bias.Forward, shape,
085: SwingConstants.EAST, bias));
086: }
087:
088: public void testGetNextVisualPositionFromWest()
089: throws BadLocationException {
090: assertEquals(1, view.getNextVisualPositionFrom(2, Bias.Forward,
091: shape, SwingConstants.WEST, bias));
092: assertSame(Bias.Forward, bias[0]);
093: bias[0] = null;
094: assertEquals(1, view.getNextVisualPositionFrom(2,
095: Bias.Backward, shape, SwingConstants.WEST, bias));
096: assertSame(Bias.Forward, bias[0]);
097: bias[0] = null;
098: }
099:
100: public void testGetNextVisualPositionFromNorth()
101: throws BadLocationException {
102: Shape posRect = view.modelToView(doc.getLength(), shape,
103: Bias.Forward);
104: caret.magicPoint = posRect.getBounds().getLocation();
105: assertNotNull(caret.getMagicCaretPosition());
106: assertEquals(17, view.getNextVisualPositionFrom(
107: doc.getLength(), Bias.Forward, shape,
108: SwingConstants.NORTH, bias));
109: assertEquals(10, view.getNextVisualPositionFrom(17,
110: Bias.Forward, shape, SwingConstants.NORTH, bias));
111: assertEquals(6, view.getNextVisualPositionFrom(10,
112: Bias.Forward, shape, SwingConstants.NORTH, bias));
113: // We reached the first line
114: assertEquals(-1, view.getNextVisualPositionFrom(6,
115: Bias.Forward, shape, SwingConstants.NORTH, bias));
116: // No magic position
117: caret.magicPoint = null;
118: assertNull(area.getCaret().getMagicCaretPosition());
119: assertEquals(2, view.getNextVisualPositionFrom(10,
120: Bias.Forward, shape, SwingConstants.NORTH, bias));
121: }
122:
123: public void testGetNextVisualPositionFromSouth()
124: throws BadLocationException {
125: Shape posRect = view.modelToView(doc.getLength(), shape,
126: Bias.Forward);
127: caret.magicPoint = posRect.getBounds().getLocation();
128: assertNotNull(area.getCaret().getMagicCaretPosition());
129: assertEquals(10, view.getNextVisualPositionFrom(6,
130: Bias.Forward, shape, SwingConstants.SOUTH, bias));
131: assertEquals(17, view.getNextVisualPositionFrom(10,
132: Bias.Forward, shape, SwingConstants.SOUTH, bias));
133: // 27 == doc.getLength()
134: assertEquals(27, view.getNextVisualPositionFrom(17,
135: Bias.Forward, shape, SwingConstants.SOUTH, bias));
136: // We reached the last line and the last character in document
137: assertEquals(27, view.getNextVisualPositionFrom(27,
138: Bias.Forward, shape, SwingConstants.SOUTH, bias));
139: // No magic position
140: caret.magicPoint = null;
141: assertNull(area.getCaret().getMagicCaretPosition());
142: assertEquals(2, view.getNextVisualPositionFrom(10,
143: Bias.Forward, shape, SwingConstants.NORTH, bias));
144: assertEquals(13, view.getNextVisualPositionFrom(10,
145: Bias.Forward, shape, SwingConstants.SOUTH, bias));
146: }
147:
148: public void testGetNextVisualPositionFromInvalid()
149: throws BadLocationException {
150: // BadLocation left
151: try {
152: int p = view.getNextVisualPositionFrom(-2, Bias.Forward,
153: shape, SwingConstants.WEST, bias);
154: if (!isHarmony()) {
155: assertEquals(0, p);
156: // If we go to WEST from -1, we get to document end
157: p = view.getNextVisualPositionFrom(-1, Bias.Forward,
158: shape, SwingConstants.WEST, bias);
159: assertEquals(doc.getLength(), p);
160: } else {
161: fail("BadLocationException is expected");
162: }
163: } catch (BadLocationException e) {
164: }
165: // BadLocation right
166: try {
167: int p = view.getNextVisualPositionFrom(doc.getLength() + 1,
168: Bias.Forward, shape, SwingConstants.EAST, bias);
169: if (!isHarmony()) {
170: assertEquals(doc.getLength(), p);
171: } else {
172: fail("BadLocationException is expected");
173: }
174: } catch (BadLocationException e) {
175: }
176: // IllegalArgument for direction
177: try {
178: view.getNextVisualPositionFrom(0, Bias.Forward, shape,
179: SwingConstants.NORTH_EAST, bias);
180: fail("IllegalArgumentException is expected");
181: } catch (IllegalArgumentException e) {
182: }
183: // ArrayIndexOutOfBounds for bias
184: try {
185: view.getNextVisualPositionFrom(0, Bias.Forward, shape,
186: SwingConstants.EAST, new Bias[0]);
187: fail("ArrayIndexOutOfBoundsException is expected");
188: } catch (ArrayIndexOutOfBoundsException e) {
189: }
190: // NullPointerException for bias
191: try {
192: view.getNextVisualPositionFrom(0, Bias.Forward, shape,
193: SwingConstants.EAST, null);
194: fail("NullPointerException is expected");
195: } catch (NullPointerException e) {
196: }
197: }
198:
199: @Override
200: protected void setUp() throws Exception {
201: super .setUp();
202: frame = new JFrame("Next Position Test");
203: area = new JTextArea();
204: area.setCaret(caret = new MagicCaret());
205: frame.getContentPane().add(area);
206: area.setText("1: 0123\n" +
207: //01234567
208: "2:\n" +
209: //890
210: "3: 012345\n" +
211: //1234567890
212: "4: 012");
213: //123456
214: doc = area.getDocument();
215: frame.setSize(100, 150);
216: frame.pack();
217: view = area.getUI().getRootView(area).getView(0);
218: shape = area.getBounds();
219: bias = new Bias[1];
220: assertNull(bias[0]);
221: assertEquals(27, doc.getLength());
222: }
223:
224: @Override
225: protected void tearDown() throws Exception {
226: super.tearDown();
227: frame.dispose();
228: }
229: }
|