Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mouse/EnterExitEvents/ResizingFrameTest.java
47311 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* @bug 715404826* @summary Programmatically resized window does not receive mouse entered/exited events27* @author alexandr.scherbatiy area=awt.event28* @run main ResizingFrameTest29*/3031import java.awt.*;32import java.awt.event.*;33import javax.swing.*;3435public class ResizingFrameTest {3637private static volatile int mouseEnteredCount = 0;38private static volatile int mouseExitedCount = 0;39private static JFrame frame;4041public static void main(String[] args) throws Exception {4243Robot robot = new Robot();44robot.setAutoDelay(50);45robot.mouseMove(100, 100);46robot.delay(200);4748// create a frame under the mouse cursor49SwingUtilities.invokeAndWait(new Runnable() {5051@Override52public void run() {53createAndShowGUI();54}55});565758robot.waitForIdle();59robot.delay(1000);6061if (mouseEnteredCount != 1 || mouseExitedCount != 0) {62throw new RuntimeException("No Mouse Entered/Exited events!");63}6465// iconify frame66SwingUtilities.invokeAndWait(new Runnable() {6768@Override69public void run() {70frame.setExtendedState(Frame.ICONIFIED);71}72});7374robot.waitForIdle();75robot.delay(1000);7677if (mouseEnteredCount != 1 || mouseExitedCount != 1) {78throw new RuntimeException("No Mouse Entered/Exited events! "+mouseEnteredCount+", "+mouseExitedCount);79}8081// deiconify frame82SwingUtilities.invokeAndWait(new Runnable() {8384@Override85public void run() {86frame.setExtendedState(Frame.NORMAL);87}88});8990robot.waitForIdle();91robot.delay(1000);9293if (mouseEnteredCount != 2 || mouseExitedCount != 1) {94throw new RuntimeException("No Mouse Entered/Exited events!");95}9697// move the mouse out of the frame98robot.mouseMove(500, 500);99robot.waitForIdle();100robot.delay(1000);101102if (mouseEnteredCount != 2 || mouseExitedCount != 2) {103throw new RuntimeException("No Mouse Entered/Exited events!");104}105106// maximize the frame107SwingUtilities.invokeAndWait(new Runnable() {108109@Override110public void run() {111frame.setExtendedState(Frame.MAXIMIZED_BOTH);112}113});114115robot.waitForIdle();116robot.delay(1000);117118if (mouseEnteredCount != 3 || mouseExitedCount != 2) {119throw new RuntimeException("No Mouse Entered/Exited events!");120}121122123// demaximize the frame124SwingUtilities.invokeAndWait(new Runnable() {125126@Override127public void run() {128frame.setExtendedState(Frame.NORMAL);129}130});131132robot.waitForIdle();133robot.delay(1000);134135if (mouseEnteredCount != 3 || mouseExitedCount != 3) {136throw new RuntimeException("No Mouse Entered/Exited events!");137138}139140// move the frame under the mouse141SwingUtilities.invokeAndWait(new Runnable() {142143@Override144public void run() {145frame.setLocation(400, 400);146}147});148149robot.waitForIdle();150robot.delay(1000);151152if (mouseEnteredCount != 4 || mouseExitedCount != 3) {153throw new RuntimeException("No Mouse Entered/Exited events!");154}155156// move the frame out of the mouse157SwingUtilities.invokeAndWait(new Runnable() {158159@Override160public void run() {161frame.setLocation(100, 100);162}163});164165robot.waitForIdle();166robot.delay(400);167168if (mouseEnteredCount != 4 || mouseExitedCount != 4) {169throw new RuntimeException("No Mouse Entered/Exited events!");170}171172// enlarge the frame bounds173SwingUtilities.invokeAndWait(new Runnable() {174175@Override176public void run() {177frame.setBounds(100, 100, 800, 800);178}179});180181robot.waitForIdle();182robot.delay(200);183184if (mouseEnteredCount != 5 || mouseExitedCount != 4) {185throw new RuntimeException("No Mouse Entered/Exited events!");186}187188// make the frame bounds smaller189SwingUtilities.invokeAndWait(new Runnable() {190191@Override192public void run() {193frame.setBounds(100, 100, 200, 300);194}195});196197robot.waitForIdle();198robot.delay(400);199200201if (mouseEnteredCount != 5 || mouseExitedCount != 5) {202throw new RuntimeException("No Mouse Entered/Exited events!");203}204}205206private static void createAndShowGUI() {207208frame = new JFrame("Main Frame");209frame.setSize(300, 200);210frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);211212frame.addMouseListener(new MouseAdapter() {213214@Override215public void mouseEntered(MouseEvent e) {216mouseEnteredCount++;217}218219@Override220public void mouseExited(MouseEvent e) {221mouseExitedCount++;222}223});224225frame.setVisible(true);226}227}228229230