Path: blob/master/test/jdk/com/sun/java/swing/plaf/windows/Test8173145.java
41162 views
/*1* Copyright (c) 2017, 2020, 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/* @test24@key headful25@bug 817314526@requires (os.family == "windows")27@summary Menu is activated after using mnemonic Alt/Key combination28@run main Test817314529*/3031import java.awt.AWTException;32import java.awt.Component;33import java.awt.KeyboardFocusManager;34import java.awt.Robot;35import java.awt.event.KeyEvent;36import java.lang.reflect.InvocationTargetException;3738import javax.swing.JButton;39import javax.swing.JFrame;40import javax.swing.JMenu;41import javax.swing.JMenuBar;42import javax.swing.JMenuItem;43import javax.swing.JPanel;44import javax.swing.JTextField;45import javax.swing.SwingUtilities;46import javax.swing.UIManager;4748public class Test8173145 {4950private volatile static JButton btn;51private volatile static JFrame f;52private volatile static boolean uiCreated;5354public static void main(String[] args) throws InvocationTargetException, InterruptedException, AWTException {55try {56SwingUtilities.invokeAndWait(new Runnable() {57@Override58public void run() {59try {60uiCreated = createGUI();61} catch (Exception e) {62throw new RuntimeException(e);63}64}65});6667if (uiCreated) {68test();69} else {70//no windows l&f, skip the test71}72}finally {73SwingUtilities.invokeAndWait(() -> f.dispose());74}75}7677private static void test() {78final Robot robot;79try {80robot = new Robot();81} catch (AWTException e) {82throw new RuntimeException(e);83}84robot.setAutoDelay(150);85robot.waitForIdle();8687robot.keyPress(KeyEvent.VK_ALT);88robot.keyPress(KeyEvent.VK_M);89robot.keyRelease(KeyEvent.VK_M);90robot.keyRelease(KeyEvent.VK_ALT);91robot.waitForIdle();9293Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();9495if (focusOwner != btn) {96throw new RuntimeException("Wrong focus owner");97}98}99100private static boolean createGUI() {101try {102UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");103} catch (Exception e) {104return false;105}106f = new JFrame();107108JPanel panel = new JPanel();109btn = new JButton("Mmmmm");110btn.setMnemonic(KeyEvent.VK_M);111btn.setDisplayedMnemonicIndex(0);112panel.add(btn);113114JTextField tf = new JTextField();115tf.setColumns(10);116panel.add(tf);117118f.setJMenuBar(getMenuBar());119f.add(panel);120f.pack();121f.setVisible(true);122f.setLocationRelativeTo(null);123tf.requestFocus();124return true;125}126127static JMenuBar getMenuBar() {128JMenuBar menuBar;129JMenu menu;130131menuBar = new JMenuBar();132133menu = new JMenu("Menu");134menuBar.add(menu);135136JMenuItem mi = new JMenuItem("test");137menu.add(mi);138139return menuBar;140}141}142143144