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: *
021: */package javax.swing.text;
022:
023: import java.awt.Container;
024: import java.awt.Rectangle;
025: import java.awt.Shape;
026: import javax.swing.BasicSwingTestCase;
027: import javax.swing.JTextArea;
028: import javax.swing.SwingConstants;
029: import javax.swing.SwingTestCase;
030: import javax.swing.text.CompositeViewTest.CompositeViewImpl;
031: import javax.swing.text.Position.Bias;
032: import javax.swing.text.ViewTestHelpers.ChildView;
033: import javax.swing.text.ViewTestHelpers.ChildrenFactory;
034:
035: public class CompositeView_NextNSVisPosTest extends SwingTestCase {
036: private Document doc;
037:
038: private Element root;
039:
040: private CompositeView view;
041:
042: private Shape shape;
043:
044: private JTextArea area;
045:
046: private Bias[] bias;
047:
048: @Override
049: protected void setUp() throws Exception {
050: area = new JTextArea() {
051: private static final long serialVersionUID = 1L;
052:
053: // Change the behavior as if area has our view
054: @Override
055: public Rectangle modelToView(final int pos)
056: throws BadLocationException {
057: return (Rectangle) view.modelToView(pos, shape,
058: Bias.Forward);
059: }
060: };
061: area.setSize(100, 150);
062: doc = area.getDocument();
063: doc
064: .insertString(0, "line1\nline2\n\u05DC\u05DD\nline3\n",
065: null);
066: // positions: 012345 678901 2 3 4 567890
067: // 0 1 2
068: root = doc.getDefaultRootElement();
069: view = new CompositeViewImpl(root) {
070: private int getY(final ChildView view) {
071: int result = 0;
072: for (int i = 0; i < view.getID(); i++) {
073: result += ViewTestHelpers.getHeight(i);
074: }
075: return result;
076: }
077:
078: @Override
079: protected void childAllocation(final int index,
080: final Rectangle rc) {
081: // The each view allocation is 16 pixels of height and
082: // represents a line-like rectangle
083: ChildView view = (ChildView) getView(index);
084: rc.y += getY(view);
085: rc.height = ViewTestHelpers.getHeight(view.getID());
086: }
087:
088: @Override
089: protected View getViewAtPoint(final int x, final int y,
090: final Rectangle rc) {
091: int index = getViewIndex(x, y, rc);
092: if (index != -1) {
093: childAllocation(index, rc);
094: return getView(index);
095: }
096: return null;
097: }
098:
099: // Link text component (area) with the view
100: @Override
101: public Container getContainer() {
102: return area;
103: }
104: };
105: view.loadChildren(new ChildrenFactory());
106: shape = new Rectangle(100, 200, 190, 560);
107: bias = new Bias[1];
108: super .setUp();
109: }
110:
111: public void testGetNextEastWestVisualPositionFrom()
112: throws BadLocationException {
113: // EAST
114: assertEquals(1, view.getNextEastWestVisualPositionFrom(0,
115: Bias.Forward, shape, SwingConstants.EAST, bias));
116: assertEquals(2, view.getNextEastWestVisualPositionFrom(1,
117: Bias.Forward, shape, SwingConstants.EAST, bias));
118: // WEST
119: assertEquals(1, view.getNextEastWestVisualPositionFrom(2,
120: Bias.Forward, shape, SwingConstants.WEST, bias));
121: assertEquals(0, view.getNextEastWestVisualPositionFrom(1,
122: Bias.Forward, shape, SwingConstants.WEST, bias));
123: // Invalid offset
124: try {
125: view.getNextEastWestVisualPositionFrom(-1, Bias.Forward,
126: shape, SwingConstants.EAST, bias);
127: if (BasicSwingTestCase.isHarmony()) {
128: fail("BadLocationException must be thrown");
129: }
130: } catch (BadLocationException e) {
131: }
132: try {
133: view.getNextEastWestVisualPositionFrom(doc.getLength() + 2,
134: Bias.Forward, shape, SwingConstants.EAST, bias);
135: if (BasicSwingTestCase.isHarmony()) {
136: fail("BadLocationException must be thrown");
137: }
138: } catch (BadLocationException e) {
139: } catch (ArrayIndexOutOfBoundsException e) {
140: if (BasicSwingTestCase.isHarmony()) {
141: fail("ArrayIndexOutOfBoundsException must not be thrown");
142: }
143: }
144: // Invalid direction
145: try {
146: view.getNextEastWestVisualPositionFrom(0, Bias.Backward,
147: shape, SwingConstants.NORTH_EAST, bias);
148: fail("IllegalArgumentException must be thrown");
149: } catch (IllegalArgumentException e) {
150: }
151: }
152:
153: public void testGetNextNorthSouthVisualPositionFrom()
154: throws BadLocationException {
155: // NORTH
156: assertEquals(-1, view.getNextNorthSouthVisualPositionFrom(0,
157: Bias.Forward, shape, SwingConstants.NORTH, bias));
158: assertEquals(1, view.getNextNorthSouthVisualPositionFrom(7,
159: Bias.Forward, shape, SwingConstants.NORTH, bias));
160: // SOUTH
161: assertEquals(8, view.getNextNorthSouthVisualPositionFrom(2,
162: Bias.Forward, shape, SwingConstants.SOUTH, bias));
163: assertEquals(14, view.getNextNorthSouthVisualPositionFrom(8,
164: Bias.Forward, shape, SwingConstants.SOUTH, bias));
165: try {
166: view.getNextNorthSouthVisualPositionFrom(-1, Bias.Forward,
167: shape, SwingConstants.NORTH, bias);
168: if (BasicSwingTestCase.isHarmony()) {
169: fail("BadLocationException must be thrown");
170: }
171: } catch (BadLocationException e) {
172: }
173: // Invalid direction
174: try {
175: view.getNextNorthSouthVisualPositionFrom(0, Bias.Backward,
176: shape, SwingConstants.NORTH_EAST, bias);
177: fail("IllegalArgumentException must be thrown");
178: } catch (IllegalArgumentException e) {
179: }
180: }
181: }
|