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