1 /*
   2  * Copyright (c) 2005, 2006, 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 Programmatically resized  window does not receive mouse entered/exited events
  28  * @author  alexandr.scherbatiy area=awt.event
  29  * @run main ResizingFrameTest
  30  */
  31 /*
  32  * To change this template, choose Tools | Templates
  33  * and open the template in the editor.
  34  */
  35 
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 import javax.swing.*;
  39 import sun.awt.SunToolkit;
  40 
  41 public class ResizingFrameTest {
  42 
  43     private static volatile int mouseEnteredCount = 0;
  44     private static volatile int mouseExitedCount = 0;
  45     private static JFrame frame;
  46 
  47     public static void main(String[] args) throws Exception {
  48 
  49         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  50         Robot robot = new Robot();
  51         robot.setAutoDelay(50);
  52         robot.mouseMove(100, 100);
  53 
  54         // create a frame under the mouse cursor
  55         SwingUtilities.invokeAndWait(new Runnable() {
  56 
  57             @Override
  58             public void run() {
  59                 createAndShowGUI();
  60             }
  61         });
  62 
  63 
  64         toolkit.realSync();
  65 
  66         if (mouseEnteredCount != 1 || mouseExitedCount != 0) {
  67             throw new RuntimeException("No Mouse Entered/Exited events!");
  68         }
  69 
  70         // iconify frame
  71         SwingUtilities.invokeAndWait(new Runnable() {
  72 
  73             @Override
  74             public void run() {
  75                 frame.setExtendedState(Frame.ICONIFIED);
  76             }
  77         });
  78 
  79         toolkit.realSync();
  80         robot.delay(200);
  81 
  82         if (mouseEnteredCount != 1 || mouseExitedCount != 1) {
  83             throw new RuntimeException("No Mouse Entered/Exited events!");
  84         }
  85 
  86         // deiconify frame
  87         SwingUtilities.invokeAndWait(new Runnable() {
  88 
  89             @Override
  90             public void run() {
  91                 frame.setExtendedState(Frame.NORMAL);
  92             }
  93         });
  94 
  95         toolkit.realSync();
  96         robot.delay(200);
  97 
  98         if (mouseEnteredCount != 2 || mouseExitedCount != 1) {
  99             throw new RuntimeException("No Mouse Entered/Exited events!");
 100         }
 101 
 102         // move the mouse out of the frame
 103         robot.mouseMove(500, 500);
 104         toolkit.realSync();
 105         robot.delay(200);
 106 
 107         if (mouseEnteredCount != 2 || mouseExitedCount != 2) {
 108             throw new RuntimeException("No Mouse Entered/Exited events!");
 109         }
 110 
 111         // maximize the frame
 112         SwingUtilities.invokeAndWait(new Runnable() {
 113 
 114             @Override
 115             public void run() {
 116                 frame.setExtendedState(Frame.MAXIMIZED_BOTH);
 117             }
 118         });
 119 
 120         toolkit.realSync();
 121         robot.delay(200);
 122 
 123         if (mouseEnteredCount != 3 || mouseExitedCount != 2) {
 124             throw new RuntimeException("No Mouse Entered/Exited events!");
 125         }
 126 
 127 
 128         // demaximize the frame
 129         SwingUtilities.invokeAndWait(new Runnable() {
 130 
 131             @Override
 132             public void run() {
 133                 frame.setExtendedState(Frame.NORMAL);
 134             }
 135         });
 136 
 137         toolkit.realSync();
 138         robot.delay(200);
 139 
 140         if (mouseEnteredCount != 3 || mouseExitedCount != 3) {
 141             throw new RuntimeException("No Mouse Entered/Exited events!");
 142 
 143         }
 144 
 145         // move the frame under the mouse
 146         SwingUtilities.invokeAndWait(new Runnable() {
 147 
 148             @Override
 149             public void run() {
 150                 frame.setLocation(400, 400);
 151             }
 152         });
 153 
 154         toolkit.realSync();
 155         robot.delay(200);
 156 
 157         if (mouseEnteredCount != 4 || mouseExitedCount != 3) {
 158             throw new RuntimeException("No Mouse Entered/Exited events!");
 159         }
 160 
 161         // move the frame out of the mouse
 162         SwingUtilities.invokeAndWait(new Runnable() {
 163 
 164             @Override
 165             public void run() {
 166                 frame.setLocation(100, 100);
 167             }
 168         });
 169 
 170         toolkit.realSync();
 171         robot.delay(400);
 172 
 173         if (mouseEnteredCount != 4 || mouseExitedCount != 4) {
 174             throw new RuntimeException("No Mouse Entered/Exited events!");
 175         }
 176 
 177         // enlarge the frame bounds
 178         SwingUtilities.invokeAndWait(new Runnable() {
 179 
 180             @Override
 181             public void run() {
 182                 frame.setBounds(100, 100, 800, 800);
 183             }
 184         });
 185 
 186         toolkit.realSync();
 187         robot.delay(200);
 188 
 189         if (mouseEnteredCount != 5 || mouseExitedCount != 4) {
 190             throw new RuntimeException("No Mouse Entered/Exited events!");
 191         }
 192 
 193         // make the frame bounds smaller
 194         SwingUtilities.invokeAndWait(new Runnable() {
 195 
 196             @Override
 197             public void run() {
 198                 frame.setBounds(100, 100, 200, 300);
 199             }
 200         });
 201 
 202         toolkit.realSync();
 203         robot.delay(400);
 204 
 205 
 206         if (mouseEnteredCount != 5 || mouseExitedCount != 5) {
 207             throw new RuntimeException("No Mouse Entered/Exited events!");
 208         }
 209     }
 210 
 211     private static void createAndShowGUI() {
 212 
 213         frame = new JFrame("Main Frame");
 214         frame.setSize(300, 200);
 215         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 216 
 217         frame.addMouseListener(new MouseAdapter() {
 218 
 219             @Override
 220             public void mouseEntered(MouseEvent e) {
 221                 mouseEnteredCount++;
 222             }
 223 
 224             @Override
 225             public void mouseExited(MouseEvent e) {
 226                 mouseExitedCount++;
 227             }
 228         });
 229 
 230         frame.setVisible(true);
 231     }
 232 }