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