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