1 /*
2  * Copyright (c) 2013, 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 import java.awt.*;
25 import java.awt.event.*;
26 import javax.swing.*;
27 
28 /**
29  * @test
30  * @key headful
31  * @bug 4670486
32  * @author Mark Davidson
33  * @summary Regression: Popup menu bindings doesn't work when a default button has been defined.
34  * @library ../../regtesthelpers
35  * @build Util
36  * @run main bug4670486
37  */
38 public class bug4670486 {
39 
40     public static volatile boolean actionExpected = false;
41     public static volatile boolean actionRecieved = false;
42     public static JFrame frame;
43 
createMenuBar()44     private static JMenuBar createMenuBar() {
45         JMenuBar menubar = new JMenuBar();
46 
47         // Control with unique menu
48         JMenu menu = new JMenu("Unique Menu");
49         menu.setMnemonic('U');
50         menu.add(createMenuItem("Sunday", 'S'));
51         menu.add(createMenuItem("Monday", 'M'));
52 
53         menu.add(createMenuItem("Tuesday", 'T'));
54         menu.add(createMenuItem("Wednesday", 'W'));
55         menu.add(createMenuItem("Thursday", 'U'));
56         menu.add(createMenuItem("Friday", 'F'));
57         menu.add(createMenuItem("Saturday", 'A'));
58 
59         menubar.add(menu);
60 
61         return menubar;
62     }
63 
createPanel(JFrame frame)64     private static JPanel createPanel(JFrame frame) {
65         JPanel panel = new JPanel();
66         JButton button = new JButton("Button");
67         JButton button2 = new JButton("Button 2");
68         JButton button3 = new JButton("Button 3");
69 
70         JRootPane root = frame.getRootPane();
71         root.setDefaultButton(button);
72 
73         panel.add(button);
74         panel.add(button2);
75         panel.add(button3);
76 
77         return panel;
78     }
79 
80     /**
81      * Creates and returns the menu item.
82      */
createMenuItem(String name, char mnemonic)83     private static JMenuItem createMenuItem(String name, char mnemonic) {
84         JMenuItem menuItem = new JMenuItem(name, mnemonic);
85         menuItem.addActionListener(new ActionListener() {
86 
87             @Override
88             public void actionPerformed(ActionEvent evt) {
89                 actionRecieved = true;
90             }
91         });
92 
93         return menuItem;
94     }
95 
checkAction()96     public static void checkAction() {
97         if (actionRecieved == true) {
98             actionRecieved = false;
99         } else {
100             throw new RuntimeException("Action has not been received");
101         }
102     }
103 
main(String[] args)104     public static void main(String[] args) throws Throwable {
105         try {
106             Robot robot = new Robot();
107             robot.setAutoDelay(250);
108 
109             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
110 
111             SwingUtilities.invokeAndWait(new Runnable() {
112 
113                 @Override
114                 public void run() {
115                     frame = new JFrame("Test");
116                     frame.setContentPane(createPanel(frame));
117                     frame.setJMenuBar(createMenuBar());
118                     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
119                     frame.pack();
120                     frame.setVisible(true);
121                 }
122             });
123 
124             robot.waitForIdle();
125 
126             // Change the default button to
127             // force a call to BasicRootPaneUI.updateDefaultButtonBindings()
128             Util.hitKeys(robot, KeyEvent.VK_TAB);
129 
130             // If the bug exists, then as soon as the menu appears,
131             // the VK_ENTER, VK_DOWN, VK_UP and VK_ESC will have no
132             // effect.
133             Util.hitMnemonics(robot, KeyEvent.VK_U);
134             Util.hitKeys(robot, KeyEvent.VK_ENTER);
135             robot.waitForIdle();
136 
137             checkAction();
138 
139             Util.hitMnemonics(robot, KeyEvent.VK_U);
140             Util.hitKeys(robot, KeyEvent.VK_DOWN);
141             Util.hitKeys(robot, KeyEvent.VK_ENTER);
142             robot.waitForIdle();
143 
144             checkAction();
145         } finally {
146             if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
147         }
148     }
149 }
150