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