Path: blob/master/test/jdk/javax/swing/JComboBox/ConsumedKeyTest/ConsumedKeyTest.java
41153 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;2627/*28@test29@key headful30@bug 8031485 8058193 806798631@summary Combo box consuming escape and enter key events32@author Petr Pchelko33@library /lib/client/34@build ExtendedRobot35@run main ConsumedKeyTest36*/37public class ConsumedKeyTest {38private static JFrame frame;39private static volatile boolean passed;40static ExtendedRobot robot;4142public static void main(String... args) throws Exception {43test(KeyEvent.VK_ESCAPE);44test(KeyEvent.VK_ENTER);45}4647private static void test(final int key) throws Exception {48passed = false;49robot = new ExtendedRobot();50try {51SwingUtilities.invokeAndWait(() -> {52frame = new JFrame();53JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"});54JPanel panel = new JPanel();55panel.add(combo);56combo.requestFocusInWindow();57frame.setBounds(100, 150, 300, 100);58addAction(panel, key);59frame.add(panel);60frame.setVisible(true);61frame.setAlwaysOnTop(true);62});6364robot.waitForIdle();65robot.delay(500);66robot.type(key);67robot.waitForIdle();68if (!passed) {69throw new RuntimeException("FAILED: " + KeyEvent.getKeyText(key) + " was consumed by combo box");70}71} finally {72if (frame != null) {73frame.dispose();74}75}76robot.delay(1000);77}7879private static void addAction(JComponent comp, final int key) {80KeyStroke k = KeyStroke.getKeyStroke(key, 0);81Object actionKey = "cancel";82comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(k, actionKey);83Action cancelAction = new AbstractAction() {84@Override85public void actionPerformed(ActionEvent ev) {86passed = true;87}88};89comp.getActionMap().put(actionKey, cancelAction);90}9192}939495