|
1
|
|
package abbot.tester;
|
|
2
|
|
|
|
3
|
|
import java.awt.*;
|
|
4
|
|
import javax.swing.*;
|
|
5
|
|
import javax.swing.tree.*;
|
|
6
|
|
|
|
7
|
|
import junit.extensions.abbot.*;
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
public class JTreeTesterTest extends ComponentTestFixture {
|
|
12
|
|
|
|
13
|
|
private JTreeTester tester;
|
|
14
|
|
private JTree tree;
|
|
15
|
|
private JScrollPane scrollPane;
|
|
16
|
|
|
|
17
|
13
|
protected void setUp() {
|
|
18
|
13
|
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
|
|
19
|
13
|
DefaultMutableTreeNode parent, child, leaf;
|
|
20
|
13
|
int COUNT = 5;
|
|
21
|
13
|
for (int i=0;i < COUNT;i++) {
|
|
22
|
65
|
parent = new DefaultMutableTreeNode("parent " + i);
|
|
23
|
65
|
root.add(parent);
|
|
24
|
65
|
for (int j=0;j < COUNT;j++) {
|
|
25
|
325
|
child = new DefaultMutableTreeNode("child " + j);
|
|
26
|
325
|
parent.add(child);
|
|
27
|
325
|
for (int k=0;k < COUNT;k++) {
|
|
28
|
1625
|
leaf = new DefaultMutableTreeNode("leaf " + k, false);
|
|
29
|
1625
|
child.add(leaf);
|
|
30
|
|
}
|
|
31
|
|
}
|
|
32
|
|
}
|
|
33
|
13
|
tree = new JTree(root);
|
|
34
|
13
|
scrollPane = new JScrollPane(tree);
|
|
35
|
13
|
tester = new JTreeTester();
|
|
36
|
|
}
|
|
37
|
|
|
|
38
|
1
|
public void testFindPathForStringPath() {
|
|
39
|
1
|
TreePath stringPath = new TreePath(new String[] {
|
|
40
|
|
"root", "parent 2", "child 2"
|
|
41
|
|
});
|
|
42
|
1
|
TreePath path = JTreeLocation.findMatchingPath(tree, stringPath);
|
|
43
|
1
|
assertNotNull("Couldn't convert path", path);
|
|
44
|
1
|
assertEquals("Wrong path",
|
|
45
|
|
"[root, parent 2, child 2]", path.toString());
|
|
46
|
|
}
|
|
47
|
|
|
|
48
|
1
|
public void testMakeVisibleWithStringPath() {
|
|
49
|
1
|
showFrame(scrollPane);
|
|
50
|
1
|
TreePath stringPath = new TreePath(new String[] {
|
|
51
|
|
"root", "parent 4", "child 3"
|
|
52
|
|
});
|
|
53
|
1
|
tester.actionMakeVisible(tree, stringPath);
|
|
54
|
1
|
TreePath path = JTreeLocation.findMatchingPath(tree, stringPath);
|
|
55
|
1
|
assertTrue("Path not visible", tree.isVisible(path));
|
|
56
|
|
}
|
|
57
|
|
|
|
58
|
1
|
public void testMakeVisibleWithRealPath() {
|
|
59
|
1
|
TreePath stringPath = new TreePath(new String[] {
|
|
60
|
|
"root", "parent 4", "child 3", "leaf 1"
|
|
61
|
|
});
|
|
62
|
1
|
TreePath path = JTreeLocation.findMatchingPath(tree, stringPath);
|
|
63
|
1
|
tester.actionMakeVisible(tree, path);
|
|
64
|
1
|
assertTrue("Path not visible", tree.isVisible(path));
|
|
65
|
|
}
|
|
66
|
|
|
|
67
|
1
|
public void testMakeVisibleWithHiddenRoot() {
|
|
68
|
1
|
tree.setRootVisible(false);
|
|
69
|
1
|
tester.actionWaitForIdle();
|
|
70
|
1
|
TreePath stringPath = new TreePath(new String[] {
|
|
71
|
|
"parent 2", "child 1", "leaf 2"
|
|
72
|
|
});
|
|
73
|
1
|
TreePath path = JTreeLocation.findMatchingPath(tree, stringPath);
|
|
74
|
1
|
tester.actionMakeVisible(tree, path);
|
|
75
|
1
|
assertTrue("Path not visible", tree.isVisible(path));
|
|
76
|
|
}
|
|
77
|
|
|
|
78
|
1
|
public void testMakeVisibleMissingPath() {
|
|
79
|
1
|
showFrame(scrollPane);
|
|
80
|
1
|
TreePath stringPath = new TreePath(new String[] {
|
|
81
|
|
"root", "parent 4", "child x"
|
|
82
|
|
});
|
|
83
|
1
|
try {
|
|
84
|
1
|
tester.actionMakeVisible(tree, stringPath);
|
|
85
|
0
|
fail("Should throw an exception when path doesn't match");
|
|
86
|
|
}
|
|
87
|
|
catch(LocationUnavailableException e) {
|
|
88
|
|
}
|
|
89
|
|
}
|
|
90
|
|
|
|
91
|
1
|
public void testSelectPath() {
|
|
92
|
1
|
showFrame(scrollPane);
|
|
93
|
|
|
|
94
|
1
|
TreePath stringPath = new TreePath(new String[] {
|
|
95
|
|
"root", "parent 2", "child 2"
|
|
96
|
|
});
|
|
97
|
1
|
TreePath path = JTreeLocation.findMatchingPath(tree, stringPath);
|
|
98
|
1
|
assertNotNull("Couldn't convert path", path);
|
|
99
|
|
|
|
100
|
1
|
tester.actionSelectRow(tree, new JTreeLocation(stringPath));
|
|
101
|
1
|
TreePath[] paths = tree.getSelectionPaths();
|
|
102
|
1
|
assertTrue("Paths not visible", tree.isVisible(path));
|
|
103
|
1
|
assertTrue("No paths selected", paths != null);
|
|
104
|
1
|
assertEquals("Too many selected", 1, paths.length);
|
|
105
|
1
|
assertEquals("Wrong path selected", stringPath.toString(), paths[0].toString());
|
|
106
|
|
}
|
|
107
|
|
|
|
108
|
1
|
public void testSelectPathHiddenRoot() {
|
|
109
|
1
|
showFrame(scrollPane);
|
|
110
|
|
|
|
111
|
1
|
TreePath stringPath = new TreePath(new String[] {
|
|
112
|
|
"root", "parent 2", "child 2"
|
|
113
|
|
});
|
|
114
|
1
|
TreePath stringPath2 = new TreePath(new String[] {
|
|
115
|
|
"parent 2", "child 1"
|
|
116
|
|
});
|
|
117
|
1
|
tree.setRootVisible(false);
|
|
118
|
1
|
TreePath path = JTreeLocation.findMatchingPath(tree, stringPath);
|
|
119
|
1
|
assertNotNull("Couldn't convert path", path);
|
|
120
|
|
|
|
121
|
1
|
tester.actionSelectRow(tree, new JTreeLocation(stringPath));
|
|
122
|
1
|
TreePath[] paths = tree.getSelectionPaths();
|
|
123
|
1
|
assertTrue("Paths not visible", tree.isVisible(path));
|
|
124
|
1
|
assertTrue("No paths selected", paths != null);
|
|
125
|
1
|
assertEquals("Too many selected", 1, paths.length);
|
|
126
|
1
|
assertEquals("Wrong path selected", stringPath.toString(), paths[0].toString());
|
|
127
|
|
|
|
128
|
1
|
path = JTreeLocation.findMatchingPath(tree, stringPath2);
|
|
129
|
1
|
tester.actionSelectRow(tree, new JTreeLocation(stringPath2));
|
|
130
|
1
|
paths = tree.getSelectionPaths();
|
|
131
|
1
|
assertTrue("Paths not visible", tree.isVisible(path));
|
|
132
|
1
|
assertTrue("No paths selected", paths != null);
|
|
133
|
1
|
assertEquals("Too many selected", 1, paths.length);
|
|
134
|
1
|
assertEquals("Wrong path selected",
|
|
135
|
|
"[root, parent 2, child 1]", paths[0].toString());
|
|
136
|
|
}
|
|
137
|
|
|
|
138
|
1
|
public void testSelectRow() {
|
|
139
|
1
|
int row = 1;
|
|
140
|
1
|
showFrame(scrollPane);
|
|
141
|
1
|
tester.actionSelectRow(tree, new JTreeLocation(row));
|
|
142
|
1
|
TreePath[] paths = tree.getSelectionPaths();
|
|
143
|
1
|
assertTrue("No paths selected", paths != null);
|
|
144
|
1
|
assertEquals("Wrong row count selected", 1, paths.length);
|
|
145
|
1
|
assertEquals("Wrong row selected", tree.getPathForRow(row), paths[0]);
|
|
146
|
|
}
|
|
147
|
|
|
|
148
|
1
|
public void testSelectInvisibleRow() {
|
|
149
|
1
|
showFrame(scrollPane);
|
|
150
|
1
|
try {
|
|
151
|
1
|
tester.actionSelectRow(tree, new JTreeLocation(200));
|
|
152
|
0
|
fail("Should not successfully select an unavailable row");
|
|
153
|
|
}
|
|
154
|
|
catch(ActionFailedException e) {
|
|
155
|
|
}
|
|
156
|
|
}
|
|
157
|
|
|
|
158
|
1
|
public void testToggleRow() {
|
|
159
|
1
|
int row = 2;
|
|
160
|
1
|
showFrame(scrollPane);
|
|
161
|
1
|
tester.actionToggleRow(tree, new JTreeLocation(row));
|
|
162
|
1
|
assertTrue("Node should have expanded", tree.isExpanded(row));
|
|
163
|
|
|
|
164
|
1
|
tester.delay(500);
|
|
165
|
1
|
tester.actionToggleRow(tree, new JTreeLocation(row));
|
|
166
|
1
|
assertTrue("Node should not still be expanded", !tree.isExpanded(row));
|
|
167
|
|
}
|
|
168
|
|
|
|
169
|
1
|
public void testGetLocation() {
|
|
170
|
1
|
showFrame(tree, new Dimension(200, 450));
|
|
171
|
|
|
|
172
|
1
|
Rectangle rect = tree.getRowBounds(1);
|
|
173
|
1
|
Point expansion = new Point(rect.x/2, rect.y + rect.height/2);
|
|
174
|
1
|
tester.actionClick(tree, new JTreeLocation(expansion));
|
|
175
|
1
|
assertTrue("Node should have expanded", tree.isExpanded(1));
|
|
176
|
1
|
ComponentLocation loc = tester.getLocation(tree, expansion);
|
|
177
|
1
|
Point where = loc.getPoint(tree);
|
|
178
|
1
|
assertTrue("Row expansion click should designate row",
|
|
179
|
|
rect.contains(where));
|
|
180
|
|
|
|
181
|
1
|
Point midRow = new Point(rect.x + rect.width/2,
|
|
182
|
|
rect.y + rect.height/2);
|
|
183
|
1
|
loc = tester.getLocation(tree, midRow);
|
|
184
|
1
|
where = loc.getPoint(tree);
|
|
185
|
1
|
assertTrue("Mid-row click should designate row",
|
|
186
|
|
rect.contains(where));
|
|
187
|
|
|
|
188
|
1
|
rect = tree.getRowBounds(tree.getRowCount()-1);
|
|
189
|
1
|
Point belowTree = new Point(rect.x + rect.width/2,
|
|
190
|
|
rect.y + rect.height + 10);
|
|
191
|
1
|
loc = tester.getLocation(tree, belowTree);
|
|
192
|
1
|
where = loc.getPoint(tree);
|
|
193
|
1
|
assertEquals("Point outside tree should store raw point",
|
|
194
|
|
belowTree, where);
|
|
195
|
|
}
|
|
196
|
|
|
|
197
|
|
static int count = 0;
|
|
198
|
1
|
public void testConvertPathToStringPath() {
|
|
199
|
|
class Foo { }
|
|
200
|
1
|
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new Foo());
|
|
201
|
1
|
DefaultMutableTreeNode parent = new DefaultMutableTreeNode(new Foo());
|
|
202
|
1
|
DefaultMutableTreeNode child = new DefaultMutableTreeNode(new Foo());
|
|
203
|
1
|
root.add(parent);
|
|
204
|
1
|
parent.add(child);
|
|
205
|
1
|
JTree tree = new JTree(root);
|
|
206
|
1
|
TreePath path = new TreePath(new Object[] { root, parent, child });
|
|
207
|
1
|
TreePath strPath = tester.pathToStringPath(tree, path);
|
|
208
|
1
|
assertNull("Should not use default toString() value as path: "
|
|
209
|
|
+ strPath, strPath);
|
|
210
|
|
|
|
211
|
1
|
count = 0;
|
|
212
|
|
class Bar {
|
|
213
|
|
private int id = count++;
|
|
214
|
8
|
public String toString() { return String.valueOf(id); }
|
|
215
|
|
}
|
|
216
|
1
|
root = new DefaultMutableTreeNode(new Bar());
|
|
217
|
1
|
parent = new DefaultMutableTreeNode(new Bar());
|
|
218
|
1
|
child = new DefaultMutableTreeNode(new Bar());
|
|
219
|
1
|
root.add(parent);
|
|
220
|
1
|
parent.add(child);
|
|
221
|
1
|
tree = new JTree(root);
|
|
222
|
1
|
showFrame(new JScrollPane(tree));
|
|
223
|
1
|
path = new TreePath(new Object[] { root, parent, child });
|
|
224
|
1
|
assertEquals("Path should be stringifiable",
|
|
225
|
|
"[0, 1, 2]",
|
|
226
|
|
tester.pathToStringPath(tree, path).toString());
|
|
227
|
|
}
|
|
228
|
|
|
|
229
|
1
|
public void testAssertPathExists() {
|
|
230
|
1
|
TreePath path = new TreePath(new String[] {
|
|
231
|
|
"root", "parent 3", "child 1",
|
|
232
|
|
});
|
|
233
|
1
|
assertTrue("Path should exist",
|
|
234
|
|
tester.assertPathExists(tree, path));
|
|
235
|
1
|
path = new TreePath(new String[] {
|
|
236
|
|
"root", "parent 2", "child y",
|
|
237
|
|
});
|
|
238
|
1
|
assertFalse("Path should not exist",
|
|
239
|
|
tester.assertPathExists(tree, path));
|
|
240
|
|
}
|
|
241
|
|
|
|
242
|
|
|
|
243
|
13
|
public JTreeTesterTest(String name) {
|
|
244
|
13
|
super(name);
|
|
245
|
|
}
|
|
246
|
|
|
|
247
|
0
|
public static void main(String[] args) {
|
|
248
|
0
|
RepeatHelper.runTests(args, JTreeTesterTest.class);
|
|
249
|
|
}
|
|
250
|
|
}
|
|
251
|
|
|
|
252
|
|
|