1 /*
   2  * Copyright (c) 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 import java.awt.*;
  25 import java.awt.event.*;
  26 import javax.swing.*;
  27 
  28 /**
  29  * @test
  30  * @bug 4515762
  31  * @author Mark Davidson
  32  * @summary Tests the ability to support duplicate mnemonics
  33  * @library ../../regtesthelpers
  34  * @build Util
  35  * @run main bug4515762
  36  */
  37 public class bug4515762 {
  38 
  39     private static volatile boolean actionExpected = false;
  40     private static volatile boolean actionRecieved = false;
  41 
  42     /**
  43      * @param str name of Menu
  44      */
  45     private static JMenuBar createMenuBar() {
  46         JMenuBar menubar = new JMenuBar();
  47 
  48         // Duplicate menu item test for 4515762
  49         JMenu menu = new JMenu("Duplicate Menu");
  50         menu.setMnemonic('D');
  51         menu.add(createMenuItem("Sunday", 'S'));
  52         menu.add(createMenuItem("Monday", 'M'));
  53 
  54         menu.add(createMenuItem("Tuesday", 'S'));
  55         menu.add(createMenuItem("Wednesday", 'S'));
  56         menu.add(createMenuItem("Thursday", 'S'));
  57         menu.add(createMenuItem("Friday", 'F'));
  58         menu.add(createMenuItem("Saturday", 'S'));
  59 
  60         // Control with unique menu
  61         JMenu menu2 = new JMenu("Unique Menu");
  62         menu2.setMnemonic('U');
  63         menu2.add(createMenuItem("Sunday", 'S'));
  64         menu2.add(createMenuItem("Monday", 'M'));
  65 
  66         menu2.add(createMenuItem("Tuesday", 'T'));
  67         menu2.add(createMenuItem("Wednesday", 'W'));
  68         menu2.add(createMenuItem("Thursday", 'U'));
  69         menu2.add(createMenuItem("Friday", 'F'));
  70         menu2.add(createMenuItem("Saturday", 'A'));
  71 
  72         menubar.add(menu);
  73         menubar.add(menu2);
  74 
  75         return menubar;
  76     }
  77 
  78     /**
  79      * Creates and returns the menu item.
  80      */
  81     private static JMenuItem createMenuItem(String name, char mnemonic) {
  82         JMenuItem menuItem = new JMenuItem(name, mnemonic);
  83         menuItem.addActionListener(new ActionListener() {
  84 
  85             @Override
  86             public void actionPerformed(ActionEvent evt) {
  87                 JMenuItem item = (JMenuItem) evt.getSource();
  88                 if (actionExpected == false) {
  89                     throw new RuntimeException("Menu Action: "
  90                             + item.getText() + " should not be called");
  91                 } else {
  92                     actionRecieved = true;
  93                 }
  94             }
  95         });
  96 
  97         return menuItem;
  98     }
  99 
 100     public static void checkAction() {
 101         if (actionRecieved == true) {
 102             actionRecieved = false;
 103         } else {
 104             throw new RuntimeException("Action has not been received");
 105         }
 106     }
 107 
 108     public static void main(String[] args) throws Throwable {
 109         Robot robot = new Robot();
 110         robot.setAutoDelay(250);
 111 
 112         SwingUtilities.invokeAndWait(new Runnable() {
 113 
 114             @Override
 115             public void run() {
 116                 JFrame frame = new JFrame("Test");
 117                 frame.setJMenuBar(createMenuBar());
 118                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 119                 frame.pack();
 120                 frame.setVisible(true);
 121             }
 122         });
 123 
 124         robot.waitForIdle();
 125 
 126         Util.hitMnemonics(robot, KeyEvent.VK_D);
 127         robot.waitForIdle();
 128 
 129         // Press the S key many times (should not cause an action peformed)
 130         int TIMES = 5;
 131         for (int i = 0; i < TIMES; i++) {
 132             Util.hitKeys(robot, KeyEvent.VK_S);
 133         }
 134         robot.waitForIdle();
 135 
 136         // Unique menu items.
 137         actionExpected = true;
 138         Util.hitMnemonics(robot, KeyEvent.VK_U);
 139 
 140         robot.keyPress(KeyEvent.VK_S);
 141         robot.keyRelease(KeyEvent.VK_S);
 142         robot.waitForIdle();
 143 
 144         checkAction();
 145 
 146         Util.hitMnemonics(robot, KeyEvent.VK_U);
 147         robot.keyPress(KeyEvent.VK_M);
 148         robot.keyRelease(KeyEvent.VK_M);
 149         robot.waitForIdle();
 150 
 151         checkAction();
 152 
 153         Util.hitMnemonics(robot, KeyEvent.VK_U);
 154         Util.hitKeys(robot, KeyEvent.VK_T);
 155         robot.waitForIdle();
 156 
 157         checkAction();
 158         Util.hitMnemonics(robot, KeyEvent.VK_U);
 159         Util.hitKeys(robot, KeyEvent.VK_W);
 160         robot.waitForIdle();
 161 
 162         checkAction();
 163 
 164         Util.hitMnemonics(robot, KeyEvent.VK_U);
 165         Util.hitKeys(robot, KeyEvent.VK_U);
 166         robot.waitForIdle();
 167 
 168         checkAction();
 169     }
 170 }