|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 | + * @key headful |
| 27 | + * @bug 8266159 |
| 28 | + * @summary Test to detect regression in pixel drawing. |
| 29 | + * A small circle is drawn and boundary pixels are compared to expected pixels. |
| 30 | + * Note : this test is specifically written for uiScale=1.0 |
| 31 | + * @run main/othervm -Dsun.java2d.uiScale=1.0 DrawOvalTest |
| 32 | + */ |
| 33 | + |
| 34 | +import javax.imageio.ImageIO; |
| 35 | +import java.awt.Color; |
| 36 | +import java.awt.Graphics2D; |
| 37 | +import java.awt.GraphicsConfiguration; |
| 38 | +import java.awt.GraphicsEnvironment; |
| 39 | +import java.awt.Transparency; |
| 40 | +import java.awt.image.BufferedImage; |
| 41 | +import java.awt.image.VolatileImage; |
| 42 | +import java.io.File; |
| 43 | +import java.io.IOException; |
| 44 | + |
| 45 | +public class DrawOvalTest { |
| 46 | + public static void main(String[] args) throws IOException { |
| 47 | + GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() |
| 48 | + .getDefaultScreenDevice().getDefaultConfiguration(); |
| 49 | + VolatileImage vi = gc.createCompatibleVolatileImage(10, 10, Transparency.TRANSLUCENT); |
| 50 | + |
| 51 | + // Draw test rendering sequence |
| 52 | + BufferedImage snapshot = null; |
| 53 | + Graphics2D g2 = vi.createGraphics(); |
| 54 | + |
| 55 | + do { |
| 56 | + vi.validate(gc); |
| 57 | + render(g2); |
| 58 | + snapshot = vi.getSnapshot(); |
| 59 | + } while (vi.contentsLost()); |
| 60 | + |
| 61 | + // Pixel color sequence expected after test rendering is complete |
| 62 | + // Blue color = -16776961 |
| 63 | + // Red color = -65536 |
| 64 | + int sequence[] = { |
| 65 | + -16776961, |
| 66 | + -16776961, |
| 67 | + -16776961, |
| 68 | + -65536, |
| 69 | + -65536, |
| 70 | + -65536, |
| 71 | + -65536, |
| 72 | + -16776961, |
| 73 | + -16776961, |
| 74 | + -16776961 |
| 75 | + }; |
| 76 | + |
| 77 | + // Test the color of pixels at the image boundary |
| 78 | + for (int i = 0; i < snapshot.getWidth(); i++) { |
| 79 | + |
| 80 | + // Test first row, last row, first column and last column |
| 81 | + if ( snapshot.getRGB(i, 0) != sequence[i] || |
| 82 | + snapshot.getRGB(i, 9) != sequence[i] || |
| 83 | + snapshot.getRGB(0, i) != sequence[i] || |
| 84 | + snapshot.getRGB(9, i) != sequence[i] ) { |
| 85 | + ImageIO.write(snapshot, "png", new File("DrawOvalTest_snapshot.png")); |
| 86 | + throw new RuntimeException("Test failed."); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static void render(Graphics2D g2) { |
| 92 | + g2.setColor(Color.BLUE); |
| 93 | + g2.fillRect(0, 0, 10, 10); |
| 94 | + g2.setColor(Color.RED); |
| 95 | + g2.drawOval(0, 0, 9, 9); |
| 96 | + } |
| 97 | +} |
0 commit comments