1 /*
   2  * Copyright (c) 2013, 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 6317481 8012325
  28   @summary REG:Pressing the mouse, dragging and releasing it outside the button triggers ActionEvent, XAWT
  29   @author Dmitry.Cherepanov@SUN.COM area=awt.event
  30   @run main EnterAsGrabbedEvent
  31 */
  32 
  33 import javax.swing.*;
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 
  37 public class EnterAsGrabbedEvent
  38 {
  39     //Declare things used in the test, like buttons and labels here
  40     private static Frame frame;
  41     private static Button button;
  42     private static volatile boolean enterTriggered = false;
  43     private static volatile boolean actionTriggered = false;
  44 
  45     private static void init()
  46     {
  47         frame = new Frame();
  48         frame.setLayout(new FlowLayout());
  49         button = new Button("button");
  50         button.addActionListener(actionEvent -> {
  51             actionTriggered = true;
  52         });
  53         frame.add(button);
  54         frame.setBounds(100, 100, 200, 200);
  55         frame.setVisible(true);
  56         frame.validate();
  57       }
  58 
  59     public static void main(String[] args) throws Exception {
  60         try {
  61             Robot r = new Robot();
  62             r.setAutoDelay(200);
  63             r.setAutoWaitForIdle(true);
  64             SwingUtilities.invokeAndWait(EnterAsGrabbedEvent::init);
  65             r.waitForIdle();
  66 
  67             Point loc = button.getLocationOnScreen();
  68             r.mouseMove(loc.x+button.getWidth()/2, loc.y+button.getHeight()/2);
  69             r.mousePress(InputEvent.BUTTON1_MASK);
  70 
  71             // in this case (drag mouse outside the button):
  72             // NotifyEnter (->MouseEnter) should be dispatched to the top-level
  73             // event if the grabbed window is the component (button)
  74             frame.addMouseListener(
  75                     new MouseAdapter() {
  76                         public void mouseEntered(MouseEvent me) {
  77                             System.out.println(me);
  78                             enterTriggered = true;
  79                         }
  80 
  81                         // Just for tracing
  82                         public void mouseExited(MouseEvent me) {
  83                             System.out.println(me);
  84                         }
  85                     });
  86 
  87             // Just for tracing
  88             button.addMouseListener(
  89                     new MouseAdapter(){
  90                         public void mouseEntered(MouseEvent me){
  91                             System.out.println(me);
  92                         }
  93                         public void mouseExited(MouseEvent me){
  94                             System.out.println(me);
  95                         }
  96                     });
  97 
  98             r.mouseMove(loc.x+button.getWidth() + 1, loc.y+button.getHeight()/2);
  99 
 100             r.mouseRelease(InputEvent.BUTTON1_MASK);
 101 
 102             if (!enterTriggered) {
 103                 throw new RuntimeException("Test failed. MouseEntered was not triggered");
 104             }
 105 
 106             if (actionTriggered) {
 107                 throw new RuntimeException("Test failed. ActionEvent triggered");
 108             }
 109         } finally {
 110             if (frame != null) {
 111                 frame.dispose();
 112             }
 113         }
 114     }
 115 }