1/*
2 * Copyright (c) 2013, 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 @summary JVM crash if the frame is disposed in DropTargetListener
26 * @key headful
27 * @author Petr Pchelko
28 * @library ../../regtesthelpers
29 * @build Util
30 * @compile DisposeFrameOnDragTest.java
31 * @run main/othervm DisposeFrameOnDragTest
32 */
33import java.awt.AWTException;
34import java.awt.Point;
35import java.awt.Robot;
36import java.awt.dnd.DropTargetAdapter;
37import java.awt.dnd.DropTargetDragEvent;
38import java.awt.dnd.DropTargetDropEvent;
39import java.awt.event.InputEvent;
40import java.lang.reflect.InvocationTargetException;
41import java.util.TooManyListenersException;
42import javax.swing.JFrame;
43import javax.swing.JTextArea;
44import javax.swing.SwingUtilities;
45import test.java.awt.regtesthelpers.Util;
46
47public class DisposeFrameOnDragTest {
48
49    private static JTextArea textArea;
50
51    public static void main(String[] args) throws Throwable {
52
53        SwingUtilities.invokeAndWait(new Runnable() {
54            @Override
55            public void run() {
56                constructTestUI();
57            }
58        });
59
60        Robot testRobot = null;
61        try {
62            testRobot = new Robot();
63        } catch(AWTException ex) {
64            throw new RuntimeException("Error while creating Robot");
65        }
66
67        Util.waitForIdle(testRobot);
68
69        Point loc = textArea.getLocationOnScreen();
70        Util.drag(testRobot,
71                new Point((int) loc.x + 3, (int) loc.y + 3),
72                new Point((int) loc.x + 40, (int) loc.y + 40),
73                InputEvent.BUTTON1_MASK);
74
75        Util.waitForIdle(testRobot);
76
77        testRobot.delay(200);
78    }
79
80    private static void constructTestUI() {
81        final JFrame frame = new JFrame("Test frame");
82        textArea = new JTextArea("Drag Me!");
83        try {
84            textArea.getDropTarget().addDropTargetListener(new DropTargetAdapter() {
85                @Override
86                public void drop(DropTargetDropEvent dtde) {
87                    //IGNORE
88                }
89
90                @Override
91                public void dragOver(DropTargetDragEvent dtde) {
92                    frame.dispose();
93                }
94            });
95        } catch (TooManyListenersException ex) {
96            throw new RuntimeException(ex);
97        }
98        textArea.setSize(100, 100);
99        textArea.setDragEnabled(true);
100        textArea.select(0, textArea.getText().length());
101        frame.add(textArea);
102        frame.setBounds(100, 100, 100, 100);
103        frame.setVisible(true);
104    }
105}
106