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 Resized programmatically window does not receive mouse entered/exited events
  28     @author  alexandr.scherbatiy area=awt.event
  29     @run main FrameStateTest
  30  */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import javax.swing.*;
  35 import sun.awt.SunToolkit;
  36 
  37 public class FrameStateTest {
  38 
  39     private static volatile int mouseEnteredCount = 0;
  40     private static volatile int mouseExitedCount = 0;
  41     private static JFrame frame;
  42 
  43     public static void main(String[] args) throws Exception {
  44 
  45         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  46         Robot robot = new Robot();
  47         robot.setAutoDelay(50);
  48         robot.mouseMove(100, 100);
  49 
  50         SwingUtilities.invokeAndWait(new Runnable() {
  51 
  52             @Override
  53             public void run() {
  54                 createAndShowGUI();
  55             }
  56         });
  57 
  58 
  59         toolkit.realSync();
  60 
  61         if (mouseEnteredCount != 1) {
  62             throw new RuntimeException("No Mouse Entered event!");
  63         }
  64 
  65         SwingUtilities.invokeAndWait(new Runnable() {
  66 
  67             @Override
  68             public void run() {
  69                 frame.setExtendedState(Frame.ICONIFIED);
  70             }
  71         });
  72 
  73         toolkit.realSync();
  74 
  75         if (mouseExitedCount != 1) {
  76             throw new RuntimeException("No Mouse Exited event!");
  77         }
  78 
  79         SwingUtilities.invokeAndWait(new Runnable() {
  80 
  81             @Override
  82             public void run() {
  83                 System.out.println("NORMAL");
  84                 frame.setExtendedState(Frame.NORMAL);
  85             }
  86         });
  87 
  88         toolkit.realSync();
  89 
  90         if (mouseEnteredCount != 2) {
  91             throw new RuntimeException("No Mouse Entered event!");
  92         }
  93 
  94         robot.mouseMove(500, 500);
  95 
  96         toolkit.realSync();
  97 
  98         if (mouseExitedCount != 2) {
  99             throw new RuntimeException("No Mouse Exited event!");
 100         }
 101 
 102 
 103         SwingUtilities.invokeAndWait(new Runnable() {
 104 
 105             @Override
 106             public void run() {
 107                 frame.setExtendedState(Frame.MAXIMIZED_BOTH);
 108             }
 109         });
 110 
 111         toolkit.realSync();
 112 
 113         if (mouseEnteredCount != 3) {
 114             throw new RuntimeException("No Mouse Entered event!");
 115         }
 116 
 117 
 118         SwingUtilities.invokeAndWait(new Runnable() {
 119 
 120             @Override
 121             public void run() {
 122                 frame.setExtendedState(Frame.NORMAL);
 123             }
 124         });
 125 
 126         toolkit.realSync();
 127 
 128         if (mouseExitedCount != 3) {
 129             throw new RuntimeException("No Mouse Exited event!");
 130         }
 131 
 132     }
 133 
 134     private static void createAndShowGUI() {
 135 
 136         frame = new JFrame("Main Frame");
 137         frame.setSize(300, 200);
 138         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 139 
 140         frame.addMouseListener(new MouseAdapter() {
 141 
 142             @Override
 143             public void mouseEntered(MouseEvent e) {
 144                 mouseEnteredCount++;
 145             }
 146 
 147             @Override
 148             public void mouseExited(MouseEvent e) {
 149                 mouseExitedCount++;
 150             }
 151         });
 152 
 153         frame.setVisible(true);
 154     }
 155 }