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