Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Robot/ModifierRobotKey/ModifierRobotKeyTest.java
38828 views
/*1* Copyright (c) 2007, 2014, 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*/222324import java.awt.*;25import java.awt.event.*;2627import static jdk.testlibrary.Asserts.assertTrue;2829/*30* @test31* @summary Make sure that modifier key mask is set when robot press32* some key with one or more modifiers.33*34* @library ../../../../lib/testlibrary/35* @build ExtendedRobot36* @run main ModifierRobotKeyTest37*/3839public class ModifierRobotKeyTest extends KeyAdapter {4041private boolean focusGained = false;42private boolean startTest = false;43private ExtendedRobot robot;44private Frame frame;45private Canvas canvas;4647private volatile boolean tempPress = false;4849private int[] textKeys, modifierKeys, inputMasks;50private boolean[] modifierStatus, textStatus;5152private final static int waitDelay = 5000;53private Object tempLock = new Object();54private Object keyLock = new Object();5556public static void main(String[] args) throws Exception {57ModifierRobotKeyTest test = new ModifierRobotKeyTest();58test.doTest();59}6061public ModifierRobotKeyTest() throws Exception {62modifierKeys = new int[3];63modifierKeys[0] = KeyEvent.VK_SHIFT;64modifierKeys[1] = KeyEvent.VK_CONTROL;65modifierKeys[2] = KeyEvent.VK_ALT;6667inputMasks = new int[3];68inputMasks[0] = InputEvent.SHIFT_MASK;69inputMasks[1] = InputEvent.CTRL_MASK;70inputMasks[2] = InputEvent.ALT_MASK;7172modifierStatus = new boolean[modifierKeys.length];7374textKeys = new int[2];75textKeys[0] = KeyEvent.VK_A;7677String os = System.getProperty("os.name").toLowerCase();7879if (os.contains("solaris") || os.contains("sunos"))80textKeys[1] = KeyEvent.VK_S;81else if (os.contains("os x"))82textKeys[1] = KeyEvent.VK_K;83else84textKeys[1] = KeyEvent.VK_I;8586textStatus = new boolean[textKeys.length];8788EventQueue.invokeAndWait( () -> { initializeGUI(); });89}9091public void keyPressed(KeyEvent event) {9293tempPress = true;94synchronized (tempLock) { tempLock.notifyAll(); }9596if (! startTest) {97return;98}99for (int x = 0; x < inputMasks.length; x++) {100if ((event.getModifiers() & inputMasks[x]) != 0) {101System.out.println("Modifier set: " + event.getKeyModifiersText(inputMasks[x]));102modifierStatus[x] = true;103}104}105for (int x = 0; x < textKeys.length; x++) {106if (event.getKeyCode() == textKeys[x]) {107System.out.println("Text set: " + event.getKeyText(textKeys[x]));108textStatus[x] = true;109}110}111112synchronized (keyLock) { keyLock.notifyAll(); }113}114115private void initializeGUI() {116frame = new Frame("Test frame");117canvas = new Canvas();118canvas.addFocusListener(new FocusAdapter() {119public void focusGained(FocusEvent event) { focusGained = true; }120});121canvas.addKeyListener(this);122frame.setLayout(new BorderLayout());123frame.add(canvas);124frame.setSize(200, 200);125frame.setVisible(true);126}127128public void doTest() throws Exception {129robot = new ExtendedRobot();130131robot.mouseMove((int) frame.getLocationOnScreen().getX() + frame.getSize().width / 2,132(int) frame.getLocationOnScreen().getY() + frame.getSize().height / 2);133robot.click(MouseEvent.BUTTON1_MASK);134robot.waitForIdle();135136assertTrue(focusGained, "FAIL: Canvas gained focus!");137138for (int i = 0; i < modifierKeys.length; i++) {139for (int j = 0; j < textKeys.length; j++) {140tempPress = false;141robot.keyPress(modifierKeys[i]);142robot.waitForIdle();143if (! tempPress) {144synchronized (tempLock) { tempLock.wait(waitDelay); }145}146assertTrue(tempPress, "FAIL: keyPressed triggered for i=" + i);147148resetStatus();149startTest = true;150robot.keyPress(textKeys[j]);151robot.waitForIdle();152if (! modifierStatus[i] || ! textStatus[j]) {153synchronized (keyLock) { keyLock.wait(waitDelay); }154}155156157assertTrue(modifierStatus[i] && textStatus[j],158"FAIL: KeyEvent not proper!"+159"Key checked: i=" + i + "; j=" + j+160"ModifierStatus = " + modifierStatus[i]+161"TextStatus = " + textStatus[j]);162startTest = false;163robot.keyRelease(textKeys[j]);164robot.waitForIdle();165robot.keyRelease(modifierKeys[i]);166robot.waitForIdle();167}168}169170for (int i = 0; i < modifierKeys.length; i++) {171for (int j = i + 1; j < modifierKeys.length; j++) {172for (int k = 0; k < textKeys.length; k++) {173tempPress = false;174robot.keyPress(modifierKeys[i]);175robot.waitForIdle();176if (! tempPress) {177synchronized (tempLock) { tempLock.wait(waitDelay); }178}179180assertTrue(tempPress, "FAIL: MultiKeyTest: keyPressed triggered for i=" + i);181182tempPress = false;183robot.keyPress(modifierKeys[j]);184robot.waitForIdle();185if (! tempPress) {186synchronized (tempLock) { tempLock.wait(waitDelay); }187}188assertTrue(tempPress, "FAIL: MultiKeyTest keyPressed triggered for j=" + j);189190resetStatus();191startTest = true;192robot.keyPress(textKeys[k]);193robot.waitForIdle();194if (! modifierStatus[i] || ! modifierStatus[j] || ! textStatus[k]) {195synchronized (keyLock) {196keyLock.wait(waitDelay);197}198}199assertTrue(modifierStatus[i] && modifierStatus[j] && textStatus[k],200"FAIL: KeyEvent not proper!"+201"Key checked: i=" + i + "; j=" + j + "; k=" + k+202"Modifier1Status = " + modifierStatus[i]+203"Modifier2Status = " + modifierStatus[j]+204"TextStatus = " + textStatus[k]);205206startTest = false;207robot.keyRelease(textKeys[k]);208robot.waitForIdle();209robot.keyRelease(modifierKeys[j]);210robot.waitForIdle();211robot.keyRelease(modifierKeys[i]);212robot.waitForIdle();213}214}215}216217frame.dispose();218}219220private void resetStatus() {221for (int i = 0; i < modifierStatus.length; i++) {222modifierStatus[i] = false;223}224for (int i = 0; i < textStatus.length; i++) {225textStatus[i] = false;226}227}228229}230231232