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 Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.tree;
021:
022: import java.awt.Rectangle;
023:
024: public class FixedHeightLayoutCacheTest extends AbstractLayoutCacheTest {
025: @Override
026: protected void setUp() throws Exception {
027: super .setUp();
028: cache = new FixedHeightLayoutCache();
029: defaultRowValue = cache.getRowForPath(null);
030: }
031:
032: @Override
033: protected void tearDown() throws Exception {
034: super .tearDown();
035: }
036:
037: @Override
038: public void testLayoutCache() {
039: assertFalse(cache.rootVisible);
040: assertNull(cache.nodeDimensions);
041: assertEquals(1, cache.rowHeight);
042: assertNull(cache.treeModel);
043: assertNull(cache.treeSelectionModel);
044: }
045:
046: @Override
047: public void testSetRootVisible() {
048: super .testSetRootVisible();
049: int height = 1111;
050: cache.setRowHeight(height);
051: TreeModel model = new UndefaultTreeModel(root);
052: root.add(node1);
053: root.add(node2);
054: cache.setModel(model);
055: cache.setNodeDimensions(dimensions1);
056: assertEquals(new Rectangle(root.hashCode(), -1 * height, 0,
057: height), cache.getBounds(rootPath, null));
058: assertEquals(new Rectangle(node1.hashCode(), 0 * height, 1,
059: height), cache.getBounds(path1, null));
060: assertEquals(new Rectangle(node2.hashCode(), 1 * height, 1,
061: height), cache.getBounds(path2, null));
062: cache.setRootVisible(true);
063: assertEquals(new Rectangle(root.hashCode(), 0 * height, 0,
064: height), cache.getBounds(rootPath, null));
065: assertEquals(new Rectangle(node1.hashCode(), 1 * height, 1,
066: height), cache.getBounds(path1, null));
067: assertEquals(new Rectangle(node2.hashCode(), 2 * height, 1,
068: height), cache.getBounds(path2, null));
069: }
070:
071: @Override
072: public void testIsFixedRowHeight() {
073: assertTrue(cache.isFixedRowHeight());
074: }
075:
076: @Override
077: public void testSetRowHeight() {
078: cache.setRowHeight(100);
079: assertEquals(100, cache.getRowHeight());
080: cache.setRowHeight(200);
081: assertEquals(200, cache.getRowHeight());
082: try {
083: cache.setRowHeight(0);
084: fail("no exception has been thrown");
085: } catch (IllegalArgumentException e) {
086: }
087: }
088:
089: /*
090: * Test method for 'javax.swing.tree.FixedHeightLayoutCache.getBounds(TreePath, Rectangle)'
091: */
092: public void testGetBounds() {
093: int height = 1111;
094: cache.setRowHeight(height);
095: TreeModel model = new UndefaultTreeModel(root);
096: root.add(node1);
097: root.add(node2);
098: node1.add(node3);
099: node1.add(node4);
100: node2.add(node5);
101: node2.add(node6);
102: cache.setModel(model);
103: cache.setExpandedState(path1, true);
104: cache.setExpandedState(path2, true);
105: assertNull(cache.getBounds(path1, new Rectangle()));
106: assertNull(cache.getBounds(path1, null));
107: cache.setNodeDimensions(dimensions1);
108: assertEquals(new Rectangle(root.hashCode(), -1 * height, 0,
109: height), cache.getBounds(rootPath, null));
110: assertEquals(new Rectangle(node1.hashCode(), 0 * height, 1,
111: height), cache.getBounds(path1, null));
112: assertEquals(new Rectangle(node2.hashCode(), 3 * height, 1,
113: height), cache.getBounds(path2, null));
114: assertEquals(new Rectangle(node3.hashCode(), 1 * height, 2,
115: height), cache.getBounds(path13, null));
116: assertEquals(new Rectangle(node4.hashCode(), 2 * height, 2,
117: height), cache.getBounds(path14, null));
118: assertEquals(new Rectangle(node5.hashCode(), 4 * height, 2,
119: height), cache.getBounds(path25, null));
120: assertEquals(new Rectangle(node6.hashCode(), 5 * height, 2,
121: height), cache.getBounds(path26, null));
122: cache.setExpandedState(rootPath, false);
123: assertNull(cache.getBounds(path25, null));
124: cache.setModel(null);
125: assertNull(cache.getBounds(path1, null));
126: }
127:
128: /*
129: * Test method for 'javax.swing.tree.FixedHeightLayoutCache.getPathClosestTo(int, int)'
130: */
131: public void testGetPathClosestTo() {
132: int height = 10;
133: cache.setRowHeight(height);
134: TreeModel model = new UndefaultTreeModel(root);
135: root.add(node1);
136: root.add(node2);
137: node1.add(node3);
138: node1.add(node4);
139: node2.add(node5);
140: node2.add(node6);
141: assertNull(cache.getPathClosestTo(-100, -100));
142: assertNull(cache.getPathClosestTo(0, 0));
143: assertNull(cache.getPathClosestTo(100, 100));
144: cache.setModel(model);
145: cache.setExpandedState(rootPath, false);
146: assertNull(cache.getPathClosestTo(-100, -100));
147: assertNull(cache.getPathClosestTo(0, 0));
148: assertNull(cache.getPathClosestTo(100, 100));
149: cache.setRootVisible(true);
150: assertEquals(rootPath, cache.getPathClosestTo(-100, -100));
151: assertEquals(rootPath, cache.getPathClosestTo(0, 0));
152: assertEquals(rootPath, cache.getPathClosestTo(100, 100));
153: cache.setExpandedState(rootPath, true);
154: assertEquals(rootPath, cache.getPathClosestTo(-100, -100));
155: assertEquals(rootPath, cache.getPathClosestTo(0, 0));
156: assertEquals(path2, cache.getPathClosestTo(100, 100));
157: cache.setExpandedState(path1, true);
158: cache.setExpandedState(path2, true);
159: assertEquals(rootPath, cache.getPathClosestTo(-100, -100));
160: assertEquals(rootPath, cache.getPathClosestTo(0, 0));
161: assertEquals(path13, cache.getPathClosestTo(0, 20));
162: assertEquals(path2, cache.getPathClosestTo(0, 40));
163: assertEquals(path26, cache.getPathClosestTo(0, 60));
164: assertEquals(path26, cache.getPathClosestTo(0, 80));
165: assertEquals(rootPath, cache.getPathClosestTo(120, 0));
166: assertEquals(path13, cache.getPathClosestTo(120, 20));
167: assertEquals(path2, cache.getPathClosestTo(120, 40));
168: assertEquals(path26, cache.getPathClosestTo(120, 60));
169: assertEquals(path26, cache.getPathClosestTo(120, 80));
170: cache.setNodeDimensions(dimensions2);
171: assertEquals(rootPath, cache.getPathClosestTo(-100, -100));
172: assertEquals(rootPath, cache.getPathClosestTo(0, 0));
173: assertEquals(path13, cache.getPathClosestTo(0, 20));
174: assertEquals(path2, cache.getPathClosestTo(0, 40));
175: assertEquals(path26, cache.getPathClosestTo(0, 60));
176: assertEquals(path26, cache.getPathClosestTo(0, 80));
177: assertEquals(rootPath, cache.getPathClosestTo(120, 0));
178: assertEquals(path13, cache.getPathClosestTo(120, 20));
179: assertEquals(path2, cache.getPathClosestTo(120, 40));
180: assertEquals(path26, cache.getPathClosestTo(120, 60));
181: assertEquals(path26, cache.getPathClosestTo(120, 80));
182: cache.setModel(null);
183: assertNull(cache.getPathClosestTo(0, 0));
184: }
185:
186: /*
187: * Test method for 'javax.swing.tree.FixedHeightLayoutCache.invalidatePathBounds(TreePath)'
188: * due to the spec this method does nothing
189: */
190: public void testInvalidatePathBounds() {
191: }
192:
193: /*
194: * Test method for 'javax.swing.tree.FixedHeightLayoutCache.invalidateSizes()'
195: * this method seems to do nothing also
196: */
197: @Override
198: public void testInvalidateSizes() {
199: }
200:
201: /*
202: * Test method for 'javax.swing.tree.FixedHeightLayoutCache.treeNodesChanged(TreeModelEvent)'
203: */
204: @Override
205: public void testTreeNodesChanged() {
206: super .testTreeNodesChanged();
207: }
208:
209: @Override
210: public void testGetPreferredHeight() {
211: int height = 1111;
212: TreeModel model = new UndefaultTreeModel(root);
213: root.add(node1);
214: root.add(node2);
215: cache.setRowHeight(height);
216: assertEquals(0, cache.getPreferredHeight());
217: cache.setModel(model);
218: if (isHarmony()) {
219: assertEquals(2222, cache.getPreferredHeight());
220: } else {
221: assertEquals(0, cache.getPreferredHeight());
222: }
223: cache.setRowHeight(height);
224: cache.setNodeDimensions(dimensions3);
225: assertEquals(2222, cache.getPreferredHeight());
226: cache.setRootVisible(true);
227: assertEquals(3333, cache.getPreferredHeight());
228: cache.setRootVisible(false);
229: assertEquals(2222, cache.getPreferredHeight());
230: cache.setModel(model);
231: assertEquals(2222, cache.getPreferredHeight());
232: cache.setNodeDimensions(null);
233: if (isHarmony()) {
234: assertEquals(2222, cache.getPreferredHeight());
235: } else {
236: assertEquals(0, cache.getPreferredHeight());
237: }
238: }
239:
240: @Override
241: public void testGetPreferredWidth() {
242: int height = 20;
243: TreeModel model = new DefaultTreeModel(root);
244: root.add(node1);
245: root.add(node2);
246: node1.add(node3);
247: node1.add(node4);
248: node2.add(node5);
249: node2.add(node6);
250: cache.setRowHeight(height);
251: assertEquals(0, cache.getPreferredWidth(null));
252: cache.setModel(model);
253: assertEquals(0, cache.getPreferredWidth(null));
254: assertEquals(0, cache.getPreferredWidth(new Rectangle(100, 100,
255: 10, 10)));
256: cache.setNodeDimensions(dimensions2);
257: assertEquals(110, cache.getPreferredWidth(new Rectangle(100,
258: 100, 10, 10)));
259: cache.setRootVisible(true);
260: assertEquals(110, cache.getPreferredWidth(new Rectangle(0, 0,
261: 10, 100)));
262: cache.setExpandedState(path1, true);
263: assertEquals(100, cache.getPreferredWidth(new Rectangle(0, 0,
264: 0, 0)));
265: assertEquals(100, cache.getPreferredWidth(new Rectangle(10, 10,
266: 10, 10)));
267: assertEquals(110, cache.getPreferredWidth(new Rectangle(20, 20,
268: 10, 10)));
269: assertEquals(110, cache.getPreferredWidth(new Rectangle(30, 30,
270: 10, 10)));
271: assertEquals(120, cache.getPreferredWidth(new Rectangle(40, 40,
272: 10, 10)));
273: assertEquals(120, cache.getPreferredWidth(new Rectangle(50, 50,
274: 10, 10)));
275: assertEquals(120, cache.getPreferredWidth(new Rectangle(60, 60,
276: 10, 10)));
277: assertEquals(120, cache.getPreferredWidth(new Rectangle(70, 70,
278: 10, 10)));
279: assertEquals(120, cache.getPreferredWidth(null));
280: }
281: }
|