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