1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*
* Copyright (c) 2015, 2024, 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.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/*
* @test
* @bug 8338103
* @key headful
* @summary Verifies that the OpenGL pipeline does not create artifacts
* with swing components after window is zoomed to maximum size and then
* resized back to normal. The test case simulates this operation using
* a JButton. A file image of the component will be saved before and after
* the window resize if the test fails. The test passes if both the button
* images are the same.
* @run main/othervm -Dsun.java2d.opengl=true -Dsun.java2d.opengl.fbobject=false SwingButtonResizeTestWithOpenGL
* @run main/othervm -Dsun.java2d.opengl=true -Dsun.java2d.opengl.fbobject=true SwingButtonResizeTestWithOpenGL
* @run main/othervm -Dsun.java2d.opengl=false SwingButtonResizeTestWithOpenGL
* @run main/othervm SwingButtonResizeTestWithOpenGL
*/
/*
* @test
* @key headful
* @requires (os.family == "windows")
* @run main/othervm -Dsun.java2d.d3d=false SwingButtonResizeTestWithOpenGL
* @run main/othervm -Dsun.java2d.d3d=true SwingButtonResizeTestWithOpenGL
*/
/*
* @test
* @key headful
* @requires (os.family == "linux")
* @run main/othervm -Dsun.java2d.xrender=false SwingButtonResizeTestWithOpenGL
* @run main/othervm -Dsun.java2d.xrender=true SwingButtonResizeTestWithOpenGL
*/
/*
* @test
* @key headful
* @requires (os.family == "mac")
* @run main/othervm -Dsun.java2d.metal=false SwingButtonResizeTestWithOpenGL
* @run main/othervm -Dsun.java2d.metal=true SwingButtonResizeTestWithOpenGL
*/
public class SwingButtonResizeTestWithOpenGL {
private static Robot robot;
private static CountDownLatch focusGainedLatch;
private JFrame frame;
private JButton button;
public SwingButtonResizeTestWithOpenGL() {
try {
SwingUtilities.invokeAndWait(() -> createGUI());
} catch (Exception e) {
throw new RuntimeException("Problems creating GUI");
}
}
private void createGUI() {
frame = new JFrame("SwingButtonResizeTestWithOpenGL");
button = new JButton("Button A");
frame.setLocation(200, 200);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
button.setPreferredSize(new Dimension(300, 300));
button.setFocusPainted(false);
button.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent fe) {
focusGainedLatch.countDown();
}
});
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
frame.toFront();
}
public static void main(String[] args) throws Exception {
focusGainedLatch = new CountDownLatch(1);
SwingButtonResizeTestWithOpenGL test =
new SwingButtonResizeTestWithOpenGL();
test.runTest();
}
public void runTest() throws Exception {
BufferedImage bimage1;
BufferedImage bimage2;
try {
robot = new Robot();
robot.waitForIdle();
robot.delay(1000);
if (focusGainedLatch.await(3, TimeUnit.SECONDS)) {
System.out.println("Button focus gained...");
} else {
System.out.println("Button focus not gained...");
throw new RuntimeException(
"Can't gain focus on button even after waiting " +
"too long..");
}
System.out.println("Getting initial button image..image1");
bimage1 = getButtonImage();
// some platforms may not support maximize frame
if (frame.getToolkit().isFrameStateSupported(
JFrame.MAXIMIZED_BOTH)) {
// maximize frame from normal size
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
System.out.println("Frame is maximized");
robot.waitForIdle();
robot.delay(100);
if (frame.getToolkit().isFrameStateSupported(JFrame.NORMAL)) {
System.out.println("Frame is back to normal");
// resize from maximum size to normal
frame.setExtendedState(JFrame.NORMAL);
robot.waitForIdle();
robot.delay(100);
// capture image of JButton after resize
System.out.println(
"Getting image of JButton after resize..image2");
bimage2 = getButtonImage();
// compare button images from before and after frame resize
DiffImage di = new DiffImage(bimage1.getWidth(),
bimage1.getHeight());
System.out.println(
"Taking the diff of two images, image1 and image2");
if (!di.compare(bimage1, bimage2)) {
throw new RuntimeException(
"Button renderings are different after window "
+ "resize, num of Diff Pixels="
+ di.getNumDiffPixels());
} else {
System.out.println("Test passed...");
}
} else {
System.out.println(
"Test skipped: JFrame.NORMAL resize is " +
"not supported");
}
} else {
System.out.println(
"Test skipped: JFrame.MAXIMIZED_BOTH resize is " +
"not supported");
}
} finally {
SwingUtilities.invokeAndWait(() -> disposeFrame());
}
}
// Capture button rendering as a BufferedImage
private BufferedImage getButtonImage() {
try {
robot.waitForIdle();
robot.delay(500);
AtomicReference<Point> buttonLocRef = new AtomicReference<>();
SwingUtilities.invokeAndWait(
() -> buttonLocRef.set(button.getLocationOnScreen()));
Point buttonLoc = buttonLocRef.get();
System.out.println("Button loc: " + buttonLoc);
return robot.createScreenCapture(
new Rectangle(buttonLoc.x, buttonLoc.y, button.getWidth(),
button.getHeight()));
} catch (Exception e) {
throw new RuntimeException(
"Problems capturing button image from Robot", e);
}
}
private void disposeFrame() {
if(frame != null) {
frame.dispose();
}
}
// Save BufferedImage to PNG file
private void saveButtonImage(BufferedImage image, File file) {
if (image != null) {
try {
System.out.println(
"Saving button image to " + file.getAbsolutePath());
ImageIO.write(image, "PNG", file);
} catch (Exception e) {
throw new RuntimeException("Could not write image file");
}
} else {
throw new RuntimeException("BufferedImage was set to null");
}
}
private class DiffImage extends BufferedImage {
public boolean diff = false;
public int nDiff = -1;
Color bgColor;
int threshold = 0;
public DiffImage(int w, int h) {
super(w, h, BufferedImage.TYPE_INT_ARGB);
bgColor = Color.LIGHT_GRAY;
}
public int getNumDiffPixels() {
return nDiff;
}
public boolean compare(BufferedImage img1, BufferedImage img2)
throws IOException {
int minx1 = img1.getMinX();
int minx2 = img2.getMinX();
int miny1 = img1.getMinY();
int miny2 = img2.getMinY();
int w1 = img1.getWidth();
int w2 = img2.getWidth();
int h1 = img1.getHeight();
int h2 = img2.getHeight();
if ((minx1 != minx2) || (miny1 != miny2) || (w1 != w2)
|| (h1 != h2)) {
// image sizes are different
throw new RuntimeException(
"img1: <" + minx1 + "," + miny1 + "," + w1 + "x" + h1
+ ">" + " img2: " + minx2 + "," + miny2 + "," + w2 + "x"
+ h2 + ">" + " are different sizes");
}
// Get the actual data behind the images
Raster ras1 = img1.getData();
Raster ras2 = img2.getData();
ColorModel cm1 = img1.getColorModel();
ColorModel cm2 = img2.getColorModel();
int r1, r2; // red
int g1, g2; // green
int b1, b2; // blue
Object o1 = null;
Object o2 = null;
nDiff = 0;
for (int x = minx1; x < (minx1 + w1); x++) {
for (int y = miny1; y < (miny1 + h1); y++) {
// Causes rasters to allocate data
o1 = ras1.getDataElements(x, y, o1);
// and we reuse the data on every loop
o2 = ras2.getDataElements(x, y, o2);
r1 = cm1.getRed(o1);
r2 = cm2.getRed(o2);
g1 = cm1.getGreen(o1);
g2 = cm2.getGreen(o2);
b1 = cm1.getBlue(o1);
b2 = cm2.getBlue(o2);
int redAbs = Math.abs(r1 - r2);
int greenAbs = Math.abs(g1 - g2);
int blueAbs = Math.abs(b1 - b2);
if ((redAbs > threshold)
|| (greenAbs > threshold)
|| (blueAbs > threshold)) {
// pixel is different
setDiffPixel(x, y, redAbs, greenAbs, blueAbs);
nDiff++;
} else {
setSamePixel(x, y);
}
}
}
if (nDiff != 0) {
ImageIO.write(this, "png",
new File("diffImage.png"));
saveButtonImage(img1, new File("image1.png"));
saveButtonImage(img2, new File("image2.png"));
}
return nDiff == 0;
}
void setDiffPixel(int x, int y, int r, int g, int b) {
diff = true;
setPixelValue(x, y, 255, r, g, b);
}
void setSamePixel(int x, int y) {
if (bgColor != null) {
setPixelValue(x, y, 255, bgColor.getRed(),
bgColor.getGreen(),
bgColor.getBlue());
} else {
setPixelValue(x, y, 255, Color.black.getRed(),
Color.black.getGreen(), Color.black.getBlue());
}
}
void setPixelValue(int x, int y, int a, int r, int g, int b) {
// setRGB uses BufferedImage.TYPE_INT_ARGB format
int pixel =
((a & 0xff) << 24) + ((r & 0xff) << 16) + ((g & 0xff) << 8)
+ ((b & 0xff));
setRGB(x, y, pixel);
}
}
}
|