Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/ConsumedKeyTest/ConsumedKeyTest.java
38853 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.swing.*;24import java.awt.event.ActionEvent;25import java.awt.event.KeyEvent;26import java.awt.Toolkit;27import java.awt.Robot;28import sun.awt.SunToolkit;2930/*31@test32@bug 8031485 805819333@summary Combo box consuming escape and enter key events34@author Petr Pchelko35@run main ConsumedKeyTest36*/37public class ConsumedKeyTest {38private static volatile JFrame frame;39private static volatile boolean passed;4041public static void main(String... args) throws Exception {42test(KeyEvent.VK_ESCAPE);43test(KeyEvent.VK_ENTER);44}4546private static void test(final int key) throws Exception {47passed = false;48try {49SwingUtilities.invokeAndWait(() -> {50frame = new JFrame();51JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"});52JPanel panel = new JPanel();53panel.add(combo);54combo.requestFocusInWindow();55frame.setBounds(100, 150, 300, 100);56addAction(panel, key);57frame.add(panel);58frame.setVisible(true);59});6061Robot robot = new Robot();62robot.waitForIdle();63((SunToolkit)Toolkit.getDefaultToolkit()).realSync();64robot.keyPress(key);65robot.waitForIdle();66((SunToolkit)Toolkit.getDefaultToolkit()).realSync();67robot.keyRelease(key);68robot.waitForIdle();69((SunToolkit)Toolkit.getDefaultToolkit()).realSync();70if (!passed) {71throw new RuntimeException("FAILED: " + KeyEvent.getKeyText(key) + " was consumed by combo box");72}73} finally {74if (frame != null) {75frame.dispose();76}77}7879}8081private static void addAction(JComponent comp, final int key) {82KeyStroke k = KeyStroke.getKeyStroke(key, 0);83Object actionKey = "cancel";84comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(k, actionKey);85Action cancelAction = new AbstractAction() {86@Override87public void actionPerformed(ActionEvent ev) {88passed = true;89}90};91comp.getActionMap().put(actionKey, cancelAction);92}93}949596