1 /*
   2  * Copyright (c) 2011, 2014, 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 /* @test
  24    @bug 6470128
  25    @summary Escape Key causes JMenu Selection to Disappear
  26    @author Alexander Potochkin
  27    @library ../../../../lib/testlibrary
  28    @build jdk.testlibrary.OSInfo
  29    @run main bug6470128
  30 */
  31 import javax.swing.*;
  32 import java.awt.*;
  33 import java.awt.event.KeyEvent;
  34 import jdk.testlibrary.OSInfo;
  35 
  36 public class bug6470128 {
  37     static JFrame frame;
  38     static JMenu subMenu;
  39 
  40     public static void main(String[] args) throws Exception {
  41         SwingUtilities.invokeAndWait(new Runnable() {
  42             public void run() {
  43                 frame = new JFrame();
  44                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45 
  46                 JMenuBar bar = new JMenuBar();
  47                 JMenu menu = new JMenu("Menu");
  48                 menu.setMnemonic('m');
  49                 subMenu = new JMenu("SubMenu");
  50                 JMenuItem item = new JMenuItem("Item");
  51 
  52                 frame.setJMenuBar(bar);
  53                 bar.add(menu);
  54                 menu.add(subMenu);
  55                 subMenu.add(item);
  56 
  57                 frame.setSize(200, 200);
  58                 frame.setLocationRelativeTo(null);
  59                 frame.setVisible(true);
  60             }
  61         });
  62         Robot robot = new Robot();
  63         robot.setAutoDelay(10);
  64         robot.waitForIdle();
  65         if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {
  66             robot.keyPress(KeyEvent.VK_CONTROL);
  67         }
  68         robot.keyPress(KeyEvent.VK_ALT);
  69         robot.keyPress(KeyEvent.VK_M);
  70         robot.keyRelease(KeyEvent.VK_M);
  71         robot.keyRelease(KeyEvent.VK_ALT);
  72         if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {
  73             robot.keyRelease(KeyEvent.VK_CONTROL);
  74         }
  75         robot.keyPress(KeyEvent.VK_ENTER);
  76         robot.keyRelease(KeyEvent.VK_ENTER);
  77         robot.keyPress(KeyEvent.VK_ESCAPE);
  78         robot.keyRelease(KeyEvent.VK_ESCAPE);
  79         robot.waitForIdle();
  80         if (!subMenu.isSelected()) {
  81             throw new RuntimeException("Submenu is unexpectedly unselected");
  82         }
  83     }
  84 }