1/*
2 * Copyright (c) 2015, 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 8061636
28 * @summary fix for 7079254 changes behavior of MouseListener, MouseMotionListener
29 * @library ../../regtesthelpers
30 * @build Util
31 * @author Alexander Zvegintsev
32 * @run main RemovedComponentMouseListener
33 */
34
35import java.awt.Robot;
36import java.awt.event.InputEvent;
37import java.awt.event.MouseAdapter;
38import java.awt.event.MouseEvent;
39import javax.swing.*;
40import test.java.awt.regtesthelpers.Util;
41
42public class RemovedComponentMouseListener extends JFrame {
43
44    static boolean mouseReleasedReceived;
45    static JButton button;
46
47    public RemovedComponentMouseListener() {
48        JPanel panel = new JPanel();
49        JPanel buttonPanel = new JPanel();
50        button = new JButton("Button");
51
52        setSize(300, 300);
53
54        buttonPanel.add(button);
55        panel.add(buttonPanel);
56        setContentPane(panel);
57
58        button.addMouseListener(new MouseAdapter() {
59            @Override
60            public void mousePressed(MouseEvent e) {
61                buttonPanel.remove(button);
62                panel.add(button);
63                button.revalidate();
64                button.repaint();
65            }
66
67            @Override
68            public void mouseReleased(MouseEvent e) {
69                mouseReleasedReceived = true;
70            }
71        });
72
73        setVisible(true);
74    }
75
76    public static void main(String[] args) throws Exception {
77        SwingUtilities.invokeAndWait(() -> {
78            new RemovedComponentMouseListener();
79        });
80
81        Robot r = Util.createRobot();
82        r.setAutoDelay(100);
83        r.waitForIdle();
84        Util.pointOnComp(button, r);
85
86        r.waitForIdle();
87        r.mousePress(InputEvent.BUTTON1_MASK);
88        r.waitForIdle();
89        r.mouseRelease(InputEvent.BUTTON1_MASK);
90        r.waitForIdle();
91        if (!mouseReleasedReceived) {
92            throw new RuntimeException("mouseReleased event was not received");
93        }
94    }
95}
96