1 /*
   2  * Copyright (c) 2005, 2012, 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 7154048
  28  * @summary Window created under a mouse does not receive mouse enter event.
  29  *     Mouse Entered/Exited events should be generated during dragging the window
  30  *     out of the frame and to the frame.
  31  * @library ../../regtesthelpers
  32  * @build Util
  33  * @author alexandr.scherbatiy area=awt.event
  34  * @run main DragWindowOutOfFrameTest
  35  */
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 import javax.swing.*;
  39 
  40 import java.util.concurrent.*;
  41 import sun.awt.SunToolkit;
  42 
  43 import test.java.awt.regtesthelpers.Util;
  44 
  45 public class DragWindowOutOfFrameTest {
  46 
  47     private static volatile int dragWindowMouseEnteredCount = 0;
  48     private static volatile int dragWindowMouseExitedCount = 0;
  49     private static volatile int dragWindowMouseReleasedCount = 0;
  50     private static volatile int buttonMouseEnteredCount = 0;
  51     private static volatile int buttonMouseExitedCount = 0;
  52     private static volatile int labelMouseEnteredCount = 0;
  53     private static volatile int labelMouseExitedCount = 0;
  54     private static volatile int labelMouseReleasedCount = 0;
  55     private static MyDragWindow dragWindow;
  56     private static JLabel label;
  57     private static JButton button;
  58 
  59     public static void main(String[] args) throws Exception {
  60 
  61         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  62         Robot robot = new Robot();
  63         robot.setAutoDelay(50);
  64 
  65         SwingUtilities.invokeAndWait(new Runnable() {
  66 
  67             @Override
  68             public void run() {
  69                 createAndShowGUI();
  70             }
  71         });
  72 
  73         toolkit.realSync();
  74 
  75         Point pointToClick = Util.invokeOnEDT(new Callable<Point>() {
  76 
  77             @Override
  78             public Point call() throws Exception {
  79                 return getCenterPoint(label);
  80             }
  81         });
  82 
  83 
  84         robot.mouseMove(pointToClick.x, pointToClick.y);
  85         robot.mousePress(InputEvent.BUTTON1_MASK);
  86         toolkit.realSync();
  87 
  88         if (dragWindowMouseEnteredCount != 1 && dragWindowMouseExitedCount != 0) {
  89             throw new RuntimeException(
  90                     "Wrong number mouse Entered/Exited events on Drag Window!");
  91         }
  92 
  93         Point pointToDrag = Util.invokeOnEDT(new Callable<Point>() {
  94 
  95             @Override
  96             public Point call() throws Exception {
  97                 label.addMouseListener(new LabelMouseListener());
  98                 button.addMouseListener(new ButtonMouseListener());
  99                 return getCenterPoint(button);
 100             }
 101         });
 102 
 103         robot.mouseMove(450, pointToClick.y);
 104         toolkit.realSync();
 105 
 106         if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {
 107             throw new RuntimeException(
 108                     "Wrong number Mouse Entered/Exited events on label!");
 109         }
 110 
 111         robot.mouseMove(450, pointToDrag.y);
 112         toolkit.realSync();
 113 
 114         if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {
 115             throw new RuntimeException(
 116                     "Wrong number Mouse Entered/Exited events on label!");
 117         }
 118 
 119         if (buttonMouseEnteredCount != 0 && buttonMouseExitedCount != 0) {
 120             throw new RuntimeException(
 121                     "Wrong number Mouse Entered/Exited events on button!");
 122         }
 123 
 124         robot.mouseMove(pointToDrag.y, pointToDrag.y);
 125         toolkit.realSync();
 126 
 127         if (buttonMouseEnteredCount != 1 && buttonMouseExitedCount != 0) {
 128             throw new RuntimeException(
 129                     "Wrong number Mouse Entered/Exited events on button!");
 130         }
 131 
 132         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 133         toolkit.realSync();
 134 
 135         if (labelMouseReleasedCount != 1) {
 136             throw new RuntimeException("No MouseReleased event on label!");
 137         }
 138     }
 139 
 140     private static Point getCenterPoint(Component comp) {
 141         Point p = comp.getLocationOnScreen();
 142         Rectangle rect = comp.getBounds();
 143         return new Point(p.x + rect.width / 2, p.y + rect.height / 2);
 144     }
 145 
 146     private static void createAndShowGUI() {
 147 
 148         JFrame frame = new JFrame("Main Frame");
 149         frame.setLocation(100, 100);
 150         frame.setSize(300, 200);
 151         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 152 
 153         label = new JLabel("Label");
 154 
 155         DragWindowCreationMouseListener listener = new DragWindowCreationMouseListener(frame);
 156         label.addMouseListener(listener);
 157         label.addMouseMotionListener(listener);
 158 
 159         button = new JButton("Button");
 160         Panel panel = new Panel(new BorderLayout());
 161 
 162         panel.add(label, BorderLayout.NORTH);
 163         panel.add(button, BorderLayout.CENTER);
 164 
 165         frame.getContentPane().add(panel);
 166         frame.setVisible(true);
 167 
 168     }
 169 
 170     private static Point getAbsoluteLocation(MouseEvent e) {
 171         return new Point(e.getXOnScreen(), e.getYOnScreen());
 172     }
 173 
 174     static class MyDragWindow extends Window {
 175 
 176         public MyDragWindow(Window parent, Point location) {
 177             super(parent);
 178             setSize(500, 300);
 179             setVisible(true);
 180             JPanel panel = new JPanel();
 181             add(panel);
 182             setLocation(location.x - 250, location.y - 150);
 183             addMouseListener(new DragWindowMouseListener());
 184         }
 185 
 186         void dragTo(Point point) {
 187             setLocation(point.x - 250, point.y - 150);
 188         }
 189     }
 190 
 191     static class DragWindowCreationMouseListener extends MouseAdapter {
 192 
 193         Point origin;
 194         Window parent;
 195 
 196         public DragWindowCreationMouseListener(Window parent) {
 197             this.parent = parent;
 198         }
 199 
 200         @Override
 201         public void mousePressed(MouseEvent e) {
 202             if (dragWindow == null) {
 203                 dragWindow = new MyDragWindow(parent, getAbsoluteLocation(e));
 204             } else {
 205                 dragWindow.setVisible(true);
 206                 dragWindow.dragTo(getAbsoluteLocation(e));
 207             }
 208         }
 209 
 210         @Override
 211         public void mouseReleased(MouseEvent e) {
 212             labelMouseReleasedCount++;
 213             if (dragWindow != null) {
 214                 dragWindow.setVisible(false);
 215             }
 216         }
 217 
 218         public void mouseDragged(MouseEvent e) {
 219             if (dragWindow != null) {
 220                 dragWindow.dragTo(getAbsoluteLocation(e));
 221             }
 222         }
 223     }
 224 
 225     static class DragWindowMouseListener extends MouseAdapter {
 226 
 227         @Override
 228         public void mouseEntered(MouseEvent e) {
 229             dragWindowMouseEnteredCount++;
 230         }
 231 
 232         @Override
 233         public void mouseExited(MouseEvent e) {
 234             dragWindowMouseExitedCount++;
 235         }
 236 
 237         @Override
 238         public void mouseReleased(MouseEvent e) {
 239             dragWindowMouseReleasedCount++;
 240         }
 241     }
 242 
 243     static class LabelMouseListener extends MouseAdapter {
 244 
 245         @Override
 246         public void mouseEntered(MouseEvent e) {
 247             labelMouseEnteredCount++;
 248         }
 249 
 250         @Override
 251         public void mouseExited(MouseEvent e) {
 252             labelMouseExitedCount++;
 253         }
 254     }
 255 
 256     static class ButtonMouseListener extends MouseAdapter {
 257 
 258         @Override
 259         public void mouseEntered(MouseEvent e) {
 260             buttonMouseEnteredCount++;
 261         }
 262 
 263         @Override
 264         public void mouseExited(MouseEvent e) {
 265             buttonMouseExitedCount++;
 266         }
 267     }
 268 }