blob: 5f7c07f0560a59e76d934fd04ba3a21ac3acef71 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright (c) 2007 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 6451578
27 @library ../../../regtesthelpers
28 @build Sysout AbstractTest Util
29 @summary A key event could have wrong time
30 @author andrei dmitriev : area=awt.event
31 @run main CorrectTime
32*/
33
34import java.awt.*;
35import java.awt.event.*;
36import test.java.awt.regtesthelpers.AbstractTest;
37import test.java.awt.regtesthelpers.Sysout;
38import test.java.awt.regtesthelpers.Util;
39
40public class CorrectTime extends Frame implements KeyListener {
41 //Maximum time the event should arrive to listener
42 final static int REASONABLE_PATH_TIME = 5000;
43 static TextField tf = new TextField("press keys", 10);
44 static TextArea ta = new TextArea("press keys", 10, 10);
45 static Button bt = new Button("press button");
46 static List list = new List();
47 final static Robot robot = Util.createRobot();
48
49 public CorrectTime(){
50 super("Check time of KeyEvents");
51
52 setPreferredSize(new Dimension(400,400));
53 setLayout(new FlowLayout());
54
55 list.add("item1");
56 list.add("item2");
57 list.add("item3");
58
59 add(bt);
60 add(tf);
61 add(ta);
62 add(list);
63
64 bt.addKeyListener(this);
65 tf.addKeyListener(this);
66 ta.addKeyListener(this);
67 list.addKeyListener(this);
68
69 pack();
70 setLocationRelativeTo(null);
71 setVisible(true);
72 }
73
74 public static void main(String []s) {
75 Frame frame = new CorrectTime();
76
77 Robot robot = Util.createRobot();
78 Util.waitForIdle(robot);
79
80 testComponent(tf);
81 testComponent(ta);
82 testComponent(bt);
83 testComponent(list);
84
85 }
86
87 private static void testComponent(final Component comp){
88 Runnable action = new Runnable(){
89 public void run(){
90 Util.clickOnComp(comp, robot);
91 Util.waitForIdle(robot);
92 }
93 };
94
95 if (! Util.trackFocusGained(comp, action, REASONABLE_PATH_TIME, true)){
96 AbstractTest.fail("Focus didn't come to " + comp);
97 }
98 testKeys();
99 Util.waitForIdle(robot);
100 }
101
102 private static void testKeys(){
103 //set of keys is random
104 typeKey(KeyEvent.VK_A);
105 typeKey(KeyEvent.VK_B);
106 typeKey(KeyEvent.VK_SPACE);
107 typeKey(KeyEvent.VK_Z);
108 }
109
110 private static void typeKey(int keyChar){
111 try {
112 robot.keyPress(keyChar);
113 robot.delay(5);
114 } finally {
115 robot.keyRelease(keyChar);
116 }
117 robot.delay(100);
118 }
119
120 private void traceKey(String k, KeyEvent e){
121 long eventTime = e.getWhen();
122 long currTime = System.currentTimeMillis();
123 long diff = currTime - eventTime;
124 Sysout.println(k + " diff is " + diff + ", event is "+ e);
125 if (diff < 0 ||
126 diff > REASONABLE_PATH_TIME)
127 {
128 AbstractTest.fail(k + " diff is " + diff + ", event = "+e);
129 }
130 }
131
132 public void keyTyped(KeyEvent e){
133 traceKey("keytyped",e);
134 }
135 public void keyPressed(KeyEvent e){
136 traceKey("keypress",e);
137 }
138 public void keyReleased(KeyEvent e){
139 traceKey("keyrelease",e);
140 }
141}