Skip to content
Closed
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 @@ -502,7 +502,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/Window/GetScreenLocation/GetScreenLocationTest.java 8225787 linux-x64
Expand Down
152 changes: 103 additions & 49 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,75 @@
* @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.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.MultiResolutionImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

public class CopyAreaOOB extends Canvas {
private static Frame frame;
private static Robot robot;
private static BufferedImage captureImg;

private static Robot robot = null;
private static StringBuffer errorLog = new StringBuffer();

private static final Point OFF_FRAME_LOC = new Point(50, 50);
private static final int SIZE = 400;

public static void main(String[] args) throws Exception {
try {
robot = new Robot();

// added to move mouse pointer away from test UI
// so that it is not captured in the screenshot
robot.mouseMove(OFF_FRAME_LOC.x, OFF_FRAME_LOC.y);
robot.waitForIdle();
robot.delay(100);

createTestUI();
robot.delay(1000);

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

private static void createTestUI() {
CopyAreaOOB canvas = new CopyAreaOOB();
frame = new Frame();
frame.setUndecorated(true);
frame.add(canvas);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public void paint(Graphics g) {
int w = getWidth();
Expand All @@ -50,73 +108,69 @@ 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);
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");
}
robot.delay(500);

Point pt1 = this.getLocationOnScreen();
Rectangle rect = new Rectangle(pt1.x, pt1.y, SIZE, SIZE);
captureImg = robot.createScreenCapture(rect);

// 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);
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);
}

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

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("Test 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]));
private static void saveImages() {
GraphicsConfiguration ge = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration();

CopyAreaOOB test = new CopyAreaOOB();
Frame frame = new Frame();
frame.setUndecorated(true);
frame.add(test);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
MultiResolutionImage mrImage = robot.createMultiResolutionScreenCapture(ge.getBounds());
List<Image> variants = mrImage.getResolutionVariants();
RenderedImage screenCapture = (RenderedImage) variants.get(variants.size() - 1);

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("canvas.png"));
} catch (IOException e) {
System.err.println("Can't write image " + e);
}
}
}