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