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.Graphics;
024: import java.awt.Rectangle;
025: import javax.swing.JFrame;
026: import javax.swing.JTextArea;
027: import javax.swing.SwingTestCase;
028:
029: /**
030: * Tests WrappedPlainView methods which require "real" initialization.
031: *
032: */
033: public class WrappedPlainViewTest extends SwingTestCase {
034: private Document doc;
035:
036: private WrappedPlainView view;
037:
038: private JTextArea textArea;
039:
040: private JFrame frame;
041:
042: private Rectangle shape;
043:
044: private static final int X_AXIS = View.X_AXIS;
045:
046: private static final int Y_AXIS = View.Y_AXIS;
047:
048: private int width = 145;
049:
050: private int height = 100;
051:
052: @Override
053: protected void setUp() throws Exception {
054: super .setUp();
055: frame = new JFrame("WrappedPlainView Test");
056: doc = new PlainDocument();
057: doc.insertString(0, "one, two, three, four, five\n"
058: + "eins, zwei, drei, vier, funf\n"
059: + "uno, dos, tres, cuatro, cinco", null);
060: textArea = new JTextArea(doc);
061: textArea.setLineWrap(true);
062: frame.getContentPane().add(textArea);
063: frame.setSize(width, height);
064: frame.pack();
065: View rootView = textArea.getUI().getRootView(textArea);
066: view = (WrappedPlainView) rootView.getView(0);
067: shape = textArea.getVisibleRect();
068: view.setSize(shape.width, shape.height);
069: }
070:
071: @Override
072: protected void tearDown() throws Exception {
073: super .tearDown();
074: frame.dispose();
075: }
076:
077: private int getSum(final int axis) {
078: int sum = 0;
079: for (int i = 0; i < view.getViewCount(); i++) {
080: View child = view.getView(i);
081: sum += (int) child.getPreferredSpan(axis);
082: }
083: return sum;
084: }
085:
086: public void testGetMaximumSpan() {
087: assertEquals(Integer.MAX_VALUE, (int) view
088: .getMaximumSpan(X_AXIS));
089: assertEquals(getSum(Y_AXIS), (int) view.getMaximumSpan(Y_AXIS));
090: }
091:
092: public void testGetMinimumSpan() {
093: assertEquals(view.getWidth(), (int) view.getMinimumSpan(X_AXIS));
094: assertEquals(getSum(Y_AXIS), (int) view.getMinimumSpan(Y_AXIS));
095: }
096:
097: public void testGetPreferredSpan() {
098: assertEquals(view.getWidth(), (int) view
099: .getPreferredSpan(X_AXIS));
100: assertEquals(getSum(Y_AXIS), (int) view
101: .getPreferredSpan(Y_AXIS));
102: assertEquals(shape.width, view.getWidth());
103: assertEquals(shape.width, (int) view.getView(0)
104: .getPreferredSpan(X_AXIS));
105: }
106:
107: public void testDrawSelectedText() throws BadLocationException {
108: textArea.setText("line1\nline2");
109: Graphics g = textArea.getGraphics();
110: g.setFont(textArea.getFont());
111: FontMetrics m = g.getFontMetrics();
112: assertEquals(m.charWidth('l'), view.drawSelectedText(g, 0, 0,
113: 0, 1));
114: assertEquals(5 + m.charWidth('l'), view.drawSelectedText(g, 5,
115: 0, 0, 1));
116: assertEquals(m.stringWidth("line1"), view.drawSelectedText(g,
117: 0, 0, 0, 5));
118: assertEquals(m.stringWidth("line1\nli"), view.drawSelectedText(
119: g, 0, 0, 0, 8));
120: try {
121: view.drawSelectedText(g, 0, 0, -1, 1);
122: fail("BadLocationException expected");
123: } catch (BadLocationException e) {
124: }
125: try {
126: view.drawUnselectedText(g, 0, 0, 13, 13);
127: fail("BadLocationException expected");
128: } catch (BadLocationException e) {
129: }
130: try {
131: view.drawSelectedText(g, 0, 0, 10, 2);
132: fail("BadLocationException expected");
133: } catch (BadLocationException e) {
134: }
135: }
136:
137: public void testDrawUnselectedText() throws BadLocationException {
138: textArea.setText("line1\nline2");
139: Graphics g = textArea.getGraphics();
140: g.setFont(textArea.getFont());
141: FontMetrics m = g.getFontMetrics();
142: assertEquals(m.charWidth('l'), view.drawUnselectedText(g, 0, 0,
143: 0, 1));
144: assertEquals(5 + m.charWidth('l'), view.drawUnselectedText(g,
145: 5, 0, 0, 1));
146: assertEquals(m.stringWidth("line1"), view.drawUnselectedText(g,
147: 0, 0, 0, 5));
148: assertEquals(m.stringWidth("line1\nli"), view
149: .drawUnselectedText(g, 0, 0, 0, 8));
150: try {
151: view.drawUnselectedText(g, 0, 0, -1, 1);
152: fail("BadLocationException expected");
153: } catch (BadLocationException e) {
154: }
155: try {
156: view.drawUnselectedText(g, 0, 0, 13, 13);
157: fail("BadLocationException expected");
158: } catch (BadLocationException e) {
159: }
160: try {
161: view.drawUnselectedText(g, 0, 0, 10, 2);
162: fail("BadLocationException expected");
163: } catch (BadLocationException e) {
164: }
165: }
166: /*
167: public void testPaint() {
168: }
169: */
170: }
|