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