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