Skip to content

Commit 79288de

Browse files
committed
8285305: Create an automated test for JDK-4495286
Backport-of: 25e9901aeacd45f8baec6d1e52a6823e7f087fa3
1 parent 3461ff3 commit 79288de

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/*
2+
* Copyright (c) 2010, 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 4495286
28+
* @summary Verify that AccessibleJTable.setAccessibleSelction
29+
* selects rows/cols if getCellSelectionEnabled() is false
30+
* @run main AccessibleJTableSelectionTest
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.Robot;
35+
36+
import javax.swing.JFrame;
37+
import javax.swing.JScrollPane;
38+
import javax.swing.JTable;
39+
import javax.swing.ListSelectionModel;
40+
import javax.swing.SwingUtilities;
41+
42+
public final class AccessibleJTableSelectionTest {
43+
44+
private static JTable jTable;
45+
private static JFrame jFrame;
46+
47+
private static Robot robot;
48+
49+
private static void createGUI() {
50+
51+
Object[][] rowData = { { "RowData1", Integer.valueOf(1) },
52+
{ "RowData2", Integer.valueOf(2) },
53+
{ "RowData3", Integer.valueOf(3) } };
54+
Object[] columnData = { "Column One", "Column Two" };
55+
56+
jTable = new JTable(rowData, columnData);
57+
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
58+
jTable.setRowSelectionAllowed(false);
59+
jTable.setColumnSelectionAllowed(false);
60+
jTable.setCellSelectionEnabled(true);
61+
62+
jFrame = new JFrame();
63+
jFrame.add(new JScrollPane(jTable), BorderLayout.CENTER);
64+
jFrame.setSize(200, 200);
65+
jFrame.setLocationRelativeTo(null);
66+
jFrame.setVisible(true);
67+
}
68+
69+
private static void doTest() throws Exception {
70+
SwingUtilities.invokeAndWait(() -> createGUI());
71+
72+
robot = new Robot();
73+
robot.waitForIdle();
74+
SwingUtilities.invokeAndWait(() -> {
75+
jTable.requestFocus();
76+
jTable.getAccessibleContext().getAccessibleSelection()
77+
.addAccessibleSelection(1);
78+
});
79+
80+
robot.waitForIdle();
81+
SwingUtilities.invokeAndWait(() -> {
82+
if (!jTable.isRowSelected(0) || !jTable.isColumnSelected(1)) {
83+
throw new RuntimeException(
84+
"Unexpected selection state of "
85+
+ "Table Row & Column");
86+
}
87+
});
88+
89+
robot.waitForIdle();
90+
SwingUtilities.invokeAndWait(() -> {
91+
jTable.setRowSelectionAllowed(true);
92+
jTable.setColumnSelectionAllowed(false);
93+
jTable.setCellSelectionEnabled(false);
94+
jTable.requestFocus();
95+
jTable.getAccessibleContext().getAccessibleSelection()
96+
.addAccessibleSelection(3);
97+
});
98+
99+
robot.waitForIdle();
100+
SwingUtilities.invokeAndWait(() -> {
101+
if (!jTable.isRowSelected(1)) {
102+
throw new RuntimeException(
103+
"Unexpected selection state of "
104+
+ "Table Row & Column");
105+
}
106+
});
107+
108+
robot.waitForIdle();
109+
SwingUtilities.invokeAndWait(() -> {
110+
jTable.setRowSelectionAllowed(false);
111+
jTable.setColumnSelectionAllowed(true);
112+
jTable.setCellSelectionEnabled(false);
113+
jTable.requestFocus();
114+
jTable.getAccessibleContext().getAccessibleSelection()
115+
.addAccessibleSelection(4);
116+
});
117+
118+
robot.waitForIdle();
119+
SwingUtilities.invokeAndWait(() -> {
120+
if (!jTable.isColumnSelected(0)) {
121+
throw new RuntimeException(
122+
"Unexpected selection state of "
123+
+ "Table Row & Column");
124+
}
125+
});
126+
127+
robot.waitForIdle();
128+
SwingUtilities.invokeAndWait(() -> {
129+
jTable.setRowSelectionAllowed(true);
130+
jTable.setColumnSelectionAllowed(true);
131+
jTable.setCellSelectionEnabled(false);
132+
jTable.requestFocus();
133+
jTable.getAccessibleContext().getAccessibleSelection()
134+
.addAccessibleSelection(5);
135+
});
136+
137+
robot.waitForIdle();
138+
SwingUtilities.invokeAndWait(() -> {
139+
if (!(jTable.isRowSelected(2) && jTable.isColumnSelected(1))) {
140+
throw new RuntimeException(
141+
"Unexpected selection state of "
142+
+ "Table Row & Column");
143+
}
144+
});
145+
146+
robot.waitForIdle();
147+
SwingUtilities.invokeAndWait(() -> {
148+
jTable.setCellSelectionEnabled(true);
149+
jTable.setColumnSelectionAllowed(true);
150+
jTable.setRowSelectionAllowed(true);
151+
jTable.requestFocus();
152+
jTable.getAccessibleContext().getAccessibleSelection()
153+
.addAccessibleSelection(4);
154+
});
155+
156+
robot.waitForIdle();
157+
SwingUtilities.invokeAndWait(() -> {
158+
if (!(jTable.isRowSelected(2) && jTable.isColumnSelected(0)
159+
&& jTable.isCellSelected(2, 0))) {
160+
throw new RuntimeException(
161+
"Unexpected selection state of "
162+
+ "Table Row & Column");
163+
}
164+
});
165+
}
166+
167+
public static void main(final String[] argv) throws Exception {
168+
doTest();
169+
SwingUtilities.invokeAndWait(() -> jFrame.dispose());
170+
System.out.println("Test Passed.");
171+
}
172+
}
173+

0 commit comments

Comments
 (0)