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