Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JMenu/4515762/bug4515762.java
38854 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 451576230* @author Mark Davidson31* @summary Tests the ability to support duplicate mnemonics32* @library ../../regtesthelpers33* @build Util34* @run main bug451576235*/36public class bug4515762 {3738private static volatile boolean actionExpected = false;39private static volatile boolean actionRecieved = false;4041/**42* @param str name of Menu43*/44private static JMenuBar createMenuBar() {45JMenuBar menubar = new JMenuBar();4647// Duplicate menu item test for 451576248JMenu menu = new JMenu("Duplicate Menu");49menu.setMnemonic('D');50menu.add(createMenuItem("Sunday", 'S'));51menu.add(createMenuItem("Monday", 'M'));5253menu.add(createMenuItem("Tuesday", 'S'));54menu.add(createMenuItem("Wednesday", 'S'));55menu.add(createMenuItem("Thursday", 'S'));56menu.add(createMenuItem("Friday", 'F'));57menu.add(createMenuItem("Saturday", 'S'));5859// Control with unique menu60JMenu menu2 = new JMenu("Unique Menu");61menu2.setMnemonic('U');62menu2.add(createMenuItem("Sunday", 'S'));63menu2.add(createMenuItem("Monday", 'M'));6465menu2.add(createMenuItem("Tuesday", 'T'));66menu2.add(createMenuItem("Wednesday", 'W'));67menu2.add(createMenuItem("Thursday", 'U'));68menu2.add(createMenuItem("Friday", 'F'));69menu2.add(createMenuItem("Saturday", 'A'));7071menubar.add(menu);72menubar.add(menu2);7374return menubar;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) {86JMenuItem item = (JMenuItem) evt.getSource();87if (actionExpected == false) {88throw new RuntimeException("Menu Action: "89+ item.getText() + " should not be called");90} else {91actionRecieved = true;92}93}94});9596return menuItem;97}9899public static void checkAction() {100if (actionRecieved == true) {101actionRecieved = false;102} else {103throw new RuntimeException("Action has not been received");104}105}106107public static void main(String[] args) throws Throwable {108Robot robot = new Robot();109robot.setAutoDelay(250);110111SwingUtilities.invokeAndWait(new Runnable() {112113@Override114public void run() {115JFrame frame = new JFrame("Test");116frame.setJMenuBar(createMenuBar());117frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);118frame.pack();119frame.setVisible(true);120}121});122123robot.waitForIdle();124125Util.hitMnemonics(robot, KeyEvent.VK_D);126robot.waitForIdle();127128// Press the S key many times (should not cause an action peformed)129int TIMES = 5;130for (int i = 0; i < TIMES; i++) {131Util.hitKeys(robot, KeyEvent.VK_S);132}133robot.waitForIdle();134135// Unique menu items.136actionExpected = true;137Util.hitMnemonics(robot, KeyEvent.VK_U);138139robot.keyPress(KeyEvent.VK_S);140robot.keyRelease(KeyEvent.VK_S);141robot.waitForIdle();142143checkAction();144145Util.hitMnemonics(robot, KeyEvent.VK_U);146robot.keyPress(KeyEvent.VK_M);147robot.keyRelease(KeyEvent.VK_M);148robot.waitForIdle();149150checkAction();151152Util.hitMnemonics(robot, KeyEvent.VK_U);153Util.hitKeys(robot, KeyEvent.VK_T);154robot.waitForIdle();155156checkAction();157Util.hitMnemonics(robot, KeyEvent.VK_U);158Util.hitKeys(robot, KeyEvent.VK_W);159robot.waitForIdle();160161checkAction();162163Util.hitMnemonics(robot, KeyEvent.VK_U);164Util.hitKeys(robot, KeyEvent.VK_U);165robot.waitForIdle();166167checkAction();168}169}170171172