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.Rectangle;
024: import javax.swing.JTextArea;
025: import javax.swing.SwingTestCase;
026: import javax.swing.event.DocumentEvent;
027: import javax.swing.event.DocumentListener;
028: import javax.swing.text.AbstractDocument.DefaultDocumentEvent;
029: import javax.swing.text.FlowView.FlowStrategy;
030: import javax.swing.text.FlowViewTest.FlowViewImplWithFactory;
031: import javax.swing.text.FlowView_FlowStrategyTest.PartFactory;
032:
033: /**
034: * This class tests the behavior of <code>{insert, remove, changed}Update</code>
035: * methods in <code>FlowView.FlowStrategy</code> in situation where FlowView
036: * has associated container.
037: *
038: */
039: public class FlowView_FlowStrategy_HostedRTest extends SwingTestCase
040: implements DocumentListener {
041: private DefaultStyledDocument doc;
042:
043: private Element p1;
044:
045: private DefaultDocumentEvent event;
046:
047: private FlowView view;
048:
049: private FlowStrategy strategy;
050:
051: private JTextArea textArea;
052:
053: private Rectangle alloc;
054:
055: private Rectangle paintRect;
056:
057: @Override
058: protected void setUp() throws Exception {
059: super .setUp();
060: doc = new DefaultStyledDocument();
061: doc.insertString(0, "test text\n2nd par", null);
062: p1 = doc.getDefaultRootElement().getElement(0);
063: textArea = new JTextArea(doc) {
064: private static final long serialVersionUID = 1L;
065:
066: @Override
067: public void repaint(int x, int y, int width, int height) {
068: paintRect = new Rectangle(x, y, width, height);
069: }
070: };
071: view = new FlowViewImplWithFactory(p1, View.Y_AXIS,
072: new PartFactory()) {
073: @Override
074: public Container getContainer() {
075: return textArea;
076: }
077: };
078: strategy = view.strategy;
079: view.loadChildren(null);
080: doc.addDocumentListener(this );
081: alloc = new Rectangle(12, 17, 125, 541);
082: paintRect = null;
083: view.layout(alloc.width, alloc.height);
084: assertTrue(view.isAllocationValid());
085: }
086:
087: public void testInsertUpdate() throws BadLocationException {
088: doc.insertString(p1.getStartOffset() + 1, "^^^", null);
089: strategy.insertUpdate(view, event, alloc);
090: assertEquals(alloc, paintRect);
091: assertTrue(view.isAllocationValid());
092: }
093:
094: public void testRemoveUpdate() throws BadLocationException {
095: doc.remove(p1.getStartOffset() + 1, 3);
096: paintRect = null;
097: strategy.removeUpdate(view, event, alloc);
098: assertEquals(alloc, paintRect);
099: assertTrue(view.isAllocationValid());
100: }
101:
102: public void testChangedUpdate() throws BadLocationException {
103: doc.setCharacterAttributes(p1.getStartOffset(), 3, doc
104: .getAttributeContext().getEmptySet(), true);
105: strategy.changedUpdate(view, event, alloc);
106: assertEquals(alloc, paintRect);
107: assertTrue(view.isAllocationValid());
108: }
109:
110: public void insertUpdate(DocumentEvent e) {
111: event = (DefaultDocumentEvent) e;
112: }
113:
114: public void removeUpdate(DocumentEvent e) {
115: event = (DefaultDocumentEvent) e;
116: }
117:
118: public void changedUpdate(DocumentEvent e) {
119: event = (DefaultDocumentEvent) e;
120: }
121: }
|