Skip to content

Commit

Permalink
8274597: Some of the dnd tests time out and fail intermittently
Browse files Browse the repository at this point in the history
8028998: [TEST_BUG] [macosx] java/awt/dnd/DropTargetEnterExitTest/MissedDragExitTest.java failed

Backport-of: 669ac611b269bbda5c53d84173e5c9d0eb4ce919
  • Loading branch information
GoeLin committed Oct 12, 2022
1 parent 9ffc4b3 commit c064dce
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 52 deletions.
Expand Up @@ -35,22 +35,44 @@

import test.java.awt.regtesthelpers.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.InputEvent;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.imageio.ImageIO;
import javax.swing.SwingUtilities;

public class AcceptDropMultipleTimes {

private static final int FRAME_SIZE = 100;
private static final int FRAME_LOCATION = 100;
private static CountDownLatch dropCompleteLatch = new CountDownLatch(1);

private static volatile Frame f;

private static void initAndShowUI() {
f = new Frame("Test frame");
f.setBounds(FRAME_LOCATION, FRAME_LOCATION, FRAME_SIZE, FRAME_SIZE);
f.setSize(FRAME_SIZE, FRAME_SIZE);
f.setLocationRelativeTo(null);
f.setUndecorated(true);

final DraggablePanel dragSource = new DraggablePanel();
dragSource.setBackground(Color.yellow);
Expand All @@ -63,11 +85,12 @@ private static void initAndShowUI() {
dtde.acceptDrop(DnDConstants.ACTION_MOVE);

dtde.dropComplete(true);
dropCompleteLatch.countDown();
}
});
dragSource.setDropTarget(dt);
f.add(dragSource);

f.setAlwaysOnTop(true);
f.setVisible(true);
}

Expand All @@ -77,19 +100,37 @@ public static void main(String[] args) throws Throwable {
SwingUtilities.invokeAndWait(() -> initAndShowUI());

Robot r = new Robot();
r.setAutoDelay(50);
Util.waitForIdle(r);
final AtomicReference<Point> frameLoc = new AtomicReference<>();
SwingUtilities.invokeAndWait(() -> frameLoc.set(f.getLocationOnScreen()));
Point loc = frameLoc.get();
Util.drag(r,
new Point(FRAME_LOCATION + FRAME_SIZE / 3, FRAME_LOCATION + FRAME_SIZE / 3),
new Point(FRAME_LOCATION + FRAME_SIZE / 3 * 2, FRAME_LOCATION + FRAME_SIZE / 3 * 2),
InputEvent.BUTTON1_MASK);
new Point(loc.x + FRAME_SIZE / 3, loc.y + FRAME_SIZE / 3),
new Point(loc.x + FRAME_SIZE / 3 * 2, loc.y + FRAME_SIZE / 3 * 2),
InputEvent.BUTTON1_DOWN_MASK);
Util.waitForIdle(r);
if(!dropCompleteLatch.await(10, TimeUnit.SECONDS)) {
captureScreen(r);
throw new RuntimeException("Waited too long, but the drop is not completed");
}
} finally {
if (f != null) {
f.dispose();
}
}
}

