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