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.Component;
023: import java.awt.Container;
024: import java.awt.Rectangle;
025: import java.awt.Shape;
026: import javax.swing.BasicSwingTestCase;
027: import javax.swing.JTextArea;
028: import javax.swing.event.DocumentEvent;
029: import javax.swing.event.DocumentListener;
030: import javax.swing.text.PlainView_ChangesTest.LineRange;
031:
032: public class PlainView_ChangesRTest extends BasicSwingTestCase
033: implements DocumentListener {
034: private boolean callSuperDamageRange;
035:
036: private Container container;
037:
038: private Document doc;
039:
040: private DocumentEvent event;
041:
042: private LineRange lineRange;
043:
044: private Rectangle paintRect;
045:
046: private Element root;
047:
048: private Shape shape;
049:
050: private PlainView view;
051:
052: /**
053: * Tests for <code>PlainView.insertUpdate</code> throws NPE.
054: * <p>
055: * If a change occured outside the widest line and document structure
056: * was not changed, <code>insertUpdate</code> throws NPE
057: * when processing the notification.
058: */
059: public void testInsertUpdateNPE() throws BadLocationException {
060: doc.insertString(0, "1:0123\n2:\n3:abcdefg", null);
061: view.updateMetrics();
062: doc.insertString(0, "^", null);
063: view.insertUpdate(event, shape, null);
064: }
065:
066: /**
067: * Tests for <code>PlainView</code> repaints more lines
068: * than necessary.
069: * <p>
070: * If a character (not a new line) is inserted into document,
071: * document structure will not change, or in other words the number of
072: * text lines will stay the same.
073: * In this case only the modified line needs to be repainted
074: * but <code>PlainView</code> repaints all following lines as well.
075: */
076: public void testInsertUpdateExtraRepaint()
077: throws BadLocationException {
078: doc.insertString(0, "1:0123\n2:\n3:abcdefg", null);
079: view.updateMetrics();
080: doc.insertString(0, "^", null);
081: view.insertUpdate(event, shape, null);
082: lineRange.check(0, 0, shape, container);
083: }
084:
085: /**
086: * Tests for calling <code>nextTabStop()</code> leaves
087: * <code>PlainView.metrics</code> field <code>null</code>.
088: * This is because <code>nextTabStop()</code> forwards to
089: * <code>paintParams</code> and updates <code>metrics</code> there,
090: * and <code>setSize()</code> doesn't update the field in
091: * the <code>PlainView</code>.
092: */
093: public void testNextTabStop05() {
094: assertNull(view.metrics);
095: if (isHarmony()) {
096: // Call to nextTabStop will lead to updateMetrics()
097: assertTrue("nextTabStop() must return value > 0", view
098: .nextTabStop(0, 0) > view.getTabSize());
099: }
100: view.setSize(100, 200);
101: assertNotNull("Metrics must be not null after setSize",
102: view.metrics);
103: }
104:
105: @Override
106: protected void setUp() throws Exception {
107: super .setUp();
108: doc = new PlainDocument();
109: root = doc.getDefaultRootElement();
110: view = new PlainView(root) {
111: @Override
112: public Container getContainer() {
113: if (container == null) {
114: container = new JTextArea() {
115: private static final long serialVersionUID = 1L;
116:
117: @Override
118: public void repaint(final int x, final int y,
119: final int w, final int h) {
120: if (paintRect == null) {
121: paintRect = new Rectangle(x, y, w, h);
122: } else {
123: paintRect
124: .add(new Rectangle(x, y, w, h));
125: }
126: }
127: };
128: }
129: return container;
130: }
131:
132: @Override
133: protected void damageLineRange(final int line0,
134: final int line1, final Shape shape,
135: final Component host) {
136: if (callSuperDamageRange) {
137: super .damageLineRange(line0, line1, shape, host);
138: } else {
139: lineRange = new LineRange(line0, line1, shape, host);
140: }
141: }
142: };
143: shape = new Rectangle(500, 500);
144: doc.addDocumentListener(this );
145: }
146:
147: public void changedUpdate(final DocumentEvent changes) {
148: fail("changedUpdate isn't supposed to be called");
149: }
150:
151: public void insertUpdate(final DocumentEvent changes) {
152: event = changes;
153: }
154:
155: public void removeUpdate(final DocumentEvent changes) {
156: event = changes;
157: }
158: }
|