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