1 /* 2 * Copyright (c) 2006, 2018, 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 /* 25 test 26 @bug 6346690 27 @summary Tests that key_typed is consumed after mnemonic key_pressed is handled for a menu item. 28 @author anton.tarasov@sun.com: area=awt-focus 29 @library /test/lib 30 @build jdk.test.lib.Platform 31 @run applet ConsumeNextMnemonicKeyTypedTest.html 32 */ 33 34 import jdk.test.lib.Platform; 35 36 import java.awt.*; 37 import javax.swing.*; 38 import java.awt.event.*; 39 import java.applet.Applet; 40 41 42 public class ConsumeNextMnemonicKeyTypedTest extends Applet { 43 Robot robot; 44 JFrame frame = new JFrame("Test Frame"); 45 JTextField text = new JTextField(); 46 JMenuBar bar = new JMenuBar(); 47 JMenu menu = new JMenu("Menu"); 48 JMenuItem item = new JMenuItem("item"); 49 50 public static void main(String[] args) { 51 ConsumeNextMnemonicKeyTypedTest app = new ConsumeNextMnemonicKeyTypedTest(); 52 app.init(); 53 app.start(); 54 } 55 56 public void init() { 57 try { 58 robot = new Robot(); 59 robot.setAutoDelay(50); 60 } catch (AWTException e) { 61 throw new RuntimeException("Error: unable to create robot", e); 62 } 63 // Create instructions for the user here, as well as set up 64 // the environment -- set the layout manager, add buttons, 65 // etc. 66 this.setLayout (new BorderLayout ()); 67 } 68 69 public void start() { 70 menu.setMnemonic('f'); 71 item.setMnemonic('i'); 72 menu.add(item); 73 bar.add(menu); 74 75 frame.add(text); 76 frame.setJMenuBar(bar); 77 frame.pack(); 78 79 frame.setLocation(800, 0); 80 frame.setVisible(true); 81 82 test(); 83 } 84 85 void test() { 86 87 robot.waitForIdle(); 88 89 if (!text.isFocusOwner()) { 90 robot.mouseMove(text.getLocationOnScreen().x + 5, text.getLocationOnScreen().y + 5); 91 robot.delay(100); 92 robot.mousePress(MouseEvent.BUTTON1_MASK); 93 robot.delay(100); 94 robot.mouseRelease(MouseEvent.BUTTON1_MASK); 95 96 int iter = 10; 97 while (!text.isFocusOwner() && iter-- > 0) { 98 robot.delay(200); 99 } 100 if (iter <= 0) { 101 System.out.println("Test: text field couldn't be focused!"); 102 return; 103 } 104 } 105 106 robot.keyPress(KeyEvent.VK_A); 107 robot.delay(100); 108 robot.keyRelease(KeyEvent.VK_A); 109 110 robot.waitForIdle(); 111 112 String charA = text.getText(); 113 System.err.println("Test: character typed with VK_A: " + charA); 114 115 robot.keyPress(KeyEvent.VK_BACK_SPACE); 116 robot.delay(100); 117 robot.keyRelease(KeyEvent.VK_BACK_SPACE); 118 119 robot.waitForIdle(); 120 121 if (Platform.isOSX()) { 122 robot.keyPress(KeyEvent.VK_CONTROL); 123 } 124 robot.keyPress(KeyEvent.VK_ALT); 125 robot.keyPress(KeyEvent.VK_F); 126 robot.delay(100); 127 robot.keyRelease(KeyEvent.VK_F); 128 robot.keyRelease(KeyEvent.VK_ALT); 129 if (Platform.isOSX()) { 130 robot.keyRelease(KeyEvent.VK_CONTROL); 131 } 132 133 robot.waitForIdle(); 134 135 String string = text.getText(); 136 137 robot.keyPress(KeyEvent.VK_I); 138 robot.delay(100); 139 robot.keyRelease(KeyEvent.VK_I); 140 141 robot.waitForIdle(); 142 143 System.out.println("Test: character typed after mnemonic key press: " + text.getText()); 144 145 if (!text.getText().equals(string)) { 146 throw new RuntimeException("Test failed!"); 147 } 148 149 robot.keyPress(KeyEvent.VK_A); 150 robot.delay(100); 151 robot.keyRelease(KeyEvent.VK_A); 152 153 robot.waitForIdle(); 154 155 System.err.println("Test: chracter typed with VK_A: " + text.getText()); 156 157 if (!charA.equals(text.getText())) { 158 throw new RuntimeException("Test failed!"); 159 } 160 161 System.out.println("Test passed."); 162 } 163 }