ChoiceFocus.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 1998, 2016, 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  @key headful
27  @bug 4053856
28  @summary Choice components don't honour key focus
29  @library ../../regtesthelpers
30  @build Util
31  @author Andrei Dmitriev : area=awt.choice
32  @run main ChoiceFocus
33*/
34
35import java.applet.*;
36import java.awt.*;
37import java.awt.event.*;
38import test.java.awt.regtesthelpers.Util;
39
40/*
41Here is the old description for the test.
42
43This bug occurs in jdk <= 1.1.6, 1.2b3. The problem is that key press
44and release events will not be delivered to a choice component with
45the focus if the mouse is over another component (of any type);
46
47This bug occurs in Motif and was fixed in the native code.
48
491. Set the focus on choice component 1 by selecting an item.
502. Move the mouse over choice component 2, BUT do not set the focus on it.
513. Type some characters e.g. 'a', 'b' etc.
524. Verify by the console output that all the key events are delivered to
53   choice component 1, not 2. If all the key events are delivered to
54   choice 1, the test passes.
55*/
56
57public class ChoiceFocus {
58    static Robot robot;
59    volatile static boolean keyPressed = false;
60    volatile static boolean keyReleased = false;
61    volatile static boolean keyTyped = false;
62
63    public static void main(String[] args) {
64        Frame f = new Frame("Test Frame");
65        f.setLayout(new GridLayout());
66
67        Choice c1 = new Choice();
68        c1.add("Choice 1, Item 1");
69        c1.add("Choice 1, Item 2");
70
71        Choice c2 = new Choice();
72        c2.add("Choice 2, Item 1");
73        c2.add("Choice 2, Item 2");
74        c1.addKeyListener(new KeyListener(){
75                public void keyPressed(KeyEvent e){
76                    System.out.println("Key Pressed Event "+e);
77                    keyPressed = true;
78                }
79                public void keyReleased(KeyEvent e){
80                    System.out.println("Key Released Event "+e);
81                    keyReleased = true;
82                }
83                public void keyTyped(KeyEvent e){
84                    System.out.println("Key Typed Event "+e);
85                    keyTyped = true;
86                }
87            });
88
89        f.add(c1);
90        f.add(c2);
91
92        f.pack();
93        f.setVisible(true);
94
95        robot = Util.createRobot();
96        robot.setAutoWaitForIdle(true);
97        robot.setAutoDelay(50);
98
99        //transfer focus to Choice
100        Util.waitForIdle(robot);
101        Util.clickOnComp(c1, robot);
102        Util.waitForIdle(robot);
103
104        //close choice
105        Util.clickOnComp(c1, robot);
106
107        //position a mouse over a different component
108        Point pt = c2.getLocationOnScreen();
109        robot.mouseMove(pt.x + c2.getWidth()/2, pt.y + c2.getHeight()/2);
110        Util.waitForIdle(robot);
111
112        robot.keyPress(KeyEvent.VK_A);
113        robot.keyRelease(KeyEvent.VK_A);
114        Util.waitForIdle(robot);
115
116        if (!keyPressed || !keyReleased || !keyTyped){
117            throw new RuntimeException("Failed. Some of event wasn't come "+keyPressed + " : "+keyReleased+" : "+keyTyped);
118        } else {
119            System.out.println("Test passed");
120        }
121    }
122}////~
123