Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JRadioButton/FocusTraversal/FocusTraversal.java
41155 views
1
/*
2
* Copyright (c) 2015, 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
* @key headful
26
* @bug 8129940 8132770 8161470 8163169
27
* @summary JRadioButton should run custom FocusTraversalKeys for all LaFs
28
* @run main FocusTraversal
29
*/
30
import java.awt.BorderLayout;
31
import java.awt.Component;
32
import java.awt.KeyboardFocusManager;
33
import java.awt.Robot;
34
import java.awt.event.KeyEvent;
35
import java.util.HashSet;
36
import java.util.Set;
37
import javax.swing.ButtonGroup;
38
import javax.swing.FocusManager;
39
import javax.swing.JButton;
40
import javax.swing.JFrame;
41
import javax.swing.JPanel;
42
import javax.swing.JRadioButton;
43
import javax.swing.JTextField;
44
import javax.swing.KeyStroke;
45
import javax.swing.SwingUtilities;
46
import javax.swing.UIManager;
47
import javax.swing.UnsupportedLookAndFeelException;
48
49
public class FocusTraversal {
50
51
private static JFrame frame;
52
private static JRadioButton a;
53
private static JRadioButton b;
54
private static JRadioButton c;
55
private static JRadioButton d;
56
private static JTextField next;
57
private static JTextField prev;
58
private static Robot robot;
59
60
public static void main(String[] args) throws Exception {
61
62
robot = new Robot();
63
robot.setAutoDelay(100);
64
robot.waitForIdle();
65
UIManager.LookAndFeelInfo[] lookAndFeelArray
66
= UIManager.getInstalledLookAndFeels();
67
for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
68
executeCase(lookAndFeelItem.getClassName());
69
}
70
}
71
72
private static void executeCase(String lookAndFeelString)
73
throws Exception {
74
if (tryLookAndFeel(lookAndFeelString)) {
75
createUI(lookAndFeelString);
76
robot.waitForIdle();
77
runTestCase();
78
robot.waitForIdle();
79
cleanUp();
80
robot.waitForIdle();
81
}
82
}
83
84
private static void createUI(final String lookAndFeelString)
85
throws Exception {
86
SwingUtilities.invokeAndWait(new Runnable() {
87
@Override
88
public void run() {
89
Set<KeyStroke> keystrokes = new HashSet<KeyStroke>();
90
keystrokes.add(KeyStroke.getKeyStroke("TAB"));
91
keystrokes.add(KeyStroke.getKeyStroke("ENTER"));
92
frame = new JFrame("FocusTraversalTest " + lookAndFeelString);
93
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
94
frame.setUndecorated(true);
95
frame.setFocusTraversalKeys(
96
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
97
keystrokes);
98
99
a = new JRadioButton("a");
100
b = new JRadioButton("b");
101
c = new JRadioButton("c");
102
d = new JRadioButton("d");
103
104
ButtonGroup radioButtonGroup = new ButtonGroup();
105
radioButtonGroup.add(a);
106
radioButtonGroup.add(b);
107
radioButtonGroup.add(c);
108
radioButtonGroup.add(d);
109
110
JPanel panel = new JPanel();
111
prev = new JTextField("text");
112
panel.add(prev);
113
panel.add(a);
114
panel.add(b);
115
panel.add(c);
116
panel.add(d);
117
next = new JTextField("text");
118
panel.add(next);
119
120
JPanel root = new JPanel();
121
root.setLayout(new BorderLayout());
122
root.add(panel, BorderLayout.CENTER);
123
root.add(new JButton("OK"), BorderLayout.SOUTH);
124
125
frame.add(root);
126
frame.pack();
127
frame.setLocationRelativeTo(null);
128
frame.setVisible(true);
129
frame.toFront();
130
}
131
});
132
}
133
134
private static void runTestCase() throws Exception {
135
focusOn(a);
136
137
robot.waitForIdle();
138
robot.delay(500);
139
robot.keyPress(KeyEvent.VK_ENTER);
140
robot.keyRelease(KeyEvent.VK_ENTER);
141
robot.waitForIdle();
142
isFocusOwner(next, "forward");
143
robot.keyPress(KeyEvent.VK_SHIFT);
144
robot.keyPress(KeyEvent.VK_TAB);
145
robot.keyRelease(KeyEvent.VK_TAB);
146
robot.keyRelease(KeyEvent.VK_SHIFT);
147
robot.waitForIdle();
148
isFocusOwner(a, "backward");
149
150
}
151
152
private static void focusOn(Component component)
153
throws Exception {
154
SwingUtilities.invokeAndWait(new Runnable() {
155
@Override
156
public void run() {
157
component.requestFocusInWindow();
158
}
159
});
160
}
161
162
private static void isFocusOwner(Component queriedFocusOwner,
163
String direction)
164
throws Exception {
165
SwingUtilities.invokeAndWait(new Runnable() {
166
@Override
167
public void run() {
168
Component actualFocusOwner
169
= FocusManager.getCurrentManager().getFocusOwner();
170
if (actualFocusOwner != queriedFocusOwner) {
171
frame.dispose();
172
throw new RuntimeException(
173
"Focus component is wrong after " + direction
174
+ " direction ");
175
176
}
177
}
178
});
179
}
180
181
private static boolean tryLookAndFeel(String lookAndFeelString)
182
throws Exception {
183
184
try {
185
UIManager.setLookAndFeel(
186
lookAndFeelString);
187
188
} catch (UnsupportedLookAndFeelException
189
| ClassNotFoundException
190
| InstantiationException
191
| IllegalAccessException e) {
192
return false;
193
}
194
System.out.println("Testing lookAndFeel " + lookAndFeelString);
195
return true;
196
}
197
198
private static void cleanUp() throws Exception {
199
SwingUtilities.invokeAndWait(new Runnable() {
200
@Override
201
public void run() {
202
frame.dispose();
203
}
204
});
205
}
206
}
207
208