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 import java.awt.Toolkit; 42 43 import java.awt.event.*; 44 45 import javax.swing.*; 46 import javax.swing.event.*; 47 import sun.awt.SunToolkit; 48 49 public class KeyEventForBadFocusOwnerTest { 50 final static String ITEM_ONE_TEXT = "one"; 51 final static String ITEM_TWO_TEXT = "two"; 52 53 volatile static boolean itemOneSelected = false; 54 volatile static boolean itemTwoSelected = false; 55 volatile static boolean unexpectedItemSelected = false; 56 57 static Robot robot; 58 static SunToolkit toolkit; 59 60 public static void main(String[] args) throws Exception { 61 SwingUtilities.invokeAndWait(new Runnable() { 62 public void run() { 63 JFrame frame = new JFrame("TEST"); 64 JMenuBar mb = new JMenuBar(); 65 JMenu one = new JMenu(ITEM_ONE_TEXT); 66 JMenu two = new JMenu(ITEM_TWO_TEXT); 67 68 mb.add(one); 69 mb.add(two); 70 71 ActionListener al = new ActionListener() { 72 public void actionPerformed(ActionEvent ae) { 73 String itemText = ((JMenuItem)ae.getSource()).getText(); 74 System.out.println("--> " + itemText); 75 unexpectedItemSelected = true; 76 } 77 }; 78 one.setMnemonic(KeyEvent.VK_O); 79 JMenuItem item = new JMenuItem("one 1"); 80 item.setMnemonic(KeyEvent.VK_O); 81 item.addActionListener(al); 82 one.add(item); 83 one.add("two"); 84 one.add("three"); 85 86 two.setMnemonic(KeyEvent.VK_T); 87 item = new JMenuItem("two 2"); 88 item.setMnemonic(KeyEvent.VK_T); 89 item.addActionListener(al); 90 two.add(item); 91 two.add("three"); 92 two.add("four"); 93 94 PopupMenuListener popupMenuListener = new PopupMenuListener() { 95 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 96 System.out.print(e); 97 System.out.print(e.getSource()); 98 String itemText = ((JPopupMenu)e.getSource()).getName(); 99 System.out.println("Menu " + itemText + "is opened."); 100 switch(itemText) { 101 case ITEM_ONE_TEXT: 102 itemOneSelected = true; 103 break; 104 case ITEM_TWO_TEXT: 105 itemTwoSelected = true; 106 break; 107 } 108 } 109 110 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {} 111 public void popupMenuCanceled(PopupMenuEvent e) {} 112 }; 113 one.getPopupMenu().setName(ITEM_ONE_TEXT); 114 two.getPopupMenu().setName(ITEM_TWO_TEXT); 115 one.getPopupMenu().addPopupMenuListener(popupMenuListener); 116 two.getPopupMenu().addPopupMenuListener(popupMenuListener); 117 frame.setJMenuBar(mb); 118 frame.setSize(100,100); 119 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 120 frame.pack(); 121 frame.setVisible(true); 122 } 123 }); 124 125 toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 126 toolkit.realSync(); 127 128 robot = new Robot(); 129 robot.setAutoDelay(100); 130 131 Util.hitMnemonics(robot, KeyEvent.VK_O); 132 Util.hitMnemonics(robot, KeyEvent.VK_T); 133 134 toolkit.realSync(); 135 Thread.sleep(1000); // workaround for MacOS 136 137 if (unexpectedItemSelected) { 138 throw new Exception("Test failed. KeyEvent dispatched to old focus owner. "); 139 } 140 if (!itemOneSelected || !itemTwoSelected) { 141 throw new Exception("Not all expected events were received"); 142 } 143 } 144 }