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