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