1 /* 2 * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 @test 26 @bug 4053856 27 @summary Choice components don't honour key focus 28 @library ../../regtesthelpers 29 @build Util 30 @author Andrei Dmitriev : area=awt.choice 31 @run main ChoiceFocus 32 */ 33 34 import java.applet.*; 35 import java.awt.*; 36 import java.awt.event.*; 37 import test.java.awt.regtesthelpers.Util; 38 39 /* 40 Here is the old description for the test. 41 42 This bug occurs in jdk <= 1.1.6, 1.2b3. The problem is that key press 43 and release events will not be delivered to a choice component with 44 the focus if the mouse is over another component (of any type); 45 46 This bug occurs in Motif and was fixed in the native code. 47 48 1. Set the focus on choice component 1 by selecting an item. 49 2. Move the mouse over choice component 2, BUT do not set the focus on it. 50 3. Type some characters e.g. 'a', 'b' etc. 51 4. Verify by the console output that all the key events are delivered to 52 choice component 1, not 2. If all the key events are delivered to 53 choice 1, the test passes. 54 */ 55 56 public class ChoiceFocus { 57 static Robot robot; 58 volatile static boolean keyPressed = false; 59 volatile static boolean keyReleased = false; 60 volatile static boolean keyTyped = false; 61 62 public static void main(String[] args) { 63 Frame f = new Frame("Test Frame"); 64 f.setLayout(new GridLayout()); 65 66 Choice c1 = new Choice(); 67 c1.add("Choice 1, Item 1"); 68 c1.add("Choice 1, Item 2"); 69 70 Choice c2 = new Choice(); 71 c2.add("Choice 2, Item 1"); 72 c2.add("Choice 2, Item 2"); 73 c1.addKeyListener(new KeyListener(){ 74 public void keyPressed(KeyEvent e){ 75 System.out.println("Key Pressed Event "+e); 76 keyPressed = true; 77 } 78 public void keyReleased(KeyEvent e){ 79 System.out.println("Key Released Event "+e); 80 keyReleased = true; 81 } 82 public void keyTyped(KeyEvent e){ 83 System.out.println("Key Typed Event "+e); 84 keyTyped = true; 85 } 86 }); 87 88 f.add(c1); 89 f.add(c2); 90 91 f.pack(); 92 f.setVisible(true); 93 94 robot = Util.createRobot(); 95 robot.setAutoWaitForIdle(true); 96 robot.setAutoDelay(50); 97 98 //transfer focus to Choice 99 Util.waitForIdle(robot); 100 Util.clickOnComp(c1, robot); 101 Util.waitForIdle(robot); 102 103 //close choice 104 Util.clickOnComp(c1, robot); 105 106 //position a mouse over a different component 107 Point pt = c2.getLocationOnScreen(); 108 robot.mouseMove(pt.x + c2.getWidth()/2, pt.y + c2.getHeight()/2); 109 Util.waitForIdle(robot); 110 111 robot.keyPress(KeyEvent.VK_A); 112 robot.keyRelease(KeyEvent.VK_A); 113 Util.waitForIdle(robot); 114 115 if (!keyPressed || !keyReleased || !keyTyped){ 116 throw new RuntimeException("Failed. Some of event wasn't come "+keyPressed + " : "+keyReleased+" : "+keyTyped); 117 } else { 118 System.out.println("Test passed"); 119 } 120 } 121 }////~