Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mouse/EnterExitEvents/DragWindowTest.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 are wrongly generated during dragging the window28* from one component to another29* @library ../../regtesthelpers30* @build Util31* @author alexandr.scherbatiy area=awt.event32* @run main DragWindowTest33*/3435import java.awt.*;36import java.awt.event.*;37import javax.swing.*;3839import java.util.concurrent.*;4041import test.java.awt.regtesthelpers.Util;4243public class DragWindowTest {4445private static volatile int dragWindowMouseEnteredCount = 0;46private static volatile int dragWindowMouseReleasedCount = 0;47private static volatile int buttonMouseEnteredCount = 0;48private static volatile int labelMouseReleasedCount = 0;49private static MyDragWindow dragWindow;50private static JLabel label;51private static JButton button;5253public static void main(String[] args) throws Exception {5455Robot robot = new Robot();56robot.setAutoDelay(50);5758SwingUtilities.invokeAndWait(new Runnable() {5960@Override61public void run() {62createAndShowGUI();63}64});6566robot.waitForIdle();6768Point pointToClick = Util.invokeOnEDT(new Callable<Point>() {6970@Override71public Point call() throws Exception {72return getCenterPoint(label);73}74});757677robot.mouseMove(pointToClick.x, pointToClick.y);78robot.mousePress(InputEvent.BUTTON1_MASK);79robot.waitForIdle();8081if (dragWindowMouseEnteredCount != 1) {82throw new RuntimeException("No MouseEntered event on Drag Window!");83}8485Point pointToDrag = Util.invokeOnEDT(new Callable<Point>() {8687@Override88public Point call() throws Exception {89button.addMouseListener(new ButtonMouseListener());90return getCenterPoint(button);91}92});9394robot.mouseMove(pointToDrag.x, pointToDrag.y);95robot.waitForIdle();9697if (buttonMouseEnteredCount != 0) {98throw new RuntimeException("Extra MouseEntered event on button!");99}100101robot.mouseRelease(InputEvent.BUTTON1_MASK);102robot.waitForIdle();103104if (labelMouseReleasedCount != 1) {105throw new RuntimeException("No MouseReleased event on label!");106}107108}109110private static Point getCenterPoint(Component comp) {111Point p = comp.getLocationOnScreen();112Rectangle rect = comp.getBounds();113return new Point(p.x + rect.width / 2, p.y + rect.height / 2);114}115116private static void createAndShowGUI() {117118JFrame frame = new JFrame("Main Frame");119frame.setSize(300, 200);120frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);121122label = new JLabel("Label");123124LabelMouseListener listener = new LabelMouseListener(frame);125label.addMouseListener(listener);126label.addMouseMotionListener(listener);127128button = new JButton("Button");129Panel panel = new Panel(new BorderLayout());130131panel.add(label, BorderLayout.NORTH);132panel.add(button, BorderLayout.CENTER);133134frame.getContentPane().add(panel);135frame.setVisible(true);136137}138139private static Point getAbsoluteLocation(MouseEvent e) {140return new Point(e.getXOnScreen(), e.getYOnScreen());141}142143static class MyDragWindow extends Window {144145static int d = 30;146147public MyDragWindow(Window parent, Point location) {148super(parent);149setSize(150, 150);150setVisible(true);151JPanel panel = new JPanel();152add(panel);153setLocation(location.x - d, location.y - d);154addMouseListener(new DragWindowMouseListener());155}156157void dragTo(Point point) {158setLocation(point.x - d, point.y - d);159}160}161162static class LabelMouseListener extends MouseAdapter {163164Point origin;165Window parent;166167public LabelMouseListener(Window parent) {168this.parent = parent;169}170171@Override172public void mousePressed(MouseEvent e) {173if (dragWindow == null) {174dragWindow = new MyDragWindow(parent, getAbsoluteLocation(e));175} else {176dragWindow.setVisible(true);177dragWindow.dragTo(getAbsoluteLocation(e));178}179}180181@Override182public void mouseReleased(MouseEvent e) {183labelMouseReleasedCount++;184if (dragWindow != null) {185dragWindow.setVisible(false);186}187}188189public void mouseDragged(MouseEvent e) {190if (dragWindow != null) {191dragWindow.dragTo(getAbsoluteLocation(e));192}193}194}195196static class DragWindowMouseListener extends MouseAdapter {197198@Override199public void mouseEntered(MouseEvent e) {200dragWindowMouseEnteredCount++;201}202203@Override204public void mouseReleased(MouseEvent e) {205dragWindowMouseReleasedCount++;206}207}208209static class ButtonMouseListener extends MouseAdapter {210211@Override212public void mouseEntered(MouseEvent e) {213buttonMouseEnteredCount++;214}215}216}217218219