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