Skip to content

JDK-8357082 : Stabilize and add debug logs to CopyAreaOOB.java #25279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion test/jdk/ProblemList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ java/awt/MenuBar/TestNoScreenMenuBar.java 8265987 macosx-all

java/awt/Graphics2D/DrawString/DrawRotatedStringUsingRotatedFont.java 8266283 generic-all
java/awt/Graphics2D/DrawString/RotTransText.java 8316878 linux-all
java/awt/Graphics2D/CopyAreaOOB.java 8343106 macosx-aarch64
java/awt/KeyboardFocusmanager/TypeAhead/ButtonActionKeyTest/ButtonActionKeyTest.java 8257529 windows-x64
java/awt/KeyboardFocusmanager/ConsumeNextMnemonicKeyTypedTest/ConsumeForModalDialogTest/ConsumeForModalDialogTest.java 8302787 windows-all
java/awt/KeyboardFocusmanager/TypeAhead/MenuItemActivatedTest/MenuItemActivatedTest.java 8302787 windows-all
Expand Down
141 changes: 87 additions & 54 deletions test/jdk/java/awt/Graphics2D/CopyAreaOOB.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2025, 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 All @@ -26,17 +26,73 @@
* @key headful
* @bug 6430601 8198613
* @summary Verifies that copyArea() works properly when the
* destination parameters are outside the destination bounds.
* destination parameters are outside the destination bounds.
* @run main/othervm CopyAreaOOB
* @author campbelc
*/

import java.awt.*;
import java.awt.image.*;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class CopyAreaOOB extends Canvas {
private static Frame frame;
private static Robot robot;
private static BufferedImage captureImg;
private static StringBuffer errorLog = new StringBuffer();

private static Robot robot = null;
public static void main(String[] args) throws Exception {
try {
robot = new Robot();
EventQueue.invokeAndWait(CopyAreaOOB::createTestUI);
robot.waitForIdle();
robot.delay(1000);

EventQueue.invokeAndWait(() -> {
Point pt1 = frame.getLocationOnScreen();
Rectangle rect = new Rectangle(pt1.x, pt1.y, 400, 400);
captureImg = robot.createScreenCapture(rect);
});

// Test pixels
testRegion("green", 0, 0, 400, 10, 0xff00ff00);
testRegion("original-red", 0, 10, 50, 400, 0xffff0000);
testRegion("background", 50, 10, 60, 400, 0xff000000);
testRegion("in-between", 60, 10, 110, 20, 0xff000000);
testRegion("copied-red", 60, 20, 110, 400, 0xffff0000);
testRegion("background", 110, 10, 400, 400, 0xff000000);

if (!errorLog.isEmpty()) {
saveImages();
throw new RuntimeException("Test failed: \n" + errorLog.toString());
}
} finally {
EventQueue.invokeAndWait(frame::dispose);
}
}

private static void createTestUI() {
frame = new Frame();
Copy link
Member

Choose a reason for hiding this comment

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

You do not need to call all these methods on the EDT, these are AWT components that should handle multithreaded usage (main/EDT/toolkit threads). It is possible that the issue is only reproducible on macOS due to synchronization problems related to the metal pipeline?

CopyAreaOOB test = new CopyAreaOOB();
frame.add(test);
frame.setUndecorated(true);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public void paint(Graphics g) {
int w = getWidth();
Expand All @@ -50,73 +106,50 @@ public void paint(Graphics g) {
g2d.fillRect(0, 0, w, 10);

g2d.setColor(Color.red);
g2d.fillRect(0, 10, 50, h-10);
g2d.fillRect(0, 10, 50, h - 10);

// copy the region such that part of it goes below the bottom of the
// destination surface
g2d.copyArea(0, 10, 50, h-10, 60, 10);

Toolkit.getDefaultToolkit().sync();

BufferedImage capture = null;
try {
Thread.sleep(500);
if (robot == null) robot = new Robot();
Point pt1 = getLocationOnScreen();
Rectangle rect = new Rectangle(pt1.x, pt1.y, 400, 400);
capture = robot.createScreenCapture(rect);
} catch (Exception e) {
throw new RuntimeException("Problems handling Robot");
}
// Test pixels
testRegion(capture, "green", 0, 0, 400, 10, 0xff00ff00);
testRegion(capture, "original red", 0, 10, 50, 400, 0xffff0000);
testRegion(capture, "background", 50, 10, 60, 400, 0xff000000);
testRegion(capture, "in-between", 60, 10, 110, 20, 0xff000000);
testRegion(capture, "copied red", 60, 20, 110, 400, 0xffff0000);
testRegion(capture, "background", 110, 10, 400, 400, 0xff000000);
g2d.copyArea(0, 10, 50, h - 10, 60, 10);
g2d.dispose();
}

public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

private static void testRegion(BufferedImage bi, String name,
private static void testRegion(String region,
int x1, int y1, int x2, int y2,
int expected)
{
int expected) {
System.out.print("Testing region: " + region);
for (int y = y1; y < y2; y++) {
for (int x = x1; x < x2; x++) {
int actual = bi.getRGB(x, y);
int actual = captureImg.getRGB(x, y);
if (actual != expected) {
throw new RuntimeException("Test failed for " + name +
" region at x="+x+" y="+y+
" (expected="+
Integer.toHexString(expected) +
" actual="+
Integer.toHexString(actual) +
")");
System.out.print(" Status: FAILED\n");
errorLog.append("Test failed for " + region
+ " region at x: " + x + " y: " + y
+ " (expected: "
+ Integer.toHexString(expected)
+ " actual: "
+ Integer.toHexString(actual) + ")\n");
return;
}
}
}
System.out.print(" Status: PASSED\n");
}

public static void main(String[] args) {
boolean show = (args.length == 1) && ("-show".equals(args[0]));

CopyAreaOOB test = new CopyAreaOOB();
Frame frame = new Frame();
frame.setUndecorated(true);
frame.add(test);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

private static void saveImages() {
GraphicsConfiguration ge = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration();
BufferedImage screenCapture = robot.createScreenCapture(ge.getBounds());
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {}
if (!show) {
frame.dispose();
ImageIO.write(screenCapture, "png", new File("fullscreen.png"));
ImageIO.write(captureImg, "png", new File("frame.png"));
} catch (IOException e) {
System.err.println("Can't write image " + e);
}
}
}