Skip to content

Commit fb82b73

Browse files
committed
8283245: Create a test for JDK-4670319
Backport-of: 4ce3cf12bff025441f658e1857e92486b5d73ee6
1 parent 64db633 commit fb82b73

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (c) 2008, 2022, 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+
/*
25+
* @test
26+
* @key headful
27+
* @bug 4670319
28+
* @summary AccessibleJTree should fire a PropertyChangeEvent
29+
* using a AccessibleJTreeNode as source.
30+
* @run main AccessibleJTreePCESourceTest
31+
*/
32+
33+
import java.awt.Robot;
34+
import java.beans.PropertyChangeEvent;
35+
import java.util.ArrayList;
36+
37+
import javax.swing.JFrame;
38+
import javax.swing.JTree;
39+
import javax.swing.SwingUtilities;
40+
41+
public class AccessibleJTreePCESourceTest {
42+
private static JTree jTree;
43+
private static JFrame jFrame;
44+
45+
private static ArrayList<PropertyChangeEvent> eventsList =
46+
new ArrayList<PropertyChangeEvent>();
47+
48+
private static void doTest() throws Exception {
49+
try {
50+
SwingUtilities.invokeAndWait(() -> createGUI());
51+
Robot robot = new Robot();
52+
robot.waitForIdle();
53+
54+
expand(1);
55+
robot.waitForIdle();
56+
collapse(1);
57+
robot.waitForIdle();
58+
expand(2);
59+
robot.waitForIdle();
60+
collapse(2);
61+
robot.waitForIdle();
62+
} finally {
63+
SwingUtilities.invokeAndWait(() -> jFrame.dispose());
64+
}
65+
}
66+
67+
public static void expand(int row) throws Exception {
68+
SwingUtilities.invokeAndWait(() -> jTree.expandRow(row));
69+
}
70+
71+
public static void collapse(int row) throws Exception {
72+
SwingUtilities.invokeAndWait(() -> jTree.collapseRow(row));
73+
}
74+
75+
private static void createGUI() {
76+
jTree = new JTree();
77+
jFrame = new JFrame();
78+
79+
jFrame.add(jTree);
80+
81+
jTree.getAccessibleContext().addPropertyChangeListener(event -> {
82+
if (event.getNewValue() != null) {
83+
eventsList.add(event);
84+
}
85+
});
86+
87+
jFrame.setSize(200, 200);
88+
jFrame.getContentPane().add(jTree);
89+
jFrame.setVisible(true);
90+
}
91+
92+
public static void main(String args[]) throws Exception {
93+
doTest();
94+
95+
for (int i = 0; i < eventsList.size(); i++) {
96+
PropertyChangeEvent obj = eventsList.get(i);
97+
String state = obj.getNewValue().toString();
98+
99+
if ((state.equals("expanded") || state.equals("collapsed"))
100+
&& (obj.getPropertyName().toString())
101+
.equals("AccessibleState")) {
102+
if (!(obj.getSource().getClass().getName()).equals(
103+
"javax.swing.JTree$AccessibleJTree$AccessibleJTreeNode")) {
104+
throw new RuntimeException("Test Failed: When tree node is "
105+
+ state + ", PropertyChangeEventSource is "
106+
+ obj.getSource().getClass().getName());
107+
}
108+
}
109+
}
110+
System.out.println(
111+
"Test Passed: When tree node is expanded/collapsed, "
112+
+ "PropertyChangeEventSource is the Node");
113+
}
114+
}
115+

0 commit comments

Comments
 (0)