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 /*
  25  * @test
  26  * @key headful
  27  * @bug 4171437
  28  * @library ../../regtesthelpers
  29  * @build Util
  30  * @author Georges Saab
  31  * @run main bug4171437
  32  */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import java.util.ArrayList;
  37 import javax.swing.*;
  38 import javax.swing.event.*;
  39 import sun.awt.SunToolkit;
  40 
  41 public class bug4171437 {
  42     static volatile boolean closeActivated = false;
  43     static volatile boolean customActivated = false;
  44 
  45     public static void main(String[] args) throws Exception {
  46         SwingUtilities.invokeAndWait(new Runnable() {
  47             public void run() {
  48                 createAndShowGUI();
  49             }
  50         });
  51 
  52         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  53         toolkit.realSync();
  54 
  55         Robot robot = new Robot();
  56         robot.setAutoDelay(50);
  57 
  58         Util.hitMnemonics(robot, KeyEvent.VK_F);
  59         Util.hitKeys(robot, KeyEvent.VK_C);
  60 
  61         toolkit.realSync();
  62         Thread.sleep(1000);
  63 
  64         if (!closeActivated || customActivated) {
  65             throw new RuntimeException("Didn't pass the muster");
  66         }
  67     }
  68     public static void createAndShowGUI() {
  69         JMenuBar menubar = new JMenuBar();
  70 
  71         JMenu fileMenu = new JMenu("File");
  72         fileMenu.setMnemonic('f');
  73 
  74         JMenuItem fmi1 = new JMenuItem();
  75         fmi1 = new JMenuItem("Open");
  76         JMenuItem fmi2 = new JMenuItem();
  77         fmi2 = new JMenuItem("Close");
  78         fmi2.setMnemonic('c');
  79         fmi2.addActionListener(new ActionListener() {
  80             public void actionPerformed(ActionEvent e) {
  81                 closeActivated = true;
  82             }
  83         });
  84 
  85         fileMenu.add( fmi1);
  86         fileMenu.add( fmi2);
  87 
  88         menubar.add( fileMenu);
  89 
  90         JMenu custom = new JMenu("Custom");
  91         custom.setMnemonic('c');
  92         JMenuItem cmi = new JMenuItem();
  93         cmi = new JMenuItem("Properties");
  94         cmi.setMnemonic('p');
  95         custom.add( cmi);
  96         custom.addMenuListener(new MenuListener() {
  97             public void menuSelected(MenuEvent e) {
  98                 customActivated = true;
  99             }
 100             public void menuDeselected(MenuEvent e) {}
 101             public void menuCanceled(MenuEvent e) {}
 102         });
 103         menubar.add( custom);
 104 
 105         JFrame frame = new JFrame();
 106         frame.setJMenuBar( menubar);
 107         frame.setSize(300, 300);
 108         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 109         frame.pack();
 110         frame.setVisible(true);
 111     }
 112 }