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