Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mouse/EnterExitEvents/DragWindowOutOfFrameTest.java
47311 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* @bug 715404826* @summary Window created under a mouse does not receive mouse enter event.27* Mouse Entered/Exited events should be generated during dragging the window28* out of the frame and to the frame.29* @library ../../regtesthelpers30* @build Util31* @author alexandr.scherbatiy area=awt.event32* @run main DragWindowOutOfFrameTest33*/34import java.awt.*;35import java.awt.event.*;36import javax.swing.*;3738import java.util.concurrent.*;3940import test.java.awt.regtesthelpers.Util;4142public class DragWindowOutOfFrameTest {4344private static volatile int dragWindowMouseEnteredCount = 0;45private static volatile int dragWindowMouseExitedCount = 0;46private static volatile int dragWindowMouseReleasedCount = 0;47private static volatile int buttonMouseEnteredCount = 0;48private static volatile int buttonMouseExitedCount = 0;49private static volatile int labelMouseEnteredCount = 0;50private static volatile int labelMouseExitedCount = 0;51private static volatile int labelMouseReleasedCount = 0;52private static MyDragWindow dragWindow;53private static JLabel label;54private static JButton button;5556public static void main(String[] args) throws Exception {5758Robot robot = new Robot();59robot.setAutoDelay(50);6061SwingUtilities.invokeAndWait(new Runnable() {6263@Override64public void run() {65createAndShowGUI();66}67});6869robot.waitForIdle();7071Point pointToClick = Util.invokeOnEDT(new Callable<Point>() {7273@Override74public Point call() throws Exception {75return getCenterPoint(label);76}77});787980robot.mouseMove(pointToClick.x, pointToClick.y);81robot.mousePress(InputEvent.BUTTON1_MASK);82robot.waitForIdle();8384if (dragWindowMouseEnteredCount != 1 && dragWindowMouseExitedCount != 0) {85throw new RuntimeException(86"Wrong number mouse Entered/Exited events on Drag Window!");87}8889Point pointToDrag = Util.invokeOnEDT(new Callable<Point>() {9091@Override92public Point call() throws Exception {93label.addMouseListener(new LabelMouseListener());94button.addMouseListener(new ButtonMouseListener());95return getCenterPoint(button);96}97});9899robot.mouseMove(450, pointToClick.y);100robot.waitForIdle();101102if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {103throw new RuntimeException(104"Wrong number Mouse Entered/Exited events on label!");105}106107robot.mouseMove(450, pointToDrag.y);108robot.waitForIdle();109110if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {111throw new RuntimeException(112"Wrong number Mouse Entered/Exited events on label!");113}114115if (buttonMouseEnteredCount != 0 && buttonMouseExitedCount != 0) {116throw new RuntimeException(117"Wrong number Mouse Entered/Exited events on button!");118}119120robot.mouseMove(pointToDrag.y, pointToDrag.y);121robot.waitForIdle();122123if (buttonMouseEnteredCount != 1 && buttonMouseExitedCount != 0) {124throw new RuntimeException(125"Wrong number Mouse Entered/Exited events on button!");126}127128robot.mouseRelease(InputEvent.BUTTON1_MASK);129robot.waitForIdle();130131if (labelMouseReleasedCount != 1) {132throw new RuntimeException("No MouseReleased event on label!");133}134}135136private static Point getCenterPoint(Component comp) {137Point p = comp.getLocationOnScreen();138Rectangle rect = comp.getBounds();139return new Point(p.x + rect.width / 2, p.y + rect.height / 2);140}141142private static void createAndShowGUI() {143144JFrame frame = new JFrame("Main Frame");145frame.setLocation(100, 100);146frame.setSize(300, 200);147frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);148149label = new JLabel("Label");150151DragWindowCreationMouseListener listener = new DragWindowCreationMouseListener(frame);152label.addMouseListener(listener);153label.addMouseMotionListener(listener);154155button = new JButton("Button");156Panel panel = new Panel(new BorderLayout());157158panel.add(label, BorderLayout.NORTH);159panel.add(button, BorderLayout.CENTER);160161frame.getContentPane().add(panel);162frame.setVisible(true);163164}165166private static Point getAbsoluteLocation(MouseEvent e) {167return new Point(e.getXOnScreen(), e.getYOnScreen());168}169170static class MyDragWindow extends Window {171172public MyDragWindow(Window parent, Point location) {173super(parent);174setSize(500, 300);175setVisible(true);176JPanel panel = new JPanel();177add(panel);178setLocation(location.x - 250, location.y - 150);179addMouseListener(new DragWindowMouseListener());180}181182void dragTo(Point point) {183setLocation(point.x - 250, point.y - 150);184}185}186187static class DragWindowCreationMouseListener extends MouseAdapter {188189Point origin;190Window parent;191192public DragWindowCreationMouseListener(Window parent) {193this.parent = parent;194}195196@Override197public void mousePressed(MouseEvent e) {198if (dragWindow == null) {199dragWindow = new MyDragWindow(parent, getAbsoluteLocation(e));200} else {201dragWindow.setVisible(true);202dragWindow.dragTo(getAbsoluteLocation(e));203}204}205206@Override207public void mouseReleased(MouseEvent e) {208labelMouseReleasedCount++;209if (dragWindow != null) {210dragWindow.setVisible(false);211}212}213214public void mouseDragged(MouseEvent e) {215if (dragWindow != null) {216dragWindow.dragTo(getAbsoluteLocation(e));217}218}219}220221static class DragWindowMouseListener extends MouseAdapter {222223@Override224public void mouseEntered(MouseEvent e) {225dragWindowMouseEnteredCount++;226}227228@Override229public void mouseExited(MouseEvent e) {230dragWindowMouseExitedCount++;231}232233@Override234public void mouseReleased(MouseEvent e) {235dragWindowMouseReleasedCount++;236}237}238239static class LabelMouseListener extends MouseAdapter {240241@Override242public void mouseEntered(MouseEvent e) {243labelMouseEnteredCount++;244}245246@Override247public void mouseExited(MouseEvent e) {248labelMouseExitedCount++;249}250}251252static class ButtonMouseListener extends MouseAdapter {253254@Override255public void mouseEntered(MouseEvent e) {256buttonMouseEnteredCount++;257}258259@Override260public void mouseExited(MouseEvent e) {261buttonMouseExitedCount++;262}263}264}265266267