1 /*
   2  * Copyright (c) 2012, 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 /*
  25  * @test
  26  * @bug 8020209
  27  * @summary [macosx] Mac OS X key event confusion for "COMMAND PLUS"
  28  * @author leonid.romanov@oracle.com
  29  * @run main bug8020209
  30  */
  31 
  32 import sun.awt.*;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 
  36 public class bug8020209 {
  37     static volatile int listenerCallCounter = 0;
  38 
  39     static AWTKeyStroke keyStrokes[] = {
  40         AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_DECIMAL,  InputEvent.META_MASK),
  41         AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_EQUALS,   InputEvent.META_MASK),
  42         AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ESCAPE,   InputEvent.CTRL_MASK),
  43     };
  44 
  45     public static void main(String[] args) throws Exception {
  46         if (sun.awt.OSInfo.getOSType() != sun.awt.OSInfo.OSType.MACOSX) {
  47             System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
  48             return;
  49         }
  50 
  51         System.setProperty("apple.laf.useScreenMenuBar", "true");
  52 
  53         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  54         Robot robot = new Robot();
  55         robot.setAutoDelay(50);
  56 
  57         createAndShowGUI();
  58         toolkit.realSync();
  59 
  60         for (int i = 0; i < keyStrokes.length; ++i) {
  61             AWTKeyStroke ks = keyStrokes[i];
  62 
  63             int modKeyCode = getModKeyCode(ks.getModifiers());
  64             robot.keyPress(modKeyCode);
  65 
  66             robot.keyPress(ks.getKeyCode());
  67             robot.keyRelease(ks.getKeyCode());
  68 
  69             robot.keyRelease(modKeyCode);
  70 
  71             toolkit.realSync();
  72 
  73             if (listenerCallCounter != 4) {
  74                 throw new Exception("Test failed: KeyListener for '" + ks.toString() +
  75                         "' called " + listenerCallCounter + " times instead of 4!");
  76             }
  77 
  78             listenerCallCounter = 0;
  79         }
  80 
  81     }
  82 
  83     private static void createAndShowGUI() {
  84         Frame frame = new Frame("Test");
  85         frame.addKeyListener(new KeyAdapter() {
  86             @Override
  87             public void keyPressed(KeyEvent e) {
  88                 listenerCallCounter++;
  89             }
  90 
  91             @Override
  92             public void keyReleased(KeyEvent e) {
  93                 listenerCallCounter++;
  94             }
  95         });
  96 
  97         frame.pack();
  98         frame.setVisible(true);
  99     }
 100 
 101     private static int getModKeyCode(int mod) {
 102         if ((mod & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
 103             return KeyEvent.VK_SHIFT;
 104         }
 105 
 106         if ((mod & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
 107             return KeyEvent.VK_CONTROL;
 108         }
 109 
 110         if ((mod & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
 111             return KeyEvent.VK_ALT;
 112         }
 113 
 114         if ((mod & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
 115             return KeyEvent.VK_META;
 116         }
 117 
 118         return 0;
 119     }
 120 }