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