1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.awt.*; 25 import java.awt.event.*; 26 import javax.swing.*; 27 28 /** 29 * @test 30 * @key headful 31 * @bug 4515762 32 * @author Mark Davidson 33 * @summary Tests the ability to support duplicate mnemonics 34 * @library ../../regtesthelpers 35 * @build Util 36 * @run main bug4515762 37 */ 38 public class bug4515762 { 39 40 private static volatile boolean actionExpected = false; 41 private static volatile boolean actionRecieved = false; 42 43 /** 44 * @param str name of Menu 45 */ 46 private static JMenuBar createMenuBar() { 47 JMenuBar menubar = new JMenuBar(); 48 49 // Duplicate menu item test for 4515762 50 JMenu menu = new JMenu("Duplicate Menu"); 51 menu.setMnemonic('D'); 52 menu.add(createMenuItem("Sunday", 'S')); 53 menu.add(createMenuItem("Monday", 'M')); 54 55 menu.add(createMenuItem("Tuesday", 'S')); 56 menu.add(createMenuItem("Wednesday", 'S')); 57 menu.add(createMenuItem("Thursday", 'S')); 58 menu.add(createMenuItem("Friday", 'F')); 59 menu.add(createMenuItem("Saturday", 'S')); 60 61 // Control with unique menu 62 JMenu menu2 = new JMenu("Unique Menu"); 63 menu2.setMnemonic('U'); 64 menu2.add(createMenuItem("Sunday", 'S')); 65 menu2.add(createMenuItem("Monday", 'M')); 66 67 menu2.add(createMenuItem("Tuesday", 'T')); 68 menu2.add(createMenuItem("Wednesday", 'W')); 69 menu2.add(createMenuItem("Thursday", 'U')); 70 menu2.add(createMenuItem("Friday", 'F')); 71 menu2.add(createMenuItem("Saturday", 'A')); 72 73 menubar.add(menu); 74 menubar.add(menu2); 75 76 return menubar; 77 } 78 79 /** 80 * Creates and returns the menu item. 81 */ 82 private static JMenuItem createMenuItem(String name, char mnemonic) { 83 JMenuItem menuItem = new JMenuItem(name, mnemonic); 84 menuItem.addActionListener(new ActionListener() { 85 86 @Override 87 public void actionPerformed(ActionEvent evt) { 88 JMenuItem item = (JMenuItem) evt.getSource(); 89 if (actionExpected == false) { 90 throw new RuntimeException("Menu Action: " 91 + item.getText() + " should not be called"); 92 } else { 93 actionRecieved = true; 94 } 95 } 96 }); 97 98 return menuItem; 99 } 100 101 public static void checkAction() { 102 if (actionRecieved == true) { 103 actionRecieved = false; 104 } else { 105 throw new RuntimeException("Action has not been received"); 106 } 107 } 108 109 public static void main(String[] args) throws Throwable { 110 Robot robot = new Robot(); 111 robot.setAutoDelay(250); 112 113 SwingUtilities.invokeAndWait(new Runnable() { 114 115 @Override 116 public void run() { 117 JFrame frame = new JFrame("Test"); 118 frame.setJMenuBar(createMenuBar()); 119 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 120 frame.pack(); 121 frame.setVisible(true); 122 } 123 }); 124 125 robot.waitForIdle(); 126 127 Util.hitMnemonics(robot, KeyEvent.VK_D); 128 robot.waitForIdle(); 129 130 // Press the S key many times (should not cause an action peformed) 131 int TIMES = 5; 132 for (int i = 0; i < TIMES; i++) { 133 Util.hitKeys(robot, KeyEvent.VK_S); 134 } 135 robot.waitForIdle(); 136 137 // Unique menu items. 138 actionExpected = true; 139 Util.hitMnemonics(robot, KeyEvent.VK_U); 140 141 robot.keyPress(KeyEvent.VK_S); 142 robot.keyRelease(KeyEvent.VK_S); 143 robot.waitForIdle(); 144 145 checkAction(); 146 147 Util.hitMnemonics(robot, KeyEvent.VK_U); 148 robot.keyPress(KeyEvent.VK_M); 149 robot.keyRelease(KeyEvent.VK_M); 150 robot.waitForIdle(); 151 152 checkAction(); 153 154 Util.hitMnemonics(robot, KeyEvent.VK_U); 155 Util.hitKeys(robot, KeyEvent.VK_T); 156 robot.waitForIdle(); 157 158 checkAction(); 159 Util.hitMnemonics(robot, KeyEvent.VK_U); 160 Util.hitKeys(robot, KeyEvent.VK_W); 161 robot.waitForIdle(); 162 163 checkAction(); 164 165 Util.hitMnemonics(robot, KeyEvent.VK_U); 166 Util.hitKeys(robot, KeyEvent.VK_U); 167 robot.waitForIdle(); 168 169 checkAction(); 170 } 171 }