Path: blob/master/test/jdk/javax/swing/JLabel/6596966/bug6596966.java
41153 views
/*1* Copyright (c) 2011, 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/*24* @test25* @key headful26* @bug 659696627* @summary Some JFileChooser mnemonics do not work with sticky keys28* @library ../../regtesthelpers29* @library /test/lib30* @build Util jdk.test.lib.Platform31* @run main bug659696632* @author Pavel Porvatov33*/3435import java.awt.EventQueue;36import java.awt.Robot;37import java.awt.Toolkit;38import java.awt.event.KeyEvent;39import java.awt.event.InputEvent;40import java.util.ArrayList;41import javax.swing.JFrame;42import javax.swing.JButton;43import javax.swing.JComboBox;44import javax.swing.JLabel;45import javax.swing.JPanel;46import javax.swing.SwingUtilities;4748import jdk.test.lib.Platform;4950public class bug6596966 {51private static JFrame frame;5253private static JLabel label;54private static JButton button;55private static JComboBox comboBox;5657public static void main(String[] args) throws Exception {58try {59Robot robot = new Robot();60robot.setAutoDelay(100);6162SwingUtilities.invokeAndWait(new Runnable() {63public void run() {64button = new JButton("Button");65comboBox = new JComboBox();6667label = new JLabel("Label");68label.setDisplayedMnemonic('L');69label.setLabelFor(comboBox);7071JPanel pnContent = new JPanel();7273pnContent.add(button);74pnContent.add(label);75pnContent.add(comboBox);7677frame = new JFrame();7879frame.add(pnContent);80frame.pack();81frame.setVisible(true);82}83});8485robot.waitForIdle();86robot.delay(1000);8788int keyMask = InputEvent.ALT_MASK;89if (Platform.isOSX()) {90keyMask = InputEvent.CTRL_MASK | InputEvent.ALT_MASK;91}92ArrayList<Integer> keys = Util.getKeyCodesFromKeyMask(keyMask);93for (int i = 0; i < keys.size(); ++i) {94robot.keyPress(keys.get(i));95}9697robot.keyPress(KeyEvent.VK_L);9899robot.waitForIdle();100Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new KeyEvent(label, KeyEvent.KEY_RELEASED,101EventQueue.getMostRecentEventTime(), 0, KeyEvent.VK_L, 'L'));102103robot.waitForIdle();104105try {106SwingUtilities.invokeAndWait(new Runnable() {107public void run() {108if (!comboBox.isFocusOwner()) {109throw new RuntimeException("comboBox isn't focus owner");110}111}112});113} finally {114robot.keyRelease(KeyEvent.VK_L);115for (int i = 0; i < keys.size(); ++i) {116robot.keyRelease(keys.get(i));117}118robot.waitForIdle();119}120} finally {121if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());122}123}124}125126127