1 /*
   2  * Copyright (c) 2013, 2016, 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.AWTException;
  25 import java.awt.Color;
  26 import java.awt.Dimension;
  27 import java.awt.DisplayMode;
  28 import java.awt.Frame;
  29 import java.awt.GraphicsDevice;
  30 import java.awt.GraphicsEnvironment;
  31 import java.awt.Insets;
  32 import java.awt.Robot;
  33 import java.awt.Toolkit;
  34 import java.awt.Window;
  35 import java.awt.image.BufferedImage;
  36 
  37 import sun.awt.SunToolkit;
  38 
  39 /**
  40  * @test
  41  * @key headful
  42  * @bug 8003173 7019055
  43  * @summary Full-screen windows should have the proper insets.
  44  * @author Sergey Bylokhov
  45  */
  46 public final class FullScreenInsets {
  47 
  48     private static boolean passed = true;
  49 
  50     public static void main(final String[] args) {
  51         final GraphicsEnvironment ge = GraphicsEnvironment
  52                 .getLocalGraphicsEnvironment();
  53         final GraphicsDevice[] devices = ge.getScreenDevices();
  54 
  55         final Window wGreen = new Frame();
  56         wGreen.setBackground(Color.GREEN);
  57         wGreen.setSize(300, 300);
  58         wGreen.setVisible(true);
  59         sleep();
  60         final Insets iGreen = wGreen.getInsets();
  61         final Dimension sGreen = wGreen.getSize();
  62 
  63         final Window wRed = new Frame();
  64         wRed.setBackground(Color.RED);
  65         wRed.setSize(300, 300);
  66         wRed.setVisible(true);
  67         sleep();
  68         final Insets iRed = wGreen.getInsets();
  69         final Dimension sRed = wGreen.getSize();
  70 
  71         for (final GraphicsDevice device : devices) {
  72             if (!device.isFullScreenSupported()) {
  73                 continue;
  74             }
  75             device.setFullScreenWindow(wGreen);
  76             sleep();
  77             testWindowBounds(device.getDisplayMode(), wGreen);
  78             testColor(wGreen, Color.GREEN);
  79 
  80             device.setFullScreenWindow(wRed);
  81             sleep();
  82             testWindowBounds(device.getDisplayMode(), wRed);
  83             testColor(wRed, Color.RED);
  84 
  85             device.setFullScreenWindow(null);
  86             sleep();
  87             testInsets(wGreen.getInsets(), iGreen);
  88             testInsets(wRed.getInsets(), iRed);
  89             testSize(wGreen.getSize(), sGreen);
  90             testSize(wRed.getSize(), sRed);
  91         }
  92         wGreen.dispose();
  93         wRed.dispose();
  94         if (!passed) {
  95             throw new RuntimeException("Test failed");
  96         }
  97     }
  98 
  99     private static void testSize(final Dimension actual, final Dimension exp) {
 100         if (!exp.equals(actual)) {
 101             System.err.println(" Wrong window size:" +
 102                                " Expected: " + exp + " Actual: " + actual);
 103             passed = false;
 104         }
 105     }
 106 
 107     private static void testInsets(final Insets actual, final Insets exp) {
 108         if (!actual.equals(exp)) {
 109             System.err.println(" Wrong window insets:" +
 110                                " Expected: " + exp + " Actual: " + actual);
 111             passed = false;
 112         }
 113     }
 114 
 115     private static void testWindowBounds(final DisplayMode dm, final Window w) {
 116         if (w.getWidth() != dm.getWidth() || w.getHeight() != dm.getHeight()) {
 117             System.err.println(" Wrong window bounds:" +
 118                                " Expected: width = " + dm.getWidth()
 119                                + ", height = " + dm.getHeight() + " Actual: "
 120                                + w.getSize());
 121             passed = false;
 122         }
 123     }
 124 
 125     private static void testColor(final Window w, final Color color) {
 126         final Robot r;
 127         try {
 128             r = new Robot(w.getGraphicsConfiguration().getDevice());
 129         } catch (AWTException e) {
 130             e.printStackTrace();
 131             passed = false;
 132             return;
 133         }
 134         final BufferedImage bi = r.createScreenCapture(w.getBounds());
 135         for (int y = 0; y < bi.getHeight(); y++) {
 136             for (int x = 0; x < bi.getWidth(); x++) {
 137                 if (bi.getRGB(x, y) != color.getRGB()) {
 138                     System.err.println(
 139                             "Incorrect pixel at " + x + "x" + y + " : " +
 140                             Integer.toHexString(bi.getRGB(x, y)) +
 141                             " ,expected : " + Integer.toHexString(
 142                                     color.getRGB()));
 143                     passed = false;
 144                     return;
 145                 }
 146             }
 147         }
 148     }
 149 
 150     private static void sleep() {
 151         ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
 152         try {
 153             Thread.sleep(2000);
 154         } catch (InterruptedException ignored) {
 155         }
 156     }
 157 }