Skip to content

Commit 88a1c05

Browse files
committedSep 18, 2024
8340078: Open source several 2D tests
Reviewed-by: honkar
1 parent 3184912 commit 88a1c05

File tree

5 files changed

+484
-0
lines changed

5 files changed

+484
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4725045
27+
* @key headful
28+
* @summary verifies that there are no artifacts due to using
29+
* GDI for copies to the back buffer (GDI should only be used
30+
* for copies to the screen)
31+
* @run main GdiBlitOffscreenTest
32+
*/
33+
34+
import java.awt.Color;
35+
import java.awt.Graphics;
36+
import java.awt.Image;
37+
import java.awt.Point;
38+
import java.awt.Robot;
39+
import java.awt.image.BufferedImage;
40+
import javax.swing.JComponent;
41+
import javax.swing.JFrame;
42+
import javax.swing.SwingUtilities;
43+
44+
public class GdiBlitOffscreenTest {
45+
46+
static volatile JFrame f;
47+
static final int imageW = 100, imageH = 100, FW = 500, FH = 500;
48+
static volatile BufferedImage greenImage;
49+
50+
public static void main(String[] args) throws Exception {
51+
52+
// First, create an image.
53+
greenImage = new BufferedImage(imageW, imageH,
54+
BufferedImage.TYPE_INT_RGB);
55+
Graphics redG = greenImage.getGraphics();
56+
redG.setColor(Color.green);
57+
redG.fillRect(0, 0, imageW, imageH);
58+
redG.setColor(Color.white);
59+
redG.drawString("Passed!", 30, 80);
60+
61+
Robot robot = new Robot();
62+
try {
63+
SwingUtilities.invokeAndWait(GdiBlitOffscreenTest::createUI);
64+
robot.delay(1000);
65+
robot.waitForIdle();
66+
Point p = f.getLocationOnScreen();
67+
Color c = robot.getPixelColor(p.x+FW/2, p.y+FH/2);
68+
if (!c.equals(Color.green)) {
69+
throw new RuntimeException("Color is " + c);
70+
}
71+
} finally {
72+
if (f != null) {
73+
SwingUtilities.invokeAndWait(f::dispose);
74+
}
75+
}
76+
}
77+
78+
private static void createUI() {
79+
f = new JFrame("GdiBlitOffscreenTest");
80+
f.setSize(FW, FH);
81+
f.setVisible(true);
82+
83+
// copy the image to the window.
84+
Graphics g = f.getGraphics();
85+
g.drawImage(greenImage, 0, 0, null);
86+
87+
// Now, get on with the rest of the test
88+
JComponent app = new GdiBlitOffscreenTestComponent(imageW, imageH, greenImage);
89+
app.setSize(500, 500);
90+
f.getContentPane().add(app);
91+
f.validate();
92+
f.repaint();
93+
}
94+
}
95+
96+
class GdiBlitOffscreenTestComponent extends JComponent {
97+
98+
int imageW, imageH;
99+
Image theImage;
100+
101+
public GdiBlitOffscreenTestComponent(int imageW, int imageH,
102+
Image theImage)
103+
{
104+
this.theImage = theImage;
105+
this.imageW = imageW;
106+
this.imageH = imageH;
107+
}
108+
109+
public void paintComponent(Graphics g) {
110+
int imageX = (getWidth() - imageW) / 2;
111+
int imageY = (getHeight() - imageH) / 2;
112+
g.setColor(Color.blue);
113+
g.fillRect(0, 0, getWidth(), getHeight());
114+
g.setColor(Color.red);
115+
g.fillRect(imageX, imageY, imageW, imageH);
116+
g.setColor(Color.white);
117+
g.drawString("Failed!", imageX + 30, imageY + 80);
118+
g.drawImage(theImage, imageX, imageY, null);
119+
}
120+
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4693644
27+
* @summary verifies that there are no artifacts due to copying with GDI
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual GdiLockTest
31+
*/
32+
33+
import java.awt.Color;
34+
import java.awt.Component;
35+
import java.awt.Frame;
36+
import java.awt.Graphics;
37+
38+
public class GdiLockTest {
39+
40+
static final String INSTRUCTIONS = """
41+
A window will open up next to these instructions.
42+
The text you see in that window should blink on and off.
43+
If it never disappears, then the test has failed.
44+
""";
45+
46+
public static void main(String[] argv) throws Exception {
47+
PassFailJFrame.builder()
48+
.title("GdiLockTest")
49+
.instructions(INSTRUCTIONS)
50+
.testTimeOut(5)
51+
.rows(5)
52+
.columns(45)
53+
.testUI(GdiLockTest::createUI)
54+
.build()
55+
.awaitAndCheck();
56+
}
57+
58+
private static Frame createUI() {
59+
Frame f = new Frame("GdiLockTest");
60+
f.setSize(300, 300);
61+
GdiLockTestComponent test = new GdiLockTestComponent();
62+
Thread t = new Thread(test);
63+
f.add(test);
64+
t.start();
65+
return f;
66+
}
67+
}
68+
69+
class GdiLockTestComponent extends Component implements Runnable {
70+
71+
boolean textVisible = true;
72+
73+
public void paint(Graphics g) {
74+
g.setColor(Color.white);
75+
g.fillRect(0, 0, getWidth(), getHeight());
76+
g.setColor(Color.black);
77+
if (!textVisible) {
78+
g.setClip(200, 200, 300, 300);
79+
}
80+
g.drawString("This text should be blinking", 10, 30);
81+
if (!textVisible) {
82+
g.setClip(0, 0, getWidth(), getHeight());
83+
}
84+
}
85+
86+
public void run() {
87+
while (true) {
88+
repaint();
89+
textVisible = !textVisible;
90+
try {
91+
Thread.sleep(500);
92+
} catch (Exception e) {}
93+
}
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4515761
27+
* @summary verify that drawRoundRect produces correct output for 0 w/h
28+
*/
29+
30+
import java.awt.Color;
31+
import static java.awt.Color.*;
32+
import java.awt.Graphics2D;
33+
import java.awt.image.BufferedImage;
34+
35+
public class DrawRoundRect0Bug {
36+
37+
public static void main(String argv[]) {
38+
BufferedImage img = new BufferedImage(250, 250, BufferedImage.TYPE_INT_RGB);
39+
Graphics2D g = img.createGraphics();
40+
41+
g.setColor(white);
42+
g.fillRect(0, 0, img.getWidth(), img.getHeight());
43+
44+
g.setColor(green);
45+
g.drawLine(150, 90, 150, 110);
46+
if (img.getRGB(150, 100) != green.getRGB()) {
47+
throw new RuntimeException("Vertical line not green");
48+
}
49+
50+
g.setColor(blue);
51+
g.drawRoundRect(160, 90, 0, 20, 4, 4);
52+
if (img.getRGB(160, 100) != blue.getRGB()) {
53+
throw new RuntimeException("Vertical (ie zero width) round rect not blue");
54+
}
55+
56+
g.setColor(green);
57+
g.drawLine(150, 140, 170, 140);
58+
if (img.getRGB(160, 140) != green.getRGB()) {
59+
throw new RuntimeException("Horizontal line not green");
60+
}
61+
62+
g.setColor(blue);
63+
g.drawRoundRect(150, 150, 20, 0, 4, 4);
64+
if (img.getRGB(160, 150) != blue.getRGB()) {
65+
throw new RuntimeException("Horizontal (ie zero height) round rect not blue");
66+
}
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4652373
27+
* @summary verify that SunGraphics2D survives surface revalidation
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual RevalidateBug
31+
* @requires (os.family == "windows")
32+
*/
33+
34+
import java.awt.Color;
35+
import java.awt.Dimension;
36+
import java.awt.GradientPaint;
37+
import java.awt.Graphics;
38+
import java.awt.Graphics2D;
39+
import java.awt.Insets;
40+
import java.awt.Rectangle;
41+
import javax.swing.JComponent;
42+
import javax.swing.JFrame;
43+
44+
public class RevalidateBug {
45+
46+
private static final String INSTRUCTIONS = """
47+
This bug only reproduces on Windows systems with a task manager that can lock the computer.
48+
49+
This test draws a grayscale gradient in a window.
50+
51+
After the gradient becomes visible above, use ctrl-alt-del to bring up
52+
the task manager and lock the computer.
53+
Then unlock the computer and the gradient should be repainted to pass.
54+
55+
If the gradient does not appear after unlocking (or if the test gets
56+
an error on its own after unlocking the computer) then it fails.
57+
""";
58+
59+
public static void main(String[] argv) throws Exception {
60+
PassFailJFrame.builder()
61+
.title("RevalidateBug")
62+
.instructions(INSTRUCTIONS)
63+
.testTimeOut(5)
64+
.rows(12)
65+
.columns(50)
66+
.testUI(RevalidateBug::createUI)
67+
.build()
68+
.awaitAndCheck();
69+
}
70+
71+
private static JFrame createUI() {
72+
73+
JComponent comp = new JComponent() {
74+
75+
protected void paintComponent(Graphics g) {
76+
super.paintComponent(g);
77+
System.out.println("paintComponent");
78+
Graphics2D g2d = (Graphics2D) g;
79+
80+
Insets insets = getInsets();
81+
Rectangle rect =
82+
new Rectangle(insets.left, insets.top,
83+
getWidth() - insets.right - insets.left,
84+
getHeight() - insets.top - insets.bottom);
85+
g2d.setPaint(new GradientPaint(rect.x, rect.y, Color.white,
86+
rect.x + rect.width, rect.y, Color.black));
87+
88+
System.out.println(rect + " w:" + getWidth() + " h:"+getHeight());
89+
90+
g2d.fillRect(0, 0, getWidth(), getHeight());
91+
}
92+
93+
public Dimension getPreferredSize() {
94+
return new Dimension(500, 500);
95+
}
96+
};
97+
98+
JFrame f = new JFrame("RevalidateTest");
99+
f.add(comp);
100+
f.pack();
101+
return f;
102+
}
103+
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4516037
27+
* @summary verify that scaled Polygons honor the transform
28+
*/
29+
30+
import java.awt.Color;
31+
import static java.awt.Color.*;
32+
import java.awt.Graphics2D;
33+
import java.awt.Polygon;
34+
import java.awt.image.BufferedImage;
35+
36+
public class ScaledPolyTest {
37+
38+
public static void main(String[] args) {
39+
40+
Polygon poly = new Polygon();
41+
poly.addPoint(20, 10);
42+
poly.addPoint(30, 30);
43+
poly.addPoint(10, 30);
44+
poly.addPoint(20, 10);
45+
46+
int height = 300;
47+
int width = 300;
48+
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
49+
Graphics2D g2d = bi.createGraphics();
50+
g2d.setColor(Color.white);
51+
g2d.fillRect(0, 0, bi.getWidth(), bi.getHeight());
52+
53+
g2d.translate(10, 10);
54+
g2d.scale(2, 2);
55+
g2d.setColor(Color.yellow);
56+
g2d.fill(poly);
57+
g2d.setColor(Color.blue);
58+
g2d.draw(poly);
59+
60+
/*
61+
* Examine each row of the image.
62+
* If the stroked polygon is correctly aligned on the filled polygon,
63+
* if there is anything except white on the line,
64+
* the transition will always be white+->blue+->yellow*->blue*->white+
65+
*/
66+
int bluePix = blue.getRGB();
67+
int yellowPix = yellow.getRGB();
68+
int whitePix = white.getRGB();
69+
for (int y = 0; y < height; y++ ) {
70+
int x = 0;
71+
int pix = whitePix;
72+
73+
while (pix == whitePix && x < width) pix = bi.getRGB(x++, y);
74+
if (pix == whitePix && x == width) continue; // all white row.
75+
76+
if (pix != bluePix) throw new RuntimeException("Expected blue");
77+
78+
while (pix == bluePix) pix = bi.getRGB(x++, y);
79+
80+
if (pix == yellowPix) {
81+
while (pix == yellowPix) pix = bi.getRGB(x++, y);
82+
if (pix != bluePix) throw new RuntimeException("Expected blue");
83+
while (pix == bluePix) pix = bi.getRGB(x++, y);
84+
if (pix != whitePix) throw new RuntimeException("Expected white");
85+
}
86+
87+
while (pix == whitePix && x < width) pix = bi.getRGB(x++, y);
88+
if (pix == whitePix && x == width) {
89+
continue;
90+
} else {
91+
throw new RuntimeException("Expected white to finish the row");
92+
}
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)
Please sign in to comment.