Path: blob/master/test/jdk/java/awt/Focus/KeyEventForBadFocusOwnerTest/KeyEventForBadFocusOwnerTest.java
41152 views
/*1* Copyright (c) 2013, 2021, 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*/22/*23@test24@key headful25@bug 447662926@library ../../../../javax/swing/regtesthelpers27@build Util28@summary KeyEvents dispatched to old focus owner that is no longer showing29@author [email protected]: area=awt.focus30@run main KeyEventForBadFocusOwnerTest31*/3233/**34* KeyEventForBadFocusOwnerTest.java35*36* summary: KeyEvents dispatched to old focus owner that is no longer showing37*/383940import java.awt.Robot;4142import java.awt.event.*;4344import javax.swing.*;45import javax.swing.event.*;4647public class KeyEventForBadFocusOwnerTest {48final static String ITEM_ONE_TEXT = "one";49final static String ITEM_TWO_TEXT = "two";5051volatile static boolean itemOneSelected = false;52volatile static boolean itemTwoSelected = false;53volatile static boolean unexpectedItemSelected = false;5455static Robot robot;56static JFrame frame;5758public static void main(String[] args) throws Exception {59try {60SwingUtilities.invokeAndWait(new Runnable() {61public void run() {62frame = new JFrame("TEST");63JMenuBar mb = new JMenuBar();64JMenu one = new JMenu(ITEM_ONE_TEXT);65JMenu two = new JMenu(ITEM_TWO_TEXT);6667mb.add(one);68mb.add(two);6970ActionListener al = new ActionListener() {71public void actionPerformed(ActionEvent ae) {72String itemText = ((JMenuItem)ae.getSource()).getText();73System.out.println("--> " + itemText);74unexpectedItemSelected = true;75}76};77one.setMnemonic(KeyEvent.VK_O);78JMenuItem item = new JMenuItem("one 1");79item.setMnemonic(KeyEvent.VK_O);80item.addActionListener(al);81one.add(item);82one.add("two");83one.add("three");8485two.setMnemonic(KeyEvent.VK_T);86item = new JMenuItem("two 2");87item.setMnemonic(KeyEvent.VK_T);88item.addActionListener(al);89two.add(item);90two.add("three");91two.add("four");9293PopupMenuListener popupMenuListener = new PopupMenuListener() {94public void popupMenuWillBecomeVisible(PopupMenuEvent e) {95System.out.print(e);96System.out.print(e.getSource());97String itemText = ((JPopupMenu)e.getSource()).getName();98System.out.println("Menu " + itemText + "is opened.");99switch(itemText) {100case ITEM_ONE_TEXT:101itemOneSelected = true;102break;103case ITEM_TWO_TEXT:104itemTwoSelected = true;105break;106}107}108109public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}110public void popupMenuCanceled(PopupMenuEvent e) {}111};112one.getPopupMenu().setName(ITEM_ONE_TEXT);113two.getPopupMenu().setName(ITEM_TWO_TEXT);114one.getPopupMenu().addPopupMenuListener(popupMenuListener);115two.getPopupMenu().addPopupMenuListener(popupMenuListener);116frame.setJMenuBar(mb);117frame.setSize(100,100);118frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);119frame.setLocationRelativeTo(null);120frame.pack();121frame.setVisible(true);122}123});124125126robot = new Robot();127robot.setAutoDelay(100);128robot.waitForIdle();129robot.delay(1000);130131Util.hitMnemonics(robot, KeyEvent.VK_O);132Util.hitMnemonics(robot, KeyEvent.VK_T);133134robot.waitForIdle();135Thread.sleep(1000); // workaround for MacOS136137if (unexpectedItemSelected) {138throw new Exception("Test failed. KeyEvent dispatched to old focus owner. ");139}140if (!itemOneSelected || !itemTwoSelected) {141throw new Exception("Not all expected events were received");142}143} finally {144SwingUtilities.invokeAndWait(() -> {145if (frame != null) {146frame.dispose();147}148});149}150}151}152153154