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