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