1 /*
   2  * Copyright (c) 2015, 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 /*
  25  * @test
  26  * @bug 6921687 8079428
  27  * @summary Mnemonic disappears after repeated attempts to open menu items using
  28  *          mnemonics
  29  * @author Semyon Sadetsky
  30  * @library /lib/testlibrary
  31  * @build jdk.testlibrary.Platform
  32  * @requires (os.family == "windows")
  33  * @modules java.desktop/com.sun.java.swing.plaf.windows
  34  * @run main bug6921687
  35  */
  36 import java.awt.Robot;
  37 import java.awt.event.KeyEvent;
  38 import javax.swing.JFrame;
  39 import javax.swing.JMenu;
  40 import javax.swing.JMenuBar;
  41 import javax.swing.JMenuItem;
  42 import javax.swing.SwingUtilities;
  43 import javax.swing.UIManager;
  44 import jdk.testlibrary.Platform;
  45 
  46 public class bug6921687 {
  47 
  48     private static Class lafClass;
  49     private static JFrame frame;
  50 
  51     public static void main(String[] args) throws Exception {
  52         if (!Platform.isWindows()) {
  53             System.out.println("Only Windows platform test. Test is skipped.");
  54             System.out.println("ok");
  55             return;
  56         }
  57         final String lafClassName = UIManager.getSystemLookAndFeelClassName();
  58         lafClass  = Class.forName(lafClassName);
  59         UIManager.setLookAndFeel(lafClassName);
  60         try {
  61             SwingUtilities.invokeAndWait(() -> {
  62                 frame = new JFrame();
  63                 frame.setUndecorated(true);
  64                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65                 setup(frame);
  66             });
  67 
  68             final Robot robot = new Robot();
  69             robot.waitForIdle();
  70             robot.setAutoDelay(20);
  71             robot.keyPress(KeyEvent.VK_ALT);
  72             robot.keyPress(KeyEvent.VK_F);
  73             robot.keyRelease(KeyEvent.VK_F);
  74             robot.keyRelease(KeyEvent.VK_ALT);
  75             robot.waitForIdle();
  76             checkMnemonics();
  77 
  78             robot.keyPress(KeyEvent.VK_ALT);
  79             robot.keyPress(KeyEvent.VK_S);
  80             robot.keyRelease(KeyEvent.VK_S);
  81             robot.keyRelease(KeyEvent.VK_ALT);
  82             robot.waitForIdle();
  83             checkMnemonics();
  84             System.out.println("ok");
  85         } finally {
  86             frame.dispose();
  87         }
  88 
  89     }
  90 
  91     private static void checkMnemonics() throws Exception {
  92         if ((Boolean) lafClass.getMethod("isMnemonicHidden").invoke(lafClass)) {
  93             throw new RuntimeException("Mnemonics are hidden");
  94         }
  95     }
  96 
  97     private static void setup(JFrame frame) {
  98         JMenuBar menuBar = new JMenuBar();
  99         frame.setJMenuBar(menuBar);
 100 
 101         // First Menu, F - Mnemonic
 102         JMenu firstMenu = new JMenu("First Menu");
 103         firstMenu.setMnemonic(KeyEvent.VK_F);
 104         firstMenu.add(new JMenuItem("One", KeyEvent.VK_O));
 105         firstMenu.add(new JMenuItem("Two", KeyEvent.VK_T));
 106         menuBar.add(firstMenu);
 107 
 108         // Second Menu, S - Mnemonic
 109         JMenu secondMenu = new JMenu("Second Menu");
 110         secondMenu.setMnemonic(KeyEvent.VK_S);
 111         secondMenu.add(new JMenuItem("A Menu Item", KeyEvent.VK_A));
 112         menuBar.add(secondMenu);
 113 
 114         frame.setSize(350, 250);
 115         frame.setVisible(true);
 116     }
 117 }