Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Focus/8073453/SwingFocusTransitionTest.java
41153 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
/*
25
* @test
26
* @key headful
27
* @bug 8073453
28
* @summary Focus doesn't move when pressing Shift + Tab keys
29
* @compile SwingFocusTransitionTest.java
30
* @run main/othervm SwingFocusTransitionTest
31
*/
32
33
import javax.swing.JButton;
34
import javax.swing.JFrame;
35
import javax.swing.JPanel;
36
import javax.swing.JTextField;
37
import javax.swing.LayoutFocusTraversalPolicy;
38
import javax.swing.SwingUtilities;
39
import java.awt.Component;
40
import java.awt.DefaultFocusTraversalPolicy;
41
import java.awt.GridLayout;
42
import java.awt.Robot;
43
import java.awt.event.KeyEvent;
44
45
public class SwingFocusTransitionTest {
46
private static Robot robot;
47
48
private static JFrame frame;
49
private static JTextField textField;
50
private static JButton button;
51
52
public static void main(String[] args) throws Exception {
53
robot = new Robot();
54
robot.setAutoDelay(100);
55
56
try {
57
SwingUtilities.invokeAndWait(new Runnable() {
58
@Override
59
public void run() {
60
createAndShowGUI();
61
}
62
});
63
64
robot.waitForIdle();
65
66
checkFocusOwner(textField);
67
68
robot.keyPress(KeyEvent.VK_TAB);
69
robot.keyRelease(KeyEvent.VK_TAB);
70
robot.waitForIdle();
71
72
checkFocusOwner(button);
73
74
robot.keyPress(KeyEvent.VK_SHIFT);
75
robot.keyPress(KeyEvent.VK_TAB);
76
robot.keyRelease(KeyEvent.VK_TAB);
77
robot.keyRelease(KeyEvent.VK_SHIFT);
78
robot.waitForIdle();
79
80
checkFocusOwner(textField);
81
82
robot.keyPress(KeyEvent.VK_SHIFT);
83
robot.keyPress(KeyEvent.VK_TAB);
84
robot.keyRelease(KeyEvent.VK_TAB);
85
robot.keyRelease(KeyEvent.VK_SHIFT);
86
robot.waitForIdle();
87
88
checkFocusOwner(button);
89
} finally {
90
SwingUtilities.invokeLater(new Runnable() {
91
@Override
92
public void run() {
93
if (frame != null) {
94
frame.dispose();
95
}
96
}
97
});
98
}
99
System.out.println("Test Passed!");
100
}
101
102
private static void createAndShowGUI() {
103
frame = new JFrame("SwingFocusTransitionTest");
104
frame.setSize(300, 300);
105
frame.setFocusTraversalPolicyProvider(true);
106
frame.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
107
108
textField = new JTextField();
109
button = new JButton();
110
111
JPanel panel = new JPanel();
112
panel.setFocusTraversalPolicyProvider(true);
113
panel.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
114
115
JPanel p = new JPanel();
116
p.setLayout(new GridLayout(3, 1));
117
p.add(textField);
118
p.add(button);
119
p.add(panel);
120
121
frame.add(p);
122
frame.setLocationRelativeTo(null);
123
frame.setVisible(true);
124
}
125
126
private static void checkFocusOwner(final Component component)
127
throws Exception {
128
SwingUtilities.invokeAndWait(new Runnable() {
129
@Override
130
public void run() {
131
if (component != frame.getFocusOwner()) {
132
throw new RuntimeException("Test Failed! Incorrect focus" +
133
" owner: " + frame.getFocusOwner() +
134
", but expected: " + component);
135
}
136
}
137
});
138
}
139
}
140
141