Path: blob/master/test/jdk/java/awt/Focus/ChoiceFocus/ChoiceFocus.java
41152 views
/*1* Copyright (c) 1998, 2016, 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@key headful26@bug 405385627@summary Choice components don't honour key focus28@library ../../regtesthelpers29@build Util30@author Andrei Dmitriev : area=awt.choice31@run main ChoiceFocus32*/3334import java.applet.*;35import java.awt.*;36import java.awt.event.*;37import test.java.awt.regtesthelpers.Util;3839/*40Here is the old description for the test.4142This bug occurs in jdk <= 1.1.6, 1.2b3. The problem is that key press43and release events will not be delivered to a choice component with44the focus if the mouse is over another component (of any type);4546This bug occurs in Motif and was fixed in the native code.47481. Set the focus on choice component 1 by selecting an item.492. Move the mouse over choice component 2, BUT do not set the focus on it.503. Type some characters e.g. 'a', 'b' etc.514. Verify by the console output that all the key events are delivered to52choice component 1, not 2. If all the key events are delivered to53choice 1, the test passes.54*/5556public class ChoiceFocus {57static Robot robot;58volatile static boolean keyPressed = false;59volatile static boolean keyReleased = false;60volatile static boolean keyTyped = false;6162public static void main(String[] args) {63Frame f = new Frame("Test Frame");64f.setLayout(new GridLayout());6566Choice c1 = new Choice();67c1.add("Choice 1, Item 1");68c1.add("Choice 1, Item 2");6970Choice c2 = new Choice();71c2.add("Choice 2, Item 1");72c2.add("Choice 2, Item 2");73c1.addKeyListener(new KeyListener(){74public void keyPressed(KeyEvent e){75System.out.println("Key Pressed Event "+e);76keyPressed = true;77}78public void keyReleased(KeyEvent e){79System.out.println("Key Released Event "+e);80keyReleased = true;81}82public void keyTyped(KeyEvent e){83System.out.println("Key Typed Event "+e);84keyTyped = true;85}86});8788f.add(c1);89f.add(c2);9091f.pack();92f.setVisible(true);9394robot = Util.createRobot();95robot.setAutoWaitForIdle(true);96robot.setAutoDelay(50);9798//transfer focus to Choice99Util.waitForIdle(robot);100Util.clickOnComp(c1, robot);101Util.waitForIdle(robot);102103//close choice104Util.clickOnComp(c1, robot);105106//position a mouse over a different component107Point pt = c2.getLocationOnScreen();108robot.mouseMove(pt.x + c2.getWidth()/2, pt.y + c2.getHeight()/2);109Util.waitForIdle(robot);110111robot.keyPress(KeyEvent.VK_A);112robot.keyRelease(KeyEvent.VK_A);113Util.waitForIdle(robot);114115if (!keyPressed || !keyReleased || !keyTyped){116throw new RuntimeException("Failed. Some of event wasn't come "+keyPressed + " : "+keyReleased+" : "+keyTyped);117} else {118System.out.println("Test passed");119}120}121}////~122123124