Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JRootPane/4670486/bug4670486.java
38918 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* @bug 467048630* @author Mark Davidson31* @summary Regression: Popup menu bindings doesn't work when a default button has been defined.32* @library ../../regtesthelpers33* @build Util34* @run main bug467048635*/36public class bug4670486 {3738public static volatile boolean actionExpected = false;39public static volatile boolean actionRecieved = false;4041private static JMenuBar createMenuBar() {42JMenuBar menubar = new JMenuBar();4344// Control with unique menu45JMenu menu = new JMenu("Unique Menu");46menu.setMnemonic('U');47menu.add(createMenuItem("Sunday", 'S'));48menu.add(createMenuItem("Monday", 'M'));4950menu.add(createMenuItem("Tuesday", 'T'));51menu.add(createMenuItem("Wednesday", 'W'));52menu.add(createMenuItem("Thursday", 'U'));53menu.add(createMenuItem("Friday", 'F'));54menu.add(createMenuItem("Saturday", 'A'));5556menubar.add(menu);5758return menubar;59}6061private static JPanel createPanel(JFrame frame) {62JPanel panel = new JPanel();63JButton button = new JButton("Button");64JButton button2 = new JButton("Button 2");65JButton button3 = new JButton("Button 3");6667JRootPane root = frame.getRootPane();68root.setDefaultButton(button);6970panel.add(button);71panel.add(button2);72panel.add(button3);7374return panel;75}7677/**78* Creates and returns the menu item.79*/80private static JMenuItem createMenuItem(String name, char mnemonic) {81JMenuItem menuItem = new JMenuItem(name, mnemonic);82menuItem.addActionListener(new ActionListener() {8384@Override85public void actionPerformed(ActionEvent evt) {86actionRecieved = true;87}88});8990return menuItem;91}9293public static void checkAction() {94if (actionRecieved == true) {95actionRecieved = false;96} else {97throw new RuntimeException("Action has not been received");98}99}100101public static void main(String[] args) throws Throwable {102Robot robot = new Robot();103robot.setAutoDelay(250);104105UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());106107SwingUtilities.invokeAndWait(new Runnable() {108109@Override110public void run() {111JFrame frame = new JFrame("Test");112frame.setContentPane(createPanel(frame));113frame.setJMenuBar(createMenuBar());114frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);115frame.pack();116frame.setVisible(true);117}118});119120robot.waitForIdle();121122// Change the default button to123// force a call to BasicRootPaneUI.updateDefaultButtonBindings()124Util.hitKeys(robot, KeyEvent.VK_TAB);125126// If the bug exists, then as soon as the menu appears,127// the VK_ENTER, VK_DOWN, VK_UP and VK_ESC will have no128// effect.129Util.hitMnemonics(robot, KeyEvent.VK_U);130Util.hitKeys(robot, KeyEvent.VK_ENTER);131robot.waitForIdle();132133checkAction();134135Util.hitMnemonics(robot, KeyEvent.VK_U);136Util.hitKeys(robot, KeyEvent.VK_DOWN);137Util.hitKeys(robot, KeyEvent.VK_ENTER);138robot.waitForIdle();139140checkAction();141}142}143144145