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