1 /* 2 * Copyright (c) 2007, 2014, 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 import javax.swing.*; 26 import java.awt.*; 27 import java.awt.event.*; 28 29 import static sun.awt.OSInfo.*; 30 import static jdk.testlibrary.Asserts.assertTrue; 31 32 /* 33 * @test 34 * @summary Make sure that modifier key mask is set when robot press 35 * some key with one or more modifiers. 36 * 37 * @library ../../../../lib/testlibrary/ 38 * @build ExtendedRobot 39 * @run main ModifierRobotKeyTest 40 */ 41 42 public class ModifierRobotKeyTest extends KeyAdapter { 43 44 private boolean focusGained = false; 45 private boolean startTest = false; 46 private ExtendedRobot robot; 47 private Frame frame; 48 private Canvas canvas; 49 50 private boolean tempPress = false; 51 52 private int[] textKeys, modifierKeys, inputMasks; 53 private boolean[] modifierStatus, textStatus; 54 55 public static void main(String[] args) throws Exception { 56 ModifierRobotKeyTest test = new ModifierRobotKeyTest(); 57 test.doTest(); 58 } 59 60 public ModifierRobotKeyTest() throws Exception { 61 modifierKeys = new int[3]; 62 modifierKeys[0] = KeyEvent.VK_SHIFT; 63 modifierKeys[1] = KeyEvent.VK_CONTROL; 64 modifierKeys[2] = KeyEvent.VK_ALT; 65 66 inputMasks = new int[3]; 67 inputMasks[0] = InputEvent.SHIFT_MASK; 68 inputMasks[1] = InputEvent.CTRL_MASK; 69 inputMasks[2] = InputEvent.ALT_MASK; 70 71 modifierStatus = new boolean[modifierKeys.length]; 72 73 textKeys = new int[2]; 74 textKeys[0] = KeyEvent.VK_A; 75 76 switch (getOSType()) { 77 case SOLARIS: 78 textKeys[1] = KeyEvent.VK_S; 79 break; 80 case MACOSX: 81 textKeys[1] = KeyEvent.VK_K; 82 break; 83 default: 84 textKeys[1] = KeyEvent.VK_I; 85 break; 86 } 87 88 textStatus = new boolean[textKeys.length]; 89 90 SwingUtilities.invokeAndWait( () -> { initializeGUI(); }); 91 } 92 93 public void keyPressed(KeyEvent event) { 94 95 tempPress = true; 96 97 if (! startTest) { 98 return; 99 } 100 for (int x = 0; x < inputMasks.length; x++) { 101 if ((event.getModifiers() & inputMasks[x]) != 0) { 102 System.out.println("Modifier set: " + event.getKeyModifiersText(inputMasks[x])); 103 modifierStatus[x] = true; 104 } 105 } 106 for (int x = 0; x < textKeys.length; x++) { 107 if (event.getKeyCode() == textKeys[x]) { 108 System.out.println("Text set: " + event.getKeyText(textKeys[x])); 109 textStatus[x] = true; 110 } 111 } 112 } 113 114 private void initializeGUI() { 115 frame = new Frame("Test frame"); 116 canvas = new Canvas(); 117 canvas.addFocusListener(new FocusAdapter() { 118 public void focusGained(FocusEvent event) { focusGained = true; } 119 }); 120 canvas.addKeyListener(this); 121 frame.setLayout(new BorderLayout()); 122 frame.add(canvas); 123 frame.setSize(200, 200); 124 frame.setVisible(true); 125 } 126 127 public void doTest() throws Exception { 128 robot = new ExtendedRobot(); 129 130 robot.mouseMove((int) frame.getLocationOnScreen().getX() + frame.getSize().width / 2, 131 (int) frame.getLocationOnScreen().getY() + frame.getSize().height / 2); 132 robot.click(MouseEvent.BUTTON1_MASK); 133 robot.waitForIdle(); 134 135 assertTrue(focusGained, "FAIL: Canvas failed to gain focus!"); 136 137 for (int i = 0; i < modifierKeys.length; i++) { 138 for (int j = 0; j < textKeys.length; j++) { 139 tempPress = false; 140 robot.keyPress(modifierKeys[i]); 141 robot.waitForIdle(); 142 143 assertTrue(tempPress, "FAIL: keyPressed not triggered for i=" + i); 144 145 resetStatus(); 146 startTest = true; 147 robot.keyPress(textKeys[j]); 148 robot.waitForIdle(); 149 150 assertTrue(modifierStatus[i] && textStatus[j], 151 "FAIL: KeyEvent not proper!"+ 152 "Key checked: i=" + i + "; j=" + j+ 153 "ModifierStatus = " + modifierStatus[i]+ 154 "TextStatus = " + textStatus[j]); 155 startTest = false; 156 robot.keyRelease(textKeys[j]); 157 robot.waitForIdle(); 158 robot.keyRelease(modifierKeys[i]); 159 robot.waitForIdle(); 160 } 161 } 162 163 for (int i = 0; i < modifierKeys.length; i++) { 164 for (int j = i + 1; j < modifierKeys.length; j++) { 165 for (int k = 0; k < textKeys.length; k++) { 166 tempPress = false; 167 robot.keyPress(modifierKeys[i]); 168 robot.waitForIdle(); 169 assertTrue(tempPress, "FAIL: MultiKeyTest: keyPressed not triggered for i=" + i); 170 171 tempPress = false; 172 robot.keyPress(modifierKeys[j]); 173 robot.waitForIdle(); 174 assertTrue(tempPress, "FAIL: MultiKeyTest keyPressed not triggered for j=" + j); 175 176 resetStatus(); 177 startTest = true; 178 robot.keyPress(textKeys[k]); 179 robot.waitForIdle(); 180 181 assertTrue(modifierStatus[i] && modifierStatus[j] && textStatus[k], 182 "FAIL: KeyEvent not proper!"+ 183 "Key checked: i=" + i + "; j=" + j + "; k=" + k+ 184 "Modifier1Status = " + modifierStatus[i]+ 185 "Modifier2Status = " + modifierStatus[j]+ 186 "TextStatus = " + textStatus[k]); 187 188 startTest = false; 189 robot.keyRelease(textKeys[k]); 190 robot.waitForIdle(); 191 robot.keyRelease(modifierKeys[j]); 192 robot.waitForIdle(); 193 robot.keyRelease(modifierKeys[i]); 194 robot.waitForIdle(); 195 } 196 } 197 } 198 } 199 200 private void resetStatus() { 201 for (int i = 0; i < modifierStatus.length; i++) { 202 modifierStatus[i] = false; 203 } 204 for (int i = 0; i < textStatus.length; i++) { 205 textStatus[i] = false; 206 } 207 } 208 209 }