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