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