Path: blob/master/test/jdk/com/sun/java/swing/plaf/windows/AltFocusIssueTest.java
41162 views
/*1* Copyright (c) 2018, 2019, 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* @bug 821198726* @key headful27* @requires (os.family == "windows")28* @summary Verify if Menu bar gets input focus even if Alt-released event is consumed.29* @modules java.desktop/com.sun.java.swing.plaf.windows30* @run main AltFocusIssueTest31*/3233import java.awt.event.KeyAdapter;34import java.awt.event.KeyEvent;35import java.lang.reflect.InvocationTargetException;36import java.awt.Robot;37import javax.swing.JFrame;38import javax.swing.JMenu;39import javax.swing.JMenuBar;40import javax.swing.JMenuItem;41import javax.swing.JTextArea;42import javax.swing.SwingUtilities;43import javax.swing.UIManager;44import javax.swing.UnsupportedLookAndFeelException;4546/**47* Try to demonstrate the wrong behavior48*/49public class AltFocusIssueTest {5051/**52* Menu inside menu bar of the frame53*/54private static JMenu menu;5556/**57* Text area to test on.58*/59private static JTextArea ta;6061private static JFrame frame;6263/**64* Test that the text area loses input focus although Alt-released event is consumed.65*66* @throws InterruptedException67* @throws InvocationTargetException68*/69public static void testAltEvents() throws Exception {70Robot robot = new Robot();71SwingUtilities.invokeAndWait(() -> {72try {73createUI();74} catch (Exception e) {75throw new RuntimeException(e);76}77});78robot.waitForIdle();79SwingUtilities.invokeAndWait(() -> ta.requestFocusInWindow());80robot.waitForIdle();81if (!ta.isFocusOwner()) {82throw new RuntimeException("textarea should have input focus");83}84if (menu.isSelected()) {85throw new RuntimeException("menu is selected...");86}8788// Simulate an Alt-typed event89robot.keyPress(KeyEvent.VK_ALT);90robot.keyRelease(KeyEvent.VK_ALT);91robot.waitForIdle();9293// Since the event is consumed, I expect the input focus to be in the text area94if (!ta.isFocusOwner()) {95throw new RuntimeException("textarea should still have input focus");96}97// OR98if (SwingUtilities.getRootPane(ta).isFocusOwner()) {99throw new RuntimeException("Focus should not be changed from the text area");100}101// OR102if (menu.isSelected()) {103throw new RuntimeException("Menu must not be selected");104}105}106107/**108* Builds UI to test.109*110*/111private static void createUI() throws Exception {112// Install Windows L&F113UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());114115frame = new JFrame();116frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);117118JMenuBar menuBar = new JMenuBar();119frame.setJMenuBar(menuBar);120121menu = new JMenu("Menu");122menu.add(new JMenuItem("Menu item"));123menuBar.add(menu);124125ta = new JTextArea();126frame.getContentPane().add(ta);127128ta.addKeyListener( new KeyAdapter() {129@Override130public void keyReleased(KeyEvent e) {131if (e.getKeyCode() == KeyEvent.VK_ALT) {132/*133* This is where I need to do special handling of the Alt-released event.134* After, nobody else must react to this event, thus I consume it.135*/136e.consume();137}138}139});140141frame.setSize(400, 300);142frame.setVisible(true);143}144145public static void main(String[] args) throws Exception {146try {147testAltEvents();148} finally {149if (frame != null) {150SwingUtilities.invokeAndWait(() -> frame.dispose());151}152}153}154}155156157