1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* 24 @test 25 @bug 4476629 26 @library ../../../../javax/swing/regtesthelpers 27 @build Util 28 @summary KeyEvents dispatched to old focus owner that is no longer showing 29 @author son@sparc.spb.su: area=awt.focus 30 @run main KeyEventForBadFocusOwnerTest 31 */ 32 33 /** 34 * KeyEventForBadFocusOwnerTest.java 35 * 36 * summary: KeyEvents dispatched to old focus owner that is no longer showing 37 */ 38 39 40 import java.awt.Robot; 41 42 import java.awt.event.*; 43 44 import javax.swing.*; 45 import javax.swing.event.*; 46 47 public class KeyEventForBadFocusOwnerTest { 48 final static String ITEM_ONE_TEXT = "one"; 49 final static String ITEM_TWO_TEXT = "two"; 50 51 volatile static boolean itemOneSelected = false; 52 volatile static boolean itemTwoSelected = false; 53 volatile static boolean unexpectedItemSelected = false; 54 55 static Robot robot; 56 57 public static void main(String[] args) throws Exception { 58 SwingUtilities.invokeAndWait(new Runnable() { 59 public void run() { 60 JFrame frame = new JFrame("TEST"); 61 JMenuBar mb = new JMenuBar(); 62 JMenu one = new JMenu(ITEM_ONE_TEXT); 63 JMenu two = new JMenu(ITEM_TWO_TEXT); 64 65 mb.add(one); 66 mb.add(two); 67 68 ActionListener al = new ActionListener() { 69 public void actionPerformed(ActionEvent ae) { 70 String itemText = ((JMenuItem)ae.getSource()).getText(); 71 System.out.println("--> " + itemText); 72 unexpectedItemSelected = true; 73 } 74 }; 75 one.setMnemonic(KeyEvent.VK_O); 76 JMenuItem item = new JMenuItem("one 1"); 77 item.setMnemonic(KeyEvent.VK_O); 78 item.addActionListener(al); 79 one.add(item); 80 one.add("two"); 81 one.add("three"); 82 83 two.setMnemonic(KeyEvent.VK_T); 84 item = new JMenuItem("two 2"); 85 item.setMnemonic(KeyEvent.VK_T); 86 item.addActionListener(al); 87 two.add(item); 88 two.add("three"); 89 two.add("four"); 90 91 PopupMenuListener popupMenuListener = new PopupMenuListener() { 92 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 93 System.out.print(e); 94 System.out.print(e.getSource()); 95 String itemText = ((JPopupMenu)e.getSource()).getName(); 96 System.out.println("Menu " + itemText + "is opened."); 97 switch(itemText) { 98 case ITEM_ONE_TEXT: 99 itemOneSelected = true; 100 break; 101 case ITEM_TWO_TEXT: 102 itemTwoSelected = true; 103 break; 104 } 105 } 106 107 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {} 108 public void popupMenuCanceled(PopupMenuEvent e) {} 109 }; 110 one.getPopupMenu().setName(ITEM_ONE_TEXT); 111 two.getPopupMenu().setName(ITEM_TWO_TEXT); 112 one.getPopupMenu().addPopupMenuListener(popupMenuListener); 113 two.getPopupMenu().addPopupMenuListener(popupMenuListener); 114 frame.setJMenuBar(mb); 115 frame.setSize(100,100); 116 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 117 frame.pack(); 118 frame.setVisible(true); 119 } 120 }); 121 122 123 robot = new Robot(); 124 robot.setAutoDelay(100); 125 robot.waitForIdle(); 126 127 Util.hitMnemonics(robot, KeyEvent.VK_O); 128 Util.hitMnemonics(robot, KeyEvent.VK_T); 129 130 robot.waitForIdle(); 131 Thread.sleep(1000); // workaround for MacOS 132 133 if (unexpectedItemSelected) { 134 throw new Exception("Test failed. KeyEvent dispatched to old focus owner. "); 135 } 136 if (!itemOneSelected || !itemTwoSelected) { 137 throw new Exception("Not all expected events were received"); 138 } 139 } 140 }