Path: blob/master/test/jdk/java/awt/Mouse/EnterExitEvents/DragWindowTest.java
41153 views
/*1* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 715404827* @summary Window created under a mouse does not receive mouse enter event.28* Mouse Entered/Exited events are wrongly generated during dragging the window29* from one component to another30* @library ../../regtesthelpers31* @build Util32* @author alexandr.scherbatiy area=awt.event33* @run main DragWindowTest34*/3536import java.awt.*;37import java.awt.event.*;38import javax.swing.*;3940import java.util.concurrent.*;4142import test.java.awt.regtesthelpers.Util;4344public class DragWindowTest {4546private static volatile int dragWindowMouseEnteredCount = 0;47private static volatile int dragWindowMouseReleasedCount = 0;48private static volatile int buttonMouseEnteredCount = 0;49private static volatile int labelMouseReleasedCount = 0;50private static MyDragWindow dragWindow;51private static JLabel label;52private static JButton button;5354public static void main(String[] args) throws Exception {5556Robot robot = new Robot();57robot.setAutoDelay(50);5859SwingUtilities.invokeAndWait(new Runnable() {6061@Override62public void run() {63createAndShowGUI();64}65});6667robot.waitForIdle();6869Point pointToClick = Util.invokeOnEDT(new Callable<Point>() {7071@Override72public Point call() throws Exception {73return getCenterPoint(label);74}75});767778robot.mouseMove(pointToClick.x, pointToClick.y);79robot.mousePress(InputEvent.BUTTON1_MASK);80robot.waitForIdle();8182if (dragWindowMouseEnteredCount != 1) {83throw new RuntimeException("No MouseEntered event on Drag Window!");84}8586Point pointToDrag = Util.invokeOnEDT(new Callable<Point>() {8788@Override89public Point call() throws Exception {90button.addMouseListener(new ButtonMouseListener());91return getCenterPoint(button);92}93});9495robot.mouseMove(pointToDrag.x, pointToDrag.y);96robot.waitForIdle();9798if (buttonMouseEnteredCount != 0) {99throw new RuntimeException("Extra MouseEntered event on button!");100}101102robot.mouseRelease(InputEvent.BUTTON1_MASK);103robot.waitForIdle();104105if (labelMouseReleasedCount != 1) {106throw new RuntimeException("No MouseReleased event on label!");107}108109}110111private static Point getCenterPoint(Component comp) {112Point p = comp.getLocationOnScreen();113Rectangle rect = comp.getBounds();114return new Point(p.x + rect.width / 2, p.y + rect.height / 2);115}116117private static void createAndShowGUI() {118119JFrame frame = new JFrame("Main Frame");120frame.setSize(300, 200);121frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);122123label = new JLabel("Label");124125LabelMouseListener listener = new LabelMouseListener(frame);126label.addMouseListener(listener);127label.addMouseMotionListener(listener);128129button = new JButton("Button");130Panel panel = new Panel(new BorderLayout());131132panel.add(label, BorderLayout.NORTH);133panel.add(button, BorderLayout.CENTER);134135frame.getContentPane().add(panel);136frame.setVisible(true);137138}139140private static Point getAbsoluteLocation(MouseEvent e) {141return new Point(e.getXOnScreen(), e.getYOnScreen());142}143144static class MyDragWindow extends Window {145146static int d = 30;147148public MyDragWindow(Window parent, Point location) {149super(parent);150setSize(150, 150);151setVisible(true);152JPanel panel = new JPanel();153add(panel);154setLocation(location.x - d, location.y - d);155addMouseListener(new DragWindowMouseListener());156}157158void dragTo(Point point) {159setLocation(point.x - d, point.y - d);160}161}162163static class LabelMouseListener extends MouseAdapter {164165Point origin;166Window parent;167168public LabelMouseListener(Window parent) {169this.parent = parent;170}171172@Override173public void mousePressed(MouseEvent e) {174if (dragWindow == null) {175dragWindow = new MyDragWindow(parent, getAbsoluteLocation(e));176} else {177dragWindow.setVisible(true);178dragWindow.dragTo(getAbsoluteLocation(e));179}180}181182@Override183public void mouseReleased(MouseEvent e) {184labelMouseReleasedCount++;185if (dragWindow != null) {186dragWindow.setVisible(false);187}188}189190public void mouseDragged(MouseEvent e) {191if (dragWindow != null) {192dragWindow.dragTo(getAbsoluteLocation(e));193}194}195}196197static class DragWindowMouseListener extends MouseAdapter {198199@Override200public void mouseEntered(MouseEvent e) {201dragWindowMouseEnteredCount++;202}203204@Override205public void mouseReleased(MouseEvent e) {206dragWindowMouseReleasedCount++;207}208}209210static class ButtonMouseListener extends MouseAdapter {211212@Override213public void mouseEntered(MouseEvent e) {214buttonMouseEnteredCount++;215}216}217}218219220