1 /* 2 * Copyright (c) 2007, 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 import java.awt.BorderLayout; 25 import java.awt.Canvas; 26 import java.awt.EventQueue; 27 import java.awt.Frame; 28 import java.awt.event.FocusAdapter; 29 import java.awt.event.FocusEvent; 30 import java.awt.event.InputEvent; 31 import java.awt.event.KeyAdapter; 32 import java.awt.event.KeyEvent; 33 import java.awt.event.MouseEvent; 34 35 import static jdk.testlibrary.Asserts.assertNull; 36 import static jdk.testlibrary.Asserts.assertTrue; 37 38 /* 39 * @test 40 * @bug 8155742 41 * @key headful 42 * @summary Make sure that modifier key mask is set when robot press 43 * some key with one or more modifiers. 44 * @library ../../../../lib/testlibrary/ 45 * @build ExtendedRobot 46 * @run main ModifierRobotKeyTest 47 */ 48 49 public class ModifierRobotKeyTest extends KeyAdapter { 50 51 private boolean focusGained = false; 52 private boolean startTest = false; 53 private ExtendedRobot robot; 54 private Frame frame; 55 private Canvas canvas; 56 57 private volatile boolean tempPress = false; 58 59 private int[] textKeys, modifierKeys, inputMasks; 60 private boolean[] modifierStatus, textStatus; 61 62 private final static int WAIT_DELAY = 5000; 63 private final Object lock = new Object(); 64 65 public static void main(String[] args) throws Exception { 66 ModifierRobotKeyTest test = new ModifierRobotKeyTest(); 67 test.doTest(); 68 } 69 70 public ModifierRobotKeyTest() throws Exception { 71 modifierKeys = new int[4]; 72 modifierKeys[0] = KeyEvent.VK_SHIFT; 73 modifierKeys[1] = KeyEvent.VK_CONTROL; 74 modifierKeys[2] = KeyEvent.VK_ALT; 75 modifierKeys[3] = KeyEvent.VK_ALT_GRAPH; 76 77 inputMasks = new int[4]; 78 inputMasks[0] = InputEvent.SHIFT_MASK; 79 inputMasks[1] = InputEvent.CTRL_MASK; 80 inputMasks[2] = InputEvent.ALT_MASK; 81 inputMasks[3] = InputEvent.ALT_GRAPH_MASK; 82 83 modifierStatus = new boolean[modifierKeys.length]; 84 85 textKeys = new int[2]; 86 textKeys[0] = KeyEvent.VK_A; 87 88 String os = System.getProperty("os.name").toLowerCase(); 89 90 if (os.contains("solaris") || os.contains("sunos")) 91 textKeys[1] = KeyEvent.VK_S; 92 else if (os.contains("os x")) 93 textKeys[1] = KeyEvent.VK_K; 94 else 95 textKeys[1] = KeyEvent.VK_I; 96 97 textStatus = new boolean[textKeys.length]; 98 99 EventQueue.invokeAndWait( () -> { initializeGUI(); }); 100 } 101 102 public void keyPressed(KeyEvent event) { 103 synchronized (lock) { 104 tempPress = true; 105 lock.notifyAll(); 106 107 if (! startTest) { 108 return; 109 } 110 for (int x = 0; x < inputMasks.length; x++) { 111 if ((event.getModifiers() & inputMasks[x]) != 0) { 112 System.out.println("Modifier set: " + 113 event.getKeyModifiersText(inputMasks[x])); 114 modifierStatus[x] = true; 115 } 116 } 117 for (int x = 0; x < textKeys.length; x++) { 118 if (event.getKeyCode() == textKeys[x]) { 119 System.out.println("Text set: " + 120 event.getKeyText(textKeys[x])); 121 textStatus[x] = true; 122 } 123 } 124 } 125 } 126 127 private void initializeGUI() { 128 frame = new Frame("Test frame"); 129 canvas = new Canvas(); 130 canvas.addFocusListener(new FocusAdapter() { 131 public void focusGained(FocusEvent event) { focusGained = true; } 132 }); 133 canvas.addKeyListener(this); 134 frame.setLayout(new BorderLayout()); 135 frame.add(canvas); 136 frame.setBounds(200, 200, 200, 200); 137 frame.setVisible(true); 138 } 139 140 public void doTest() throws Exception { 141 robot = new ExtendedRobot(); 142 robot.setAutoDelay(20); 143 robot.waitForIdle(); 144 145 robot.mouseMove((int) frame.getLocationOnScreen().getX() + 146 frame.getSize().width / 2, 147 (int) frame.getLocationOnScreen().getY() + 148 frame.getSize().height / 2); 149 robot.click(MouseEvent.BUTTON1_MASK); 150 robot.waitForIdle(); 151 assertTrue(focusGained, "FAIL: Canvas gained focus!"); 152 153 String error = null; 154 exit1: 155 for (int i = 0; i < modifierKeys.length; i++) { 156 for (int j = 0; j < textKeys.length; j++) { 157 if (error != null) { 158 break exit1; 159 } 160 robot.waitForIdle(100); 161 synchronized (lock) { 162 tempPress = false; 163 robot.keyPress(modifierKeys[i]); 164 lock.wait(WAIT_DELAY); 165 } 166 if (!tempPress) { 167 error ="FAIL: keyPressed triggered for i=" + i; 168 } 169 170 synchronized (lock) { 171 resetStatus(); 172 startTest = true; 173 robot.keyPress(textKeys[j]); 174 lock.wait(WAIT_DELAY); 175 } 176 177 if (!(modifierStatus[i] && textStatus[j])) { 178 error = "FAIL: KeyEvent not proper!"+ 179 "Key checked: i=" + i + "; j=" + j+ 180 "ModifierStatus = " + modifierStatus[i]+ 181 "TextStatus = " + textStatus[j]; 182 } 183 184 startTest = false; 185 robot.keyRelease(textKeys[j]); 186 robot.keyRelease(modifierKeys[i]); 187 } 188 } 189 190 exit2: 191 for (int i = 0; i < modifierKeys.length; i++) { 192 for (int j = i + 1; j < modifierKeys.length; j++) { 193 for (int k = 0; k < textKeys.length; k++) { 194 if (error != null) { 195 break exit2; 196 } 197 robot.waitForIdle(100); 198 synchronized (lock) { 199 tempPress = false; 200 robot.keyPress(modifierKeys[i]); 201 lock.wait(WAIT_DELAY); 202 } 203 204 if (!tempPress) { 205 error = "FAIL: MultiKeyTest: keyPressed " + 206 "triggered for i=" + i; 207 } 208 209 synchronized (lock) { 210 tempPress = false; 211 robot.keyPress(modifierKeys[j]); 212 lock.wait(WAIT_DELAY); 213 } 214 if (!tempPress) { 215 error = "FAIL: MultiKeyTest keyPressed " + 216 "triggered for j=" + j; 217 }; 218 219 synchronized (lock) { 220 resetStatus(); 221 startTest = true; 222 robot.keyPress(textKeys[k]); 223 lock.wait(WAIT_DELAY); 224 } 225 if (!(modifierStatus[i] && modifierStatus[j] 226 && textStatus[k])) 227 { 228 error = "FAIL: KeyEvent not proper!" + 229 "Key checked: i=" + i + "; j=" + j + "; k=" + k + 230 "Modifier1Status = " + modifierStatus[i] + 231 "Modifier2Status = " + modifierStatus[j] + 232 "TextStatus = " + textStatus[k]; 233 } 234 235 startTest = false; 236 robot.keyRelease(textKeys[k]); 237 robot.keyRelease(modifierKeys[j]); 238 robot.keyRelease(modifierKeys[i]); 239 } 240 } 241 } 242 243 frame.dispose(); 244 assertNull(error, error); 245 } 246 247 private void resetStatus() { 248 for (int i = 0; i < modifierStatus.length; i++) { 249 modifierStatus[i] = false; 250 } 251 for (int i = 0; i < textStatus.length; i++) { 252 textStatus[i] = false; 253 } 254 } 255 256 }