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