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