|
| 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 | +import java.awt.Color; |
| 25 | +import java.awt.Dimension; |
| 26 | +import java.awt.EventQueue; |
| 27 | +import java.awt.Frame; |
| 28 | +import java.awt.GraphicsConfiguration; |
| 29 | +import java.awt.GraphicsEnvironment; |
| 30 | +import java.awt.Point; |
| 31 | +import java.awt.Rectangle; |
| 32 | +import java.awt.Robot; |
| 33 | +import java.awt.image.BufferedImage; |
| 34 | +import java.io.File; |
| 35 | +import java.io.IOException; |
| 36 | +import javax.imageio.ImageIO; |
| 37 | + |
| 38 | +/* |
| 39 | + * @test |
| 40 | + * @bug 4328588 |
| 41 | + * @key headful |
| 42 | + * @summary Non-default visual on top-level Frame should work |
| 43 | + * @run main FrameVisualTest |
| 44 | + */ |
| 45 | + |
| 46 | +public class FrameVisualTest { |
| 47 | + private static GraphicsConfiguration[] gcs; |
| 48 | + private static volatile Frame[] frames; |
| 49 | + private static volatile int index; |
| 50 | + |
| 51 | + private static Frame f; |
| 52 | + private static Robot robot; |
| 53 | + private static volatile Point p; |
| 54 | + private static volatile Dimension d; |
| 55 | + private static final int TOLERANCE = 5; |
| 56 | + |
| 57 | + public static void main(String[] args) throws Exception { |
| 58 | + gcs = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getConfigurations(); |
| 59 | + robot = new Robot(); |
| 60 | + robot.setAutoDelay(100); |
| 61 | + try { |
| 62 | + EventQueue.invokeAndWait(() -> { |
| 63 | + createAndShowUI(); |
| 64 | + }); |
| 65 | + robot.delay(1000); |
| 66 | + System.out.println("frames.length: "+frames.length); |
| 67 | + for (index = 0; index < frames.length; index++) { |
| 68 | + EventQueue.invokeAndWait(() -> { |
| 69 | + p = frames[index].getLocation(); |
| 70 | + d = frames[index].getSize(); |
| 71 | + }); |
| 72 | + Rectangle rect = new Rectangle(p, d); |
| 73 | + BufferedImage img = robot.createScreenCapture(rect); |
| 74 | + if (chkImgBackgroundColor(img)) { |
| 75 | + try { |
| 76 | + ImageIO.write(img, "png", new File("Frame_" + index + ".png")); |
| 77 | + } catch (IOException ignored) {} |
| 78 | + throw new RuntimeException("Frame visual test failed with non-white background color"); |
| 79 | + } |
| 80 | + } |
| 81 | + } finally { |
| 82 | + for (index = 0; index < frames.length; index++) { |
| 83 | + EventQueue.invokeAndWait(() -> { |
| 84 | + if (frames[index] != null) { |
| 85 | + frames[index].dispose(); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static void createAndShowUI() { |
| 93 | + frames = new Frame[gcs.length]; |
| 94 | + for (int i = 0; i < frames.length; i++) { |
| 95 | + frames[i] = new Frame("Frame w/ gc " + i, gcs[i]); |
| 96 | + frames[i].setSize(100, 100); |
| 97 | + frames[i].setUndecorated(true); |
| 98 | + frames[i].setBackground(Color.WHITE); |
| 99 | + frames[i].setVisible(true); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static boolean chkImgBackgroundColor(BufferedImage img) { |
| 104 | + |
| 105 | + // scan for mid-line and if it is non-white color then return true. |
| 106 | + for (int x = 1; x < img.getWidth() - 1; ++x) { |
| 107 | + Color c = new Color(img.getRGB(x, img.getHeight() / 2)); |
| 108 | + if ((c.getRed() - Color.WHITE.getRed()) > TOLERANCE && |
| 109 | + (c.getGreen() - Color.WHITE.getGreen()) > TOLERANCE && |
| 110 | + (c.getBlue() - Color.WHITE.getBlue()) > TOLERANCE) { |
| 111 | + return true; |
| 112 | + } |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | +} |
| 117 | + |
0 commit comments