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