1 /*
   2  * Copyright (c) 2015, 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 import java.awt.Component;
  25 import java.awt.EventQueue;
  26 import java.awt.MouseInfo;
  27 import java.awt.Rectangle;
  28 import java.awt.Robot;
  29 import java.awt.event.InputEvent;
  30 import java.awt.event.KeyAdapter;
  31 import java.awt.event.KeyEvent;
  32 
  33 import javax.swing.JFrame;
  34 import javax.swing.JTextField;
  35 
  36 /**
  37  * @test
  38  * @key headful
  39  * @bug 8143054
  40  */
  41 public final class MouseModifiersInKeyEvent {
  42 
  43     private static int modifiersEX = 0;
  44     private static int modifiers = 0;
  45     private static JFrame f;
  46     private static Rectangle bounds;
  47 
  48     public static void main(final String[] args) throws Exception {
  49         for (int i = 1; i <= MouseInfo.getNumberOfButtons(); ++i) {
  50             test(InputEvent.getMaskForButton(i));
  51         }
  52     }
  53 
  54     private static void test(final int mask) throws Exception {
  55         final Robot r = new Robot();
  56         r.setAutoDelay(100);
  57         r.setAutoWaitForIdle(true);
  58 
  59         EventQueue.invokeAndWait(MouseModifiersInKeyEvent::createAndShowGUI);
  60         r.waitForIdle();
  61         EventQueue.invokeAndWait(() -> bounds = f.getBounds());
  62 
  63         r.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
  64         r.mousePress(mask);
  65         r.keyPress(KeyEvent.VK_A);
  66         r.keyRelease(KeyEvent.VK_A);
  67 
  68         EventQueue.invokeAndWait(() -> f.dispose());
  69 
  70         r.mouseRelease(mask);
  71 
  72         // all extended modifiers should work
  73         if (modifiersEX != mask) {
  74             System.err.println("Expected modifiersEX: " + mask);
  75             System.err.println("Actual modifiersEX: " + modifiersEX);
  76             throw new RuntimeException();
  77         }
  78         // old modifiers work only for button1
  79         if (modifiersEX == InputEvent.BUTTON1_DOWN_MASK) {
  80             if (modifiers != InputEvent.BUTTON1_MASK) {
  81                 System.err.println("Expected modifiers: " + InputEvent.BUTTON1_MASK);
  82                 System.err.println("Actual modifiers: " + modifiers);
  83                 throw new RuntimeException();
  84             }
  85         }
  86         modifiersEX = 0;
  87         modifiers = 0;
  88     }
  89 
  90     private static void createAndShowGUI() {
  91         f = new JFrame();
  92 
  93         final Component component = new JTextField();
  94         component.addKeyListener(new MyKeyListener());
  95 
  96         f.add(component);
  97         f.setSize(300, 300);
  98         f.setLocationRelativeTo(null);
  99         f.setAlwaysOnTop(true);
 100         f.setVisible(true);
 101     }
 102 
 103     static final class MyKeyListener extends KeyAdapter {
 104 
 105         public void keyPressed(final KeyEvent e) {
 106             modifiersEX = e.getModifiersEx();
 107             modifiers = e.getModifiers();
 108         }
 109     }
 110 }
 111