1 /*
   2  * Copyright (c) 2006, 2014, 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 6249972
  28  * @summary Tests that JMenuItem(String,int) handles lower-case mnemonics properly.
  29  * @library ../../../../lib/client
  30  * @build ExtendedRobot
  31  * @author Mikhail Lapshin
  32  * @run main bug6249972
  33  */
  34 
  35 import javax.swing.*;
  36 import java.awt.event.ActionListener;
  37 import java.awt.event.ActionEvent;
  38 import java.awt.event.KeyEvent;
  39 
  40 public class bug6249972 implements ActionListener {
  41 
  42 
  43     private JFrame frame;
  44     private JMenu menu;
  45     private volatile boolean testPassed = false;
  46 
  47     public static void main(String[] args) throws Exception {
  48         bug6249972 bugTest = new bug6249972();
  49         bugTest.test();
  50     }
  51 
  52     public bug6249972() throws Exception {
  53         SwingUtilities.invokeAndWait(
  54                 new Runnable() {
  55                     public void run() {
  56                         frame = new JFrame("bug6249972");
  57                         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58 
  59                         JMenuBar bar = new JMenuBar();
  60                         frame.setJMenuBar(bar);
  61 
  62                         menu = new JMenu("Problem");
  63                         bar.add(menu);
  64 
  65                         JMenuItem item = new JMenuItem("JMenuItem(String,'z')", 'z');
  66                         item.addActionListener(bug6249972.this);
  67                         menu.add(item);
  68 
  69                         frame.setLocationRelativeTo(null);
  70                         frame.pack();
  71                         frame.setVisible(true);
  72                     }
  73                 }
  74         );
  75     }
  76 
  77 
  78     private void test() throws Exception {
  79         ExtendedRobot robot = new ExtendedRobot();
  80         robot.waitForIdle();
  81         java.awt.Point p = menu.getLocationOnScreen();
  82         java.awt.Dimension size = menu.getSize();
  83         p.x += size.width / 2;
  84         p.y += size.height / 2;
  85         robot.mouseMove(p.x, p.y);
  86         robot.click();
  87         robot.delay(100);
  88 
  89         robot.waitForIdle();
  90         robot.type(KeyEvent.VK_Z);
  91 
  92         robot.waitForIdle();
  93         frame.dispose(); // Try to stop the event dispatch thread
  94 
  95         if (!testPassed) {
  96             throw new RuntimeException("JMenuItem(String,int) does not handle " +
  97                     "lower-case mnemonics properly.");
  98         }
  99 
 100         System.out.println("Test passed");
 101     }
 102 
 103     public void actionPerformed(ActionEvent e) {
 104         // We are in the actionPerformed() method -
 105         // JMenuItem(String,int) handles lower-case mnemonics properly
 106         testPassed = true;
 107     }
 108 }