Path: blob/master/test/jdk/javax/swing/JFrame/NSTexturedJFrame/NSTexturedJFrame.java
41155 views
/*1* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Rectangle;24import java.awt.Toolkit;25import java.awt.Color;26import java.awt.image.BufferedImage;2728import javax.swing.JFrame;29import javax.swing.SwingUtilities;3031import jdk.test.lib.Platform;3233/**34* @test35* @key headful36* @bug 712451337* @requires (os.family == "mac")38* @summary We should support NSTexturedBackgroundWindowMask style on OSX.39* @library /test/lib40* /lib/client41* @build ExtendedRobot jdk.test.lib.Platform42* @run main NSTexturedJFrame43*/4445public final class NSTexturedJFrame {4647private static final String BRUSH = "apple.awt.brushMetalLook";48private static final String STYLE = "Window.style";49private static final BufferedImage[] images = new BufferedImage[3];50private static Rectangle bounds;51private static volatile int step;52private static JFrame frame;53private static ExtendedRobot robot;5455public static void main(final String[] args) throws Exception {56if (!Platform.isOSX()) {57System.out.println("This test is for OSX, considered passed.");58return;59}60robot = new ExtendedRobot();61robot.setAutoDelay(50);62// Default window appearance63showFrame();64step++;65// apple.awt.brushMetalLook appearance66showFrame();67step++;68// Window.style appearance69showFrame();7071// images on step 1 and 2 should be same72testImages(images[1], images[2], false);73// images on step 1 and 2 should be different from default74testImages(images[0], images[1], true);75testImages(images[0], images[2], true);76}7778private static void testImages(BufferedImage img1, BufferedImage img2,79boolean shouldbeDifferent) {80boolean different = false;81int tol = 5;82for (int x = 0; x < img1.getWidth(); ++x) {83for (int y = 0; y < img1.getHeight(); ++y) {84Color c1 = new Color(img1.getRGB(x, y));85Color c2 = new Color(img2.getRGB(x, y));8687if ((Math.abs(c1.getRed() - c2.getRed()) > tol) &&88(Math.abs(c1.getBlue() - c2.getBlue()) > tol) &&89(Math.abs(c1.getGreen() - c2.getGreen()) > tol )) {9091different = true;92}93}94}95if (different != shouldbeDifferent) {96throw new RuntimeException("Textured property does not work");97}98}99100private static void showFrame() throws Exception {101createUI();102images[step] = robot.createScreenCapture(bounds);103SwingUtilities.invokeAndWait(frame::dispose);104robot.waitForIdle(1000);105}106107private static void createUI() throws Exception {108SwingUtilities.invokeAndWait(() -> {109frame = new JFrame();110frame.setUndecorated(true);111frame.setSize(400, 400);112frame.setLocationRelativeTo(null);113switch (step) {114case 1:115frame.getRootPane().putClientProperty(BRUSH, true);116break;117case 2:118frame.getRootPane().putClientProperty(STYLE, "textured");119}120frame.setVisible(true);121});122robot.waitForIdle(1000);123SwingUtilities.invokeAndWait(() -> {124bounds = frame.getBounds();125});126robot.waitForIdle(1000);127}128129}130131132