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