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