1 /*
   2  * Copyright (c) 2013, 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         Util.waitForIdle(null);
  60         try {
  61             Point loc = textArea.getLocationOnScreen();
  62             Util.drag(new Robot(),
  63                     new Point((int) loc.x + 3, (int) loc.y + 3),
  64                     new Point((int) loc.x + 40, (int) loc.y + 40),
  65                     InputEvent.BUTTON1_MASK);
  66         } catch (AWTException ex) {
  67             throw new RuntimeException("Could not initiate a drag operation");
  68         }
  69         Util.waitForIdle(null);
  70     }
  71 
  72     private static void constructTestUI() {
  73         final JFrame frame = new JFrame("Test frame");
  74         textArea = new JTextArea("Drag Me!");
  75         try {
  76             textArea.getDropTarget().addDropTargetListener(new DropTargetAdapter() {
  77                 @Override
  78                 public void drop(DropTargetDropEvent dtde) {
  79                     //IGNORE
  80                 }
  81 
  82                 @Override
  83                 public void dragOver(DropTargetDragEvent dtde) {
  84                     frame.dispose();
  85                 }
  86             });
  87         } catch (TooManyListenersException ex) {
  88             throw new RuntimeException(ex);
  89         }
  90         textArea.setSize(100, 100);
  91         textArea.setDragEnabled(true);
  92         textArea.select(0, textArea.getText().length());
  93         frame.add(textArea);
  94         frame.setBounds(100, 100, 100, 100);
  95         frame.setVisible(true);
  96     }
  97 }