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.BasicSwingTestCase;
023: import javax.swing.text.CompositeViewTest.CompositeViewImpl;
024: import junit.framework.TestCase;
025:
026: public class CompositeViewRTest extends TestCase {
027: private PlainDocument doc; // Document used in tests
028:
029: private Element root; // Default root element of the document
030:
031: private CompositeView view; // View object used in tests
032:
033: private ViewFactory factory; // View factory used to create new views
034:
035: @Override
036: protected void setUp() throws Exception {
037: super .setUp();
038: doc = new PlainDocument();
039: doc
040: .insertString(0, "line1\nline2\n\u05DC\u05DD\nline3\n",
041: null);
042: // positions: 012345 678901 2 3 4 567890
043: // 0 1 2
044: view = new CompositeViewImpl(root = doc.getDefaultRootElement());
045: view.loadChildren(factory = new ViewFactory() {
046: public View create(final Element line) {
047: return new PlainView(line);
048: }
049: });
050: }
051:
052: /**
053: * NullPointerException is thrown if <code>replace</code>
054: * is called when <code>views</code> parameter is <code>null</code>.
055: */
056: public void testReplace02() {
057: final int count = view.getViewCount();
058: view.replace(0, 0, null);
059: assertEquals(count, view.getViewCount());
060: assertTrue(count > 0);
061: view.replace(0, view.getViewCount(), null); // = removeAll()
062: assertEquals(0, view.getViewCount());
063: }
064:
065: public void testReplace03() throws Exception {
066: View child = view.getView(0);
067: assertSame(view, child.getParent());
068: // This removes and places the same view at the same place
069: view.replace(0, 1, new View[] { child });
070: assertSame(view, child.getParent());
071: }
072:
073: public void testReplace04() throws Exception {
074: View child = view.getView(0);
075: assertSame(view, child.getParent());
076: View parent = new PlainView(root);
077: child.setParent(parent);
078: assertSame(parent, child.getParent());
079: view.remove(0);
080: assertSame(parent, child.getParent());
081: }
082:
083: /**
084: * <code>loadChildren</code> doesn't call <code>replace</code>.
085: */
086: public void testLoadChildren02() {
087: final boolean[] called = new boolean[] { false };
088: view = new CompositeViewImpl(root) {
089: @Override
090: public void replace(final int index, final int length,
091: final View[] views) {
092: called[0] = true;
093: assertEquals(0, index);
094: if (BasicSwingTestCase.isHarmony()) {
095: assertEquals(getViewCount(), length);
096: } else {
097: assertEquals(0, length);
098: }
099: assertEquals(root.getElementCount(), views.length);
100: super .replace(index, length, views);
101: }
102: };
103: view.loadChildren(factory);
104: assertTrue(called[0]);
105: called[0] = false;
106: assertEquals(root.getElementCount(), view.getViewCount());
107: assertTrue(view.getViewCount() > 0);
108: view.loadChildren(factory);
109: assertTrue(called[0]);
110: if (BasicSwingTestCase.isHarmony()) {
111: assertEquals(root.getElementCount(), view.getViewCount());
112: } else {
113: assertEquals(2 * root.getElementCount(), view
114: .getViewCount());
115: }
116: }
117: }
|