Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ChoiceFocus/ChoiceFocus.java
48094 views
/*1* Copyright (c) 1998, 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/*24@test25@bug 405385626@summary Choice components don't honour key focus27@library ../../regtesthelpers28@build Util29@author Andrei Dmitriev : area=awt.choice30@run main ChoiceFocus31*/3233import java.applet.*;34import java.awt.*;35import java.awt.event.*;36import test.java.awt.regtesthelpers.Util;3738/*39Here is the old description for the test.4041This bug occurs in jdk <= 1.1.6, 1.2b3. The problem is that key press42and release events will not be delivered to a choice component with43the focus if the mouse is over another component (of any type);4445This bug occurs in Motif and was fixed in the native code.46471. Set the focus on choice component 1 by selecting an item.482. Move the mouse over choice component 2, BUT do not set the focus on it.493. Type some characters e.g. 'a', 'b' etc.504. Verify by the console output that all the key events are delivered to51choice component 1, not 2. If all the key events are delivered to52choice 1, the test passes.53*/5455public class ChoiceFocus {56static Robot robot;57volatile static boolean keyPressed = false;58volatile static boolean keyReleased = false;59volatile static boolean keyTyped = false;6061public static void main(String[] args) {62Frame f = new Frame("Test Frame");63f.setLayout(new GridLayout());6465Choice c1 = new Choice();66c1.add("Choice 1, Item 1");67c1.add("Choice 1, Item 2");6869Choice c2 = new Choice();70c2.add("Choice 2, Item 1");71c2.add("Choice 2, Item 2");72c1.addKeyListener(new KeyListener(){73public void keyPressed(KeyEvent e){74System.out.println("Key Pressed Event "+e);75keyPressed = true;76}77public void keyReleased(KeyEvent e){78System.out.println("Key Released Event "+e);79keyReleased = true;80}81public void keyTyped(KeyEvent e){82System.out.println("Key Typed Event "+e);83keyTyped = true;84}85});8687f.add(c1);88f.add(c2);8990f.pack();91f.setVisible(true);9293robot = Util.createRobot();94robot.setAutoWaitForIdle(true);95robot.setAutoDelay(50);9697//transfer focus to Choice98Util.waitForIdle(robot);99Util.clickOnComp(c1, robot);100Util.waitForIdle(robot);101102//close choice103Util.clickOnComp(c1, robot);104105//position a mouse over a different component106Point pt = c2.getLocationOnScreen();107robot.mouseMove(pt.x + c2.getWidth()/2, pt.y + c2.getHeight()/2);108Util.waitForIdle(robot);109110robot.keyPress(KeyEvent.VK_A);111robot.keyRelease(KeyEvent.VK_A);112Util.waitForIdle(robot);113114if (!keyPressed || !keyReleased || !keyTyped){115throw new RuntimeException("Failed. Some of event wasn't come "+keyPressed + " : "+keyReleased+" : "+keyTyped);116} else {117System.out.println("Test passed");118}119}120}////~121122123