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