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 * @key headful 28 * @bug 8043126 29 * @summary Check whether KeyEvent.getModifiers() returns correct modifiers 30 * when Ctrl, Alt or Shift keys are pressed. 31 * 32 * @library ../../../../../lib/testlibrary/ ../../helpers/lwcomponents/ 33 * @build LWComponent 34 * @build LWButton 35 * @build LWList 36 * @build ExtendedRobot 37 * @run main/timeout=600 KeyMaskTest 38 */ 39 40 41 import java.awt.*; 42 import java.awt.event.InputEvent; 43 44 import java.awt.event.KeyAdapter; 45 import java.awt.event.KeyEvent; 46 47 import java.util.ArrayList; 48 49 import static jdk.testlibrary.Asserts.*; 50 51 import test.java.awt.event.helpers.lwcomponents.LWButton; 52 import test.java.awt.event.helpers.lwcomponents.LWList; 53 54 55 56 public class KeyMaskTest extends KeyAdapter { 57 58 Frame frame; 59 60 Button button; 61 LWButton buttonLW; 62 TextField textField; 63 TextArea textArea; 64 List list; 65 LWList listLW; 66 67 int buttonPressedNumber; 68 int buttonReleasedNumber; 69 70 ExtendedRobot robot; 71 72 private final static int robotDelay = 1500; 73 private final static int waitDelay = 3500; 74 75 final Object lock; 76 77 private boolean keyPressReceived = false; 78 private int keyCode = -1; 79 80 KeyMaskTest() throws Exception { 81 lock = new Object(); 82 robot = new ExtendedRobot(); 83 EventQueue.invokeAndWait( this::createGUI ); 84 } 85 86 public void createGUI() { 87 88 frame = new Frame(); 89 frame.setTitle("KeyMaskTest"); 90 frame.setLayout(new GridLayout(1, 6)); 91 92 button = new Button(); 93 button.addKeyListener(this); 94 frame.add(button); 95 96 buttonLW = new LWButton(); 97 buttonLW.addKeyListener(this); 98 frame.add(buttonLW); 99 100 textField = new TextField(5); 101 textField.addKeyListener(this); 102 frame.add(textField); 103 104 textArea = new TextArea(5, 5); 105 textArea.addKeyListener(this); 106 frame.add(textArea); 107 108 list = new List(); 109 for (int i = 1; i <= 5; ++i) { list.add("item " + i); } 110 list.addKeyListener(this); 111 frame.add(list); 112 113 listLW = new LWList(); 114 for (int i = 1; i <= 5; ++i) { listLW.add("item " + i); } 115 listLW.addKeyListener(this); 116 frame.add(listLW); 117 118 119 frame.setBackground(Color.gray); 120 frame.setSize(500, 100); 121 frame.setVisible(true); 122 frame.toFront(); 123 } 124 125 @Override 126 public void keyPressed(KeyEvent e) { 127 128 keyPressReceived = true; 129 130 int code = e.getKeyCode(); 131 132 assertEQ(code, keyCode, "wrong key code"); 133 134 int mask = 0; 135 136 if (code == KeyEvent.VK_SHIFT) { 137 mask = InputEvent.SHIFT_MASK; 138 } else if (code == KeyEvent.VK_CONTROL) { 139 mask = InputEvent.CTRL_MASK; 140 } else if (code == KeyEvent.VK_ALT) { 141 mask = InputEvent.ALT_MASK; 142 } else if (code == KeyEvent.VK_META) { 143 mask = InputEvent.META_MASK; 144 } 145 146 int mod = e.getModifiers() & mask; 147 assertEQ(mod, mask, "invalid key mask"); 148 149 synchronized (lock) { lock.notifyAll(); } 150 } 151 152 153 void doTest() throws Exception { 154 155 ArrayList<Component> components = new ArrayList(); 156 components.add(button); 157 components.add(buttonLW); 158 components.add(textField); 159 components.add(textArea); 160 components.add(list); 161 components.add(listLW); 162 163 int keys[]; 164 String OS = System.getProperty("os.name").toLowerCase(); 165 System.out.println(OS); 166 if (OS.contains("os x") || OS.contains("sunos")) { 167 keys = new int[] {KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_META}; 168 } else { 169 keys = new int[] {KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT}; 170 } 171 172 for (Component c: components) { 173 174 System.out.print(c.getClass().getName() + ": "); 175 176 Point origin = c.getLocationOnScreen(); 177 int xc = origin.x + c.getWidth() / 2; 178 int yc = origin.y + c.getHeight() / 2; 179 Point center = new Point(xc, yc); 180 181 robot.delay(robotDelay); 182 robot.glide(origin, center); 183 robot.click(); 184 robot.delay(robotDelay); 185 186 for (int k = 0; k < keys.length; ++k) { 187 188 keyPressReceived = false; 189 190 keyCode = keys[k]; 191 192 robot.type(keyCode); 193 194 robot.delay(robotDelay); 195 196 if (!keyPressReceived) { 197 synchronized (lock) { 198 try { 199 lock.wait(waitDelay); 200 } catch (InterruptedException e) {} 201 } 202 } 203 204 assertTrue(keyPressReceived, "key press event was not received"); 205 } 206 207 System.out.println("passed"); 208 } 209 210 robot.waitForIdle(); 211 frame.dispose(); 212 } 213 214 215 public static void main(String[] args) throws Exception { 216 217 KeyMaskTest test = new KeyMaskTest(); 218 test.doTest(); 219 } 220 }