Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mouse/EnterExitEvents/ModalDialogEnterExitEventsTest.java
47311 views
/*1* Copyright (c) 2016, 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 805047826* @summary Cursor not updating correctly after closing a modal dialog.27* The root cause of the issue was the lack of a mouse exit event28* during displaying of a modal dialog.29* @author Dmitry Markov30* @library ../../regtesthelpers31* @build Util32* @run main ModalDialogEnterExitEventsTest33*/3435import javax.swing.JButton;36import javax.swing.JDialog;37import javax.swing.JFrame;38import javax.swing.SwingUtilities;39import java.awt.FlowLayout;40import java.awt.Frame;41import java.awt.Robot;42import java.awt.event.ActionEvent;43import java.awt.event.ActionListener;44import java.awt.event.MouseAdapter;45import java.awt.event.MouseEvent;46import java.util.concurrent.atomic.AtomicInteger;4748import test.java.awt.regtesthelpers.Util;4950public class ModalDialogEnterExitEventsTest {51private static volatile AtomicInteger mouseEnterCount = new AtomicInteger();52private static volatile AtomicInteger mouseExitCount = new AtomicInteger();5354private static JFrame frame;55private static JButton openButton;56private static JButton closeButton;5758public static void main(String[] args) {59Robot robot = Util.createRobot();6061SwingUtilities.invokeLater(new Runnable() {62@Override63public void run() {64createAndShowGUI();65}66});67Util.waitForIdle(robot);6869Util.clickOnComp(frame, robot, 500);70Util.waitForIdle(robot);7172mouseEnterCount.set(0);73mouseExitCount.set(0);7475Util.clickOnComp(openButton, robot, 500);76Util.waitForIdle(robot);77if (mouseExitCount.get() != 1) {78throw new RuntimeException("Test FAILED. Wrong number of MouseExited events = " + mouseExitCount.get());79}8081Util.clickOnComp(closeButton, robot, 500);82Util.waitForIdle(robot);83if (mouseEnterCount.get() != 1) {84throw new RuntimeException("Test FAILED. Wrong number of MouseEntered events = "+ mouseEnterCount.get());85}86}8788private static void createAndShowGUI() {89frame = new JFrame("ModalDialogEnterExitEventsTest");90frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);91frame.setLayout(new FlowLayout());92frame.addMouseListener(new MouseAdapter() {93@Override94public void mouseExited(MouseEvent e) {95mouseExitCount.getAndIncrement();96}9798@Override99public void mouseEntered(MouseEvent e) {100mouseEnterCount.getAndIncrement();101}102});103openButton = new JButton("Open Dialog");104openButton.addActionListener(new ActionListener() {105@Override106public void actionPerformed(ActionEvent e) {107JDialog dialog = new JDialog(frame, "Modal Dialog", true);108dialog.setLayout(new FlowLayout());109closeButton = new JButton("Close");110closeButton.addActionListener(new ActionListener() {111@Override112public void actionPerformed(ActionEvent e) {113dialog.dispose();114}115});116dialog.add(closeButton);117dialog.setSize(200, 200);118dialog.setLocationRelativeTo(null);119dialog.setVisible(true);120}121});122frame.add(openButton);123frame.setExtendedState(Frame.MAXIMIZED_BOTH);124frame.setVisible(true);125}126}127128129130