bug4692443.java revision 11111:4ef86895869c
1/*
2 * Copyright (c) 2009, 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 * @library ../../regtesthelpers
27 * @build Util
28 * @bug 4692443 7105030
29 * @summary JMenu: MenuListener.menuSelected() event fired too late when using mnemonics
30 * @author Alexander Zuev
31 * @run main bug4692443
32 */
33
34import javax.swing.*;
35import javax.swing.event.*;
36import java.awt.event.*;
37import java.awt.*;
38
39public class bug4692443 {
40
41    public static PassedListener pass;
42    public static FailedListener fail;
43    public static volatile Boolean passed;
44
45    public static void main(String args[]) throws Throwable {
46
47        fail = new FailedListener();
48        pass = new PassedListener();
49        passed = false;
50        Robot robo = new Robot();
51
52        SwingUtilities.invokeAndWait(new Runnable() {
53            public void run() {
54                createAndShowGUI();
55            }
56        });
57
58        robo.waitForIdle();
59
60        int altKey = java.awt.event.KeyEvent.VK_ALT;
61        robo.setAutoDelay(100);
62        Util.hitMnemonics(robo, KeyEvent.VK_F); // Enter File menu
63        robo.keyPress(KeyEvent.VK_S);  // Enter submenu
64        robo.keyRelease(KeyEvent.VK_S);
65        robo.keyPress(KeyEvent.VK_O); // Launch "One" action
66        robo.keyRelease(KeyEvent.VK_O);
67        robo.keyPress(KeyEvent.VK_M); // Launch "One" action
68        robo.keyRelease(KeyEvent.VK_M);
69
70        robo.waitForIdle();
71
72        if (!passed) {
73            throw new RuntimeException("Test failed.");
74        }
75
76    }
77
78    private static void createAndShowGUI() {
79        JFrame mainFrame = new JFrame("Bug 4692443");
80        JMenuBar mbar = new JMenuBar();
81        JMenu menu = new JMenu("File");
82        menu.setMnemonic('F');
83        menu.add(new JMenuItem("Menu Item 1")).setMnemonic('I');
84        final JMenu submenu = new JMenu("Submenu");
85        submenu.setMnemonic('S');
86        submenu.addMenuListener(new MenuListener() {
87            public void menuSelected(MenuEvent e) {
88                JMenuItem item = submenu.add(new JMenuItem("One", 'O'));
89                item.addActionListener(pass);
90                submenu.add(new JMenuItem("Two", 'w'));
91                submenu.add(new JMenuItem("Three", 'r'));
92            }
93            public void menuDeselected(MenuEvent e) {
94                submenu.removeAll();
95            }
96            public void menuCanceled(MenuEvent e) {
97                submenu.removeAll();
98            }
99        });
100        menu.add(submenu);
101        JMenuItem menuItem = menu.add(new JMenuItem("Menu Item 2"));
102        menuItem.setMnemonic('M');
103        menuItem.addActionListener(fail);
104        mbar.add(menu);
105        mainFrame.setJMenuBar(mbar);
106
107        mainFrame.setSize(200, 200);
108        mainFrame.setLocation(200, 200);
109        mainFrame.setVisible(true);
110        mainFrame.toFront();
111    }
112
113    public static class FailedListener implements ActionListener {
114        public void actionPerformed(ActionEvent ev) {
115            throw new RuntimeException("Test failed.");
116        }
117    }
118
119    public static class PassedListener implements ActionListener {
120        public void actionPerformed(ActionEvent ev) {
121            passed = true;
122        }
123    }
124
125}
126