001: package abbot.tester;
002:
003: import java.awt.Point;
004: import java.awt.Rectangle;
005:
006: import javax.swing.JTree;
007: import javax.swing.tree.*;
008:
009: import junit.extensions.abbot.TestHelper;
010: import junit.framework.TestCase;
011: import abbot.script.parsers.TreePathParser;
012:
013: public class JTreeLocationTest extends TestCase {
014:
015: protected JTree createTree() {
016: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
017: DefaultMutableTreeNode parent = new DefaultMutableTreeNode(
018: "parent");
019: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
020: "child");
021: root.add(parent);
022: parent.add(child);
023: return new JTree(root);
024: }
025:
026: public void testRowPoint() {
027: JTree tree = createTree();
028: Point p = new JTreeLocation(0).getPoint(tree);
029: Rectangle rect = tree.getRowBounds(0);
030: assertTrue("Row point should be within row", rect.contains(p));
031:
032: p = new JTreeLocation(0, true).getPoint(tree);
033: assertTrue("Row point should be in expansion control",
034: JTreeTester.isLocationInExpandControl(tree, p.x, p.y));
035: }
036:
037: public void testPathPoint() {
038: JTree tree = createTree();
039: TreePath path = new TreePath(tree.getModel().getRoot());
040: Point p = new JTreeLocation(path).getPoint(tree);
041: Rectangle rect = tree.getRowBounds(0);
042: assertTrue("Path point should be within path's row", rect
043: .contains(p));
044:
045: p = new JTreeLocation(path, true).getPoint(tree);
046: assertTrue("Path point should be in expansion control",
047: JTreeTester.isLocationInExpandControl(tree, p.x, p.y));
048: }
049:
050: public void testParsePoint() {
051: JTreeLocation loc = new JTreeLocation();
052: String parse = "(1,1)";
053: assertEquals("Badly parsed: " + parse, new JTreeLocation(
054: new Point(1, 1)), loc.parse(parse));
055: }
056:
057: public void testParseRow() {
058: JTreeLocation loc = new JTreeLocation();
059: String parse = "[1]";
060: JTreeLocation loc2 = new JTreeLocation(1);
061: assertEquals("Badly parsed: " + parse, loc2, loc.parse(parse));
062: assertEquals("Wrong row-based toString", parse, loc.toString());
063:
064: parse = " [ 10 ] ";
065: loc2 = new JTreeLocation(10);
066: assertEquals("Badly parsed: " + parse, loc2, loc.parse(parse));
067: assertEquals("Wrong row-based toString", "[10]", loc.toString());
068:
069: parse = "+[10]";
070: loc2 = new JTreeLocation(10, true);
071: assertEquals("Badly parsed: " + parse, loc2, loc.parse(parse));
072:
073: assertEquals("Wrong row-based toString", parse, loc.toString());
074: }
075:
076: public void testParsePath() {
077: JTreeLocation loc = new JTreeLocation();
078: String parse = "[root, parent, child]";
079: TreePath path = (TreePath) new TreePathParser().parse(parse);
080: JTreeLocation loc2 = new JTreeLocation(path);
081: assertEquals("Badly parsed: " + parse, loc2, loc.parse("\""
082: + parse + "\""));
083: assertEquals("Wrong path-based toString", "\"" + parse + "\"",
084: loc.toString());
085:
086: loc2.setInExpansion(true);
087: assertEquals("Badly parsed: +" + parse, loc2, loc.parse("+\""
088: + parse + "\""));
089: assertEquals("Wrong path-based toString", "+\"" + parse + "\"",
090: loc.toString());
091: }
092:
093: public void testParsePathHiddenRoot() {
094: JTreeLocation loc = new JTreeLocation();
095: String parse = "[null, parent, child]";
096: assertEquals("Badly parsed: " + parse, new JTreeLocation(
097: (TreePath) new TreePathParser().parse(parse)), loc
098: .parse("\"" + parse + "\""));
099: }
100:
101: public void testConvertHiddenRoot() {
102: // Should be able to use path with or without the root reference
103: JTree tree = createTree();
104: tree.setRootVisible(false);
105: Object root = tree.getModel().getRoot();
106: Object parent = tree.getModel().getChild(root, 0);
107: Object child = tree.getModel().getChild(parent, 0);
108: TreePath p1 = (TreePath) new TreePathParser()
109: .parse("[null, parent, child]");
110: TreePath p2 = JTreeLocation.findMatchingPath(tree, p1);
111: assertTrue("Got a null path", p2 != null);
112: assertEquals("Wrong root", root, p2.getPathComponent(0));
113: assertEquals("Wrong parent", parent, p2.getPathComponent(1));
114: assertEquals("Wrong child", child, p2.getPathComponent(2));
115:
116: p1 = (TreePath) new TreePathParser().parse("[parent, child]");
117: p2 = JTreeLocation.findMatchingPath(tree, p1);
118: assertTrue("Got a null path", p2 != null);
119: assertEquals("Wrong root", root, p2.getPathComponent(0));
120: assertEquals("Wrong parent", parent, p2.getPathComponent(1));
121: assertEquals("Wrong child", child, p2.getPathComponent(2));
122: }
123:
124: private static int count = 0;
125:
126: public void testFindMatchingPath() {
127: class Bar {
128: private int id = count++;
129:
130: public String toString() {
131: return String.valueOf(id);
132: }
133: }
134: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
135: new Bar());
136: DefaultMutableTreeNode parent = new DefaultMutableTreeNode(
137: new Bar());
138: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
139: new Bar());
140: root.add(parent);
141: parent.add(child);
142: JTree tree = new JTree(root);
143: TreePath path = new TreePath(
144: new Object[] { root, parent, child });
145: TreePath strPath = new TreePath(new String[] { "0", "1", "2" });
146: JTreeLocation loc = new JTreeLocation(path);
147: assertEquals(
148: "String path should be converted to real TreePath",
149: path, JTreeLocation.findMatchingPath(tree, strPath));
150:
151: class Foo {
152: }
153: root = new DefaultMutableTreeNode(new Foo());
154: parent = new DefaultMutableTreeNode(new Foo());
155: child = new DefaultMutableTreeNode(new Foo());
156: root.add(parent);
157: parent.add(child);
158: tree = new JTree(root);
159:
160: path = new TreePath(new Object[] { root, parent, child });
161: loc = new JTreeLocation(path);
162: assertEquals("Should return original tree path if it's not a "
163: + "string-based path", path, JTreeLocation
164: .findMatchingPath(tree, path));
165: }
166:
167: public void testFindDuplicateMatchingPath() {
168: class Bar {
169: private int id;
170:
171: public Bar(int id) {
172: this .id = id;
173: }
174:
175: public String toString() {
176: return String.valueOf(id);
177: }
178: }
179: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
180: new Bar(0));
181: DefaultMutableTreeNode parent = new DefaultMutableTreeNode(
182: new Bar(0));
183: DefaultMutableTreeNode parent2 = new DefaultMutableTreeNode(
184: new Bar(0));
185: // Make these have the same string value
186: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
187: new Bar(1));
188: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(
189: new Bar(1));
190: DefaultMutableTreeNode child3 = new DefaultMutableTreeNode(
191: new Bar(1));
192: DefaultMutableTreeNode child4 = new DefaultMutableTreeNode(
193: new Bar(1));
194: root.add(parent);
195: root.add(parent2);
196: parent.add(child);
197: parent.add(child2);
198: parent2.add(child3);
199: parent2.add(child4);
200: JTree tree = new JTree(root);
201: TreePath path = new TreePath(new Object[] { root, parent,
202: child2 });
203: TreePath strpath = new TreePath(
204: new String[] { "0", "0", "1[1]" });
205: JTreeLocation loc = new JTreeLocation(path);
206: assertEquals(
207: "String path should be converted to real TreePath",
208: path, JTreeLocation.findMatchingPath(tree, strpath));
209:
210: path = new TreePath(new Object[] { root, parent, child });
211: strpath = new TreePath(new String[] { "0", "0", "1" });
212: loc = new JTreeLocation(path);
213: assertEquals(
214: "String path should be converted to real TreePath",
215: path, JTreeLocation.findMatchingPath(tree, strpath));
216:
217: path = new TreePath(new Object[] { root, parent2, child3 });
218: strpath = new TreePath(new String[] { "0", "0[1]", "1" });
219: loc = new JTreeLocation(path);
220: assertEquals(
221: "String path should be converted to real TreePath",
222: path, JTreeLocation.findMatchingPath(tree, strpath));
223:
224: path = new TreePath(new Object[] { root, parent2, child4 });
225: strpath = new TreePath(new String[] { "0", "0[1]", "1[1]" });
226: loc = new JTreeLocation(path);
227: assertEquals(
228: "String path should be converted to real TreePath",
229: path, JTreeLocation.findMatchingPath(tree, strpath));
230:
231: }
232:
233: // TODO: add recorder test as well
234: public JTreeLocationTest(String name) {
235: super (name);
236: }
237:
238: public static void main(String[] args) {
239: TestHelper.runTests(args, JTreeLocationTest.class);
240: }
241: }
|