Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/awt/Focus/FocusTraversalPolicy/ButtonGroupLayoutTraversal/ButtonGroupLayoutTraversalTest.java
66646 views
1
/*
2
* Copyright (c) 2016, 2021, 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 8154043 8172509
28
* @summary Fields not reachable anymore by tab-key, because of new tabbing
29
* behaviour of radio button groups.
30
* @run main ButtonGroupLayoutTraversalTest
31
*/
32
33
import java.awt.BorderLayout;
34
import java.awt.Color;
35
import java.awt.GridLayout;
36
import java.awt.Robot;
37
import java.awt.event.FocusAdapter;
38
import java.awt.event.FocusEvent;
39
import java.awt.event.KeyEvent;
40
import java.util.Arrays;
41
import javax.swing.JFrame;
42
import javax.swing.SwingUtilities;
43
import javax.swing.UIManager;
44
import javax.swing.JPanel;
45
import javax.swing.ButtonGroup;
46
import javax.swing.JComponent;
47
import javax.swing.JRadioButton;
48
import javax.swing.JToggleButton;
49
import javax.swing.LayoutFocusTraversalPolicy;
50
import javax.swing.UnsupportedLookAndFeelException;
51
52
public class ButtonGroupLayoutTraversalTest {
53
54
private static final int NX = 3;
55
private static final int NY = 3;
56
57
private static final int[] focusCnt = new int[NX * NY];
58
59
private static JFrame window;
60
private static Robot robot;
61
62
public static void main(String[] args) throws Exception {
63
robot = new Robot();
64
robot.setAutoDelay(100);
65
66
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
67
try {
68
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
69
SwingUtilities.invokeAndWait(() -> initLayout(NX, NY));
70
test();
71
} finally {
72
SwingUtilities.invokeAndWait(() -> {
73
if (window != null) {
74
window.dispose();
75
}
76
window = null;
77
synchronized (focusCnt) {
78
Arrays.fill(focusCnt, 0);
79
}
80
});
81
}
82
}
83
}
84
85
86
private static void test() {
87
robot.waitForIdle();
88
robot.delay(1000);
89
90
for (int i = 0; i < NX * NY - NX * NY / 2 - 1; i++) {
91
robot.keyPress(KeyEvent.VK_RIGHT);
92
robot.keyRelease(KeyEvent.VK_RIGHT);
93
robot.waitForIdle();
94
}
95
96
for (int i = 0; i < NX * NY / 2; i++) {
97
robot.keyPress(KeyEvent.VK_TAB);
98
robot.keyRelease(KeyEvent.VK_TAB);
99
robot.waitForIdle();
100
}
101
102
robot.delay(200);
103
104
synchronized (focusCnt) {
105
for (int i = 0; i < NX * NY; i++) {
106
if (focusCnt[i] < 1) {
107
throw new RuntimeException("Component " + i
108
+ " is not reachable in the forward focus cycle");
109
} else if (focusCnt[i] > 1) {
110
throw new RuntimeException("Component " + i
111
+ " got focus more than once in the forward focus cycle");
112
}
113
}
114
}
115
116
for (int i = 0; i < NX * NY / 2; i++) {
117
robot.keyPress(KeyEvent.VK_SHIFT);
118
robot.keyPress(KeyEvent.VK_TAB);
119
robot.keyRelease(KeyEvent.VK_TAB);
120
robot.keyRelease(KeyEvent.VK_SHIFT);
121
robot.waitForIdle();
122
}
123
124
for (int i = 0; i < NX * NY - NX * NY / 2 - 1; i++) {
125
robot.keyPress(KeyEvent.VK_LEFT);
126
robot.keyRelease(KeyEvent.VK_LEFT);
127
robot.waitForIdle();
128
}
129
130
robot.keyPress(KeyEvent.VK_SHIFT);
131
robot.keyPress(KeyEvent.VK_TAB);
132
robot.keyRelease(KeyEvent.VK_TAB);
133
robot.keyRelease(KeyEvent.VK_SHIFT);
134
robot.waitForIdle();
135
136
robot.delay(200);
137
138
synchronized (focusCnt) {
139
for (int i = 0; i < NX * NY; i++) {
140
if (focusCnt[i] < 2) {
141
throw new RuntimeException("Component " + i
142
+ " is not reachable in the backward focus cycle");
143
} else if (focusCnt[i] > 2) {
144
throw new RuntimeException("Component " + i
145
+ " got focus more than once in the backward focus cycle");
146
}
147
}
148
}
149
150
}
151
152
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
153
try {
154
UIManager.setLookAndFeel(laf.getClassName());
155
System.out.println(laf.getName());
156
} catch (UnsupportedLookAndFeelException ignored){
157
System.out.println("Unsupported LookAndFeel: " + laf.getClassName());
158
} catch (ClassNotFoundException | InstantiationException |
159
IllegalAccessException e) {
160
throw new RuntimeException(e);
161
}
162
}
163
164
public static void initLayout(int nx, int ny) {
165
window = new JFrame("Test");
166
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
167
JPanel rootPanel = new JPanel();
168
rootPanel.setLayout(new BorderLayout());
169
JPanel formPanel = new JPanel(new GridLayout(nx, ny));
170
formPanel.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
171
formPanel.setFocusCycleRoot(true);
172
ButtonGroup radioButtonGroup = new ButtonGroup();
173
for (int i = 0; i < nx * ny; i++) {
174
JToggleButton comp;
175
if (i % 2 == 0) {
176
comp = new JRadioButton("Grouped component");
177
radioButtonGroup.add(comp);
178
} else {
179
comp = new JRadioButton("Single component");
180
}
181
formPanel.add(comp);
182
int fi = i;
183
comp.setBackground(Color.red);
184
comp.addFocusListener(new FocusAdapter() {
185
@Override
186
public void focusGained(FocusEvent e) {
187
synchronized (focusCnt) {
188
focusCnt[fi]++;
189
JComponent btn = (JComponent) e.getSource();
190
if (focusCnt[fi] == 1) {
191
btn.setBackground(Color.yellow);
192
} else if (focusCnt[fi] == 2) {
193
btn.setBackground(Color.green);
194
} else {
195
btn.setBackground(Color.red);
196
}
197
}
198
}
199
});
200
}
201
rootPanel.add(formPanel, BorderLayout.CENTER);
202
window.add(rootPanel);
203
window.setLocationRelativeTo(null);
204
window.pack();
205
window.setVisible(true);
206
}
207
}
208
209