private static void captureScreen(Robot r) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
try {
ImageIO.write(
r.createScreenCapture(new Rectangle(0, 0, screenSize.width, screenSize.height)),
"png",
new File("FailedScreenImage.png")
);
} catch (IOException ignore) {
}
}
private static class DraggablePanel extends Panel implements DragGestureListener {

public DraggablePanel() {
Expand Down
Expand Up @@ -35,8 +35,13 @@

import test.java.awt.regtesthelpers.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
Expand All @@ -47,23 +52,27 @@
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.InputEvent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.SwingUtilities;

public class ExtraDragEnterTest {

private static final int FRAME_SIZE = 100;
private static final int FRAME_LOCATION = 100;

private static AtomicInteger dragEnterCalled = new AtomicInteger(0);

private static volatile Panel mainPanel;
private static volatile Frame f;
private static CountDownLatch dropCompleteLatch = new CountDownLatch(1);

private static void initAndShowUI() {
f = new Frame("Test frame");
f.setBounds(FRAME_LOCATION,FRAME_LOCATION,FRAME_SIZE,FRAME_SIZE);
f.setLocationRelativeTo(null);
f.setSize(FRAME_SIZE,FRAME_SIZE);
mainPanel = new Panel();
mainPanel.setBounds(0, 0, FRAME_SIZE, FRAME_SIZE);
mainPanel.setSize(FRAME_SIZE, FRAME_SIZE);
mainPanel.setBackground(Color.black);
mainPanel.setLayout(new GridLayout(2, 1));

Expand All @@ -75,7 +84,10 @@ private static void initAndShowUI() {
Panel dropTarget = new Panel();
dropTarget.setBackground(Color.red);
DropTarget dt = new DropTarget(dropTarget, new DropTargetAdapter() {
@Override public void drop(DropTargetDropEvent dtde) { }
@Override public void drop(DropTargetDropEvent dtde) {
System.out.println("Drop complete");
dropCompleteLatch.countDown();
}

@Override
public void dragEnter(DropTargetDragEvent dtde) {
Expand All @@ -86,6 +98,7 @@ public void dragEnter(DropTargetDragEvent dtde) {
mainPanel.add(dropTarget);

f.add(mainPanel);
f.setAlwaysOnTop(true);
f.setVisible(true);
}

Expand Down Expand Up @@ -113,6 +126,9 @@ public void run() {
if (called != 1) {
throw new RuntimeException("Failed. Drag enter called " + called + " times. Expected 1" );
}
if(!dropCompleteLatch.await(10, TimeUnit.SECONDS)) {
throw new RuntimeException("Waited too long, but the drop is not completed");
}
} finally {
if (f != null) {
f.dispose();
Expand Down
Expand Up @@ -35,8 +35,12 @@

import test.java.awt.regtesthelpers.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
Expand All @@ -48,15 +52,17 @@
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.event.InputEvent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.swing.SwingUtilities;

public class MissedDragExitTest {

private static final int FRAME_SIZE = 100;
private static final int FRAME_LOCATION = 100;

private static volatile boolean dragExitCalled = false;

private static volatile Frame f;
private static CountDownLatch dragLatch = new CountDownLatch(2);

private static void initAndShowUI() {
f = new Frame("Test frame");
Expand All @@ -70,21 +76,30 @@ private static void initAndShowUI() {

@Override
public void dragExit(DropTargetEvent dte) {
dragExitCalled = true;
System.out.println("Drag Exit");
dragLatch.countDown();
}

@Override
public void dragOver(DropTargetDragEvent dtde) {
Panel newDropTarget = new Panel();
newDropTarget.setDropTarget(new DropTarget());
newDropTarget.setBackground(Color.red);
newDropTarget.setBounds(0, 0, FRAME_SIZE, FRAME_SIZE);
dragSource.add(newDropTarget);
Panel newDropTargetPanel = new Panel();
final DropTarget dropTarget = new DropTarget(null,new DropTargetAdapter() {
@Override
public void drop(DropTargetDropEvent dtde) {
System.out.println("Drop complete");
dragLatch.countDown();
}
});
newDropTargetPanel.setDropTarget(dropTarget);
newDropTargetPanel.setBackground(Color.red);
newDropTargetPanel.setSize(FRAME_SIZE, FRAME_SIZE);
dragSource.add(newDropTargetPanel);
}
});
dragSource.setDropTarget(dt);
f.add(dragSource);

f.setAlwaysOnTop(true);
f.setVisible(true);
}

Expand All @@ -105,8 +120,7 @@ public void run() {
new Point(FRAME_LOCATION + FRAME_SIZE / 3 * 2, FRAME_LOCATION + FRAME_SIZE / 3 * 2),
InputEvent.BUTTON1_DOWN_MASK);
Util.waitForIdle(r);

if (!dragExitCalled) {
if(!dragLatch.await(10, TimeUnit.SECONDS)) {
throw new RuntimeException("Failed. Drag exit was not called" );
}
} finally {
Expand Down

1 comment on commit c064dce

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.