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   @bug 6451578
  27   @library ../../../regtesthelpers
  28   @build Sysout AbstractTest Util
  29   @summary A mouse listener method happens to process mouse events whose time is in the future.
  30   @author andrei dmitriev : area=awt.event
  31   @run main EventTimeInFuture
  32 */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import test.java.awt.regtesthelpers.AbstractTest;
  37 import test.java.awt.regtesthelpers.Sysout;
  38 import test.java.awt.regtesthelpers.Util;
  39 
  40 public class EventTimeInFuture {
  41 
  42     public static void main(String []s) {
  43         Frame frame = new SensibleFrame();
  44 
  45         frame.pack();
  46         frame.setLocationRelativeTo(null);
  47         frame.setVisible(true);
  48 
  49         Robot robot = Util.createRobot();
  50         Util.waitForIdle(robot);
  51 
  52         /* The defect may appear on every kind of mouse event: movement, press, etc.
  53          * so start mouse move from frame's outside. Use small threshhold depending on the
  54          * frame's size.
  55          */
  56         Point start = new Point(frame.getLocationOnScreen().x - frame.getWidth()/5,
  57                                 frame.getLocationOnScreen().y - frame.getHeight()/5);
  58         Point end = new Point(frame.getLocationOnScreen().x + frame.getWidth() * 6 / 5,
  59                               frame.getLocationOnScreen().y + frame.getHeight() * 6 / 5);
  60         Sysout.println("start = " + start);
  61         Sysout.println("end = " + end);
  62         Util.mouseMove(robot, start, end);
  63 
  64         // Start drag inside toplevel.
  65         start = new Point(frame.getLocationOnScreen().x + frame.getWidth()/2,
  66                           frame.getLocationOnScreen().y + frame.getHeight()/2);
  67         end = new Point(frame.getLocationOnScreen().x + frame.getWidth() * 6 / 5,
  68                         frame.getLocationOnScreen().y + frame.getHeight() * 6 / 5);
  69         Util.drag(robot, start, end, MouseEvent.BUTTON1_MASK);
  70     }
  71 }
  72 
  73 class SensibleFrame extends Frame implements MouseListener,
  74     MouseMotionListener{
  75 
  76     public SensibleFrame(){
  77         super("Is event time in future");
  78         setPreferredSize(new Dimension(100,100));
  79         setBackground(Color.white);
  80         addMouseListener(this);
  81         addMouseMotionListener(this);
  82     }
  83 
  84     private void traceMouse(String k, MouseEvent e){
  85         long eventTime = e.getWhen();
  86         long currTime = System.currentTimeMillis();
  87         long diff = currTime - eventTime;
  88 
  89         Sysout.println(k + " diff is " + diff + ", event is "+ e);
  90 
  91         if (diff < 0){
  92             AbstractTest.fail(k + " diff is " + diff + ", event = "+e);
  93         }
  94     }
  95 
  96     public void mouseMoved(MouseEvent e){
  97         traceMouse("moved",e);
  98     }
  99 
 100     public void mouseEntered(MouseEvent e){
 101         traceMouse("entered",e);
 102     }
 103     public void mouseExited(MouseEvent e){
 104         traceMouse("exited",e);
 105     }
 106     public void mouseClicked(MouseEvent e){
 107         traceMouse("clicked",e);
 108     }
 109     public void mousePressed(MouseEvent e){
 110         traceMouse("pressed",e);
 111     }
 112     public void mouseReleased(MouseEvent e){
 113         traceMouse("released",e);
 114     }
 115     public void mouseDragged(MouseEvent e){
 116         traceMouse("dragged",e);
 117     }
 118 }