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