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