Button2DragTest.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2006, 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
24import java.awt.Frame;
25import java.awt.Point;
26import java.awt.Robot;
27import java.awt.datatransfer.StringSelection;
28import java.awt.dnd.DnDConstants;
29import java.awt.dnd.DragGestureEvent;
30import java.awt.dnd.DragGestureListener;
31import java.awt.dnd.DragSource;
32import java.awt.dnd.DragSourceAdapter;
33import java.awt.dnd.DragSourceDropEvent;
34import java.awt.dnd.DragSourceListener;
35import java.awt.dnd.DropTarget;
36import java.awt.dnd.DropTargetAdapter;
37import java.awt.dnd.DropTargetDropEvent;
38import java.awt.event.InputEvent;
39
40import test.java.awt.regtesthelpers.Util;
41
42/**
43 * @test
44 * @key headful
45 * @bug 4955110
46 * @summary tests that DragSourceDragEvent.getDropAction() accords to its new
47 *          spec (does not depend on the user drop action)
48 * @library ../../regtesthelpers
49 * @build Util
50 * @run main/othervm Button2DragTest
51 * @author Alexander.Gerasimov area=dnd
52 */
53public final class Button2DragTest {
54
55    private volatile boolean dropSuccess;
56
57    private static Frame frame;
58
59    public static void main(final String[] args) {
60        Button2DragTest test = new Button2DragTest();
61        try {
62            test.run();
63        } finally {
64            if (frame != null) {
65                frame.dispose();
66            }
67        }
68    }
69
70    public void run() {
71        frame = new Frame();
72
73        final DragSourceListener dragSourceListener = new DragSourceAdapter() {
74            public void dragDropEnd(DragSourceDropEvent e) {
75                dropSuccess = e.getDropSuccess();
76                System.err.println("Drop was successful: " + dropSuccess);
77            }
78        };
79        DragGestureListener dragGestureListener = new DragGestureListener() {
80            public void dragGestureRecognized(DragGestureEvent dge) {
81                dge.startDrag(null, new StringSelection("OK"), dragSourceListener);
82            }
83        };
84        new DragSource().createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_MOVE,
85                                                            dragGestureListener);
86
87        DropTargetAdapter dropTargetListener = new DropTargetAdapter() {
88            public void drop(DropTargetDropEvent dtde) {
89                dtde.acceptDrop(DnDConstants.ACTION_MOVE);
90                dtde.dropComplete(true);
91                System.err.println("Drop");
92            }
93        };
94        new DropTarget(frame, dropTargetListener);
95
96        //What would normally go into main() will probably go here.
97        //Use System.out.println for diagnostic messages that you want
98        //to read after the test is done.
99        frame.setUndecorated(true);
100        frame.setBounds(100, 100, 200, 200);
101        frame.setLocationRelativeTo(null);
102        frame.setVisible(true);
103
104        Robot robot = Util.createRobot();
105
106        Util.waitForIdle(robot);
107
108        Point startPoint = frame.getLocationOnScreen();
109        Point endPoint = new Point(startPoint);
110        startPoint.translate(50, 50);
111        endPoint.translate(150, 150);
112
113        Util.drag(robot, startPoint, endPoint, InputEvent.BUTTON2_MASK);
114
115        Util.waitForIdle(robot);
116        robot.delay(500);
117
118        if (dropSuccess) {
119            System.err.println("test passed");
120        } else {
121            throw new RuntimeException("test failed: drop was not successful");
122        }
123    }
124
125}
126