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.FontMetrics;
023: import java.awt.Rectangle;
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.JFrame;
026: import javax.swing.JTextArea;
027: import javax.swing.SwingConstants;
028: import javax.swing.SwingTestCase;
029: import javax.swing.text.Position.Bias;
030:
031: /**
032: * Tests model/view conversions in PlainViewI18N class.
033: * <p>Some tests tests our implementation only since
034: * getNextVisualPosition differs from the 1.5 and it was made knowingly.
035: *
036: */
037: public class PlainViewI18N_ModelViewTest extends SwingTestCase {
038: private Document doc;
039:
040: private View view;
041:
042: private Rectangle shape;
043:
044: private JFrame frame;
045:
046: private JTextArea area;
047:
048: private Bias[] bias;
049:
050: private FontMetrics metrics;
051:
052: private int rtlWidth;
053:
054: @Override
055: protected void setUp() throws Exception {
056: super .setUp();
057: frame = new JFrame("PlainViewI18N Model/View Test");
058: area = new JTextArea("\u05DC\u05DD\t\u05DE\u05DF\u05E0abcd");
059: // 0 1 2 3 4 5 6789
060: frame.getContentPane().add(area);
061: frame.setSize(150, 100);
062: frame.pack();
063: doc = area.getDocument();
064: view = area.getUI().getRootView(area).getView(0);
065: shape = area.getVisibleRect();
066: bias = new Bias[1];
067: metrics = area.getFontMetrics(area.getFont());
068: rtlWidth = metrics.stringWidth(doc.getText(0, 2));
069: }
070:
071: @Override
072: protected void tearDown() throws Exception {
073: super .tearDown();
074: frame.dispose();
075: }
076:
077: /**
078: * Tests view.getNextVisualPositionFrom position 0 when moving EAST
079: * with Forward and Backward biases.
080: */
081: public void testGetNextVisualPositionEast01()
082: throws BadLocationException {
083: if (!isHarmony()) {
084: return;
085: }
086: assertEquals(2, view.getNextVisualPositionFrom(0, Bias.Forward,
087: shape, SwingConstants.EAST, bias));
088: assertSame(Bias.Forward, bias[0]);
089: bias[0] = null;
090: assertEquals(2, view.getNextVisualPositionFrom(0,
091: Bias.Backward, shape, SwingConstants.EAST, bias));
092: assertSame(Bias.Backward, bias[0]);
093: bias[0] = null;
094: }
095:
096: /**
097: * Tests view.getNextVisualPositionFrom position 1 when moving EAST
098: * with Forward and Backward biases.
099: */
100: public void testGetNextVisualPositionEast02()
101: throws BadLocationException {
102: assertEquals(0, view.getNextVisualPositionFrom(1, Bias.Forward,
103: shape, SwingConstants.EAST, bias));
104: assertSame(Bias.Forward, bias[0]);
105: bias[0] = null;
106: assertEquals(0, view.getNextVisualPositionFrom(1,
107: Bias.Backward, shape, SwingConstants.EAST, bias));
108: assertSame(Bias.Forward, bias[0]);
109: bias[0] = null;
110: }
111:
112: /**
113: * Tests view.getNextVisualPositionFrom position 2 when moving EAST
114: * with Forward and Backward biases.
115: */
116: public void testGetNextVisualPositionEast03()
117: throws BadLocationException {
118: if (!isHarmony()) {
119: return;
120: }
121: assertEquals(3, view.getNextVisualPositionFrom(2, Bias.Forward,
122: shape, SwingConstants.EAST, bias));
123: assertSame(Bias.Backward, bias[0]);
124: bias[0] = null;
125: assertEquals(1, view.getNextVisualPositionFrom(2,
126: Bias.Backward, shape, SwingConstants.EAST, bias));
127: assertSame(Bias.Forward, bias[0]);
128: bias[0] = null;
129: }
130:
131: /**
132: * Tests view.getNextVisualPositionFrom position 2 when moving EAST
133: * with Forward and Backward biases.
134: */
135: public void testGetNextVisualPositionEast04()
136: throws BadLocationException {
137: assertEquals(6, view.getNextVisualPositionFrom(3, Bias.Forward,
138: shape, SwingConstants.EAST, bias));
139: assertSame(Bias.Forward, bias[0]);
140: bias[0] = null;
141: assertEquals(6, view.getNextVisualPositionFrom(3,
142: Bias.Backward, shape, SwingConstants.EAST, bias));
143: assertSame(Bias.Backward, bias[0]);
144: bias[0] = null;
145: }
146:
147: /**
148: * Tests view.getNextVisualPositionFrom position 0 when moving WEST
149: * with Forward and Backward biases.
150: */
151: public void testGetNextVisualPositionWest01()
152: throws BadLocationException {
153: if (!isHarmony()) {
154: return;
155: }
156: assertEquals(1, view.getNextVisualPositionFrom(0, Bias.Forward,
157: shape, SwingConstants.WEST, bias));
158: assertSame(Bias.Forward, bias[0]);
159: bias[0] = null;
160: assertEquals(-1, view.getNextVisualPositionFrom(0,
161: Bias.Backward, shape, SwingConstants.WEST, bias));
162: assertSame(Bias.Forward, bias[0]);
163: bias[0] = null;
164: }
165:
166: /**
167: * Tests view.getNextVisualPositionFrom position 1 when moving WEST
168: * with Forward and Backward biases.
169: */
170: public void testGetNextVisualPositionWest02()
171: throws BadLocationException {
172: if (!isHarmony()) {
173: return;
174: }
175: assertEquals(2, view.getNextVisualPositionFrom(1, Bias.Forward,
176: shape, SwingConstants.WEST, bias));
177: assertSame(Bias.Backward, bias[0]);
178: bias[0] = null;
179: assertEquals(2, view.getNextVisualPositionFrom(1,
180: Bias.Backward, shape, SwingConstants.WEST, bias));
181: assertSame(Bias.Forward, bias[0]);
182: bias[0] = null;
183: }
184:
185: /**
186: * Tests view.getNextVisualPositionFrom position 2 when moving WEST
187: * with Forward and Backward biases.
188: */
189: public void testGetNextVisualPositionWest03()
190: throws BadLocationException {
191: if (!isHarmony()) {
192: return;
193: }
194: assertEquals(0, view.getNextVisualPositionFrom(2, Bias.Forward,
195: shape, SwingConstants.WEST, bias));
196: assertSame(Bias.Forward, bias[0]);
197: bias[0] = null;
198: assertEquals(0, view.getNextVisualPositionFrom(2,
199: Bias.Backward, shape, SwingConstants.WEST, bias));
200: assertSame(Bias.Forward, bias[0]);
201: bias[0] = null;
202: }
203:
204: /**
205: * Tests view.getNextVisualPositionFrom position 2 when moving WEST
206: * with Forward and Backward biases.
207: */
208: public void testGetNextVisualPositionWest04()
209: throws BadLocationException {
210: assertEquals(4, view.getNextVisualPositionFrom(3, Bias.Forward,
211: shape, SwingConstants.WEST, bias));
212: assertSame(Bias.Forward, bias[0]);
213: bias[0] = null;
214: assertEquals(2, view.getNextVisualPositionFrom(3,
215: Bias.Backward, shape, SwingConstants.WEST, bias));
216: assertSame(Bias.Forward, bias[0]);
217: bias[0] = null;
218: }
219:
220: private static void assertRectEquals(Rectangle rc, int x, int y,
221: int width, int height) {
222: assertEquals("Unexpected X", x, rc.x);
223: assertEquals("Unexpected Y", y, rc.y);
224: assertEquals("Unexpected width", width, rc.width);
225: assertEquals("Unexpected height", height, rc.height);
226: }
227:
228: /**
229: * Shows that modelToView may return the same value while positions
230: * are different (biases are the same).
231: * <p>In the same time it may return different values for the same offset
232: * when biases are different.
233: * <p>The offsets used are 0, 2.
234: */
235: public void testModelToView01() throws Exception {
236: Rectangle viewAt0F = (Rectangle) view.modelToView(0, shape,
237: Bias.Forward);
238: Rectangle viewAt2B = (Rectangle) view.modelToView(2, shape,
239: Bias.Forward);
240: assertEquals(viewAt0F.x, viewAt2B.x);
241: final int beforeTabWidth = metrics.stringWidth(doc
242: .getText(0, 2));
243: assertRectEquals(viewAt0F, beforeTabWidth, 0, 1, metrics
244: .getHeight());
245: assertRectEquals((Rectangle) view.modelToView(0, shape,
246: Bias.Backward),
247: BasicSwingTestCase.isHarmony() ? beforeTabWidth : 0, 0,
248: 1, metrics.getHeight());
249: }
250:
251: public void testModelToView02() throws Exception {
252: Rectangle viewAt1F = (Rectangle) view.modelToView(1, shape,
253: Bias.Forward);
254: Rectangle viewAt1B = (Rectangle) view.modelToView(1, shape,
255: Bias.Backward);
256: assertEquals(viewAt1F, viewAt1B);
257: assertRectEquals(viewAt1F, metrics.stringWidth(doc
258: .getText(1, 1)), 0, 1, metrics.getHeight());
259: }
260:
261: public void testViewToModel() throws Exception {
262: assertEquals(2, view.viewToModel(rtlWidth, 0, shape, bias));
263: assertSame(Bias.Forward, bias[0]);
264: bias[0] = null;
265: assertEquals(2, view.viewToModel(0, 0, shape, bias));
266: assertSame(Bias.Backward, bias[0]);
267: bias[0] = null;
268: assertEquals(0, view.viewToModel(rtlWidth - 1, 0, shape, bias));
269: assertSame(Bias.Forward, bias[0]);
270: bias[0] = null;
271: assertEquals(2, view.viewToModel(rtlWidth + 1, 0, shape, bias));
272: assertSame(Bias.Forward, bias[0]);
273: bias[0] = null;
274: }
275:
276: public void testViewToModelRTL() throws Exception {
277: View rtl;
278: if (BasicSwingTestCase.isHarmony()) {
279: rtl = view.getView(0).getView(0);
280: } else {
281: rtl = view.getView(0).getView(0).getView(0);
282: }
283: assertEquals(0, rtl.getStartOffset());
284: assertEquals(2, rtl.getEndOffset());
285: assertEquals(2, rtl.viewToModel(0, 0, shape, bias));
286: assertSame(Bias.Backward, bias[0]);
287: bias[0] = null;
288: assertEquals(2, rtl.viewToModel(rtlWidth, 0, shape, bias));
289: assertSame(Bias.Forward, bias[0]);
290: bias[0] = null;
291: assertEquals(2, rtl.viewToModel(rtlWidth + 1, 0, shape, bias));
292: assertSame(Bias.Forward, bias[0]);
293: bias[0] = null;
294: }
295:
296: public void testViewToModelLTR() throws Exception {
297: View ltr;
298: if (BasicSwingTestCase.isHarmony()) {
299: ltr = view.getView(0).getView(1);
300: } else {
301: ltr = view.getView(0).getView(0).getView(1);
302: }
303: assertEquals(2, ltr.getStartOffset());
304: assertEquals(3, ltr.getEndOffset());
305: assertEquals(2, ltr.viewToModel(rtlWidth, 0, shape, bias));
306: assertSame(Bias.Forward, bias[0]);
307: bias[0] = null;
308: assertEquals(2, ltr.viewToModel(rtlWidth - 1, 0, shape, bias));
309: assertSame(Bias.Forward, bias[0]);
310: bias[0] = null;
311: assertEquals(2, ltr.viewToModel(rtlWidth + 1, 0, shape, bias));
312: assertSame(Bias.Forward, bias[0]);
313: bias[0] = null;
314: Object tabProperty = view.getDocument().getProperty(
315: PlainDocument.tabSizeAttribute);
316: int tab = metrics.charWidth('m')
317: * ((Integer) tabProperty).intValue();
318: assertEquals(BasicSwingTestCase.isHarmony() ? 3 : 2, ltr
319: .viewToModel(tab, 0, shape, bias));
320: assertSame(Bias.Forward, bias[0]);
321: bias[0] = null;
322: }
323: }
|