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