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 the dragEnter event is correctly generated
  29  * @library ../../regtesthelpers
  30  * @build Util
  31  * @compile ExtraDragEnterTest.java
  32  * @run main/othervm ExtraDragEnterTest
  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.event.InputEvent;
  50 import java.util.concurrent.atomic.AtomicInteger;
  51 
  52 public class ExtraDragEnterTest {
  53 
  54     private static final int FRAME_SIZE = 100;
  55     private static final int FRAME_LOCATION = 100;
  56 
  57     private static AtomicInteger dragEnterCalled = new AtomicInteger(0);
  58 
  59     private static volatile Panel mainPanel;
  60     private static volatile Frame f;
  61 
  62     private static void initAndShowUI() {
  63         f = new Frame("Test frame");
  64         f.setBounds(FRAME_LOCATION,FRAME_LOCATION,FRAME_SIZE,FRAME_SIZE);
  65         mainPanel = new Panel();
  66         mainPanel.setBounds(0, 0, FRAME_SIZE, FRAME_SIZE);
  67         mainPanel.setBackground(Color.black);
  68         mainPanel.setLayout(new GridLayout(2, 1));
  69 
  70         final DraggablePanel dragSource = new DraggablePanel();
  71         dragSource.setBackground(Color.yellow);
  72         dragSource.setDropTarget(null);
  73         mainPanel.add(dragSource);
  74 
  75         Panel dropTarget = new Panel();
  76         dropTarget.setBackground(Color.red);
  77         DropTarget dt = new DropTarget(dropTarget, new DropTargetAdapter() {
  78             @Override public void drop(DropTargetDropEvent dtde) { }
  79 
  80             @Override
  81             public void dragEnter(DropTargetDragEvent dtde) {
  82                 dragEnterCalled.incrementAndGet();
  83             }
  84         });
  85         dropTarget.setDropTarget(dt);
  86         mainPanel.add(dropTarget);
  87 
  88         f.add(mainPanel);
  89         f.setVisible(true);
  90     }
  91 
  92     public static void main(String[] args) throws Throwable {
  93         try {
  94 
  95             SwingUtilities.invokeAndWait(new Runnable() {
  96                 @Override
  97                 public void run() {
  98                     initAndShowUI();
  99                 }
 100             });
 101 
 102             Robot r = new Robot();
 103             Util.waitForIdle(r);
 104             Point leftCorner = new Point(mainPanel.getLocationOnScreen());
 105             leftCorner.translate(5, 5);
 106             Point rightCorner = new Point(mainPanel.getLocationOnScreen());
 107             rightCorner.translate(mainPanel.getWidth(), mainPanel.getHeight());
 108             rightCorner.translate(-5, -5);
 109             Util.drag(r, leftCorner, rightCorner, InputEvent.BUTTON1_MASK);
 110             Util.waitForIdle(r);
 111 
 112             int called = dragEnterCalled.get();
 113             if (called != 1) {
 114                 throw new RuntimeException("Failed. Drag enter called " + called + " times. Expected 1" );
 115             }
 116         } finally {
 117             if (f != null) {
 118                 f.dispose();
 119             }
 120         }
 121     }
 122 
 123     private static class DraggablePanel extends Panel implements DragGestureListener {
 124 
 125         public DraggablePanel() {
 126             (new DragSource()).createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);
 127         }
 128 
 129         @Override
 130         public void dragGestureRecognized(DragGestureEvent dge) {
 131             dge.startDrag(Cursor.getDefaultCursor(), new StringSelection("test"));
 132         }
 133     }
 134 }