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