Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/KeyEventForBadFocusOwnerTest/KeyEventForBadFocusOwnerTest.java
47626 views
/*1* Copyright (c) 2013, 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@bug 447662925@library ../../../../javax/swing/regtesthelpers26@build Util27@summary KeyEvents dispatched to old focus owner that is no longer showing28@author [email protected]: area=awt.focus29@run main KeyEventForBadFocusOwnerTest30*/3132/**33* KeyEventForBadFocusOwnerTest.java34*35* summary: KeyEvents dispatched to old focus owner that is no longer showing36*/373839import java.awt.Robot;4041import java.awt.event.*;4243import javax.swing.*;44import javax.swing.event.*;4546public class KeyEventForBadFocusOwnerTest {47final static String ITEM_ONE_TEXT = "one";48final static String ITEM_TWO_TEXT = "two";4950volatile static boolean itemOneSelected = false;51volatile static boolean itemTwoSelected = false;52volatile static boolean unexpectedItemSelected = false;5354static Robot robot;5556public static void main(String[] args) throws Exception {57SwingUtilities.invokeAndWait(new Runnable() {58public void run() {59JFrame frame = new JFrame("TEST");60JMenuBar mb = new JMenuBar();61JMenu one = new JMenu(ITEM_ONE_TEXT);62JMenu two = new JMenu(ITEM_TWO_TEXT);6364mb.add(one);65mb.add(two);6667ActionListener al = new ActionListener() {68public void actionPerformed(ActionEvent ae) {69String itemText = ((JMenuItem)ae.getSource()).getText();70System.out.println("--> " + itemText);71unexpectedItemSelected = true;72}73};74one.setMnemonic(KeyEvent.VK_O);75JMenuItem item = new JMenuItem("one 1");76item.setMnemonic(KeyEvent.VK_O);77item.addActionListener(al);78one.add(item);79one.add("two");80one.add("three");8182two.setMnemonic(KeyEvent.VK_T);83item = new JMenuItem("two 2");84item.setMnemonic(KeyEvent.VK_T);85item.addActionListener(al);86two.add(item);87two.add("three");88two.add("four");8990PopupMenuListener popupMenuListener = new PopupMenuListener() {91public void popupMenuWillBecomeVisible(PopupMenuEvent e) {92System.out.print(e);93System.out.print(e.getSource());94String itemText = ((JPopupMenu)e.getSource()).getName();95System.out.println("Menu " + itemText + "is opened.");96switch(itemText) {97case ITEM_ONE_TEXT:98itemOneSelected = true;99break;100case ITEM_TWO_TEXT:101itemTwoSelected = true;102break;103}104}105106public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}107public void popupMenuCanceled(PopupMenuEvent e) {}108};109one.getPopupMenu().setName(ITEM_ONE_TEXT);110two.getPopupMenu().setName(ITEM_TWO_TEXT);111one.getPopupMenu().addPopupMenuListener(popupMenuListener);112two.getPopupMenu().addPopupMenuListener(popupMenuListener);113frame.setJMenuBar(mb);114frame.setSize(100,100);115frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);116frame.pack();117frame.setVisible(true);118}119});120121122robot = new Robot();123robot.setAutoDelay(100);124robot.waitForIdle();125126Util.hitMnemonics(robot, KeyEvent.VK_O);127Util.hitMnemonics(robot, KeyEvent.VK_T);128129robot.waitForIdle();130Thread.sleep(1000); // workaround for MacOS131132if (unexpectedItemSelected) {133throw new Exception("Test failed. KeyEvent dispatched to old focus owner. ");134}135if (!itemOneSelected || !itemTwoSelected) {136throw new Exception("Not all expected events were received");137}138}139}140141142