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