jdk/test/javax/swing/JRootPane/4670486/bug4670486.java
changeset 16464 7ea81d352fd5
child 28071 3acb75b8df45
equal deleted inserted replaced
16463:9f9632c2aede 16464:7ea81d352fd5
       
     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  * @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         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
       
   105         Robot robot = new Robot();
       
   106         robot.setAutoDelay(250);
       
   107 
       
   108         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       
   109 
       
   110         SwingUtilities.invokeAndWait(new Runnable() {
       
   111 
       
   112             @Override
       
   113             public void run() {
       
   114                 JFrame frame = new JFrame("Test");
       
   115                 frame.setContentPane(createPanel(frame));
       
   116                 frame.setJMenuBar(createMenuBar());
       
   117                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
   118                 frame.pack();
       
   119                 frame.setVisible(true);
       
   120             }
       
   121         });
       
   122 
       
   123         toolkit.realSync();
       
   124 
       
   125         // Change the default button to
       
   126         // force a call to BasicRootPaneUI.updateDefaultButtonBindings()
       
   127         Util.hitKeys(robot, KeyEvent.VK_TAB);
       
   128 
       
   129         // If the bug exists, then as soon as the menu appears,
       
   130         // the VK_ENTER, VK_DOWN, VK_UP and VK_ESC will have no
       
   131         // effect.
       
   132         Util.hitMnemonics(robot, KeyEvent.VK_U);
       
   133         Util.hitKeys(robot, KeyEvent.VK_ENTER);
       
   134         toolkit.realSync();
       
   135 
       
   136         checkAction();
       
   137 
       
   138         Util.hitMnemonics(robot, KeyEvent.VK_U);
       
   139         Util.hitKeys(robot, KeyEvent.VK_DOWN);
       
   140         Util.hitKeys(robot, KeyEvent.VK_ENTER);
       
   141         toolkit.realSync();
       
   142 
       
   143         checkAction();
       
   144     }
       
   145 }