Path: blob/master/test/jdk/javax/swing/JMenu/4515762/bug4515762.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 451576231* @author Mark Davidson32* @summary Tests the ability to support duplicate mnemonics33* @library ../../regtesthelpers34* @build Util35* @run main bug451576236*/37public class bug4515762 {3839private static volatile boolean actionExpected = false;40private static volatile boolean actionRecieved = false;41private static JFrame frame;4243/**44* @param str name of Menu45*/46private static JMenuBar createMenuBar() {47JMenuBar menubar = new JMenuBar();4849// Duplicate menu item test for 451576250JMenu menu = new JMenu("Duplicate Menu");51menu.setMnemonic('D');52menu.add(createMenuItem("Sunday", 'S'));53menu.add(createMenuItem("Monday", 'M'));5455menu.add(createMenuItem("Tuesday", 'S'));56menu.add(createMenuItem("Wednesday", 'S'));57menu.add(createMenuItem("Thursday", 'S'));58menu.add(createMenuItem("Friday", 'F'));59menu.add(createMenuItem("Saturday", 'S'));6061// Control with unique menu62JMenu menu2 = new JMenu("Unique Menu");63menu2.setMnemonic('U');64menu2.add(createMenuItem("Sunday", 'S'));65menu2.add(createMenuItem("Monday", 'M'));6667menu2.add(createMenuItem("Tuesday", 'T'));68menu2.add(createMenuItem("Wednesday", 'W'));69menu2.add(createMenuItem("Thursday", 'U'));70menu2.add(createMenuItem("Friday", 'F'));71menu2.add(createMenuItem("Saturday", 'A'));7273menubar.add(menu);74menubar.add(menu2);7576return menubar;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) {88JMenuItem item = (JMenuItem) evt.getSource();89if (actionExpected == false) {90throw new RuntimeException("Menu Action: "91+ item.getText() + " should not be called");92} else {93actionRecieved = true;94}95}96});9798return menuItem;99}100101public static void checkAction() {102if (actionRecieved == true) {103actionRecieved = false;104} else {105throw new RuntimeException("Action has not been received");106}107}108109public static void main(String[] args) throws Throwable {110try {111Robot robot = new Robot();112robot.setAutoDelay(250);113114SwingUtilities.invokeAndWait(new Runnable() {115116@Override117public void run() {118frame = new JFrame("Test");119frame.setJMenuBar(createMenuBar());120frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);121frame.pack();122frame.setVisible(true);123frame.toFront();124}125});126127robot.waitForIdle();128129Util.hitMnemonics(robot, KeyEvent.VK_D);130robot.waitForIdle();131132// Press the S key many times (should not cause an action peformed)133int TIMES = 5;134for (int i = 0; i < TIMES; i++) {135Util.hitKeys(robot, KeyEvent.VK_S);136}137robot.waitForIdle();138139// Unique menu items.140actionExpected = true;141Util.hitMnemonics(robot, KeyEvent.VK_U);142143robot.waitForIdle();144robot.keyPress(KeyEvent.VK_S);145robot.keyRelease(KeyEvent.VK_S);146robot.waitForIdle();147148checkAction();149150Util.hitMnemonics(robot, KeyEvent.VK_U);151robot.waitForIdle();152153robot.keyPress(KeyEvent.VK_M);154robot.keyRelease(KeyEvent.VK_M);155robot.waitForIdle();156157checkAction();158159Util.hitMnemonics(robot, KeyEvent.VK_U);160robot.waitForIdle();161Util.hitKeys(robot, KeyEvent.VK_T);162robot.waitForIdle();163164checkAction();165166Util.hitMnemonics(robot, KeyEvent.VK_U);167robot.waitForIdle();168Util.hitKeys(robot, KeyEvent.VK_W);169robot.waitForIdle();170171checkAction();172173Util.hitMnemonics(robot, KeyEvent.VK_U);174robot.waitForIdle();175Util.hitKeys(robot, KeyEvent.VK_U);176robot.waitForIdle();177178checkAction();179} finally {180if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());181}182}183}184185186