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