Path: blob/master/test/jdk/javax/swing/JRootPane/4670486/bug4670486.java
41153 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.*;24import java.awt.event.*;25import javax.swing.*;2627/**28* @test29* @key headful30* @bug 467048631* @author Mark Davidson32* @summary Regression: Popup menu bindings doesn't work when a default button has been defined.33* @library ../../regtesthelpers34* @build Util35* @run main bug467048636*/37public class bug4670486 {3839public static volatile boolean actionExpected = false;40public static volatile boolean actionRecieved = false;41public static JFrame frame;4243private static JMenuBar createMenuBar() {44JMenuBar menubar = new JMenuBar();4546// Control with unique menu47JMenu menu = new JMenu("Unique Menu");48menu.setMnemonic('U');49menu.add(createMenuItem("Sunday", 'S'));50menu.add(createMenuItem("Monday", 'M'));5152menu.add(createMenuItem("Tuesday", 'T'));53menu.add(createMenuItem("Wednesday", 'W'));54menu.add(createMenuItem("Thursday", 'U'));55menu.add(createMenuItem("Friday", 'F'));56menu.add(createMenuItem("Saturday", 'A'));5758menubar.add(menu);5960return menubar;61}6263private static JPanel createPanel(JFrame frame) {64JPanel panel = new JPanel();65JButton button = new JButton("Button");66JButton button2 = new JButton("Button 2");67JButton button3 = new JButton("Button 3");6869JRootPane root = frame.getRootPane();70root.setDefaultButton(button);7172panel.add(button);73panel.add(button2);74panel.add(button3);7576return panel;77}7879/**80* Creates and returns the menu item.81*/82private static JMenuItem createMenuItem(String name, char mnemonic) {83JMenuItem menuItem = new JMenuItem(name, mnemonic);84menuItem.addActionListener(new ActionListener() {8586@Override87public void actionPerformed(ActionEvent evt) {88actionRecieved = true;89}90});9192return menuItem;93}9495public static void checkAction() {96if (actionRecieved == true) {97actionRecieved = false;98} else {99throw new RuntimeException("Action has not been received");100}101}102103public static void main(String[] args) throws Throwable {104try {105Robot robot = new Robot();106robot.setAutoDelay(250);107108UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());109110SwingUtilities.invokeAndWait(new Runnable() {111112@Override113public void run() {114frame = new JFrame("Test");115frame.setContentPane(createPanel(frame));116frame.setJMenuBar(createMenuBar());117frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);118frame.pack();119frame.setVisible(true);120}121});122123robot.waitForIdle();124125// Change the default button to126// force a call to BasicRootPaneUI.updateDefaultButtonBindings()127Util.hitKeys(robot, KeyEvent.VK_TAB);128129// If the bug exists, then as soon as the menu appears,130// the VK_ENTER, VK_DOWN, VK_UP and VK_ESC will have no131// effect.132Util.hitMnemonics(robot, KeyEvent.VK_U);133Util.hitKeys(robot, KeyEvent.VK_ENTER);134robot.waitForIdle();135136checkAction();137138Util.hitMnemonics(robot, KeyEvent.VK_U);139Util.hitKeys(robot, KeyEvent.VK_DOWN);140Util.hitKeys(robot, KeyEvent.VK_ENTER);141robot.waitForIdle();142143checkAction();144} finally {145if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());146}147}148}149150151