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