Path: blob/master/test/jdk/java/awt/Mouse/EnterExitEvents/ModalDialogEnterExitEventsTest.java
41153 views
/*1* Copyright (c) 2016, 2019, 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 805047827* @summary Cursor not updating correctly after closing a modal dialog.28* The root cause of the issue was the lack of a mouse exit event29* during displaying of a modal dialog.30* @author Dmitry Markov31* @library ../../regtesthelpers32* @build Util33* @run main ModalDialogEnterExitEventsTest34*/3536import javax.swing.JButton;37import javax.swing.JDialog;38import javax.swing.JFrame;39import javax.swing.SwingUtilities;40import java.awt.FlowLayout;41import java.awt.Frame;42import java.awt.Robot;43import java.awt.event.ActionEvent;44import java.awt.event.ActionListener;45import java.awt.event.MouseAdapter;46import java.awt.event.MouseEvent;47import java.util.concurrent.atomic.AtomicInteger;4849import test.java.awt.regtesthelpers.Util;5051public class ModalDialogEnterExitEventsTest {52private static volatile AtomicInteger mouseEnterCount = new AtomicInteger();53private static volatile AtomicInteger mouseExitCount = new AtomicInteger();5455private static JFrame frame;56private static JDialog dialog;57private static JButton openButton;58private static JButton closeButton;5960public static void main(String[] args) throws Exception {61try {62Robot robot = Util.createRobot();6364SwingUtilities.invokeAndWait(new Runnable() {65@Override66public void run() {67createAndShowGUI();68}69});70Util.waitForIdle(robot);7172Util.clickOnComp(frame, robot, 500);73Util.waitForIdle(robot);7475mouseEnterCount.set(0);76mouseExitCount.set(0);7778Util.clickOnComp(openButton, robot, 500);79Util.waitForIdle(robot);80Util.waitTillShown(dialog);81if (mouseExitCount.get() != 1) {82throw new RuntimeException("Test FAILED. Wrong number of "83+ "MouseExited events = " + mouseExitCount.get());84}8586Util.clickOnComp(closeButton, robot, 500);87Util.waitForIdle(robot);88robot.delay(200);89if (mouseEnterCount.get() != 1) {90throw new RuntimeException("Test FAILED. Wrong number of "91+ "MouseEntered events = "+ mouseEnterCount.get());92}93} finally {94if (frame != null) {95SwingUtilities.invokeAndWait(() -> frame.dispose());96}97}98}99100private static void createAndShowGUI() {101frame = new JFrame("ModalDialogEnterExitEventsTest");102frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);103frame.setLayout(new FlowLayout());104frame.addMouseListener(new MouseAdapter() {105@Override106public void mouseExited(MouseEvent e) {107mouseExitCount.getAndIncrement();108}109110@Override111public void mouseEntered(MouseEvent e) {112mouseEnterCount.getAndIncrement();113}114});115openButton = new JButton("Open Dialog");116openButton.addActionListener(new ActionListener() {117@Override118public void actionPerformed(ActionEvent e) {119dialog = new JDialog(frame, "Modal Dialog", true);120dialog.setLayout(new FlowLayout());121closeButton = new JButton("Close");122closeButton.addActionListener(new ActionListener() {123@Override124public void actionPerformed(ActionEvent e) {125dialog.dispose();126}127});128dialog.add(closeButton);129dialog.setSize(200, 200);130dialog.setLocationRelativeTo(null);131dialog.setVisible(true);132}133});134frame.add(openButton);135frame.setExtendedState(Frame.MAXIMIZED_BOTH);136frame.setVisible(true);137}138}139140141142