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.Container;
023: import java.awt.FontMetrics;
024: import java.awt.Rectangle;
025: import javax.swing.JTextArea;
026: import javax.swing.SwingTestCase;
027:
028: /**
029: * Tests PlainViewI18N.LineView class.
030: */
031: public class PlainViewI18N_LineViewTest extends SwingTestCase {
032: /**
033: * This is helper class which has a JTextArea as a container without
034: * actually being associated with it. (Having it doesn't require
035: * to create JFrame etc.)
036: */
037: public static class PlainViewI18NWithTextArea extends PlainViewI18N {
038: public final JTextArea textArea;
039:
040: public PlainViewI18NWithTextArea(Element element, Document doc) {
041: super (element);
042: textArea = new JTextArea(doc);
043: }
044:
045: @Override
046: public Container getContainer() {
047: return textArea;
048: }
049: }
050:
051: public static final String LTR = "\u0061\u0062";
052:
053: public static final String RTL = "\u05DC\u05DD";
054:
055: public static final String newLine = "\n";
056:
057: public static final String defText = LTR + newLine + RTL + newLine
058: + LTR + RTL + newLine + RTL + LTR + RTL;
059:
060: public static final int LTRLength = LTR.length();
061:
062: public static final int RTLLength = RTL.length();
063:
064: private static final int HEIGHT = 200;
065:
066: private static final int WIDTH = 100;
067:
068: private static final int X_AXIS = View.X_AXIS;
069:
070: private static final int Y_AXIS = View.Y_AXIS;
071:
072: private Segment buffer;
073:
074: private PlainDocument doc;
075:
076: private PlainViewI18N parent;
077:
078: private Element root;
079:
080: private PlainViewI18N.LineView view;
081:
082: /**
083: * Tests getPreferredSpan with LRT only text
084: */
085: public void testGetPreferredSpan01() throws Exception {
086: if (!isHarmony()) {
087: return;
088: }
089: Element line = root.getElement(0);
090: view = parent.new LineView(line);
091: view.setParent(parent);
092: parent.setSize(WIDTH, HEIGHT); // this will initialize metrics
093: final FontMetrics metrics = getMetrics();
094: assertEquals(metrics.stringWidth(getViewText()), (int) view
095: .getPreferredSpan(X_AXIS));
096: assertEquals(metrics.getHeight(), (int) view
097: .getPreferredSpan(Y_AXIS));
098: // Insert tab character and recreate view
099: doc.insertString(line.getStartOffset() + 1, "\t", null);
100: view = parent.new LineView(line);
101: view.setParent(parent);
102: assertEquals(Utilities.getTabbedTextWidth(getViewTextSegment(),
103: metrics, 0, parent, 0), (int) view
104: .getPreferredSpan(X_AXIS));
105: assertEquals(metrics.getHeight(), (int) view
106: .getPreferredSpan(Y_AXIS));
107: }
108:
109: /**
110: * Tests getPreferredSpan with RTL only text
111: */
112: public void testGetPreferredSpan02() throws Exception {
113: if (!isHarmony()) {
114: return;
115: }
116: Element line = root.getElement(1);
117: view = parent.new LineView(line);
118: view.setParent(parent);
119: parent.setSize(WIDTH, HEIGHT); // this will initialize metrics
120: final FontMetrics metrics = getMetrics();
121: assertEquals(metrics.stringWidth(getViewText()), (int) view
122: .getPreferredSpan(X_AXIS));
123: assertEquals(metrics.getHeight(), (int) view
124: .getPreferredSpan(Y_AXIS));
125: // Insert tab character and recreate view
126: doc.insertString(line.getStartOffset() + 1, "\t", null);
127: view = parent.new LineView(line);
128: view.setParent(parent);
129: assertEquals(4, view.getViewCount());
130: assertEquals(Utilities.getTabbedTextWidth(getViewTextSegment(),
131: metrics, 0, parent, 0), (int) view
132: .getPreferredSpan(X_AXIS));
133: assertEquals(metrics.getHeight(), (int) view
134: .getPreferredSpan(Y_AXIS));
135: }
136:
137: public void testGetResizeWeight() {
138: if (!isHarmony()) {
139: return;
140: }
141: parent.getPreferredSpan(X_AXIS); // Update metrics
142: view = parent.new LineView(root.getElement(3));
143: view.loadChildren(null);
144: assertEquals(0, view.getResizeWeight(X_AXIS));
145: assertEquals(0, view.getResizeWeight(Y_AXIS));
146: }
147:
148: /**
149: * Tests constructor and loadChildren behaviour: start/end offset and
150: * number of children.
151: * <b>Only LTR text</b>.
152: */
153: public void testLineView01() {
154: if (!isHarmony()) {
155: return;
156: }
157: view = parent.new LineView(root.getElement(0));
158: view.loadChildren(null);
159: assertEquals(1, view.getViewCount());
160: checkChild(view.getView(0), view.getStartOffset(), view
161: .getEndOffset());
162: }
163:
164: /**
165: * <b>Only RTL text</b>.
166: */
167: public void testLineView02() {
168: if (!isHarmony()) {
169: return;
170: }
171: view = parent.new LineView(root.getElement(1));
172: view.loadChildren(null);
173: assertEquals(1, view.getViewCount());
174: checkChild(view.getView(0), view.getStartOffset(), view
175: .getEndOffset());
176: }
177:
178: /**
179: * <b>LTR + RTL</b>.
180: */
181: public void testLineView03() {
182: if (!isHarmony()) {
183: return;
184: }
185: view = parent.new LineView(root.getElement(2));
186: view.loadChildren(null);
187: assertEquals(3, view.getViewCount());
188: int offset = view.getStartOffset();
189: checkChild(view.getView(0), offset, offset + LTRLength);
190: offset += LTRLength;
191: checkChild(view.getView(1), offset, offset + RTLLength);
192: offset += RTLLength;
193: checkChild(view.getView(2), offset, offset + newLine.length());
194: }
195:
196: /**
197: * <b>RTL + LTR + RTL</b>.
198: */
199: public void testLineView04() {
200: if (!isHarmony()) {
201: return;
202: }
203: view = parent.new LineView(root.getElement(3));
204: view.loadChildren(null);
205: assertEquals(3, view.getViewCount());
206: int offset = view.getStartOffset();
207: checkChild(view.getView(0), offset, offset + RTLLength);
208: offset += RTLLength;
209: checkChild(view.getView(1), offset, offset + LTRLength);
210: offset += LTRLength;
211: checkChild(view.getView(2), offset, offset + RTLLength + 1); // +newLine
212: }
213:
214: public void testIsAfter() throws Exception {
215: // Regression for HARMONY-2212
216: if (!isHarmony()) {
217: return;
218: }
219: view = parent.new LineView(root.getElement(0));
220: view.loadChildren(null);
221: assertEquals(1, view.getViewCount());
222: assertFalse(view.isAfter(31, 10, new Rectangle(30, 5, 5, 10)));
223: }
224:
225: @Override
226: protected void setUp() throws Exception {
227: super .setUp();
228: if (!isHarmony()) {
229: return;
230: }
231: doc = new PlainDocument();
232: doc.insertString(0, defText, null);
233: root = doc.getDefaultRootElement();
234: buffer = new Segment();
235: parent = new PlainViewI18NWithTextArea(root, doc);
236: }
237:
238: private void checkChild(View child, int start, int end) {
239: assertSame("Element of a child must equal to one of parent",
240: view.getElement(), child.getElement());
241: assertEquals("Start offsets are different", start, child
242: .getStartOffset());
243: assertEquals("End offsets are different", end, child
244: .getEndOffset());
245: }
246:
247: private FontMetrics getMetrics() {
248: final Container container = view.getContainer();
249: return container.getFontMetrics(container.getFont());
250: }
251:
252: private String getViewText() throws BadLocationException {
253: return getViewTextSegment().toString();
254: }
255:
256: private Segment getViewTextSegment() throws BadLocationException {
257: final int start = view.getStartOffset();
258: final int end = view.getEndOffset();
259: doc.getText(start, end - start - 1, buffer);
260: return buffer;
261: }
262: }
|