Path: blob/master/test/jdk/java/awt/Mouse/EnterExitEvents/ResizingFrameTest.java
41153 views
/*1* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 715404827* @summary Programmatically resized window does not receive mouse entered/exited events28* @author alexandr.scherbatiy area=awt.event29* @run main ResizingFrameTest30*/3132import java.awt.*;33import java.awt.event.*;34import javax.swing.*;3536public class ResizingFrameTest {3738private static volatile int mouseEnteredCount = 0;39private static volatile int mouseExitedCount = 0;40private static JFrame frame;4142public static void main(String[] args) throws Exception {4344Robot robot = new Robot();45robot.setAutoDelay(50);46robot.mouseMove(100, 100);47robot.delay(200);4849// create a frame under the mouse cursor50SwingUtilities.invokeAndWait(new Runnable() {5152@Override53public void run() {54createAndShowGUI();55}56});575859robot.waitForIdle();60robot.delay(1000);6162if (mouseEnteredCount != 1 || mouseExitedCount != 0) {63throw new RuntimeException("No Mouse Entered/Exited events!");64}6566// iconify frame67SwingUtilities.invokeAndWait(new Runnable() {6869@Override70public void run() {71frame.setExtendedState(Frame.ICONIFIED);72}73});7475robot.waitForIdle();76robot.delay(1000);7778if (mouseEnteredCount != 1 || mouseExitedCount != 1) {79throw new RuntimeException("No Mouse Entered/Exited events! "+mouseEnteredCount+", "+mouseExitedCount);80}8182// deiconify frame83SwingUtilities.invokeAndWait(new Runnable() {8485@Override86public void run() {87frame.setExtendedState(Frame.NORMAL);88}89});9091robot.waitForIdle();92robot.delay(1000);9394if (mouseEnteredCount != 2 || mouseExitedCount != 1) {95throw new RuntimeException("No Mouse Entered/Exited events!");96}9798// move the mouse out of the frame99robot.mouseMove(500, 500);100robot.waitForIdle();101robot.delay(1000);102103if (mouseEnteredCount != 2 || mouseExitedCount != 2) {104throw new RuntimeException("No Mouse Entered/Exited events!");105}106107// maximize the frame108SwingUtilities.invokeAndWait(new Runnable() {109110@Override111public void run() {112frame.setExtendedState(Frame.MAXIMIZED_BOTH);113}114});115116robot.waitForIdle();117robot.delay(1000);118119if (mouseEnteredCount != 3 || mouseExitedCount != 2) {120throw new RuntimeException("No Mouse Entered/Exited events!");121}122123124// demaximize the frame125SwingUtilities.invokeAndWait(new Runnable() {126127@Override128public void run() {129frame.setExtendedState(Frame.NORMAL);130}131});132133robot.waitForIdle();134robot.delay(1000);135136if (mouseEnteredCount != 3 || mouseExitedCount != 3) {137throw new RuntimeException("No Mouse Entered/Exited events!");138139}140141// move the frame under the mouse142SwingUtilities.invokeAndWait(new Runnable() {143144@Override145public void run() {146frame.setLocation(400, 400);147}148});149150robot.waitForIdle();151robot.delay(1000);152153if (mouseEnteredCount != 4 || mouseExitedCount != 3) {154throw new RuntimeException("No Mouse Entered/Exited events!");155}156157// move the frame out of the mouse158SwingUtilities.invokeAndWait(new Runnable() {159160@Override161public void run() {162frame.setLocation(100, 100);163}164});165166robot.waitForIdle();167robot.delay(400);168169if (mouseEnteredCount != 4 || mouseExitedCount != 4) {170throw new RuntimeException("No Mouse Entered/Exited events!");171}172173// enlarge the frame bounds174SwingUtilities.invokeAndWait(new Runnable() {175176@Override177public void run() {178frame.setBounds(100, 100, 800, 800);179}180});181182robot.waitForIdle();183robot.delay(200);184185if (mouseEnteredCount != 5 || mouseExitedCount != 4) {186throw new RuntimeException("No Mouse Entered/Exited events!");187}188189// make the frame bounds smaller190SwingUtilities.invokeAndWait(new Runnable() {191192@Override193public void run() {194frame.setBounds(100, 100, 200, 300);195}196});197198robot.waitForIdle();199robot.delay(400);200201202if (mouseEnteredCount != 5 || mouseExitedCount != 5) {203throw new RuntimeException("No Mouse Entered/Exited events!");204}205}206207private static void createAndShowGUI() {208209frame = new JFrame("Main Frame");210frame.setSize(300, 200);211frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);212213frame.addMouseListener(new MouseAdapter() {214215@Override216public void mouseEntered(MouseEvent e) {217mouseEnteredCount++;218}219220@Override221public void mouseExited(MouseEvent e) {222mouseExitedCount++;223}224});225226frame.setVisible(true);227}228}229230231