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 javax.swing.JTextArea;
025: import javax.swing.SwingTestCase;
026:
027: /**
028: * Tests PlainViewI18N class.
029: */
030: public class PlainViewI18NTest extends SwingTestCase {
031: private JTextArea area;
032:
033: private Document doc;
034:
035: private int line2Width;
036:
037: private int lineCount;
038:
039: private FontMetrics metrics;
040:
041: private Element root;
042:
043: private PlainViewI18N view;
044:
045: public void testGetMaximumSpan() throws Exception {
046: if (!isHarmony()) {
047: return;
048: }
049: assertEquals(lineCount * metrics.getHeight(), (int) view
050: .getMaximumSpan(View.Y_AXIS));
051: assertEquals(Integer.MAX_VALUE, (int) view
052: .getMaximumSpan(View.X_AXIS));
053: }
054:
055: public void testGetMinimumSpan() throws Exception {
056: if (!isHarmony()) {
057: return;
058: }
059: assertEquals(lineCount * metrics.getHeight(), (int) view
060: .getMinimumSpan(View.Y_AXIS));
061: assertEquals(line2Width, (int) view.getMinimumSpan(View.X_AXIS));
062: }
063:
064: public void testGetPreferredSpan() throws Exception {
065: if (!isHarmony()) {
066: return;
067: }
068: assertEquals(lineCount * metrics.getHeight(), (int) view
069: .getPreferredSpan(View.Y_AXIS));
070: assertEquals(line2Width, (int) view
071: .getPreferredSpan(View.X_AXIS));
072: }
073:
074: public void testGetViewFactory() {
075: if (!isHarmony()) {
076: return;
077: }
078: assertNull(view.getParent());
079: assertNotNull(view.getViewFactory());
080: }
081:
082: public void testNextTabStop() throws Exception {
083: if (!isHarmony()) {
084: return;
085: }
086: Object tabSizeProperty = doc
087: .getProperty(PlainDocument.tabSizeAttribute);
088: int tabSize = ((Integer) tabSizeProperty).intValue();
089: int tabStop = metrics.charWidth('m') * tabSize;
090: assertEquals(tabStop, (int) view.nextTabStop(0, 0));
091: assertEquals(tabStop, (int) view.nextTabStop(tabStop / 2, 0));
092: assertEquals(2 * tabStop, (int) view.nextTabStop(tabStop, 0));
093: }
094:
095: public void testPlainViewI18N() {
096: if (!isHarmony()) {
097: return;
098: }
099: view = new PlainViewI18N(root);
100: assertEquals(View.Y_AXIS, view.getAxis());
101: assertEquals(0, view.getViewCount());
102: view.loadChildren(view.getViewFactory());
103: assertEquals(root.getElementCount(), view.getViewCount());
104: }
105:
106: @Override
107: protected void setUp() throws Exception {
108: super .setUp();
109: if (!isHarmony()) {
110: return;
111: }
112: doc = new PlainDocument();
113: doc.insertString(0, "line1\nline two", null);
114: root = doc.getDefaultRootElement();
115: view = new PlainViewI18N(root) {
116: @Override
117: public Container getContainer() {
118: return area;
119: }
120: };
121: view.loadChildren(view.getViewFactory());
122: area = new JTextArea();
123: metrics = area.getFontMetrics(area.getFont());
124: lineCount = root.getElementCount();
125: Element line2 = root.getElement(1);
126: line2Width = metrics.stringWidth(doc.getText(line2
127: .getStartOffset(), line2.getEndOffset()
128: - line2.getStartOffset()));
129: }
130: }
|