Skip to content

Commit 129e856

Browse files
committed
8266249: javax/swing/JPopupMenu/7156657/bug7156657.java fails on macOS
Backport-of: 9b76955
1 parent c124213 commit 129e856

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

test/jdk/javax/swing/JPopupMenu/7156657/bug7156657.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@
2525
import java.awt.Color;
2626
import java.awt.Graphics;
2727
import java.awt.Graphics2D;
28+
import java.awt.GraphicsConfiguration;
29+
import java.awt.GraphicsDevice;
30+
import java.awt.GraphicsEnvironment;
31+
import java.awt.Point;
2832
import java.awt.Rectangle;
2933
import java.awt.Robot;
3034
import java.awt.Window;
3135
import java.awt.image.BufferedImage;
36+
import java.io.File;
3237
import java.util.concurrent.Callable;
3338

39+
import javax.imageio.ImageIO;
3440
import javax.swing.JFrame;
3541
import javax.swing.JMenuItem;
3642
import javax.swing.JPanel;
@@ -103,24 +109,56 @@ public void run() {
103109
Rectangle popupRectangle = Util.invokeOnEDT(new Callable<Rectangle>() {
104110
@Override
105111
public Rectangle call() throws Exception {
106-
return popupMenu.getBounds();
112+
return new Rectangle(popupMenu.getLocationOnScreen(),
113+
popupMenu.getSize());
107114
}
108115
});
109116

110117
BufferedImage redBackgroundCapture = robot.createScreenCapture(popupRectangle);
118+
BufferedImage redFrame = robot.createScreenCapture(frame.getBounds());
111119

112120
SwingUtilities.invokeAndWait(new Runnable() {
113121
@Override
114122
public void run() {
115123
lowerFrame.getContentPane().setBackground(Color.GREEN);
124+
lowerFrame.invalidate();
116125
}
117126
});
118127

119128
robot.waitForIdle();
129+
robot.delay(1000); // Give frame time to repaint
120130

121131
BufferedImage greenBackgroundCapture = robot.createScreenCapture(popupRectangle);
132+
BufferedImage greenFrame = robot.createScreenCapture(frame.getBounds());
122133

123134
if (Util.compareBufferedImages(redBackgroundCapture, greenBackgroundCapture)) {
135+
try {
136+
GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
137+
for (int i = 0; i < devices.length; i++) {
138+
GraphicsConfiguration[] screens = devices[i].getConfigurations();
139+
for (int j = 0; j < screens.length; j++) {
140+
BufferedImage fullScreen = robot.createScreenCapture(screens[j].getBounds());
141+
if (screens[j].getBounds().intersects(popupRectangle)) {
142+
Graphics g = fullScreen.getGraphics();
143+
g.setColor(Color.CYAN);
144+
g.drawRect(popupRectangle.x - 1, popupRectangle.y - 1,
145+
popupRectangle.width + 2, popupRectangle.height + 2);
146+
g.dispose();
147+
}
148+
ImageIO.write(fullScreen, "png", new File("dev" + i + "scr" + j + ".png"));
149+
}
150+
}
151+
ImageIO.write(redFrame, "png", new File("redframe.png"));
152+
ImageIO.write(redBackgroundCapture, "png", new File("redbg.png"));
153+
ImageIO.write(greenFrame, "png", new File("greenframe.png"));
154+
ImageIO.write(greenBackgroundCapture, "png", new File("greenbg.png"));
155+
} finally {
156+
SwingUtilities.invokeAndWait(() -> {
157+
frame.dispose();
158+
lowerFrame.dispose();
159+
});
160+
}
161+
robot.waitForIdle();
124162
throw new RuntimeException("The test failed");
125163
}
126164

@@ -148,8 +186,8 @@ public static void setOpaque(Window window, boolean opaque) {
148186
private static JFrame createFrame() {
149187
JFrame result = new JFrame();
150188

151-
result.setLocation(0, 0);
152189
result.setSize(400, 300);
190+
result.setLocationRelativeTo(null);
153191
result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
154192
result.setUndecorated(true);
155193

0 commit comments

Comments
 (0)