1 /*
   2  * Copyright (c) 2013, 2017, 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.Rectangle;
  25 import java.awt.Toolkit;
  26 import java.awt.image.BufferedImage;
  27 
  28 import javax.swing.JFrame;
  29 import javax.swing.SwingUtilities;
  30 import jdk.testlibrary.OSInfo;
  31 
  32 /**
  33  * @test
  34  * @key headful
  35  * @bug 7124513
  36  * @summary We should support NSTexturedBackgroundWindowMask style on OSX.
  37  * @author Sergey Bylokhov
  38  * @library ../../../../lib/testlibrary
  39  * @build ExtendedRobot jdk.testlibrary.OSInfo
  40  * @run main NSTexturedJFrame
  41  */
  42 
  43 public final class NSTexturedJFrame {
  44 
  45     private static final String BRUSH = "apple.awt.brushMetalLook";
  46     private static final String STYLE = "Window.style";
  47     private static final BufferedImage[] images = new BufferedImage[3];
  48     private static Rectangle bounds;
  49     private static volatile int step;
  50     private static JFrame frame;
  51     private static ExtendedRobot robot;
  52 
  53     public static void main(final String[] args) throws Exception {
  54         if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
  55             System.out.println("This test is for OSX, considered passed.");
  56             return;
  57         }
  58         robot = new ExtendedRobot();
  59         robot.setAutoDelay(50);
  60         // Default window appearance
  61         showFrame();
  62         step++;
  63         // apple.awt.brushMetalLook appearance
  64         showFrame();
  65         step++;
  66         // Window.style appearance
  67         showFrame();
  68 
  69         // images on step 1 and 2 should be same
  70         testImages(images[1], images[2], false);
  71         // images on step 1 and 2 should be different from default
  72         testImages(images[0], images[1], true);
  73         testImages(images[0], images[2], true);
  74     }
  75 
  76     private static void testImages(BufferedImage img1, BufferedImage img2,
  77                                    boolean shouldbeDifferent) {
  78         boolean different = false;
  79         for (int x = 0; x < img1.getWidth(); ++x) {
  80             for (int y = 0; y < img1.getHeight(); ++y) {
  81                 if (img1.getRGB(x, y) != img2.getRGB(x, y)) {
  82                     different = true;
  83                 }
  84             }
  85         }
  86         if (different != shouldbeDifferent) {
  87             throw new RuntimeException("Textured property does not work");
  88         }
  89     }
  90 
  91     private static void showFrame() throws Exception {
  92         createUI();
  93         images[step] = robot.createScreenCapture(bounds);
  94         SwingUtilities.invokeAndWait(frame::dispose);
  95         robot.waitForIdle(1000);
  96     }
  97 
  98     private static void createUI() throws Exception {
  99         SwingUtilities.invokeAndWait(() -> {
 100             frame = new JFrame();
 101             frame.setUndecorated(true);
 102             frame.setSize(400, 400);
 103             frame.setLocationRelativeTo(null);
 104             switch (step) {
 105                 case 1:
 106                     frame.getRootPane().putClientProperty(BRUSH, true);
 107                     break;
 108                 case 2:
 109                     frame.getRootPane().putClientProperty(STYLE, "textured");
 110             }
 111             frame.setVisible(true);
 112         });
 113         robot.waitForIdle(1000);
 114         SwingUtilities.invokeAndWait(() -> {
 115             bounds = frame.getBounds();
 116         });
 117         robot.waitForIdle(1000);
 118     }
 119 
 120 }