Skip to content

Commit

Permalink
8288415: java/awt/PopupMenu/PopupMenuLocation.java is unstable in Mac…
Browse files Browse the repository at this point in the history
…OS machines

Reviewed-by: dnguyen, aivanov
  • Loading branch information
Manukumar V S authored and aivanov-jdk committed Jan 16, 2023
1 parent a734285 commit a2f6766
Showing 1 changed file with 57 additions and 24 deletions.
81 changes: 57 additions & 24 deletions test/jdk/java/awt/PopupMenu/PopupMenuLocation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 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
Expand Down Expand Up @@ -33,10 +33,13 @@
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.*;
import java.io.File;
import java.util.stream.IntStream;
import javax.imageio.ImageIO;

/**
/*
* @test
* @key headful
* @bug 8160270
Expand All @@ -47,43 +50,59 @@ public final class PopupMenuLocation {
private static final int SIZE = 350;
public static final String TEXT =
"Long-long-long-long-long-long-long text in the item-";
public static final int OFFSET = 50;
private static volatile boolean action = false;
private static Robot robot;
private static Frame frame;
private static Rectangle screenBounds;


public static void main(final String[] args) throws Exception {
robot = new Robot();
robot.setAutoDelay(200);
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] sds = ge.getScreenDevices();
for (GraphicsDevice sd : sds) {
GraphicsConfiguration gc = sd.getDefaultConfiguration();
Rectangle bounds = gc.getBounds();
Point point = new Point(bounds.x, bounds.y);
screenBounds = gc.getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
while (point.y < bounds.y + bounds.height - insets.bottom - SIZE) {
while (point.x
< bounds.x + bounds.width - insets.right - SIZE) {
Point point = new Point(screenBounds.x + insets.left,
screenBounds.y + insets.top);
final int yBound = screenBounds.y + screenBounds.height
- insets.bottom - SIZE;
final int xBound = screenBounds.x + screenBounds.width
- insets.right - SIZE;
while (point.y < yBound) {
while (point.x < xBound) {
test(point);
point.translate(bounds.width / 5, 0);
point.translate(screenBounds.width / 5, 0);
}
point.setLocation(bounds.x, point.y + bounds.height / 5);
point.setLocation(screenBounds.x,
point.y + screenBounds.height / 5);
}
}
}

private static void test(final Point tmp) throws Exception {
private static void test(final Point loc) {
frame = new Frame();
PopupMenu pm = new PopupMenu();
for (int i = 1; i < 7; i++) {
pm.add(TEXT + i);
}
pm.addActionListener(e -> action = true);
Frame frame = new Frame();
IntStream.rangeClosed(1, 6).forEach(i -> pm.add(TEXT + i));
pm.addActionListener(e -> {
action = true;
System.out.println(" Got action event " + e);
});

try {
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setLayout(new FlowLayout());
frame.add(pm);
frame.pack();
frame.setSize(SIZE, SIZE);
frame.setLocation(loc);
frame.setVisible(true);
frame.setLocation(tmp.x, tmp.y);

frame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
show(e);
Expand All @@ -105,23 +124,37 @@ private void show(MouseEvent e) {
}
}

private static void openPopup(final Frame frame) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(200);
private static void openPopup(final Frame frame) {
robot.waitForIdle();
Point pt = frame.getLocationOnScreen();
robot.mouseMove(pt.x + frame.getWidth() / 2, pt.y + 50);
int x = pt.x + frame.getWidth() / 2;
int y = pt.y + OFFSET;
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
int x = pt.x + frame.getWidth() / 2;
int y = pt.y + 130;
robot.delay(200);
y += OFFSET;
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.waitForIdle();
if (!action) {
throw new RuntimeException();
captureScreen();
throw new RuntimeException(
"Failed, didn't receive the PopupMenu ActionEvent on " +
"frame= " + frame + ", isFocused = " + frame.isFocused());
}
action = false;
}

private static void captureScreen() {
try {
ImageIO.write(robot.createScreenCapture(screenBounds),
"png",
new File("screen.png"));
} catch (Exception e) {
e.printStackTrace();
}
}

}

5 comments on commit a2f6766

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on a2f6766 Feb 10, 2023

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 a2f6766 Feb 10, 2023

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch GoeLin-backport-a2f67660 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 a2f67660 from the openjdk/jdk repository.

The commit being backported was authored by Manukumar V S on 16 Jan 2023 and was reviewed by Damon Nguyen and Alexey Ivanov.

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 GoeLin-backport-a2f67660:GoeLin-backport-a2f67660
$ git checkout GoeLin-backport-a2f67660
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-a2f67660

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on a2f6766 Oct 24, 2023

Choose a reason for hiding this comment

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

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on a2f6766 Oct 24, 2023

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch GoeLin-backport-a2f67660 in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-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 a2f67660 from the openjdk/jdk repository.

The commit being backported was authored by Manukumar V S on 16 Jan 2023 and was reviewed by Damon Nguyen and Alexey Ivanov.

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/jdk11u-dev:

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

Please sign in to comment.