Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Choice/ChoiceKeyEventReaction/ChoiceKeyEventReaction.java
47525 views
/*1* Copyright (c) 2005, 2006, 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/*24test25@bug 625298226@summary PIT: Keyboard FocusTraversal not working when choice's drop-down is visible, on XToolkit27@author andrei.dmitriev : area=awt.choice28@run applet ChoiceKeyEventReaction.html29*/3031import java.applet.Applet;32import java.awt.*;33import java.awt.event.*;34import test.java.awt.regtesthelpers.Util;3536public class ChoiceKeyEventReaction extends Applet37{38Robot robot;39Choice choice1 = new Choice();40Point pt;41TextField tf = new TextField("Hi");4243boolean keyTypedOnTextField = false;44boolean itemChanged = false;45String toolkit;4647public void init()48{49toolkit = Toolkit.getDefaultToolkit().getClass().getName();50System.out.println("Current toolkit is :" +toolkit);51for (int i = 1; i<20; i++){52choice1.add("item-0"+i);53}54tf.addKeyListener(new KeyAdapter(){55public void keyPressed(KeyEvent ke) {56keyTypedOnTextField = true;57System.out.println(ke);58}59});606162choice1.addItemListener(new ItemListener() {63public void itemStateChanged(ItemEvent e) {64itemChanged = true;65System.out.println(e);66}67});6869choice1.setFocusable(false);70add(tf);71add(choice1);72setLayout (new FlowLayout());73}//End init()7475public void start ()76{77setSize (200,200);78setVisible(true);79validate();80try{81robot = new Robot();82Util.waitForIdle(robot);83moveFocusToTextField();84testKeyOnChoice(InputEvent.BUTTON1_MASK, KeyEvent.VK_UP);85} catch (Throwable e) {86throw new RuntimeException("Test failed. Exception thrown: "+e);87}88}// start()8990public void testKeyOnChoice(int button, int key){91pt = choice1.getLocationOnScreen();92robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2);93Util.waitForIdle(robot);94robot.mousePress(button);95robot.delay(10);96robot.mouseRelease(button);97Util.waitForIdle(robot);9899robot.keyPress(key);100robot.keyRelease(key);101102Util.waitForIdle(robot);103104System.out.println("keyTypedOnTextField = "+keyTypedOnTextField +": itemChanged = " + itemChanged);105106if (itemChanged){107throw new RuntimeException("Test failed. ItemChanged event occur on Choice.");108}109110// We may just write111// if (toolkit.equals("sun.awt.windows.WToolkit") == keyTypedOnTextField) {fail;}112// but must report differently in these cases so put two separate if statements for simplicity.113if (toolkit.equals("sun.awt.windows.WToolkit") &&114!keyTypedOnTextField)115{116throw new RuntimeException("Test failed. (Win32) KeyEvent wasn't addressed to TextField. ");117}118119if (!toolkit.equals("sun.awt.windows.WToolkit") &&120keyTypedOnTextField)121{122throw new RuntimeException("Test failed. (XToolkit/MToolkit). KeyEvent was addressed to TextField.");123}124125System.out.println("Test passed. Unfocusable Choice doesn't react on keys.");126//close opened choice127robot.keyPress(KeyEvent.VK_ESCAPE);128robot.keyRelease(KeyEvent.VK_ESCAPE);129Util.waitForIdle(robot);130}131132public void moveFocusToTextField(){133pt = tf.getLocationOnScreen();134robot.mouseMove(pt.x + tf.getWidth()/2, pt.y + tf.getHeight()/2);135Util.waitForIdle(robot);136robot.mousePress(InputEvent.BUTTON1_MASK);137robot.delay(10);138robot.mouseRelease(InputEvent.BUTTON1_MASK);139Util.waitForIdle(robot);140}141}//:~142143144