1 /*
   2  * Copyright (c) 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.image.BufferedImage;
  26 import java.awt.Robot;
  27 import java.awt.Color;
  28 import javax.swing.JFrame;
  29 import javax.swing.SwingUtilities;
  30 
  31 /**
  32  * @test
  33  * @key headful
  34  * @bug 8181910
  35  * @requires (os.family == "mac")
  36  * @summary [macos] Support dark title bars on macOS
  37  * @run main DarkTitleBar
  38  */
  39 
  40 public final class DarkTitleBar {
  41     private static BufferedImage image = null;
  42     private static Rectangle bounds;
  43     private static JFrame frame;
  44     private static Robot robot;
  45 
  46     public static void main(final String[] args) throws Exception {
  47         robot = new Robot();
  48         robot.setAutoDelay(50);
  49 
  50         // Capture and compare pixel color
  51         testFrame();
  52     }
  53 
  54     private static void testFrame() throws Exception {
  55         createUI();
  56         image = robot.createScreenCapture(bounds);
  57         SwingUtilities.invokeAndWait(frame::dispose);
  58         robot.waitForIdle();
  59 
  60         Color titleColor = new Color(image.getRGB(100, 5), true);
  61         if(titleColor.getRed() > 50 || titleColor.getGreen() > 50 || titleColor.getBlue() > 50) {
  62             throw new RuntimeException("Test failed: Title bar is not of dark colored - " + titleColor);
  63         }
  64     }
  65 
  66     private static void createUI() throws Exception {
  67         SwingUtilities.invokeAndWait(() -> {
  68             frame = new JFrame();
  69             frame.setUndecorated(false);
  70             frame.setSize(400, 400);
  71             frame.setLocationRelativeTo(null);
  72             frame.getRootPane().putClientProperty("apple.awt.windowDarkAppearance", true);
  73             frame.setVisible(true);
  74         });
  75 
  76         robot.waitForIdle();
  77         SwingUtilities.invokeAndWait(() -> {
  78             bounds = frame.getBounds();
  79         });
  80         robot.waitForIdle();
  81 
  82         robot.mouseMove(frame.getX() + 100, frame.getY() + 5); // pixel at which color will be captured
  83         robot.waitForIdle();
  84     }
  85 }