1 /*
 2  * Copyright (c) 2003, 2023, 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 4767393
27  * @summary Disabled JMenu is selectable via mnemonic
28  * @key headful
29  * @run main bug4767393
30  */
31 
32 import java.awt.Robot;
33 import java.awt.event.KeyEvent;
34 import javax.swing.JFrame;
35 import javax.swing.JMenu;
36 import javax.swing.JMenuBar;
37 import javax.swing.SwingUtilities;
38 
39 public class bug4767393 {
40 
41     public static JFrame mainFrame;
42     public static JMenuBar menuBar;
43     public static JMenu menu;
44     public static JMenu disabled;
45     public static volatile boolean disabledMenuSelected = true;
46 
47     public static void main(String[] args) throws Exception {
48         try {
49             Robot robo = new Robot();
50             robo.setAutoDelay(100);
51             SwingUtilities.invokeAndWait(() -> {
52                 mainFrame = new JFrame("Bug4767393");
53                 menuBar = new JMenuBar();
54                 menu = new JMenu("File");
55                 disabled = new JMenu("Disabled");
56                 menuBar.add(menu);
57                 menu.add("Menu Item 1");
58                 menu.add("Menu Item 2");
59                 disabled.setEnabled(false);
60                 disabled.setMnemonic('D');
61                 disabled.add("Dummy menu item");
62                 menu.add(disabled);
63                 menu.add("Menu Item 3");
64                 menu.add("Menu Item 4");
65                 mainFrame.setJMenuBar(menuBar);
66 
67                 mainFrame.setSize(200, 200);
68                 mainFrame.setLocationRelativeTo(null);
69                 mainFrame.setVisible(true);
70             });
71             robo.waitForIdle();
72             robo.delay(500);
73 
74             robo.keyPress(KeyEvent.VK_F10);
75             robo.keyRelease(KeyEvent.VK_F10);
76             robo.keyPress(KeyEvent.VK_DOWN);
77             robo.keyRelease(KeyEvent.VK_DOWN);
78             robo.delay(500);
79             robo.keyPress(KeyEvent.VK_D);
80             robo.keyRelease(KeyEvent.VK_D);
81             robo.delay(100);
82 
83             SwingUtilities.invokeAndWait(() -> {
84                 disabledMenuSelected = disabled.isSelected();
85             });
86 
87             if (disabledMenuSelected) {
88                 throw new RuntimeException("Disabled JMenu is selected" +
89                         " by the mnemonic. Test failed.");
90             }
91         } finally {
92             SwingUtilities.invokeAndWait(() -> {
93                 if (mainFrame != null) {
94                     mainFrame.dispose();
95                 }
96             });
97         }
98     }
99 }