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