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