1 /*
   2  * Copyright (c) 2019, 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 8218917
  28  * @summary Tests whether sending an ALT_GRAPH key once, will result in the
  29  * system reporting only ALT_GRAPH even if an ALT was sent and vice versa.
  30  * @run main AltKeyBug
  31  */
  32 import javax.swing.JTextField;
  33 import javax.swing.JFrame;
  34 import javax.swing.SwingUtilities;
  35 
  36 import java.awt.Robot;
  37 import java.awt.event.KeyListener;
  38 import java.awt.event.KeyEvent;
  39 
  40 public class AltKeyBug {
  41     private static JFrame f;
  42     private static boolean rightAltPressed = false;
  43     private static boolean throwException = false;
  44     private static String errorString;
  45     public static void main(String[] args) throws Exception {
  46         try {
  47             Robot robot = new Robot();
  48             robot.setAutoDelay(50);
  49 
  50             SwingUtilities.invokeAndWait(() -> {
  51                 JTextField comp = new JTextField();
  52                 comp.addKeyListener(new KeyListener() {
  53                     @Override public void keyTyped(KeyEvent e) {}
  54                     @Override public void keyPressed(KeyEvent e) {
  55                         System.out.println("ModEx : " +e.getModifiersEx());
  56                         System.out.println("Mod : " +e.getModifiers());
  57                         System.out.println("ALT_DOWN : " + e.isAltDown());
  58                         System.out.println("ALT_GR_DOWN: " + e.isAltGraphDown());
  59                         System.out.println("-----------");
  60                         if (rightAltPressed && !e.isAltGraphDown()) {
  61                             throwException = true;
  62                             errorString = "Right Alt press was sent but not received back.";
  63                         } else if (!rightAltPressed && e.isAltGraphDown()) {
  64                             throwException = true;
  65                             errorString = "Left Alt press was sent, but received Right Alt";
  66                         }
  67                     }
  68                     @Override public void keyReleased(KeyEvent e) {}
  69                 });
  70                 f = new JFrame();
  71                 f.add(comp);
  72                 f.setSize(100,100);
  73                 f.setVisible(true);
  74             });
  75 
  76             for(int i = 0; i < 20; i++) {
  77                 rightAltPressed = true;
  78                 robot.keyPress(KeyEvent.VK_ALT_GRAPH);
  79                 robot.keyRelease(KeyEvent.VK_ALT_GRAPH);
  80 
  81                 robot.waitForIdle();
  82 
  83                 if (throwException) {
  84                     throw new RuntimeException(errorString);
  85                 }
  86                 rightAltPressed = false;
  87                 robot.keyPress(KeyEvent.VK_ALT);
  88                 robot.keyRelease(KeyEvent.VK_ALT);
  89 
  90                 robot.waitForIdle();
  91 
  92                 if (throwException) {
  93                     throw new RuntimeException(errorString);
  94                 }
  95             }
  96         } finally {
  97             SwingUtilities.invokeAndWait(()-> {
  98                 if (f != null)
  99                     f.dispose();
 100             });
 101         }
 102 
 103         System.out.println("Test passed.");
 104     }
 105 }