Skip to content

Commit

Permalink
8306466: Open source more AWT Drag & Drop related tests
Browse files Browse the repository at this point in the history
Reviewed-by: prr
  • Loading branch information
Damon Nguyen committed May 2, 2023
1 parent 74667e3 commit 418a825
Show file tree
Hide file tree
Showing 8 changed files with 848 additions and 0 deletions.
124 changes: 124 additions & 0 deletions test/jdk/java/awt/dnd/RejectDragDropActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
@test
@bug 4774532
@summary tests that DropTargetDragEvent.getDropAction() returns correct value
after DropTargetDragEvent.rejectDrag()
@key headful
@run main RejectDragDropActionTest
*/

import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Robot;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.InputEvent;
import java.lang.reflect.InvocationTargetException;


public class RejectDragDropActionTest {

private static volatile boolean incorrectActionDetected = false;

private static final int FRAME_ACTIVATION_TIMEOUT = 3000;

private static Frame frame;
private static DragSource ds;
private static DragGestureListener dgl;
private static DragGestureRecognizer dgr;
private final DropTargetListener dtl = new DropTargetAdapter() {
public void dragEnter(DropTargetDragEvent dtde) {
dtde.rejectDrag();
}
public void dragOver(DropTargetDragEvent dtde) {
if (dtde.getDropAction() == DnDConstants.ACTION_NONE) {
incorrectActionDetected = true;
}
}
public void drop(DropTargetDropEvent dtde) {
dtde.rejectDrop();
}
};
private final DropTarget dt = new DropTarget(frame, dtl);

public static void main(String[] args) throws InterruptedException,
InvocationTargetException, AWTException {
EventQueue.invokeAndWait(() -> {
frame = new Frame("RejectDragDropActionTest");
ds = DragSource.getDefaultDragSource();
dgl = dge -> dge.startDrag(null, new StringSelection("OOKK"));
dgr = ds.createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_COPY, dgl);
frame.setBounds(100, 100, 200, 200);
frame.setVisible(true);
});

try {
Robot robot = new Robot();
robot.waitForIdle();
robot.delay(FRAME_ACTIVATION_TIMEOUT);

Point startPoint = frame.getLocationOnScreen();
Point endPoint = new Point(startPoint);
startPoint.translate(50, 50);
endPoint.translate(150, 150);

robot.mouseMove(startPoint.x, startPoint.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
for (Point p = new Point(startPoint); !p.equals(endPoint);
p.translate(sign(endPoint.x - p.x),
sign(endPoint.y - p.y))) {
robot.mouseMove(p.x, p.y);
robot.delay(50);
}

robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}

if (incorrectActionDetected) {
throw new RuntimeException("User action reported incorrectly.");
}
}

public static int sign(int n) {
return n < 0 ? -1 : n == 0 ? 0 : 1;
}
}
82 changes: 82 additions & 0 deletions test/jdk/java/awt/dnd/RemoveDragSourceListenerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
@test
@bug 4530216
@summary tests that DragSourceListeners are properly removed
@key headful
@run main RemoveDragSourceListenerTest
*/

import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceAdapter;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DragSourceMotionListener;


public class RemoveDragSourceListenerTest {
public static void main(String[] args) {
class TestDragSourceAdapter extends DragSourceAdapter {}

final DragSource dragSource = DragSource.getDefaultDragSource();

final DragSourceAdapter listeners[] = {
new TestDragSourceAdapter(),
new TestDragSourceAdapter(),
new TestDragSourceAdapter() // should be three or more listeners
};

for (int i = 0; i < listeners.length; i++) {
dragSource.addDragSourceListener(listeners[i]);
}

DragSourceListener[] dragSourceListeners =
dragSource.getDragSourceListeners();

if (dragSourceListeners.length != listeners.length) {
throw new RuntimeException("Unexpected length: " +
dragSourceListeners.length);
}

for (int i = 0; i < listeners.length; i++) {
dragSource.removeDragSourceListener(listeners[i]);
}

for (int i = 0; i < listeners.length; i++) {
dragSource.addDragSourceMotionListener(listeners[i]);
}

DragSourceMotionListener[] dragSourceMotionListeners =
dragSource.getDragSourceMotionListeners();

if (dragSourceMotionListeners.length != listeners.length) {
throw new RuntimeException("Unexpected length: " +
dragSourceMotionListeners.length);
}

for (int i = 0; i < listeners.length; i++) {
dragSource.removeDragSourceMotionListener(listeners[i]);
}
}
}
77 changes: 77 additions & 0 deletions test/jdk/java/awt/dnd/RemoveParentChildDropTargetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
@test
@bug 4411368
@summary tests the app doesn't crash if the child drop target is removed
after the parent drop target is removed
@key headful
@run main RemoveParentChildDropTargetTest
*/

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.lang.reflect.InvocationTargetException;


public class RemoveParentChildDropTargetTest {

static Frame frame;
static Panel panel;
static Label label;

public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
EventQueue.invokeAndWait(() -> {
frame = new Frame("RemoveParentChildDropTargetTest");
panel = new Panel();
label = new Label("Label");
panel.add(label);
frame.add(panel);
frame.pack();

panel.setDropTarget(new DropTarget(panel, new DropTargetAdapter() {
public void drop(DropTargetDropEvent dtde) {}
}));
label.setDropTarget(new DropTarget(label, new DropTargetAdapter() {
public void drop(DropTargetDropEvent dtde) {}
}));
panel.setDropTarget(null);
frame.setVisible(true);

label.setDropTarget(null);
});

EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
Loading

3 comments on commit 418a825

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rudometov
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 418a825 May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rudometov the backport was successfully created on the branch Rudometov-backport-418a8255 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 418a8255 from the openjdk/jdk repository.

The commit being backported was authored by Damon Nguyen on 2 May 2023 and was reviewed by Phil Race.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git Rudometov-backport-418a8255:Rudometov-backport-418a8255
$ git checkout Rudometov-backport-418a8255
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git Rudometov-backport-418a8255

Please sign in to comment.