5850N/A/*
5850N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
5850N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5850N/A *
5850N/A * This code is free software; you can redistribute it and/or modify it
5850N/A * under the terms of the GNU General Public License version 2 only, as
5850N/A * published by the Free Software Foundation.
5850N/A *
5850N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5850N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5850N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5850N/A * version 2 for more details (a copy is included in the LICENSE file that
5850N/A * accompanied this code).
5850N/A *
5850N/A * You should have received a copy of the GNU General Public License version
5850N/A * 2 along with this work; if not, write to the Free Software Foundation,
5850N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5850N/A *
5850N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5850N/A * or visit www.oracle.com if you need additional information or have any
5850N/A * questions.
5850N/A */
5850N/A
5850N/Aimport java.awt.*;
5850N/Aimport java.awt.event.*;
5850N/Aimport javax.swing.*;
5850N/Aimport sun.awt.SunToolkit;
5850N/A
5850N/A/**
5850N/A * @test
5850N/A * @bug 4515762
5850N/A * @author Mark Davidson
5850N/A * @summary Tests the ability to support duplicate mnemonics
5850N/A * @library ../../regtesthelpers
5850N/A * @build Util
5850N/A * @run main bug4515762
5850N/A */
5850N/Apublic class bug4515762 {
5850N/A
5850N/A private static volatile boolean actionExpected = false;
5850N/A private static volatile boolean actionRecieved = false;
5850N/A
5850N/A /**
5850N/A * @param str name of Menu
5850N/A */
5850N/A private static JMenuBar createMenuBar() {
5850N/A JMenuBar menubar = new JMenuBar();
5850N/A
5850N/A // Duplicate menu item test for 4515762
5850N/A JMenu menu = new JMenu("Duplicate Menu");
5850N/A menu.setMnemonic('D');
5850N/A menu.add(createMenuItem("Sunday", 'S'));
5850N/A menu.add(createMenuItem("Monday", 'M'));
5850N/A
5850N/A menu.add(createMenuItem("Tuesday", 'S'));
5850N/A menu.add(createMenuItem("Wednesday", 'S'));
5850N/A menu.add(createMenuItem("Thursday", 'S'));
5850N/A menu.add(createMenuItem("Friday", 'F'));
5850N/A menu.add(createMenuItem("Saturday", 'S'));
5850N/A
5850N/A // Control with unique menu
5850N/A JMenu menu2 = new JMenu("Unique Menu");
5850N/A menu2.setMnemonic('U');
5850N/A menu2.add(createMenuItem("Sunday", 'S'));
5850N/A menu2.add(createMenuItem("Monday", 'M'));
5850N/A
5850N/A menu2.add(createMenuItem("Tuesday", 'T'));
5850N/A menu2.add(createMenuItem("Wednesday", 'W'));
5850N/A menu2.add(createMenuItem("Thursday", 'U'));
5850N/A menu2.add(createMenuItem("Friday", 'F'));
5850N/A menu2.add(createMenuItem("Saturday", 'A'));
5850N/A
5850N/A menubar.add(menu);
5850N/A menubar.add(menu2);
5850N/A
5850N/A return menubar;
5850N/A }
5850N/A
5850N/A /**
5850N/A * Creates and returns the menu item.
5850N/A */
5850N/A private static JMenuItem createMenuItem(String name, char mnemonic) {
5850N/A JMenuItem menuItem = new JMenuItem(name, mnemonic);
5850N/A menuItem.addActionListener(new ActionListener() {
5850N/A
5850N/A @Override
5850N/A public void actionPerformed(ActionEvent evt) {
5850N/A JMenuItem item = (JMenuItem) evt.getSource();
5850N/A if (actionExpected == false) {
5850N/A throw new RuntimeException("Menu Action: "
5850N/A + item.getText() + " should not be called");
5850N/A } else {
5850N/A actionRecieved = true;
5850N/A }
5850N/A }
5850N/A });
5850N/A
5850N/A return menuItem;
5850N/A }
5850N/A
5850N/A public static void checkAction() {
5850N/A if (actionRecieved == true) {
5850N/A actionRecieved = false;
5850N/A } else {
5850N/A throw new RuntimeException("Action has not been received");
5850N/A }
5850N/A }
5850N/A
5850N/A public static void main(String[] args) throws Throwable {
5850N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
5850N/A Robot robot = new Robot();
5850N/A robot.setAutoDelay(250);
5850N/A
5850N/A SwingUtilities.invokeAndWait(new Runnable() {
5850N/A
5850N/A @Override
5850N/A public void run() {
5850N/A JFrame frame = new JFrame("Test");
5850N/A frame.setJMenuBar(createMenuBar());
5850N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
5850N/A frame.pack();
5850N/A frame.setVisible(true);
5850N/A }
5850N/A });
5850N/A
5850N/A toolkit.realSync();
5850N/A
5850N/A Util.hitMnemonics(robot, KeyEvent.VK_D);
5850N/A toolkit.realSync();
5850N/A
5850N/A // Press the S key many times (should not cause an action peformed)
5850N/A int TIMES = 5;
5850N/A for (int i = 0; i < TIMES; i++) {
5850N/A Util.hitKeys(robot, KeyEvent.VK_S);
5850N/A }
5850N/A toolkit.realSync();
5850N/A
5850N/A // Unique menu items.
5850N/A actionExpected = true;
5850N/A Util.hitMnemonics(robot, KeyEvent.VK_U);
5850N/A
5850N/A robot.keyPress(KeyEvent.VK_S);
5850N/A robot.keyRelease(KeyEvent.VK_S);
5850N/A toolkit.realSync();
5850N/A
5850N/A checkAction();
5850N/A
5850N/A Util.hitMnemonics(robot, KeyEvent.VK_U);
5850N/A robot.keyPress(KeyEvent.VK_M);
5850N/A robot.keyRelease(KeyEvent.VK_M);
5850N/A toolkit.realSync();
5850N/A
5850N/A checkAction();
5850N/A
5850N/A Util.hitMnemonics(robot, KeyEvent.VK_U);
5850N/A Util.hitKeys(robot, KeyEvent.VK_T);
5850N/A toolkit.realSync();
5850N/A
5850N/A checkAction();
5850N/A Util.hitMnemonics(robot, KeyEvent.VK_U);
5850N/A Util.hitKeys(robot, KeyEvent.VK_W);
5850N/A toolkit.realSync();
5850N/A
5850N/A checkAction();
5850N/A
5850N/A Util.hitMnemonics(robot, KeyEvent.VK_U);
5850N/A Util.hitKeys(robot, KeyEvent.VK_U);
5850N/A toolkit.realSync();
5850N/A
5850N/A checkAction();
5850N/A }
5850N/A}