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 import sun.awt.SunToolkit; 28 29 /** 30 * @test 31 * @key headful 32 * @bug 4670486 33 * @author Mark Davidson 34 * @summary Regression: Popup menu bindings doesn't work when a default button has been defined. 35 * @library ../../regtesthelpers 36 * @build Util 37 * @run main bug4670486 38 */ 39 public class bug4670486 { 40 41 public static volatile boolean actionExpected = false; 42 public static volatile boolean actionRecieved = false; 43 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 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 */ 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 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 104 public static void main(String[] args) throws Throwable { 105 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 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 JFrame 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 toolkit.realSync(); 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 toolkit.realSync(); 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 toolkit.realSync(); 143 144 checkAction(); 145 } 146 }