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.Graphics;
024: import javax.swing.JTextArea;
025: import junit.framework.TestCase;
026:
027: /**
028: * Tests methods that don't require the proper view initialization.
029: *
030: * <p>This class uses very simple initialization. It is not valid to
031: * call methods that depend on the "real" state of the view (assigned
032: * to a text component).
033: *
034: */
035: public class PlainView_SimpleTest extends TestCase {
036: private JTextArea area;
037:
038: private Document doc;
039:
040: private Element root;
041:
042: private boolean updateMetricsCalled;
043:
044: private PlainView view;
045:
046: public void testGetLineBuffer() {
047: Segment buffer = view.getLineBuffer();
048: assertNotNull(buffer);
049: // An instance always returns the same line buffer
050: assertSame(buffer, view.getLineBuffer());
051: // Another instance returns distinct line buffer
052: assertNotSame(buffer, new PlainView(root).getLineBuffer());
053: }
054:
055: public void testGetTabSize() {
056: assertEquals(8, view.getTabSize());
057: doc.putProperty(PlainDocument.tabSizeAttribute, new Integer(4));
058: assertEquals(4, view.getTabSize());
059: }
060:
061: public void testPlainView() {
062: view = new PlainView(null);
063: assertNull(view.getElement());
064: view = new PlainView(root);
065: assertSame(root, view.getElement());
066: assertNull(view.metrics); // metrics are lazily initialized
067: view = new PlainView(root) {
068: @Override
069: public Container getContainer() {
070: return area;
071: }
072:
073: @Override
074: public Graphics getGraphics() {
075: return area.getGraphics();
076: }
077: };
078: assertSame(root, view.getElement());
079: assertNull(view.metrics);
080: }
081:
082: public void testSetSize() {
083: view = new PlainView(root) {
084: @Override
085: public Container getContainer() {
086: return new JTextArea();
087: }
088:
089: @Override
090: protected void updateMetrics() {
091: updateMetricsCalled = true;
092: }
093: };
094: assertFalse(updateMetricsCalled);
095: view.setSize(500, 500);
096: assertTrue("setSize is expected to call updateMetrics",
097: updateMetricsCalled);
098: }
099:
100: /**
101: * Creates PlainDocument, PlainView on <code>doc</code>'s
102: * default root, and rectangular shape.
103: */
104: @Override
105: protected void setUp() throws Exception {
106: super .setUp();
107: doc = new PlainDocument();
108: root = doc.getDefaultRootElement();
109: view = new PlainView(root);
110: }
111: }
|