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