1 /*
   2  * Copyright (c) 2011, 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 import java.awt.Frame;
  25 import java.awt.Robot;
  26 import java.awt.Toolkit;
  27 import java.awt.event.KeyEvent;
  28 import java.awt.event.KeyAdapter;
  29 import sun.awt.ExtendedKeyCodes;
  30 import sun.awt.SunToolkit;
  31 
  32 /*
  33  * @test
  34  * @key headful
  35  * @bug 8007156 8025126
  36  * @summary Extended key code is not set for a key event
  37  * @author Alexandr Scherbatiy
  38  * @run main ExtendedKeyCodeTest
  39  */
  40 public class ExtendedKeyCodeTest {
  41 
  42     private static volatile boolean setExtendedKeyCode = true;
  43     private static volatile int eventsCount = 0;
  44 
  45     public static void main(String[] args) throws Exception {
  46         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  47         Robot robot = new Robot();
  48         robot.setAutoDelay(50);
  49 
  50         Frame frame = new Frame();
  51         frame.setSize(300, 300);
  52 
  53         frame.addKeyListener(new KeyAdapter() {
  54 
  55             @Override
  56             public void keyPressed(KeyEvent e) {
  57                 eventsCount++;
  58                 setExtendedKeyCode = setExtendedKeyCode && (e.getExtendedKeyCode()
  59                         == ExtendedKeyCodes.getExtendedKeyCodeForChar(e.getKeyChar()));
  60             }
  61 
  62             @Override
  63             public void keyReleased(KeyEvent e) {
  64                 eventsCount++;
  65                 setExtendedKeyCode = setExtendedKeyCode && (e.getExtendedKeyCode()
  66                         == ExtendedKeyCodes.getExtendedKeyCodeForChar(e.getKeyChar()));
  67             }
  68         });
  69 
  70         frame.setVisible(true);
  71         toolkit.realSync();
  72 
  73         robot.keyPress(KeyEvent.VK_D);
  74         robot.keyRelease(KeyEvent.VK_D);
  75         toolkit.realSync();
  76 
  77         frame.dispose();
  78 
  79         if (eventsCount != 2 || !setExtendedKeyCode) {
  80             throw new RuntimeException("Wrong extended key code");
  81         }
  82 
  83         frame = new Frame();
  84         frame.setSize(300, 300);
  85         setExtendedKeyCode = false;
  86 
  87         frame.addKeyListener(new KeyAdapter() {
  88 
  89             @Override
  90             public void keyPressed(KeyEvent e) {
  91                 setExtendedKeyCode = e.getExtendedKeyCode() == KeyEvent.VK_LEFT;
  92             }
  93         });
  94 
  95         frame.setVisible(true);
  96         toolkit.realSync();
  97 
  98         robot.keyPress(KeyEvent.VK_LEFT);
  99         robot.keyRelease(KeyEvent.VK_LEFT);
 100         toolkit.realSync();
 101         frame.dispose();
 102 
 103         if (!setExtendedKeyCode) {
 104             throw new RuntimeException("Wrong extended key code!");
 105         }
 106     }
 107 }