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