Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/KeyboardManager/8013370/Test8013370.java
38918 views
/*1* Copyright (c) 2013, 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 java.awt.Robot;24import java.awt.Toolkit;25import java.awt.event.ActionEvent;26import java.awt.event.KeyEvent;2728import javax.swing.AbstractAction;29import javax.swing.InputMap;30import javax.swing.JFrame;31import javax.swing.JMenuBar;32import javax.swing.JMenuItem;33import javax.swing.KeyStroke;34import sun.awt.SunToolkit;3536import static java.awt.event.InputEvent.CTRL_DOWN_MASK;37import static javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW;38import static javax.swing.JOptionPane.showMessageDialog;39import static javax.swing.SwingUtilities.invokeAndWait;4041/*42* @test43* @bug 801337044* @summary Ensure that key stroke is not null45* @author Sergey Malenkov46*/4748public class Test8013370 implements Runnable {49public static void main(String[] args) throws Exception {50Test8013370 task = new Test8013370();51invokeAndWait(task);5253Robot robot = new Robot();54robot.waitForIdle();55robot.keyPress(KeyEvent.VK_CONTROL);56robot.keyRelease(KeyEvent.VK_CONTROL);57robot.waitForIdle();5859invokeAndWait(task);60task.validate();61}6263private JFrame frame;64private boolean error;6566@Override67public void run() {68if (this.frame == null) {69JMenuBar menu = new JMenuBar() {70@Override71protected boolean processKeyBinding(KeyStroke stroke, KeyEvent event, int condition, boolean pressed) {72if (stroke == null) {73Test8013370.this.error = true;74return false;75}76return super.processKeyBinding(stroke, event, condition, pressed);77}78};79menu.add(new JMenuItem("Menu"));8081InputMap map = menu.getInputMap(WHEN_IN_FOCUSED_WINDOW);82// We add exactly 10 actions because the ArrayTable is converted83// from a array to a hashtable when more than 8 values are added.84for (int i = 0; i < 9; i++) {85String name = " Action #" + i;86map.put(KeyStroke.getKeyStroke(KeyEvent.VK_A + i, CTRL_DOWN_MASK), name);8788menu.getActionMap().put(name, new AbstractAction(name) {89@Override90public void actionPerformed(ActionEvent event) {91showMessageDialog(null, getValue(NAME));92}93});94}95this.frame = new JFrame("8013370");96this.frame.setJMenuBar(menu);97this.frame.setVisible(true);98}99else {100this.frame.dispose();101}102}103104private void validate() {105if (this.error) {106throw new Error("KeyStroke is null");107}108}109}110111112