Path: blob/master/test/jdk/java/awt/InputMethods/InputMethodKeyEventsTest/InputMethodKeyEventsTest.java
41153 views
/*1* Copyright (c) 2017, 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*/2223/* @test24* @bug 817741425* @summary KEY_PRESSED and KEY_TYPED events are not generated if a key's held26* @library ../../regtesthelpers27* @build Util28* @run main/manual InputMethodKeyEventsTest29*/3031import java.awt.*;32import java.awt.event.ActionEvent;33import java.awt.event.ActionListener;34import java.awt.event.KeyEvent;35import java.awt.event.KeyListener;36import java.util.concurrent.atomic.AtomicBoolean;3738import test.java.awt.regtesthelpers.Util;3940public class InputMethodKeyEventsTest {41private static final AtomicBoolean testCompleted = new AtomicBoolean(false);42private static volatile boolean testResult = false;4344private static Dialog controlDialog;45private static Frame testFrame;4647private static final String instructions =48"Verify that KEY_PRESSED and KEY_TYPED events are generated after a key's " +49"\npressed and held.\n" +50"\nThis test is for OS X only. For other platforms please simply press \"Pass\".\n" +51"\n1. Go to \"System Preferences -> Keyboard -> Input Sources\" and add \"British\" IM." +52"\n2. Set current IM to \"British\"." +53"\n3. Set focus to the frame located at north-west corner." +54"\n4. Pres and hold the \"i\" key for 3 seconds." +55"\n5. Press and release the \"i\" key again. Use log area to ensure that " +56"\n KEY_TYPED and KEY_PRESSED events are still generated." +57"\nIf KEY_PRESSED, KEY_TYPED and KEY_RELEASED are generated for every key press then " +58"\npress \"Pass\", otherwise press \"Fail\".";5960public static void main(String[] args) {61try {62Robot robot = Util.createRobot();6364createAndShowGUI();65Util.waitForIdle(robot);6667Util.waitForCondition(testCompleted);68if (!testResult) {69throw new RuntimeException("Test FAILED!");70}71} finally {72if (controlDialog != null) {73controlDialog.dispose();74}75if (testFrame != null) {76testFrame.dispose();77}78}79}8081private static void createAndShowGUI() {82controlDialog = new Dialog((Frame)null, "InputMethodKeyEventsTest");8384TextArea messageArea = new TextArea(instructions, 15, 80, TextArea.SCROLLBARS_BOTH);85controlDialog.add("North", messageArea);8687TextArea logArea = new TextArea("Test's logs are displayed here\n", 15, 80, TextArea.SCROLLBARS_BOTH);88controlDialog.add("Center", logArea);8990Button passedButton = new Button("Pass");91passedButton.addActionListener(new ActionListener() {92@Override93public void actionPerformed(ActionEvent e) {94testResult = true;95completeTest();96}97});9899Button failedButton = new Button("Fail");100failedButton.addActionListener(new ActionListener() {101@Override102public void actionPerformed(ActionEvent e) {103testResult = false;104completeTest();105}106});107108Panel buttonPanel = new Panel();109buttonPanel.add("West",passedButton);110buttonPanel.add("East", failedButton);111controlDialog.add("South", buttonPanel);112113controlDialog.setBounds(250, 0, 500, 500);114controlDialog.setVisible(true);115116testFrame = new Frame("InputMethodKeyEventsTest");117testFrame.setSize(200, 200);118testFrame.addKeyListener(new KeyListener() {119@Override120public void keyTyped(KeyEvent e) {121logArea.append("KEY_TYPED keyCode = " + e.getKeyCode() + "\n");122}123124@Override125public void keyPressed(KeyEvent e) {126logArea.append("KEY_PRESSED keyCode = " + e.getKeyCode() + "\n");127}128129@Override130public void keyReleased(KeyEvent e) {131logArea.append("KEY_RELEASED keyCode = " + e.getKeyCode() + "\n");132}133});134testFrame.setVisible(true);135}136137private static void completeTest() {138testCompleted.set(true);139synchronized (testCompleted) {140testCompleted.notifyAll();141}142}143}144145146