1/*
2 * Copyright (c) 2016, 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 * @key headful
27 * @bug 8050478
28 * @summary Cursor not updating correctly after closing a modal dialog.
29 *    The root cause of the issue was the lack of a mouse exit event
30 *    during displaying of a modal dialog.
31 * @author Dmitry Markov
32 * @library ../../regtesthelpers
33 * @build Util
34 * @run main ModalDialogEnterExitEventsTest
35 */
36
37import javax.swing.JButton;
38import javax.swing.JDialog;
39import javax.swing.JFrame;
40import javax.swing.SwingUtilities;
41import java.awt.FlowLayout;
42import java.awt.Frame;
43import java.awt.Robot;
44import java.awt.event.ActionEvent;
45import java.awt.event.ActionListener;
46import java.awt.event.MouseAdapter;
47import java.awt.event.MouseEvent;
48import java.util.concurrent.atomic.AtomicInteger;
49
50import test.java.awt.regtesthelpers.Util;
51
52public class ModalDialogEnterExitEventsTest {
53    private static volatile AtomicInteger mouseEnterCount = new AtomicInteger();
54    private static volatile AtomicInteger mouseExitCount = new AtomicInteger();
55
56    private static JFrame frame;
57    private static JButton openButton;
58    private static JButton closeButton;
59
60    public static void main(String[] args) {
61        Robot robot = Util.createRobot();
62
63        SwingUtilities.invokeLater(new Runnable() {
64            @Override
65            public void run() {
66                createAndShowGUI();
67            }
68        });
69        Util.waitForIdle(robot);
70
71        Util.clickOnComp(frame, robot, 500);
72        Util.waitForIdle(robot);
73
74        mouseEnterCount.set(0);
75        mouseExitCount.set(0);
76
77        Util.clickOnComp(openButton, robot, 500);
78        Util.waitForIdle(robot);
79        if (mouseExitCount.get() != 1) {
80            throw new RuntimeException("Test FAILED. Wrong number of MouseExited events = " + mouseExitCount.get());
81        }
82
83        Util.clickOnComp(closeButton, robot, 500);
84        Util.waitForIdle(robot);
85        if (mouseEnterCount.get() != 1) {
86            throw new RuntimeException("Test FAILED. Wrong number of MouseEntered events = "+ mouseEnterCount.get());
87        }
88    }
89
90    private static void createAndShowGUI() {
91        frame = new JFrame("ModalDialogEnterExitEventsTest");
92        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
93        frame.setLayout(new FlowLayout());
94        frame.addMouseListener(new MouseAdapter() {
95            @Override
96            public void mouseExited(MouseEvent e) {
97                mouseExitCount.getAndIncrement();
98            }
99
100            @Override
101            public void mouseEntered(MouseEvent e) {
102                mouseEnterCount.getAndIncrement();
103            }
104        });
105        openButton = new JButton("Open Dialog");
106        openButton.addActionListener(new ActionListener() {
107            @Override
108            public void actionPerformed(ActionEvent e) {
109                JDialog dialog = new JDialog(frame, "Modal Dialog", true);
110                dialog.setLayout(new FlowLayout());
111                closeButton = new JButton("Close");
112                closeButton.addActionListener(new ActionListener() {
113                    @Override
114                    public void actionPerformed(ActionEvent e) {
115                        dialog.dispose();
116                    }
117                });
118                dialog.add(closeButton);
119                dialog.setSize(200, 200);
120                dialog.setLocationRelativeTo(null);
121                dialog.setVisible(true);
122            }
123        });
124        frame.add(openButton);
125        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
126        frame.setVisible(true);
127    }
128}
129
130