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
  26  * @key headful
  27  * @bug 8024163
  28  * @summary Checks that dragExit is generated when the new DropTarget is created under the drag
  29  * @library ../../regtesthelpers
  30  * @build Util
  31  * @compile MissedDragExitTest.java
  32  * @run main/othervm MissedDragExitTest
  33  * @author Petr Pchelko
  34  */
  35 
  36 import test.java.awt.regtesthelpers.Util;
  37 
  38 import javax.swing.*;
  39 import java.awt.*;
  40 import java.awt.datatransfer.StringSelection;
  41 import java.awt.dnd.DnDConstants;
  42 import java.awt.dnd.DragGestureEvent;
  43 import java.awt.dnd.DragGestureListener;
  44 import java.awt.dnd.DragSource;
  45 import java.awt.dnd.DropTarget;
  46 import java.awt.dnd.DropTargetAdapter;
  47 import java.awt.dnd.DropTargetDragEvent;
  48 import java.awt.dnd.DropTargetDropEvent;
  49 import java.awt.dnd.DropTargetEvent;
  50 import java.awt.event.InputEvent;
  51 
  52 public class MissedDragExitTest {
  53 
  54     private static final int FRAME_SIZE = 100;
  55     private static final int FRAME_LOCATION = 100;
  56 
  57     private static volatile boolean dragExitCalled = false;
  58 
  59     private static volatile Frame f;
  60 
  61     private static void initAndShowUI() {
  62         f = new Frame("Test frame");
  63         f.setBounds(FRAME_LOCATION,FRAME_LOCATION,FRAME_SIZE,FRAME_SIZE);
  64 
  65         final DraggablePanel dragSource = new DraggablePanel();
  66         dragSource.setBackground(Color.yellow);
  67         DropTarget dt = new DropTarget(dragSource, new DropTargetAdapter() {
  68             @Override public void drop(DropTargetDropEvent dtde) { }
  69 
  70             @Override
  71             public void dragExit(DropTargetEvent dte) {
  72                 dragExitCalled = true;
  73             }
  74 
  75             @Override
  76             public void dragOver(DropTargetDragEvent dtde) {
  77                 Panel newDropTarget = new Panel();
  78                 newDropTarget.setDropTarget(new DropTarget());
  79                 newDropTarget.setBackground(Color.red);
  80                 newDropTarget.setBounds(0, 0, FRAME_SIZE, FRAME_SIZE);
  81                 dragSource.add(newDropTarget);
  82             }
  83         });
  84         dragSource.setDropTarget(dt);
  85         f.add(dragSource);
  86 
  87         f.setVisible(true);
  88     }
  89 
  90     public static void main(String[] args) throws Throwable {
  91         try {
  92 
  93             SwingUtilities.invokeAndWait(new Runnable() {
  94                 @Override
  95                 public void run() {
  96                     initAndShowUI();
  97                 }
  98             });
  99 
 100             Robot r = new Robot();
 101             Util.waitForIdle(r);
 102             Util.drag(r,
 103                     new Point(FRAME_LOCATION + FRAME_SIZE / 3, FRAME_LOCATION + FRAME_SIZE / 3),
 104                     new Point(FRAME_LOCATION + FRAME_SIZE / 3 * 2, FRAME_LOCATION + FRAME_SIZE / 3 * 2),
 105                     InputEvent.BUTTON1_MASK);
 106             Util.waitForIdle(r);
 107 
 108             if (!dragExitCalled) {
 109                 throw new RuntimeException("Failed. Drag exit was not called" );
 110             }
 111         } finally {
 112             if (f != null) {
 113                 f.dispose();
 114             }
 115         }
 116     }
 117 
 118     private static class DraggablePanel extends Panel implements DragGestureListener {
 119 
 120         public DraggablePanel() {
 121             (new DragSource()).createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);
 122         }
 123 
 124         @Override
 125         public void dragGestureRecognized(DragGestureEvent dge) {
 126             dge.startDrag(Cursor.getDefaultCursor(), new StringSelection("test"));
 127         }
 128     }
 129 }