Skip to content

Commit

Permalink
8324238: [macOS] java/awt/Frame/ShapeNotSetSometimes/ShapeNotSetSomet…
Browse files Browse the repository at this point in the history
…imes.java fails with the shape has not been applied msg

Backport-of: 62c9530c056dbaaf65be0f43295af3d225326a4c
  • Loading branch information
Andrew Lu committed Mar 14, 2024
1 parent 27cf2f4 commit 0b20589
Showing 1 changed file with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
* questions.
*/



import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
Expand All @@ -44,7 +42,7 @@
* @key headful
* @bug 6988428
* @summary Tests whether shape is always set
* @run main ShapeNotSetSometimes
* @run main/othervm -Dsun.java2d.uiScale=1 ShapeNotSetSometimes
*/

public class ShapeNotSetSometimes {
Expand All @@ -55,22 +53,24 @@ public class ShapeNotSetSometimes {
private Point[] pointsOutsideToCheck;
private Point[] shadedPointsToCheck;
private Point innerPoint;
private final Rectangle bounds = new Rectangle(220, 400, 300, 300);

private static Robot robot;
private static final Color BACKGROUND_COLOR = Color.GREEN;
private static final Color SHAPE_COLOR = Color.WHITE;
private static final int DIM = 300;
private static final int DELTA = 2;

public ShapeNotSetSometimes() throws Exception {
EventQueue.invokeAndWait(this::initializeGUI);
robot.waitForIdle();
robot.delay(1000);
robot.delay(500);
}

private void initializeGUI() {
backgroundFrame = new BackgroundFrame();
backgroundFrame.setUndecorated(true);
backgroundFrame.setBounds(bounds);
backgroundFrame.setSize(DIM, DIM);
backgroundFrame.setLocationRelativeTo(null);
backgroundFrame.setVisible(true);

Area area = new Area();
Expand All @@ -81,23 +81,27 @@ private void initializeGUI() {
area.add(new Area(new Ellipse2D.Float(150, 50, 100, 100)));
area.add(new Area(new Ellipse2D.Float(150, 100, 100, 100)));


// point at the center of white ellipse
innerPoint = new Point(150, 130);

// mid points on the 4 sides - on the green background frame
pointsOutsideToCheck = new Point[] {
new Point(150, 20),
new Point(280, 120),
new Point(150, 250),
new Point(20, 120)
};

// points just outside the ellipse (opposite side of diagonal)
shadedPointsToCheck = new Point[] {
new Point(62, 62),
new Point(240, 185)
};

window = new TestFrame();
window.setUndecorated(true);
window.setBounds(bounds);
window.setSize(DIM, DIM);
window.setLocationRelativeTo(null);
window.setShape(area);
window.setVisible(true);
}
Expand All @@ -108,7 +112,7 @@ static class BackgroundFrame extends Frame {
public void paint(Graphics g) {

g.setColor(BACKGROUND_COLOR);
g.fillRect(0, 0, 300, 300);
g.fillRect(0, 0, DIM, DIM);

super.paint(g);
}
Expand All @@ -120,7 +124,7 @@ class TestFrame extends Frame {
public void paint(Graphics g) {

g.setColor(SHAPE_COLOR);
g.fillRect(0, 0, bounds.width, bounds.height);
g.fillRect(0, 0, DIM, DIM);

super.paint(g);
}
Expand Down Expand Up @@ -155,17 +159,24 @@ private void doTest() throws Exception {
}
} finally {
EventQueue.invokeAndWait(() -> {
backgroundFrame.dispose();
window.dispose();
if (backgroundFrame != null) {
backgroundFrame.dispose();
}
if (window != null) {
window.dispose();
}
});
}
}

private void colorCheck(int x, int y, Color expectedColor, boolean mustBeExpectedColor) {

int screenX = window.getX() + x;
int screenY = window.getY() + y;

robot.mouseMove(screenX, screenY);
robot.waitForIdle();
robot.delay(50);

Color actualColor = robot.getPixelColor(screenX, screenY);

System.out.printf(
Expand All @@ -176,7 +187,7 @@ private void colorCheck(int x, int y, Color expectedColor, boolean mustBeExpecte
expectedColor
);

if (mustBeExpectedColor != expectedColor.equals(actualColor)) {
if (mustBeExpectedColor != colorCompare(expectedColor, actualColor)) {
captureScreen();
System.out.printf("window.getX() = %3d, window.getY() = %3d\n", window.getX(), window.getY());
System.err.printf(
Expand All @@ -190,6 +201,15 @@ private void colorCheck(int x, int y, Color expectedColor, boolean mustBeExpecte
}
}

private static boolean colorCompare(Color expected, Color actual) {
if (Math.abs(expected.getRed() - actual.getRed()) <= DELTA
&& Math.abs(expected.getGreen() - actual.getGreen()) <= DELTA
&& Math.abs(expected.getBlue() - actual.getBlue()) <= DELTA) {
return true;
}
return false;
}

private static void captureScreen() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenBounds = new Rectangle(0, 0, screenSize.width, screenSize.height);
Expand Down

1 comment on commit 0b20589

@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.