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