1/*
2 * Copyright (c) 2013, 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
24import java.awt.Robot;
25import java.awt.Toolkit;
26import java.awt.event.ActionEvent;
27import java.awt.event.KeyEvent;
28
29import javax.swing.AbstractAction;
30import javax.swing.InputMap;
31import javax.swing.JFrame;
32import javax.swing.JMenuBar;
33import javax.swing.JMenuItem;
34import javax.swing.KeyStroke;
35
36import 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;
40
41/*
42 * @test
43 * @key headful
44 * @bug 8013370
45 * @summary Ensure that key stroke is not null
46 * @author Sergey Malenkov
47 */
48
49public class Test8013370 implements Runnable {
50    public static void main(String[] args) throws Exception {
51        Test8013370 task = new Test8013370();
52        invokeAndWait(task);
53
54        Robot robot = new Robot();
55        robot.waitForIdle();
56        robot.keyPress(KeyEvent.VK_CONTROL);
57        robot.keyRelease(KeyEvent.VK_CONTROL);
58        robot.waitForIdle();
59
60        invokeAndWait(task);
61        task.validate();
62    }
63
64    private JFrame frame;
65    private boolean error;
66
67    @Override
68    public void run() {
69        if (this.frame == null) {
70            JMenuBar menu = new JMenuBar() {
71                @Override
72                protected boolean processKeyBinding(KeyStroke stroke, KeyEvent event, int condition, boolean pressed) {
73                    if (stroke == null) {
74                        Test8013370.this.error = true;
75                        return false;
76                    }
77                    return super.processKeyBinding(stroke, event, condition, pressed);
78                }
79            };
80            menu.add(new JMenuItem("Menu"));
81
82            InputMap map = menu.getInputMap(WHEN_IN_FOCUSED_WINDOW);
83            // We add exactly 10 actions because the ArrayTable is converted
84            // from a array to a hashtable when more than 8 values are added.
85            for (int i = 0; i < 9; i++) {
86                String name = " Action #" + i;
87                map.put(KeyStroke.getKeyStroke(KeyEvent.VK_A + i, CTRL_DOWN_MASK), name);
88
89                menu.getActionMap().put(name, new AbstractAction(name) {
90                    @Override
91                    public void actionPerformed(ActionEvent event) {
92                        showMessageDialog(null, getValue(NAME));
93                    }
94                });
95            }
96            this.frame = new JFrame("8013370");
97            this.frame.setJMenuBar(menu);
98            this.frame.setVisible(true);
99        }
100        else {
101            this.frame.dispose();
102        }
103    }
104
105    private void validate() {
106        if (this.error) {
107            throw new Error("KeyStroke is null");
108        }
109    }
110}
111