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.JFrame;
025: import javax.swing.JTextArea;
026: import javax.swing.SwingTestCase;
027: import javax.swing.text.Position.Bias;
028:
029: public class WrappedPlainViewRTest extends SwingTestCase {
030: private Document doc;
031:
032: private Element root;
033:
034: private WrappedPlainView view;
035:
036: private JTextArea textArea;
037:
038: private JFrame frame;
039:
040: private Rectangle shape;
041:
042: private int width = 145;
043:
044: private int height = 500;
045:
046: @Override
047: protected void setUp() throws Exception {
048: super .setUp();
049: frame = new JFrame("WrappedPlainView Test");
050: doc = new PlainDocument();
051: doc.insertString(0, "one, two, three, four, five\n"
052: + "eins, zwei, drei, vier, funf\n"
053: + "uno, dos, tres, cuatro, \tcinco", null);
054: root = doc.getDefaultRootElement();
055: textArea = new JTextArea(doc);
056: textArea.setLineWrap(true);
057: frame.getContentPane().add(textArea);
058: textArea.setSize(width, height);
059: View rootView = textArea.getUI().getRootView(textArea);
060: view = (WrappedPlainView) rootView.getView(0);
061: shape = new Rectangle(width, height);
062: view.setSize(shape.width, shape.height);
063: }
064:
065: @Override
066: protected void tearDown() throws Exception {
067: super .tearDown();
068: frame.dispose();
069: }
070:
071: /**
072: * Tests the value returned where the point lies below the last line.
073: */
074: public void testViewToModel() throws BadLocationException {
075: final Bias[] bias = new Bias[1];
076: width = getTextWidth(root.getElementCount() - 1) * 3 / 4;
077: view.setSize(width, height);
078: assertEquals(view.getEndOffset() - 1, view.viewToModel(
079: width / 2, height - 10, new Rectangle(width, height),
080: bias));
081: }
082:
083: /**
084: * Tests the value returned where the point is in a line which has no
085: * breakes in it.
086: */
087: public void testViewToModelNoBreaks() throws BadLocationException {
088: final Bias[] bias = new Bias[1];
089: textArea.setSize(500, height);
090: shape = new Rectangle(500, height);
091: view.setSize(shape.width, shape.height);
092: assertTrue("The first line must be not wrapped",
093: 500 > getTextWidth(0));
094: FontMetrics metrics = getFontMetrics();
095: Element line = root.getElement(0);
096: String text = doc.getText(line.getStartOffset(), 2);
097: assertEquals(2, view.viewToModel(metrics.stringWidth(text),
098: metrics.getHeight() / 2, shape, bias));
099: }
100:
101: /**
102: * This tests that no NPE is thrown when a line is removed.
103: */
104: public void testInsertUpdate() throws BadLocationException {
105: textArea.setText("line1\n\n\n\n");
106: assertEquals(5, view.getViewCount());
107: assertSame(doc, textArea.getDocument());
108: doc.insertString(7, "\n", null);
109: }
110:
111: /**
112: * This tests that no NPE is thrown when a line is removed.
113: */
114: public void testRemoveUpdate() throws BadLocationException {
115: textArea.setText("line1\n\n\n\n");
116: assertEquals(5, view.getViewCount());
117: assertSame(doc, textArea.getDocument());
118: doc.remove(6, 1);
119: }
120:
121: private int getTextWidth(final int lineNo)
122: throws BadLocationException {
123: Element line = root.getElement(lineNo);
124: String text = doc.getText(line.getStartOffset(), line
125: .getEndOffset()
126: - 1 - line.getStartOffset());
127: FontMetrics metrics = getFontMetrics();
128: return metrics.stringWidth(text);
129: }
130:
131: private FontMetrics getFontMetrics() {
132: return textArea.getFontMetrics(textArea.getFont());
133: }
134: }
|