1 /*
   2  * Copyright (c) 2012, 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 7199180
  27  * @summary [macosx] Dead keys handling for input methods
  28  * @author alexandr.scherbatiy area=awt.event
  29  * @library ../../../../../lib/testlibrary
  30  * @build jdk.testlibrary.OSInfo
  31  * @run main DeadKeyMacOSXInputText
  32  */
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.awt.event.KeyEvent;
  36 import javax.swing.JTextField;
  37 
  38 import jdk.testlibrary.OSInfo;
  39 
  40 public class DeadKeyMacOSXInputText {
  41 
  42     private static volatile int state = 0;
  43 
  44     public static void main(String[] args) throws Exception {
  45 
  46         if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
  47             return;
  48         }
  49 
  50         Robot robot = new Robot();
  51         robot.setAutoDelay(50);
  52 
  53         createAndShowGUI(robot);
  54 
  55         // Pressed keys: Alt + E + A
  56         // Results:  ALT + VK_DEAD_ACUTE + a with accute accent
  57         robot.keyPress(KeyEvent.VK_ALT);
  58         robot.keyPress(KeyEvent.VK_E);
  59         robot.keyRelease(KeyEvent.VK_E);
  60         robot.keyRelease(KeyEvent.VK_ALT);
  61 
  62         robot.keyPress(KeyEvent.VK_A);
  63         robot.keyRelease(KeyEvent.VK_A);
  64         robot.waitForIdle();
  65 
  66         if (state != 3) {
  67             throw new RuntimeException("Wrong number of key events.");
  68         }
  69     }
  70 
  71     static void createAndShowGUI(Robot robot) {
  72         Frame frame = new Frame();
  73         frame.setSize(300, 300);
  74         Panel panel = new Panel(new BorderLayout());
  75         JTextField textField = new JTextField();
  76         textField.addKeyListener(new DeadKeyListener());
  77         panel.add(textField, BorderLayout.CENTER);
  78         frame.add(panel);
  79         frame.setVisible(true);
  80         robot.waitForIdle();
  81 
  82         textField.requestFocusInWindow();
  83         robot.waitForIdle();
  84 
  85     }
  86 
  87     static class DeadKeyListener extends KeyAdapter {
  88 
  89         @Override
  90         public void keyPressed(KeyEvent e) {
  91             int keyCode = e.getKeyCode();
  92             char keyChar = e.getKeyChar();
  93 
  94             switch (state) {
  95                 case 0:
  96                     if (keyCode != KeyEvent.VK_ALT) {
  97                         throw new RuntimeException("Alt is not pressed.");
  98                     }
  99                     state++;
 100                     break;
 101                 case 1:
 102                     if (keyCode != KeyEvent.VK_DEAD_ACUTE) {
 103                         throw new RuntimeException("Dead ACUTE is not pressed.");
 104                     }
 105                     if (keyChar != 0xB4) {
 106                         throw new RuntimeException("Pressed char is not dead acute.");
 107                     }
 108                     state++;
 109                     break;
 110             }
 111         }
 112 
 113         @Override
 114         public void keyTyped(KeyEvent e) {
 115             int keyCode = e.getKeyCode();
 116             char keyChar = e.getKeyChar();
 117 
 118             if (state == 2) {
 119                 if (keyCode != 0) {
 120                     throw new RuntimeException("Key code should be undefined.");
 121                 }
 122                 if (keyChar != 0xE1) {
 123                     throw new RuntimeException("A char does not have ACCUTE accent");
 124                 }
 125                 state++;
 126             } else {
 127                 throw new RuntimeException("Wron number of keyTyped events.");
 128             }
 129         }
 130     }
 131 }