Path: blob/master/test/jdk/java/awt/Mouse/EnterExitEvents/DragWindowOutOfFrameTest.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 should be generated during dragging the window29* out of the frame and to the frame.30* @library ../../regtesthelpers31* @build Util32* @author alexandr.scherbatiy area=awt.event33* @run main DragWindowOutOfFrameTest34*/35import java.awt.*;36import java.awt.event.*;37import javax.swing.*;3839import java.util.concurrent.*;4041import test.java.awt.regtesthelpers.Util;4243public class DragWindowOutOfFrameTest {4445private static volatile int dragWindowMouseEnteredCount = 0;46private static volatile int dragWindowMouseExitedCount = 0;47private static volatile int dragWindowMouseReleasedCount = 0;48private static volatile int buttonMouseEnteredCount = 0;49private static volatile int buttonMouseExitedCount = 0;50private static volatile int labelMouseEnteredCount = 0;51private static volatile int labelMouseExitedCount = 0;52private static volatile int labelMouseReleasedCount = 0;53private static MyDragWindow dragWindow;54private static JLabel label;55private static JButton button;5657public static void main(String[] args) throws Exception {5859Robot robot = new Robot();60robot.setAutoDelay(50);6162SwingUtilities.invokeAndWait(new Runnable() {6364@Override65public void run() {66createAndShowGUI();67}68});6970robot.waitForIdle();7172Point pointToClick = Util.invokeOnEDT(new Callable<Point>() {7374@Override75public Point call() throws Exception {76return getCenterPoint(label);77}78});798081robot.mouseMove(pointToClick.x, pointToClick.y);82robot.mousePress(InputEvent.BUTTON1_MASK);83robot.waitForIdle();8485if (dragWindowMouseEnteredCount != 1 && dragWindowMouseExitedCount != 0) {86throw new RuntimeException(87"Wrong number mouse Entered/Exited events on Drag Window!");88}8990Point pointToDrag = Util.invokeOnEDT(new Callable<Point>() {9192@Override93public Point call() throws Exception {94label.addMouseListener(new LabelMouseListener());95button.addMouseListener(new ButtonMouseListener());96return getCenterPoint(button);97}98});99100robot.mouseMove(450, pointToClick.y);101robot.waitForIdle();102103if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {104throw new RuntimeException(105"Wrong number Mouse Entered/Exited events on label!");106}107108robot.mouseMove(450, pointToDrag.y);109robot.waitForIdle();110111if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {112throw new RuntimeException(113"Wrong number Mouse Entered/Exited events on label!");114}115116if (buttonMouseEnteredCount != 0 && buttonMouseExitedCount != 0) {117throw new RuntimeException(118"Wrong number Mouse Entered/Exited events on button!");119}120121robot.mouseMove(pointToDrag.y, pointToDrag.y);122robot.waitForIdle();123124if (buttonMouseEnteredCount != 1 && buttonMouseExitedCount != 0) {125throw new RuntimeException(126"Wrong number Mouse Entered/Exited events on button!");127}128129robot.mouseRelease(InputEvent.BUTTON1_MASK);130robot.waitForIdle();131132if (labelMouseReleasedCount != 1) {133throw new RuntimeException("No MouseReleased event on label!");134}135}136137private static Point getCenterPoint(Component comp) {138Point p = comp.getLocationOnScreen();139Rectangle rect = comp.getBounds();140return new Point(p.x + rect.width / 2, p.y + rect.height / 2);141}142143private static void createAndShowGUI() {144145JFrame frame = new JFrame("Main Frame");146frame.setLocation(100, 100);147frame.setSize(300, 200);148frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);149150label = new JLabel("Label");151152DragWindowCreationMouseListener listener = new DragWindowCreationMouseListener(frame);153label.addMouseListener(listener);154label.addMouseMotionListener(listener);155156button = new JButton("Button");157Panel panel = new Panel(new BorderLayout());158159panel.add(label, BorderLayout.NORTH);160panel.add(button, BorderLayout.CENTER);161162frame.getContentPane().add(panel);163frame.setVisible(true);164165}166167private static Point getAbsoluteLocation(MouseEvent e) {168return new Point(e.getXOnScreen(), e.getYOnScreen());169}170171static class MyDragWindow extends Window {172173public MyDragWindow(Window parent, Point location) {174super(parent);175setSize(500, 300);176setVisible(true);177JPanel panel = new JPanel();178add(panel);179setLocation(location.x - 250, location.y - 150);180addMouseListener(new DragWindowMouseListener());181}182183void dragTo(Point point) {184setLocation(point.x - 250, point.y - 150);185}186}187188static class DragWindowCreationMouseListener extends MouseAdapter {189190Point origin;191Window parent;192193public DragWindowCreationMouseListener(Window parent) {194this.parent = parent;195}196197@Override198public void mousePressed(MouseEvent e) {199if (dragWindow == null) {200dragWindow = new MyDragWindow(parent, getAbsoluteLocation(e));201} else {202dragWindow.setVisible(true);203dragWindow.dragTo(getAbsoluteLocation(e));204}205}206207@Override208public void mouseReleased(MouseEvent e) {209labelMouseReleasedCount++;210if (dragWindow != null) {211dragWindow.setVisible(false);212}213}214215public void mouseDragged(MouseEvent e) {216if (dragWindow != null) {217dragWindow.dragTo(getAbsoluteLocation(e));218}219}220}221222static class DragWindowMouseListener extends MouseAdapter {223224@Override225public void mouseEntered(MouseEvent e) {226dragWindowMouseEnteredCount++;227}228229@Override230public void mouseExited(MouseEvent e) {231dragWindowMouseExitedCount++;232}233234@Override235public void mouseReleased(MouseEvent e) {236dragWindowMouseReleasedCount++;237}238}239240static class LabelMouseListener extends MouseAdapter {241242@Override243public void mouseEntered(MouseEvent e) {244labelMouseEnteredCount++;245}246247@Override248public void mouseExited(MouseEvent e) {249labelMouseExitedCount++;250}251}252253static class ButtonMouseListener extends MouseAdapter {254255@Override256public void mouseEntered(MouseEvent e) {257buttonMouseEnteredCount++;258}259260@Override261public void mouseExited(MouseEvent e) {262buttonMouseExitedCount++;263}264}265}266267268