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