|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 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 | +import java.awt.Color; |
| 25 | +import java.awt.Dimension; |
| 26 | +import java.awt.EventQueue; |
| 27 | +import java.awt.Frame; |
| 28 | +import java.awt.Point; |
| 29 | +import java.awt.Rectangle; |
| 30 | +import java.awt.Robot; |
| 31 | +import java.awt.image.BufferedImage; |
| 32 | +import java.io.File; |
| 33 | +import java.io.IOException; |
| 34 | +import javax.imageio.ImageIO; |
| 35 | + |
| 36 | +/* |
| 37 | + * @test |
| 38 | + * @bug 4237529 |
| 39 | + * @key headful |
| 40 | + * @summary Test repainting of an empty frame |
| 41 | + * @run main EmptyFrameTest |
| 42 | + */ |
| 43 | + |
| 44 | +public class EmptyFrameTest { |
| 45 | + private static Frame f; |
| 46 | + private static Robot robot; |
| 47 | + private static volatile Point p; |
| 48 | + private static volatile Dimension d; |
| 49 | + private static final int TOLERANCE = 5; |
| 50 | + public static void main(String[] args) throws Exception { |
| 51 | + robot = new Robot(); |
| 52 | + robot.setAutoDelay(100); |
| 53 | + try { |
| 54 | + EventQueue.invokeAndWait(() -> { |
| 55 | + createAndShowUI(); |
| 56 | + }); |
| 57 | + robot.delay(1000); |
| 58 | + f.setSize(50, 50); |
| 59 | + robot.delay(500); |
| 60 | + EventQueue.invokeAndWait(() -> { |
| 61 | + p = f.getLocation(); |
| 62 | + d = f.getSize(); |
| 63 | + }); |
| 64 | + Rectangle rect = new Rectangle(p, d); |
| 65 | + BufferedImage img = robot.createScreenCapture(rect); |
| 66 | + if (chkImgBackgroundColor(img)) { |
| 67 | + try { |
| 68 | + ImageIO.write(img, "png", new File("Frame.png")); |
| 69 | + } catch (IOException ignored) {} |
| 70 | + throw new RuntimeException("Frame doesn't repaint itself on resize"); |
| 71 | + } |
| 72 | + } finally { |
| 73 | + EventQueue.invokeAndWait(() -> { |
| 74 | + if (f != null) { |
| 75 | + f.dispose(); |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static void createAndShowUI() { |
| 82 | + f = new Frame("EmptyFrameTest"); |
| 83 | + f.setUndecorated(true); |
| 84 | + f.setBackground(Color.RED); |
| 85 | + f.setVisible(true); |
| 86 | + } |
| 87 | + |
| 88 | + private static boolean chkImgBackgroundColor(BufferedImage img) { |
| 89 | + for (int x = 1; x < img.getWidth() - 1; ++x) { |
| 90 | + for (int y = 1; y < img.getHeight() - 1; ++y) { |
| 91 | + Color c = new Color(img.getRGB(x, y)); |
| 92 | + if ((c.getRed() - Color.RED.getRed()) > TOLERANCE) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | +} |
0 commit comments