Path: blob/master/test/jdk/javax/swing/JMenuItem/6249972/bug6249972.java
41153 views
/*1* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 624997227* @summary Tests that JMenuItem(String,int) handles lower-case mnemonics properly.28* @author Mikhail Lapshin29* @run main bug624997230*/3132import javax.swing.*;33import java.awt.event.ActionListener;34import java.awt.event.ActionEvent;35import java.awt.event.KeyEvent;36import java.awt.event.InputEvent;37import java.awt.Dimension;38import java.awt.Point;39import java.awt.Robot;4041public class bug6249972 implements ActionListener {4243private static JFrame frame;44private static Robot robot;45private JMenu menu;46private volatile boolean testPassed = false;47private volatile Point p = null;48private volatile Dimension size = null;4950public static void main(String[] args) throws Exception {51try {52robot = new Robot();53robot.setAutoDelay(100);54bug6249972 bugTest = new bug6249972();55robot.waitForIdle();56robot.delay(1000);57bugTest.test();58} finally {59if (frame != null) {60SwingUtilities.invokeAndWait(() -> frame.dispose());61}62}63}6465public bug6249972() throws Exception {66SwingUtilities.invokeAndWait(() -> {67frame = new JFrame("bug6249972");68frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);6970JMenuBar bar = new JMenuBar();71frame.setJMenuBar(bar);7273menu = new JMenu("Problem");74bar.add(menu);7576JMenuItem item = new JMenuItem("JMenuItem(String,'z')", 'z');77item.addActionListener(bug6249972.this);78menu.add(item);7980frame.setLocationRelativeTo(null);81frame.pack();82frame.setVisible(true);83});84}858687private void test() throws Exception {88SwingUtilities.invokeAndWait(() -> {89p = menu.getLocationOnScreen();90size = menu.getSize();91});92p.x += size.width / 2;93p.y += size.height / 2;94robot.mouseMove(p.x, p.y);95robot.waitForIdle();96robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);97robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);9899robot.waitForIdle();100robot.delay(100);101robot.keyPress(KeyEvent.VK_Z);102robot.keyRelease(KeyEvent.VK_Z);103104robot.waitForIdle();105robot.delay(1000);106107if (!testPassed) {108throw new RuntimeException("JMenuItem(String,int) does not handle " +109"lower-case mnemonics properly.");110}111112System.out.println("Test passed");113}114115public void actionPerformed(ActionEvent e) {116// We are in the actionPerformed() method -117// JMenuItem(String,int) handles lower-case mnemonics properly118testPassed = true;119}120}121122123