Skip to content

Commit a98f9cf

Browse files
committed
8316056: Open source several Swing JTree tests
Backport-of: 5f6cee86ef765677b0b9dc3662f4f20b636732bc
1 parent c7c7280 commit a98f9cf

File tree

5 files changed

+347
-0
lines changed

5 files changed

+347
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
* @bug 4210432
26+
* @summary Tests if JTree allows nodes not visible to be selected
27+
* @run main bug4210432
28+
*/
29+
30+
import javax.swing.JPanel;
31+
import javax.swing.JTree;
32+
import javax.swing.SwingUtilities;
33+
import javax.swing.tree.DefaultMutableTreeNode;
34+
import javax.swing.tree.DefaultTreeModel;
35+
import javax.swing.tree.TreePath;
36+
37+
public class bug4210432 {
38+
public static void main(String[] args) throws Exception {
39+
SwingUtilities.invokeAndWait(() -> {
40+
JPanel p = new JPanel();
41+
DefaultMutableTreeNode expansible =
42+
new DefaultMutableTreeNode("expansible");
43+
DefaultMutableTreeNode unexpansible =
44+
new DefaultMutableTreeNode("unexpansible");
45+
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
46+
DefaultMutableTreeNode subexpansible1 =
47+
new DefaultMutableTreeNode("sub-expansible 1");
48+
DefaultMutableTreeNode subexpansible2 =
49+
new DefaultMutableTreeNode("sub-expansible 2");
50+
DefaultMutableTreeNode subsubexpansible1 =
51+
new DefaultMutableTreeNode("sub-sub-expansible 1");
52+
DefaultMutableTreeNode subsubexpansible2 =
53+
new DefaultMutableTreeNode("sub-sub-expansible 2");
54+
expansible.add(subexpansible1);
55+
expansible.add(subexpansible2);
56+
subexpansible1.add(subsubexpansible1);
57+
subexpansible1.add(subsubexpansible2);
58+
root.add(expansible);
59+
root.add(unexpansible);
60+
DefaultTreeModel model = new DefaultTreeModel(root);
61+
JTree t = new JTree(model);
62+
Object[] tpa = {root, expansible, subexpansible1};
63+
Object[] tpa2 = {root, expansible};
64+
t.setExpandsSelectedPaths(false);
65+
t.setSelectionPath(new TreePath(tpa));
66+
p.add(t);
67+
if (t.isExpanded(new TreePath(tpa2))) {
68+
throw new RuntimeException("Test failed: JTree should not have " +
69+
"expanded path");
70+
}
71+
t.clearSelection();
72+
t.setExpandsSelectedPaths(true);
73+
t.setSelectionPath(new TreePath(tpa));
74+
if (!t.isExpanded(new TreePath(tpa2))) {
75+
throw new RuntimeException("Test failed: JTree should have " +
76+
"expanded path");
77+
}
78+
});
79+
}
80+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
* @bug 4213868
26+
* @summary Tests if AccessibleJTreeNode.getAccessibleIndexInParent() returns
27+
* correct value
28+
* @run main bug4213868
29+
*/
30+
31+
import javax.accessibility.AccessibleContext;
32+
import javax.swing.JTree;
33+
import javax.swing.SwingUtilities;
34+
import javax.swing.tree.DefaultMutableTreeNode;
35+
36+
public class bug4213868 {
37+
public static JTree createTree() {
38+
DefaultMutableTreeNode root =
39+
new DefaultMutableTreeNode(0, true);
40+
JTree tree = new JTree(root);
41+
for (int i = 1; i < 10; i++) {
42+
root.add(new DefaultMutableTreeNode(i));
43+
}
44+
return tree;
45+
}
46+
47+
public static void main(String[] args) throws Exception {
48+
SwingUtilities.invokeAndWait(() -> {
49+
JTree parent = createTree();
50+
AccessibleContext c = parent.getAccessibleContext()
51+
.getAccessibleChild(0)
52+
.getAccessibleContext();
53+
if (c.getAccessibleChild(1)
54+
.getAccessibleContext()
55+
.getAccessibleIndexInParent() != 1) {
56+
throw new RuntimeException("Test failed: " +
57+
"AccessibleJTreeNode.getAccessibleIndexInParent() " +
58+
"returns incorrect value");
59+
}
60+
});
61+
}
62+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
* @bug 4224491
26+
* @summary Tests if JTree's model & invokesStopCellEditing bound properties
27+
* are working
28+
* @run main bug4224491
29+
*/
30+
31+
import javax.swing.JTree;
32+
import javax.swing.SwingUtilities;
33+
import javax.swing.tree.DefaultMutableTreeNode;
34+
import javax.swing.tree.DefaultTreeModel;
35+
36+
public class bug4224491 {
37+
private static boolean modelChanged = false;
38+
private static boolean invokesStopCellEditingChanged = false;
39+
40+
public static void main(String[] args) throws Exception {
41+
SwingUtilities.invokeAndWait(() -> {
42+
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
43+
JTree jt = new JTree(new DefaultTreeModel(root));
44+
jt.addPropertyChangeListener(evt -> {
45+
if (evt.getPropertyName().equals("model")) {
46+
modelChanged = true;
47+
}
48+
if (evt.getPropertyName().equals("invokesStopCellEditing")) {
49+
invokesStopCellEditingChanged = true;
50+
}
51+
});
52+
jt.setModel(new DefaultTreeModel(root));
53+
jt.setInvokesStopCellEditing(true);
54+
if (!(modelChanged && invokesStopCellEditingChanged)) {
55+
throw new RuntimeException("Test failed: JTree's model " +
56+
"& invokesStopCellEditing bound properties " +
57+
"are not working");
58+
}
59+
});
60+
}
61+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
* @bug 4237370
26+
* @summary Tests that JTree calls TreeExpansionListener methods
27+
* after it has been updated due to expanded/collapsed event
28+
* @run main bug4237370
29+
*/
30+
31+
import java.lang.reflect.InvocationTargetException;
32+
33+
import javax.swing.JTree;
34+
import javax.swing.SwingUtilities;
35+
import javax.swing.event.TreeExpansionEvent;
36+
import javax.swing.event.TreeExpansionListener;
37+
import javax.swing.tree.DefaultMutableTreeNode;
38+
import javax.swing.tree.DefaultTreeModel;
39+
import javax.swing.tree.TreeModel;
40+
41+
public class bug4237370 {
42+
static class TestTree extends JTree implements TreeExpansionListener {
43+
int[] testMap = {1, 2};
44+
int testIndex = 0;
45+
46+
private void testRowCount() {
47+
int rows = getRowCount();
48+
if (rows != testMap[testIndex]) {
49+
throw new RuntimeException("Bad row count: reported " + rows +
50+
" instead of " + testMap[testIndex]);
51+
} else {
52+
testIndex++;
53+
}
54+
}
55+
56+
public void treeExpanded(TreeExpansionEvent e) {
57+
testRowCount();
58+
}
59+
60+
public void treeCollapsed(TreeExpansionEvent e) {
61+
testRowCount();
62+
}
63+
64+
public TestTree() {
65+
super((TreeModel)null);
66+
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Root");
67+
top.add(new DefaultMutableTreeNode("Sub 1"));
68+
setModel(new DefaultTreeModel(top));
69+
addTreeExpansionListener(this);
70+
}
71+
}
72+
73+
public static void main(String[] args) throws InterruptedException,
74+
InvocationTargetException {
75+
SwingUtilities.invokeAndWait(() -> {
76+
TestTree tree = new TestTree();
77+
tree.collapseRow(0);
78+
tree.expandRow(0);
79+
});
80+
}
81+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
* @bug 4662505
26+
* @summary IllegalArgumentException with empty JTree and key event
27+
* @run main bug4662505
28+
*/
29+
30+
import java.awt.event.KeyEvent;
31+
import java.util.Date;
32+
33+
import javax.swing.JTree;
34+
import javax.swing.SwingUtilities;
35+
36+
public class bug4662505 {
37+
static DummyTree tree;
38+
39+
public static void main(String[] args) throws Exception {
40+
SwingUtilities.invokeAndWait(() -> {
41+
tree = new DummyTree();
42+
43+
try {
44+
tree.doTest();
45+
} catch (Exception e) {
46+
throw new RuntimeException("Empty JTree shouldn't handle " +
47+
"first letter navigation", e);
48+
}
49+
});
50+
}
51+
52+
static class DummyTree extends JTree {
53+
public DummyTree() {
54+
super(new Object[]{});
55+
}
56+
57+
public void doTest() {
58+
KeyEvent key = new KeyEvent(tree, KeyEvent.KEY_TYPED,
59+
new Date().getTime(), 0, KeyEvent.VK_UNDEFINED, 'a');
60+
processKeyEvent(key);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)