Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JComboBox/ConsumedKeyTest/ConsumedKeyTest.java
41153 views
1
/*
2
* Copyright (c) 2014, 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 javax.swing.*;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.KeyEvent;
27
28
/*
29
@test
30
@key headful
31
@bug 8031485 8058193 8067986
32
@summary Combo box consuming escape and enter key events
33
@author Petr Pchelko
34
@library /lib/client/
35
@build ExtendedRobot
36
@run main ConsumedKeyTest
37
*/
38
public class ConsumedKeyTest {
39
private static JFrame frame;
40
private static volatile boolean passed;
41
static ExtendedRobot robot;
42
43
public static void main(String... args) throws Exception {
44
test(KeyEvent.VK_ESCAPE);
45
test(KeyEvent.VK_ENTER);
46
}
47
48
private static void test(final int key) throws Exception {
49
passed = false;
50
robot = new ExtendedRobot();
51
try {
52
SwingUtilities.invokeAndWait(() -> {
53
frame = new JFrame();
54
JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"});
55
JPanel panel = new JPanel();
56
panel.add(combo);
57
combo.requestFocusInWindow();
58
frame.setBounds(100, 150, 300, 100);
59
addAction(panel, key);
60
frame.add(panel);
61
frame.setVisible(true);
62
frame.setAlwaysOnTop(true);
63
});
64
65
robot.waitForIdle();
66
robot.delay(500);
67
robot.type(key);
68
robot.waitForIdle();
69
if (!passed) {
70
throw new RuntimeException("FAILED: " + KeyEvent.getKeyText(key) + " was consumed by combo box");
71
}
72
} finally {
73
if (frame != null) {
74
frame.dispose();
75
}
76
}
77
robot.delay(1000);
78
}
79
80
private static void addAction(JComponent comp, final int key) {
81
KeyStroke k = KeyStroke.getKeyStroke(key, 0);
82
Object actionKey = "cancel";
83
comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(k, actionKey);
84
Action cancelAction = new AbstractAction() {
85
@Override
86
public void actionPerformed(ActionEvent ev) {
87
passed = true;
88
}
89
};
90
comp.getActionMap().put(actionKey, cancelAction);
91
}
92
93
}
94
95