Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/swing/JSplitPane/4164779/JSplitPaneKeyboardNavigationTest.java
66646 views
1
/*
2
* Copyright (c) 2002, 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
import java.awt.BorderLayout;
25
import java.awt.Point;
26
import java.awt.Robot;
27
import java.awt.event.InputEvent;
28
import java.awt.event.KeyEvent;
29
import java.util.Arrays;
30
import java.util.List;
31
import java.util.concurrent.atomic.AtomicBoolean;
32
import java.util.concurrent.atomic.AtomicReference;
33
import java.util.stream.Collectors;
34
import javax.swing.JButton;
35
import javax.swing.JFrame;
36
import javax.swing.JPanel;
37
import javax.swing.JSplitPane;
38
import javax.swing.SwingUtilities;
39
import javax.swing.UIManager;
40
import javax.swing.UIManager.LookAndFeelInfo;
41
import javax.swing.UnsupportedLookAndFeelException;
42
43
import static javax.swing.UIManager.getInstalledLookAndFeels;
44
45
/*
46
* @test
47
* @key headful
48
* @bug 4164779
49
* @summary This test confirms that JSplitPane keyboard navigation supports F6 and Ctrl+Tab.
50
* @run main JSplitPaneKeyboardNavigationTest
51
*/
52
public class JSplitPaneKeyboardNavigationTest {
53
54
private static final StringBuffer failedVerifiers = new StringBuffer();
55
private static JPanel panel;
56
private static JButton leftButton;
57
private static JButton rightButton1;
58
private static JButton rightButton2;
59
private static JButton topButton;
60
private static JButton bottomButton;
61
private static Robot robot;
62
private static JFrame frame;
63
64
public static void main(String[] s) throws Exception {
65
robot = new Robot();
66
robot.setAutoWaitForIdle(true);
67
robot.setAutoDelay(200);
68
List<String> lafs = Arrays.stream(getInstalledLookAndFeels())
69
.map(LookAndFeelInfo::getClassName)
70
.collect(Collectors.toList());
71
for (final String laf : lafs) {
72
try {
73
AtomicBoolean lafSetSuccess = new AtomicBoolean(false);
74
SwingUtilities.invokeAndWait(() -> {
75
lafSetSuccess.set(setLookAndFeel(laf));
76
if (lafSetSuccess.get()) {
77
createUI();
78
}
79
});
80
if (!lafSetSuccess.get()) {
81
continue;
82
}
83
robot.waitForIdle();
84
85
// Press Right button 1 and move focus to it.
86
pressButton(rightButton1);
87
hitKeys(KeyEvent.VK_F6);
88
89
// Verifier1 - Verifies that, F6 transfers focus to the right/bottom side of the splitpane
90
if (isFocusOwner(rightButton2)) {
91
System.out.println("Verifier 1 passed");
92
} else {
93
failedVerifiers.append("1,");
94
System.out.println("Verifier 1 failed, rightButton2 is not focus owner," +
95
"F6 doesn't transfer focus to the right/bottom side of the splitpane");
96
}
97
98
// Press Right button 2 and move focus to it.
99
pressButton(rightButton2);
100
hitKeys(KeyEvent.VK_F6);
101
102
// Verifier2 - Verifies that, F6 transfers focus to the left side of the parent splitpane,
103
// if the right/bottom side of splitpane already has focus, and it is contained within another splitpane
104
if (isFocusOwner(leftButton)) {
105
System.out.println("Verifier 2 passed");
106
} else {
107
failedVerifiers.append("2,");
108
System.out.println("Verifier 2 failed, leftButton is not focus owner, " +
109
"F6 doesn't transfer focus to the left side of the splitpane");
110
}
111
112
// Press Left button and move focus to it.
113
pressButton(leftButton);
114
hitKeys(KeyEvent.VK_CONTROL, KeyEvent.VK_TAB);
115
// Verifier3 - Verifies that, CTRL-TAB navigates forward outside the JSplitPane
116
if (isFocusOwner(bottomButton)) {
117
System.out.println("Verifier 3 passed");
118
} else {
119
failedVerifiers.append("3,");
120
System.out.println("Verifier 3 failed, bottomButton is not focus owner, " +
121
"CTRL-TAB doesn't navigate forward outside the JSplitPane");
122
}
123
124
// Press Left button and move focus to it.
125
pressButton(leftButton);
126
hitKeys(KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
127
128
// Verifier4 - Verifies that, CTRL-SHIFT-TAB navigates backward outside the JSplitPane
129
if (isFocusOwner(topButton)) {
130
System.out.println("Verifier 4 passed");
131
} else {
132
failedVerifiers.append("4");
133
System.out.println("Verifier 4 failed, topButton is not focus owner, " +
134
"CTRL-SHIFT-TAB doesn't navigate backward outside the JSplitPane");
135
}
136
137
if (failedVerifiers.toString().isEmpty()) {
138
System.out.println("Test passed, All verifiers succeeded for " + laf);
139
} else {
140
throw new RuntimeException("Test failed, verifiers " + failedVerifiers.toString() + " failed for " + laf);
141
}
142
} finally {
143
SwingUtilities.invokeAndWait(JSplitPaneKeyboardNavigationTest::disposeFrame);
144
}
145
}
146
}
147
148
private static boolean isFocusOwner(JButton button) throws Exception {
149
final AtomicBoolean isFocusOwner = new AtomicBoolean(false);
150
SwingUtilities.invokeAndWait(() -> {
151
isFocusOwner.set(button.isFocusOwner());
152
});
153
return isFocusOwner.get();
154
}
155
156
private static void pressButton(JButton button) throws Exception {
157
final AtomicReference<Point> loc = new AtomicReference<>();
158
SwingUtilities.invokeAndWait(() -> {
159
loc.set(button.getLocationOnScreen());
160
});
161
final Point buttonLoc = loc.get();
162
robot.mouseMove(buttonLoc.x + 8, buttonLoc.y + 8);
163
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
164
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
165
}
166
167
public static void createUI() {
168
frame = new JFrame();
169
panel = new JPanel();
170
panel.setLayout(new BorderLayout());
171
leftButton = new JButton("Left Button");
172
rightButton1 = new JButton("Right Button 1");
173
rightButton2 = new JButton("Right Button 2");
174
topButton = new JButton("Top Button");
175
bottomButton = new JButton("Bottom Button");
176
panel.add(topButton, BorderLayout.NORTH);
177
panel.add(bottomButton, BorderLayout.SOUTH);
178
final JSplitPane splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, rightButton1, rightButton2);
179
final JSplitPane splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, leftButton, splitPane2);
180
panel.add(splitPane1, BorderLayout.CENTER);
181
frame.setContentPane(panel);
182
frame.setSize(200, 200);
183
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
184
frame.pack();
185
frame.setAlwaysOnTop(true);
186
frame.setLocationRelativeTo(null);
187
frame.setVisible(true);
188
}
189
190
private static void hitKeys(int... keys) {
191
for (int key : keys) {
192
robot.keyPress(key);
193
}
194
195
for (int i = keys.length - 1; i >= 0; i--) {
196
robot.keyRelease(keys[i]);
197
}
198
}
199
200
private static boolean setLookAndFeel(String lafName) {
201
try {
202
UIManager.setLookAndFeel(lafName);
203
} catch (UnsupportedLookAndFeelException ignored) {
204
System.out.println("Ignoring Unsupported L&F: " + lafName);
205
return false;
206
} catch (ClassNotFoundException | InstantiationException
207
| IllegalAccessException e) {
208
throw new RuntimeException(e);
209
}
210
return true;
211
}
212
213
private static void disposeFrame() {
214
if (frame != null) {
215
frame.dispose();
216
frame = null;
217
}
218
}
219
220
}
221
222