Skip to content

Commit 93fd1a1

Browse files
author
Andrew Lu
committed
8324238: [macOS] java/awt/Frame/ShapeNotSetSometimes/ShapeNotSetSometimes.java fails with the shape has not been applied msg
Backport-of: 62c9530c056dbaaf65be0f43295af3d225326a4c
1 parent ad5a087 commit 93fd1a1

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

test/jdk/java/awt/Frame/ShapeNotSetSometimes/ShapeNotSetSometimes.java

+34-14
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
* questions.
2222
*/
2323

24-
25-
2624
import java.awt.Color;
2725
import java.awt.Dimension;
2826
import java.awt.EventQueue;
@@ -44,7 +42,7 @@
4442
* @key headful
4543
* @bug 6988428
4644
* @summary Tests whether shape is always set
47-
* @run main ShapeNotSetSometimes
45+
* @run main/othervm -Dsun.java2d.uiScale=1 ShapeNotSetSometimes
4846
*/
4947

5048
public class ShapeNotSetSometimes {
@@ -55,22 +53,24 @@ public class ShapeNotSetSometimes {
5553
private Point[] pointsOutsideToCheck;
5654
private Point[] shadedPointsToCheck;
5755
private Point innerPoint;
58-
private final Rectangle bounds = new Rectangle(220, 400, 300, 300);
5956

6057
private static Robot robot;
6158
private static final Color BACKGROUND_COLOR = Color.GREEN;
6259
private static final Color SHAPE_COLOR = Color.WHITE;
60+
private static final int DIM = 300;
61+
private static final int DELTA = 2;
6362

6463
public ShapeNotSetSometimes() throws Exception {
6564
EventQueue.invokeAndWait(this::initializeGUI);
6665
robot.waitForIdle();
67-
robot.delay(1000);
66+
robot.delay(500);
6867
}
6968

7069
private void initializeGUI() {
7170
backgroundFrame = new BackgroundFrame();
7271
backgroundFrame.setUndecorated(true);
73-
backgroundFrame.setBounds(bounds);
72+
backgroundFrame.setSize(DIM, DIM);
73+
backgroundFrame.setLocationRelativeTo(null);
7474
backgroundFrame.setVisible(true);
7575

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

84-
84+
// point at the center of white ellipse
8585
innerPoint = new Point(150, 130);
86+
87+
// mid points on the 4 sides - on the green background frame
8688
pointsOutsideToCheck = new Point[] {
8789
new Point(150, 20),
8890
new Point(280, 120),
8991
new Point(150, 250),
9092
new Point(20, 120)
9193
};
9294

95+
// points just outside the ellipse (opposite side of diagonal)
9396
shadedPointsToCheck = new Point[] {
9497
new Point(62, 62),
9598
new Point(240, 185)
9699
};
97100

98101
window = new TestFrame();
99102
window.setUndecorated(true);
100-
window.setBounds(bounds);
103+
window.setSize(DIM, DIM);
104+
window.setLocationRelativeTo(null);
101105
window.setShape(area);
102106
window.setVisible(true);
103107
}
@@ -108,7 +112,7 @@ static class BackgroundFrame extends Frame {
108112
public void paint(Graphics g) {
109113

110114
g.setColor(BACKGROUND_COLOR);
111-
g.fillRect(0, 0, 300, 300);
115+
g.fillRect(0, 0, DIM, DIM);
112116

113117
super.paint(g);
114118
}
@@ -120,7 +124,7 @@ class TestFrame extends Frame {
120124
public void paint(Graphics g) {
121125

122126
g.setColor(SHAPE_COLOR);
123-
g.fillRect(0, 0, bounds.width, bounds.height);
127+
g.fillRect(0, 0, DIM, DIM);
124128

125129
super.paint(g);
126130
}
@@ -155,17 +159,24 @@ private void doTest() throws Exception {
155159
}
156160
} finally {
157161
EventQueue.invokeAndWait(() -> {
158-
backgroundFrame.dispose();
159-
window.dispose();
162+
if (backgroundFrame != null) {
163+
backgroundFrame.dispose();
164+
}
165+
if (window != null) {
166+
window.dispose();
167+
}
160168
});
161169
}
162170
}
163171

164172
private void colorCheck(int x, int y, Color expectedColor, boolean mustBeExpectedColor) {
165-
166173
int screenX = window.getX() + x;
167174
int screenY = window.getY() + y;
168175

176+
robot.mouseMove(screenX, screenY);
177+
robot.waitForIdle();
178+
robot.delay(50);
179+
169180
Color actualColor = robot.getPixelColor(screenX, screenY);
170181

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

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

204+
private static boolean colorCompare(Color expected, Color actual) {
205+
if (Math.abs(expected.getRed() - actual.getRed()) <= DELTA
206+
&& Math.abs(expected.getGreen() - actual.getGreen()) <= DELTA
207+
&& Math.abs(expected.getBlue() - actual.getBlue()) <= DELTA) {
208+
return true;
209+
}
210+
return false;
211+
}
212+
193213
private static void captureScreen() {
194214
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
195215
Rectangle screenBounds = new Rectangle(0, 0, screenSize.width, screenSize.height);

0 commit comments

Comments
 (0)