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 javax.swing.text.ViewTest.DisAbstractedView;
023: import junit.framework.TestCase;
024:
025: /**
026: * Tests View class methods which manage child views.
027: *
028: */
029: public class View_ChildrenTest extends TestCase {
030: private Document doc;
031:
032: private Element line;
033:
034: private boolean replaceIsCalled = false;
035:
036: private int replaceLength;
037:
038: private int replaceOffset;
039:
040: private View[] replaceViews;
041:
042: private View view;
043:
044: private View viewToAdd;
045:
046: public void testAppend() {
047: view.append(viewToAdd);
048: assertTrue(replaceIsCalled);
049: assertEquals(view.getViewCount(), replaceOffset);
050: assertEquals(0, replaceLength);
051: assertEquals(1, replaceViews.length);
052: assertSame(viewToAdd, replaceViews[0]);
053: }
054:
055: public void testInsert() {
056: view.insert(0, viewToAdd);
057: assertTrue(replaceIsCalled);
058: assertEquals(0, replaceOffset);
059: assertEquals(0, replaceLength);
060: assertEquals(1, replaceViews.length);
061: assertSame(viewToAdd, replaceViews[0]);
062: replaceIsCalled = false; // Reset the flag
063: view.insert(2, viewToAdd);
064: assertTrue(replaceIsCalled);
065: assertEquals(2, replaceOffset);
066: assertEquals(0, replaceLength);
067: assertEquals(1, replaceViews.length);
068: assertSame(viewToAdd, replaceViews[0]);
069: }
070:
071: public void testRemove() {
072: view.remove(0);
073: assertTrue(replaceIsCalled);
074: assertEquals(0, replaceOffset);
075: assertEquals(1, replaceLength);
076: assertNull(replaceViews);
077: replaceIsCalled = false; // Reset the flag
078: view.remove(2);
079: assertTrue(replaceIsCalled);
080: assertEquals(2, replaceOffset);
081: assertEquals(1, replaceLength);
082: assertNull(replaceViews);
083: }
084:
085: public void testRemoveAll() {
086: view.removeAll();
087: assertTrue(replaceIsCalled);
088: assertEquals(0, replaceOffset);
089: assertEquals(view.getViewCount(), replaceLength);
090: assertNull(replaceViews);
091: }
092:
093: public void testReplace() {
094: assertEquals(0, view.getViewCount());
095: View viewsToAdd[] = new View[3];
096: view.replace(5, 7, viewsToAdd);
097: assertTrue(replaceIsCalled);
098: assertEquals(5, replaceOffset);
099: assertEquals(7, replaceLength);
100: assertEquals(3, replaceViews.length);
101: assertSame(viewsToAdd, replaceViews);
102: }
103:
104: @Override
105: protected void setUp() throws Exception {
106: super .setUp();
107: doc = new PlainDocument();
108: doc.insertString(0, "01234\nabcde", null);
109: line = doc.getDefaultRootElement().getElement(1);
110: view = new DisAbstractedView(line) {
111: @Override
112: public void replace(final int offset, final int length,
113: final View[] views) {
114: replaceIsCalled = true;
115: replaceOffset = offset;
116: replaceLength = length;
117: replaceViews = views;
118: }
119: };
120: viewToAdd = ViewTest.getLine1View(doc);
121: }
122: }
|