Path: blob/master/test/jdk/java/awt/Mouse/MouseModifiersUnitTest/MouseModifiersInKeyEvent.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Component;24import java.awt.EventQueue;25import java.awt.MouseInfo;26import java.awt.Rectangle;27import java.awt.Robot;28import java.awt.event.InputEvent;29import java.awt.event.KeyAdapter;30import java.awt.event.KeyEvent;3132import javax.swing.JFrame;33import javax.swing.JTextField;3435/**36* @test37* @key headful38* @bug 814305439*/40public final class MouseModifiersInKeyEvent {4142private static int modifiersEX = 0;43private static int modifiers = 0;44private static JFrame f;45private static Rectangle bounds;4647public static void main(final String[] args) throws Exception {48for (int i = 1; i <= MouseInfo.getNumberOfButtons(); ++i) {49test(InputEvent.getMaskForButton(i));50}51}5253private static void test(final int mask) throws Exception {54final Robot r = new Robot();55r.setAutoDelay(100);56r.setAutoWaitForIdle(true);5758EventQueue.invokeAndWait(MouseModifiersInKeyEvent::createAndShowGUI);59r.waitForIdle();60EventQueue.invokeAndWait(() -> bounds = f.getBounds());6162r.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);63r.mousePress(mask);64r.keyPress(KeyEvent.VK_A);65r.keyRelease(KeyEvent.VK_A);6667EventQueue.invokeAndWait(() -> f.dispose());6869r.mouseRelease(mask);7071// all extended modifiers should work72if (modifiersEX != mask) {73System.err.println("Expected modifiersEX: " + mask);74System.err.println("Actual modifiersEX: " + modifiersEX);75throw new RuntimeException();76}77// old modifiers work only for button178if (modifiersEX == InputEvent.BUTTON1_DOWN_MASK) {79if (modifiers != InputEvent.BUTTON1_MASK) {80System.err.println("Expected modifiers: " + InputEvent.BUTTON1_MASK);81System.err.println("Actual modifiers: " + modifiers);82throw new RuntimeException();83}84}85modifiersEX = 0;86modifiers = 0;87}8889private static void createAndShowGUI() {90f = new JFrame();9192final Component component = new JTextField();93component.addKeyListener(new MyKeyListener());9495f.add(component);96f.setSize(300, 300);97f.setLocationRelativeTo(null);98f.setAlwaysOnTop(true);99f.setVisible(true);100}101102static final class MyKeyListener extends KeyAdapter {103104public void keyPressed(final KeyEvent e) {105modifiersEX = e.getModifiersEx();106modifiers = e.getModifiers();107}108}109}110111112113