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: 1 addition & 0 deletions test/jdk/ProblemList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ java/awt/Focus/TestDisabledAutoTransferSwing.java 6962362 windows-all
java/awt/Focus/ActivateOnProperAppContextTest.java 8136516 macosx-all
java/awt/Focus/FocusPolicyTest.java 7160904 linux-all
java/awt/event/KeyEvent/CorrectTime/CorrectTime.java 6626492 generic-all
java/awt/Graphics/SmallPrimitives.java 8047070 macosx-all,linux-all
java/awt/EventQueue/6980209/bug6980209.java 8198615 macosx-all
java/awt/EventQueue/PushPopDeadlock/PushPopDeadlock.java 8024034 generic-all
java/awt/grab/EmbeddedFrameTest1/EmbeddedFrameTest1.java 7080150 macosx-all
Expand Down
121 changes: 121 additions & 0 deletions test/jdk/java/awt/Graphics/GDIResourceExhaustionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 1999, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import javax.imageio.ImageIO;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
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 java.lang.reflect.InvocationTargetException;

/*
* @test
* @bug 4191297
* @summary Tests that unreferenced GDI resources are correctly
* destroyed when no longer needed.
* @key headful
* @run main GDIResourceExhaustionTest
*/

public class GDIResourceExhaustionTest extends Frame {
public void initUI() {
setSize(200, 200);
setUndecorated(true);
setLocationRelativeTo(null);
Panel labelPanel = new Panel();
Label label = new Label("Red label");
label.setBackground(Color.red);
labelPanel.add(label);
labelPanel.setLocation(20, 50);
add(labelPanel);
setVisible(true);
}

public void paint(Graphics graphics) {
super.paint(graphics);
for (int rgb = 0; rgb <= 0xfff; rgb++) {
graphics.setColor(new Color(rgb));
graphics.fillRect(0, 0, 5, 5);
}
}

public void requestCoordinates(Rectangle r) {
Insets insets = getInsets();
Point location = getLocationOnScreen();
Dimension size = getSize();
r.x = location.x + insets.left;
r.y = location.y + insets.top;
r.width = size.width - (insets.left + insets.right);
r.height = size.height - (insets.top + insets.bottom);
}

public static void main(String[] args) throws InterruptedException,
InvocationTargetException, AWTException, IOException {
GDIResourceExhaustionTest test = new GDIResourceExhaustionTest();
try {
EventQueue.invokeAndWait(test::initUI);
Robot robot = new Robot();
robot.delay(2000);
Rectangle coords = new Rectangle();
EventQueue.invokeAndWait(() -> {
test.requestCoordinates(coords);
});
robot.mouseMove(coords.x - 50, coords.y - 50);
robot.waitForIdle();
robot.delay(5000);
BufferedImage capture = robot.createScreenCapture(coords);
robot.delay(500);
boolean redFound = false;
int redRGB = Color.red.getRGB();
for (int y = 0; y < capture.getHeight(); y++) {
for (int x = 0; x < capture.getWidth(); x++) {
if (capture.getRGB(x, y) == redRGB) {
redFound = true;
break;
}
if (redFound) {
break;
}
}
}
if (!redFound) {
File errorImage = new File("screenshot.png");
ImageIO.write(capture, "png", errorImage);
throw new RuntimeException("Red label is not detected, possibly GDI resources exhausted");
}
} finally {
EventQueue.invokeAndWait(test::dispose);
}
}
}
137 changes: 137 additions & 0 deletions test/jdk/java/awt/Graphics/RepeatedRepaintTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 1998, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;

/*
* @test
* @bug 4081126 4129709
* @summary Test for proper repainting on multiprocessor systems.
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual RepeatedRepaintTest
*/
public class RepeatedRepaintTest extends Frame {
private Font font = null;
private Image background;

static String INSTRUCTIONS = """
The frame next to this window called "AWT Draw Test" has
some elements drawn on it. Move this window partially outside of the
screen bounds and then drag it back. Repeat it couple of times.
Drag the instructions window over the frame partially obscuring it.
If after number of attempts the frame content stops repainting
press "Fail", otherwise press "Pass".
""";

public RepeatedRepaintTest() {
setTitle("AWT Draw Test");
setSize(300, 300);
background = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
Graphics g = background.getGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, 300, 300);
g.dispose();
}

public void paint(Graphics g) {
Dimension dim = this.getSize();
super.paint(g);
g.drawImage(background, 0, 0, dim.width, dim.height, null);
g.setColor(Color.white);
if (font == null) {
font = new Font("SansSerif", Font.PLAIN, 24);
}
g.setFont(font);
FontMetrics metrics = g.getFontMetrics();
String message = "Draw Test";
g.drawString(message, (dim.width / 2) - (metrics.stringWidth(message) / 2),
(dim.height / 2) + (metrics.getHeight() / 2));

int counter = 50;
for (int i = 0; i < 50; i++) {
counter += 4;
g.drawOval(counter, 50, i, i);
}

counter = 20;
for (int i = 0; i < 100; i++) {
counter += 4;
g.drawOval(counter, 150, i, i);
}
g.setColor(Color.black);
g.drawLine(0, dim.height - 25, dim.width, dim.height - 25);
g.setColor(Color.gray);
g.drawLine(0, dim.height - 24, dim.width, dim.height - 24);
g.setColor(Color.lightGray);
g.drawLine(0, dim.height - 23, dim.width, dim.height - 23);
g.fillRect(0, dim.height - 22, dim.width, dim.height);


g.setXORMode(Color.blue);
g.fillRect(0, 0, 25, dim.height - 26);
g.setColor(Color.red);
g.fillRect(0, 0, 25, dim.height - 26);
g.setColor(Color.green);
g.fillRect(0, 0, 25, dim.height - 26);
g.setPaintMode();

Image img = createImage(50, 50);
Graphics imgGraphics = img.getGraphics();
imgGraphics.setColor(Color.magenta);
imgGraphics.fillRect(0, 0, 50, 50);
imgGraphics.setColor(Color.yellow);
imgGraphics.drawString("offscreen", 0, 20);
imgGraphics.drawString("image", 0, 30);

g.drawImage(img, dim.width - 100, dim.height - 100, Color.blue, null);

g.setXORMode(Color.white);
drawAt(g, 100, 100, 50, 50);
drawAt(g, 105, 105, 50, 50);
drawAt(g, 110, 110, 50, 50);
}

public void drawAt(Graphics g, int x, int y, int width, int height) {
g.setColor(Color.magenta);
g.fillRect(x, y, width, height);
}

public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.title("Repeated Repaint Test Instructions")
.instructions(INSTRUCTIONS)
.testUI(RepeatedRepaintTest::new)
.build()
.awaitAndCheck();
}
}
Loading