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