1 /* 2 * Copyright (c) 2007, 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 /* 25 * @test 26 * @key headful 27 * @bug 6827786 28 * @summary Tests duplicate mnemonics 29 * @author Peter Zhelezniakov 30 * @library ../../regtesthelpers 31 * @build Util 32 * @run main bug6827786 33 */ 34 import java.awt.*; 35 import java.awt.event.KeyEvent; 36 import javax.swing.*; 37 import sun.awt.SunToolkit; 38 39 public class bug6827786 { 40 41 private static JMenu menu; 42 private static Component focusable; 43 44 public static void main(String[] args) throws Exception { 45 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 46 Robot robot = new Robot(); 47 robot.setAutoDelay(50); 48 49 SwingUtilities.invokeAndWait(new Runnable() { 50 51 public void run() { 52 createAndShowGUI(); 53 } 54 }); 55 56 toolkit.realSync(); 57 58 SwingUtilities.invokeAndWait(new Runnable() { 59 60 public void run() { 61 focusable.requestFocus(); 62 } 63 }); 64 65 toolkit.realSync(); 66 checkfocus(); 67 68 // select menu 69 if (sun.awt.OSInfo.getOSType() == sun.awt.OSInfo.OSType.MACOSX) { 70 Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_F); 71 } else { 72 Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_F); 73 } 74 // select submenu 75 Util.hitKeys(robot, KeyEvent.VK_S); 76 toolkit.realSync(); 77 // verify submenu is selected 78 verify(1); 79 80 Util.hitKeys(robot, KeyEvent.VK_S); 81 toolkit.realSync(); 82 // verify last item is selected 83 verify(2); 84 85 Util.hitKeys(robot, KeyEvent.VK_S); 86 toolkit.realSync(); 87 // selection should wrap to first item 88 verify(0); 89 90 System.out.println("PASSED"); 91 92 } 93 94 private static void checkfocus() throws Exception { 95 SwingUtilities.invokeAndWait(new Runnable() { 96 97 public void run() { 98 if (!focusable.isFocusOwner()) { 99 throw new RuntimeException("Button is not the focus owner."); 100 } 101 } 102 }); 103 } 104 105 private static void verify(final int index) throws Exception { 106 SwingUtilities.invokeAndWait(new Runnable() { 107 108 @Override 109 public void run() { 110 MenuElement[] path = 111 MenuSelectionManager.defaultManager().getSelectedPath(); 112 MenuElement item = path[3]; 113 if (item != menu.getMenuComponent(index)) { 114 System.err.println("Selected: " + item); 115 System.err.println("Should be: " 116 + menu.getMenuComponent(index)); 117 throw new RuntimeException("Test Failed"); 118 } 119 } 120 }); 121 } 122 123 private static JMenuBar createMenuBar() { 124 menu = new JMenu("File"); 125 menu.setMnemonic('F'); 126 127 menu.add(new JMenuItem("Save", 'S')); 128 129 JMenu sub = new JMenu("Submenu"); 130 sub.setMnemonic('S'); 131 sub.add(new JMenuItem("Sub Item")); 132 menu.add(sub); 133 134 menu.add(new JMenuItem("Special", 'S')); 135 136 JMenuBar bar = new JMenuBar(); 137 bar.add(menu); 138 return bar; 139 } 140 141 private static void createAndShowGUI() { 142 JFrame frame = new JFrame("bug6827786"); 143 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 144 frame.setJMenuBar(createMenuBar()); 145 focusable = new JButton("Set Focus Here"); 146 frame.add(focusable); 147 frame.pack(); 148 frame.setVisible(true); 149 } 150 }