Path: blob/master/test/jdk/java/awt/Choice/ChoiceKeyEventReaction/ChoiceKeyEventReaction.java
41152 views
/*1* Copyright (c) 2005, 2019, 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/*24* @test25* @bug 625298226* @key headful27* @summary PIT: Keyboard FocusTraversal not working when choice's drop-down is visible, on XToolkit28* @author andrei.dmitriev : area=awt.choice29* @run main ChoiceKeyEventReaction30*/3132import java.awt.Choice;33import java.awt.FlowLayout;34import java.awt.Frame;35import java.awt.Point;36import java.awt.Robot;37import java.awt.TextField;38import java.awt.Toolkit;39import java.awt.event.InputEvent;40import java.awt.event.ItemEvent;41import java.awt.event.ItemListener;42import java.awt.event.KeyAdapter;43import java.awt.event.KeyEvent;4445public class ChoiceKeyEventReaction46{47private static Robot robot;48private static Choice choice1 = new Choice();49private static Point pt;50private static TextField tf = new TextField("Hi");51private static boolean keyTypedOnTextField = false;52private static boolean itemChanged = false;53private static Frame frame;54private static String toolkit;5556public static void main(String[] args) {57createAndShowGUI();5859try {60robot = new Robot();61robot.setAutoDelay(100);62robot.waitForIdle();6364moveFocusToTextField();65testKeyOnChoice(InputEvent.BUTTON1_MASK, KeyEvent.VK_UP);66} catch (Exception e) {67throw new RuntimeException("Test failed. Exception thrown: "+e);68} finally {69if (frame != null) {70frame.dispose();71}72}73}7475private static void createAndShowGUI() {76frame = new Frame();77toolkit = Toolkit.getDefaultToolkit().getClass().getName();78System.out.println("Current toolkit is :" +toolkit);79for (int i = 1; i<20; i++){80choice1.add("item-0"+i);81}8283tf.addKeyListener(new KeyAdapter(){84public void keyPressed(KeyEvent ke) {85keyTypedOnTextField = true;86System.out.println(ke);87}88});8990choice1.addItemListener(new ItemListener() {91public void itemStateChanged(ItemEvent e) {92itemChanged = true;93System.out.println(e);94}95});96choice1.setFocusable(false);9798frame.add(tf);99frame.add(choice1);100frame.setLayout (new FlowLayout());101frame.setSize (200,200);102frame.setLocationRelativeTo(null);103frame.setVisible(true);104}105106private static void testKeyOnChoice(int button, int key) {107pt = choice1.getLocationOnScreen();108robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2);109110robot.mousePress(button);111robot.mouseRelease(button);112robot.waitForIdle();113114robot.keyPress(key);115robot.keyRelease(key);116robot.waitForIdle();117118System.out.println("keyTypedOnTextField = "+keyTypedOnTextField +": itemChanged = " + itemChanged);119if (itemChanged) {120throw new RuntimeException("Test failed. ItemChanged event occur on Choice.");121}122123// We may just write124// if (toolkit.equals("sun.awt.windows.WToolkit") == keyTypedOnTextField) {fail;}125// but must report differently in these cases so put two separate if statements for simplicity.126if (!toolkit.equals("sun.awt.X11.XToolkit") &&127!keyTypedOnTextField) {128throw new RuntimeException("Test failed. (Win32/MacOS) KeyEvent wasn't addressed to TextField. ");129}130131if (toolkit.equals("sun.awt.X11.XToolkit") &&132keyTypedOnTextField) {133throw new RuntimeException("Test failed. (XToolkit/MToolkit). KeyEvent was addressed to TextField.");134}135136System.out.println("Test passed. Unfocusable Choice doesn't react on keys.");137}138139public static void moveFocusToTextField() {140pt = tf.getLocationOnScreen();141robot.mouseMove(pt.x + tf.getWidth()/2, pt.y + tf.getHeight()/2);142143robot.mousePress(InputEvent.BUTTON1_MASK);144robot.mouseRelease(InputEvent.BUTTON1_MASK);145}146}147148149