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