1 /* 2 * Copyright (c) 2001, 2018, 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 /* 26 * @test 27 * @key headful 28 * @bug 8043126 8145116 29 * @summary Check whether 30 * 1. correct extended modifiers are returned 31 * by KeyEvent.getModifiersEx() 32 * 2. InputEvent.getModifiersExText() returns 33 * correct extended modifier keys description 34 * 35 * @library /lib/client/ ../../helpers/lwcomponents/ 36 * @library /test/lib 37 * @build LWComponent 38 * @build LWButton 39 * @build LWList 40 * @build ExtendedRobot 41 * @run main/timeout=600 ExtendedModifiersTest 42 */ 43 import java.awt.Button; 44 import java.awt.Color; 45 import java.awt.Component; 46 import java.awt.EventQueue; 47 import java.awt.Frame; 48 import java.awt.GridLayout; 49 import java.awt.List; 50 import java.awt.Point; 51 import java.awt.TextArea; 52 import java.awt.TextField; 53 import java.awt.event.InputEvent; 54 import java.awt.event.KeyEvent; 55 import java.awt.event.KeyListener; 56 import java.util.ArrayList; 57 58 import static jdk.test.lib.Asserts.*; 59 import test.java.awt.event.helpers.lwcomponents.LWButton; 60 import test.java.awt.event.helpers.lwcomponents.LWList; 61 62 public class ExtendedModifiersTest implements KeyListener { 63 64 Frame frame; 65 Button button; 66 LWButton buttonLW; 67 TextField textField; 68 TextArea textArea; 69 List list; 70 LWList listLW; 71 72 private final ExtendedRobot robot; 73 private static final int WAIT_DELAY = 5000; 74 private static final int KEY_DELAY = 500; 75 private final Object lock; 76 77 private boolean keyPressedFlag; 78 private int modifiersEx = 0; 79 private String exText = ""; 80 81 @Override 82 public void keyTyped(KeyEvent e) { 83 } 84 85 @Override 86 public void keyPressed(KeyEvent e) { 87 88 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { 89 return; 90 } 91 modifiersEx = e.getModifiersEx(); 92 exText = InputEvent.getModifiersExText(modifiersEx); 93 keyPressedFlag = true; 94 95 synchronized (lock) { 96 lock.notifyAll(); 97 } 98 } 99 100 @Override 101 public void keyReleased(KeyEvent e) { 102 } 103 104 public void createGUI() { 105 106 frame = new Frame(); 107 frame.setTitle("ExtendedModifiersTest"); 108 frame.setLayout(new GridLayout(1, 6)); 109 110 button = new Button(); 111 button.addKeyListener(this); 112 frame.add(button); 113 114 buttonLW = new LWButton(); 115 buttonLW.addKeyListener(this); 116 frame.add(buttonLW); 117 118 textField = new TextField(5); 119 textField.addKeyListener(this); 120 frame.add(textField); 121 122 textArea = new TextArea(5, 5); 123 textArea.addKeyListener(this); 124 frame.add(textArea); 125 126 list = new List(); 127 for (int i = 1; i <= 5; ++i) { 128 list.add("item " + i); 129 } 130 list.addKeyListener(this); 131 frame.add(list); 132 133 listLW = new LWList(); 134 for (int i = 1; i <= 5; ++i) { 135 listLW.add("item " + i); 136 } 137 listLW.addKeyListener(this); 138 frame.add(listLW); 139 140 frame.setBackground(Color.gray); 141 frame.setSize(500, 100); 142 frame.setVisible(true); 143 frame.toFront(); 144 } 145 146 public ExtendedModifiersTest() throws Exception { 147 lock = new Object(); 148 robot = new ExtendedRobot(); 149 EventQueue.invokeAndWait(this::createGUI); 150 } 151 152 private void runScenario(int keys[], int expectedMask) { 153 if (keys.length < 1) { 154 return; 155 } 156 157 for (int k = 0; k < keys.length; ++k) { 158 159 keyPressedFlag = false; 160 robot.keyPress(keys[k]); 161 robot.delay(KEY_DELAY); 162 163 if (!keyPressedFlag) { 164 synchronized (lock) { 165 try { 166 lock.wait(WAIT_DELAY); 167 } catch (InterruptedException ex) { 168 ex.printStackTrace(); 169 } 170 } 171 } 172 173 if (!keyPressedFlag) { 174 robot.keyRelease(keys[k]); 175 robot.delay(KEY_DELAY); 176 assertTrue(false, "key press event was not received"); 177 } 178 } 179 180 int modEx = modifiersEx & expectedMask; 181 182 for (int k = keys.length - 1; k >= 0; --k) { 183 robot.keyRelease(keys[k]); 184 robot.delay(KEY_DELAY); 185 } 186 187 assertEQ(expectedMask, modEx, "invalid extended modifiers"); 188 189 for (int k = 0; k < keys.length; ++k) { 190 String keyText = KeyEvent.getKeyText(keys[k]).toLowerCase(); 191 assertTrue(exText.toLowerCase().contains(keyText), 192 "invalid extended modifier keys description"); 193 } 194 195 System.out.println(exText + " : passed"); 196 197 robot.type(KeyEvent.VK_ESCAPE); 198 robot.waitForIdle(); 199 } 200 201 private void doTest() throws Exception { 202 203 ArrayList<Component> components = new ArrayList(); 204 components.add(button); 205 components.add(buttonLW); 206 components.add(textField); 207 components.add(textArea); 208 components.add(list); 209 components.add(listLW); 210 211 String OS = System.getProperty("os.name").toLowerCase(); 212 System.out.println(OS); 213 214 for (Component c : components) { 215 216 String className = c.getClass().getName(); 217 System.out.println("component class : " + className); 218 219 Point origin = c.getLocationOnScreen(); 220 int xc = origin.x + c.getWidth() / 2; 221 int yc = origin.y + c.getHeight() / 2; 222 Point center = new Point(xc, yc); 223 224 robot.waitForIdle(); 225 robot.glide(origin, center); 226 robot.click(); 227 robot.waitForIdle(); 228 229 // 1. shift + control 230 runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL}, 231 InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK); 232 233 // 2. alt + shift + control 234 runScenario(new int[]{KeyEvent.VK_ALT, KeyEvent.VK_SHIFT, 235 KeyEvent.VK_CONTROL}, InputEvent.ALT_DOWN_MASK 236 | InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK); 237 238 // 3. shift 239 runScenario(new int[]{KeyEvent.VK_SHIFT}, 240 InputEvent.SHIFT_DOWN_MASK); 241 242 // 4. alt + control 243 runScenario(new int[]{KeyEvent.VK_ALT, KeyEvent.VK_CONTROL}, 244 InputEvent.ALT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK); 245 246 // 5. shift + alt 247 runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_ALT}, 248 InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK); 249 250 if (OS.contains("os x")) { 251 // 6. meta 252 runScenario(new int[]{KeyEvent.VK_META}, 253 InputEvent.META_DOWN_MASK); 254 255 // 7. shift + ctrl + alt + meta 256 runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL, 257 KeyEvent.VK_ALT, KeyEvent.VK_META}, 258 InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK 259 | InputEvent.ALT_DOWN_MASK | InputEvent.META_DOWN_MASK); 260 261 // 8. meta + shift + ctrl 262 runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_SHIFT, 263 KeyEvent.VK_CONTROL}, InputEvent.META_DOWN_MASK 264 | InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK); 265 266 // 9. meta + shift + alt 267 runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_SHIFT, 268 KeyEvent.VK_ALT}, InputEvent.META_DOWN_MASK 269 | InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK); 270 271 // 10. meta + ctrl + alt 272 runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_CONTROL, 273 KeyEvent.VK_ALT}, InputEvent.META_DOWN_MASK 274 | InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK); 275 } 276 } 277 278 robot.waitForIdle(); 279 frame.dispose(); 280 } 281 282 public static void main(String[] args) throws Exception { 283 ExtendedModifiersTest test = new ExtendedModifiersTest(); 284 test.doTest(); 285 } 286 }