blob: 79dbef7dd7e6f74120c77b0e6ad8dc76f18d1f02 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 test
26 @bug 6252982
27 @summary PIT: Keyboard FocusTraversal not working when choice's drop-down is visible, on XToolkit
28 @author andrei.dmitriev : area=awt.choice
29 @run applet ChoiceKeyEventReaction.html
30*/
31
32import java.applet.Applet;
33import java.awt.*;
34import java.awt.event.*;
35import test.java.awt.regtesthelpers.Util;
36
37public class ChoiceKeyEventReaction extends Applet
38{
39 Robot robot;
40 Choice choice1 = new Choice();
41 Point pt;
42 TextField tf = new TextField("Hi");
43
44 boolean keyTypedOnTextField = false;
45 boolean itemChanged = false;
46 String toolkit;
47
48 public void init()
49 {
50 toolkit = Toolkit.getDefaultToolkit().getClass().getName();
51 System.out.println("Current toolkit is :" +toolkit);
52 for (int i = 1; i<20; i++){
53 choice1.add("item-0"+i);
54 }
55 tf.addKeyListener(new KeyAdapter(){
56 public void keyPressed(KeyEvent ke) {
57 keyTypedOnTextField = true;
58 System.out.println(ke);
59 }
60 });
61
62
63 choice1.addItemListener(new ItemListener() {
64 public void itemStateChanged(ItemEvent e) {
65 itemChanged = true;
66 System.out.println(e);
67 }
68 });
69
70 choice1.setFocusable(false);
71 add(tf);
72 add(choice1);
73 setLayout (new FlowLayout());
74 }//End init()
75
76 public void start ()
77 {
78 setSize (200,200);
79 setVisible(true);
80 validate();
81 try{
82 robot = new Robot();
83 Util.waitForIdle(robot);
84 moveFocusToTextField();
85 testKeyOnChoice(InputEvent.BUTTON1_MASK, KeyEvent.VK_UP);
86 } catch (Throwable e) {
87 throw new RuntimeException("Test failed. Exception thrown: "+e);
88 }
89 }// start()
90
91 public void testKeyOnChoice(int button, int key){
92 pt = choice1.getLocationOnScreen();
93 robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2);
94 Util.waitForIdle(robot);
95 robot.mousePress(button);
96 robot.delay(10);
97 robot.mouseRelease(button);
98 Util.waitForIdle(robot);
99
100 robot.keyPress(key);
101 robot.keyRelease(key);
102
103 Util.waitForIdle(robot);
104
105 System.out.println("keyTypedOnTextField = "+keyTypedOnTextField +": itemChanged = " + itemChanged);
106
107 if (itemChanged){
108 throw new RuntimeException("Test failed. ItemChanged event occur on Choice.");
109 }
110
111 // We may just write
112 // if (toolkit.equals("sun.awt.windows.WToolkit") == keyTypedOnTextField) {fail;}
113 // but must report differently in these cases so put two separate if statements for simplicity.
114 if (toolkit.equals("sun.awt.windows.WToolkit") &&
115 !keyTypedOnTextField)
116 {
117 throw new RuntimeException("Test failed. (Win32) KeyEvent wasn't addressed to TextField. ");
118 }
119
120 if (!toolkit.equals("sun.awt.windows.WToolkit") &&
121 keyTypedOnTextField)
122 {
123 throw new RuntimeException("Test failed. (XToolkit/MToolkit). KeyEvent was addressed to TextField.");
124 }
125
126 System.out.println("Test passed. Unfocusable Choice doesn't react on keys.");
127 //close opened choice
128 robot.keyPress(KeyEvent.VK_ESCAPE);
129 robot.keyRelease(KeyEvent.VK_ESCAPE);
130 Util.waitForIdle(robot);
131 }
132
133 public void moveFocusToTextField(){
134 pt = tf.getLocationOnScreen();
135 robot.mouseMove(pt.x + tf.getWidth()/2, pt.y + tf.getHeight()/2);
136 Util.waitForIdle(robot);
137 robot.mousePress(InputEvent.BUTTON1_MASK);
138 robot.delay(10);
139 robot.mouseRelease(InputEvent.BUTTON1_MASK);
140 Util.waitForIdle(robot);
141 }
142}//:~