01: package abbot.editor;
02:
03: import java.awt.*;
04: import javax.swing.*;
05: import javax.swing.tree.*;
06:
07: import junit.extensions.abbot.*;
08:
09: public class ComponentTreeTest extends ComponentTestFixture {
10:
11: private ComponentTree tree;
12:
13: protected void setUp() {
14: tree = new ComponentTree(new CompactHierarchy(getHierarchy()));
15: }
16:
17: protected void tearDown() {
18: Container c = tree.getParent();
19: if (c != null)
20: c.remove(tree);
21: tree = null;
22: // Get rid of the event listener
23: System.gc();
24: }
25:
26: private void assertPath(Object[] expected, Object[] path) {
27: for (int i = 0; i < expected.length && i < path.length; i++) {
28: assertEquals("Wrong path component " + i, expected[i],
29: ((ComponentNode) path[i]).getComponent());
30: }
31: assertEquals("Wrong path length", expected.length, path.length);
32: }
33:
34: public void testDisplayNewFrames() {
35: Frame f = showFrame(new JLabel(getName()));
36: TreePath path = tree.getPath(f);
37: assertPath(new Object[] { null, f }, path.getPath());
38: }
39:
40: public void testRefreshOnPopup() {
41: JLabel label = new JLabel(getName());
42: showFrame(label);
43: JPopupMenu popup = new JPopupMenu();
44: popup.add(new JMenuItem("nothing to see"));
45: installPopup(label, popup);
46: getRobot().showPopupMenu(label);
47: assertTrue("Tree didn't update after popup shown", tree
48: .getPath(popup) != null);
49: }
50:
51: public void testPreserveSelection() {
52: JLabel label = new JLabel(getName());
53: JFrame f = new JFrame(getName());
54: f.getContentPane().add(label);
55: showWindow(f);
56: ComponentNode root = (ComponentNode) ((DefaultTreeModel) tree
57: .getModel()).getRoot();
58: TreePath path = root.getPath(label);
59: tree.setSelectionPath(path);
60: tree.reload();
61: TreePath newPath = tree.getSelectionPath();
62: assertTrue("Tree should still have a selection",
63: newPath != null);
64: assertTrue("path should be visible", tree.isVisible(newPath));
65: assertPath(new Object[] { null, f, f.getContentPane(), label },
66: newPath.getPath());
67: }
68:
69: public void testRefreshOnWindowEvents() {
70: Frame f = showFrame(new JLabel(getName()));
71: assertPath(new Object[] { null, f }, tree.getPath(f).getPath());
72: Frame f2 = new JFrame(getName() + "2");
73: showWindow(f);
74: assertPath(new Object[] { null, f2 }, tree.getPath(f2)
75: .getPath());
76: }
77:
78: public ComponentTreeTest(String name) {
79: super (name);
80: }
81:
82: public static void main(String[] args) {
83: TestHelper.runTests(args, ComponentTreeTest.class);
84: }
85: }
|