Path: blob/master/test/jdk/javax/swing/JMenu/4213634/bug4213634.java
41153 views
/*1* Copyright (c) 2015, 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*/2223import java.awt.AWTException;24import java.awt.Robot;25import java.awt.event.ActionEvent;26import java.awt.event.ActionListener;27import java.awt.event.KeyEvent;28import java.lang.reflect.InvocationTargetException;29import javax.swing.JButton;30import javax.swing.JFrame;31import javax.swing.JMenu;32import javax.swing.JMenuBar;33import javax.swing.JMenuItem;34import javax.swing.JTextArea;35import javax.swing.SwingUtilities;3637/*38* @test39* @key headful40* @bug 4213634 801718741* @author Scott Violet42* @library ../../regtesthelpers43* @build Util44* @run main bug421363445*/464748public class bug4213634 {4950private JMenu menu;5152private JFrame frame;5354public static void main(String[] args) throws Throwable {55new bug4213634();56}5758bug4213634() throws AWTException, InterruptedException, InvocationTargetException {59SwingUtilities.invokeAndWait(new Runnable() {60@Override61public void run() {62createAndShowGUI();63}64});6566test();67}6869public void createAndShowGUI() {70frame = new JFrame("TEST");71JMenuBar mb = new JMenuBar();72menu = mb.add(createMenu("1 - First Menu", true));73mb.add(createMenu("2 - Second Menu", false));74frame.setJMenuBar(mb);75JTextArea ta = new JTextArea("This test dedicated to Nancy and Kathleen, testers and bowlers extraordinaire\n\n\nNo exception means pass.");76frame.getContentPane().add("Center", ta);77JButton button = new JButton("Test");78frame.getContentPane().add("South", button);79frame.setBounds(100, 100, 400, 400);80frame.setVisible(true);81button.requestFocusInWindow();82}8384private void test() throws AWTException, InterruptedException, InvocationTargetException {85Robot robot = new Robot();86robot.setAutoDelay(50);87robot.waitForIdle();8889Util.hitMnemonics(robot, KeyEvent.VK_1);90robot.waitForIdle();9192SwingUtilities.invokeAndWait(new Runnable() {93@Override94public void run() {95if (!menu.isSelected()) {96throw new RuntimeException(97"Failed: Menu didn't remain posted at end of test");98} else {99System.out.println("Test passed!");100frame.dispose();101}102}103});104}105private JMenu createMenu(String str, boolean bFlag) {106JMenuItem menuitem;107JMenu menu = new JMenu(str);108menu.setMnemonic(str.charAt(0));109110for(int i = 0; i < 10; i ++) {111menuitem = new JMenuItem("JMenuItem" + i);112menuitem.addActionListener(new ActionListener() {113public void actionPerformed(ActionEvent e) {114throw new RuntimeException(115"Failed: Mnemonic activated");116}117});118if(bFlag)119menuitem.setMnemonic('0' + i);120menu.add(menuitem);121}122return menu;123}124}125126127