1/*
2 * Copyright (c) 2007, 2017, 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.*;
25
26/**
27 * @test
28 * @key headful
29 * @bug 8065739 8131339
30 * @summary When Frame.setExtendedState(Frame.MAXIMIZED_BOTH)
31 *          is called for a Frame after been called setMaximizedBounds() with
32 *          certain value, Frame bounds must equal to this value.
33 *
34 * @run main SetMaximizedBounds
35 */
36
37public class SetMaximizedBounds {
38
39    public static void main(String[] args) throws Exception {
40
41        //Supported platforms are Windows and OS X.
42        String os = System.getProperty("os.name").toLowerCase();
43        if (!os.contains("windows") && !os.contains("os x")) {
44            return;
45        }
46
47        if (!Toolkit.getDefaultToolkit().
48                isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {
49            return;
50        }
51
52        GraphicsEnvironment ge = GraphicsEnvironment.
53                getLocalGraphicsEnvironment();
54
55        if (ge.isHeadlessInstance()) {
56            return;
57        }
58
59        for (GraphicsDevice gd : ge.getScreenDevices()) {
60            for (GraphicsConfiguration gc : gd.getConfigurations()) {
61                testMaximizedBounds(gc, false);
62                testMaximizedBounds(gc, true);
63            }
64        }
65    }
66
67    static void testMaximizedBounds(GraphicsConfiguration gc, boolean undecorated)
68            throws Exception {
69
70        Frame frame = null;
71        try {
72
73            Rectangle maxArea = getMaximizedScreenArea(gc);
74
75            Robot robot = new Robot();
76            robot.setAutoDelay(50);
77
78            frame = new Frame();
79            frame.setUndecorated(undecorated);
80            Rectangle maximizedBounds = new Rectangle(
81                    maxArea.x + maxArea.width / 6,
82                    maxArea.y + maxArea.height / 6,
83                    maxArea.width / 3,
84                    maxArea.height / 3);
85            frame.setMaximizedBounds(maximizedBounds);
86            frame.setSize(maxArea.width / 8, maxArea.height / 8);
87            frame.setVisible(true);
88            robot.waitForIdle();
89
90            frame.setExtendedState(Frame.MAXIMIZED_BOTH);
91            robot.waitForIdle();
92            robot.delay(1000);
93
94            Rectangle bounds = frame.getBounds();
95            if (!bounds.equals(maximizedBounds)) {
96                throw new RuntimeException("The bounds of the Frame do not equal to what"
97                        + " is specified when the frame is in Frame.MAXIMIZED_BOTH state");
98            }
99
100            frame.setExtendedState(Frame.NORMAL);
101            robot.waitForIdle();
102            robot.delay(1000);
103
104            maximizedBounds = new Rectangle(
105                    maxArea.x + maxArea.width / 10,
106                    maxArea.y + maxArea.height / 10,
107                    maxArea.width / 5,
108                    maxArea.height / 5);
109            frame.setMaximizedBounds(maximizedBounds);
110            frame.setExtendedState(Frame.MAXIMIZED_BOTH);
111            robot.waitForIdle();
112            robot.delay(1000);
113
114            bounds = frame.getBounds();
115            if (!bounds.equals(maximizedBounds)) {
116                throw new RuntimeException("The bounds of the Frame do not equal to what"
117                        + " is specified when the frame is in Frame.MAXIMIZED_BOTH state");
118            }
119        } finally {
120            if (frame != null) {
121                frame.dispose();
122            }
123        }
124    }
125
126    static Rectangle getMaximizedScreenArea(GraphicsConfiguration gc) {
127        Rectangle bounds = gc.getBounds();
128        Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
129        return new Rectangle(
130                bounds.x + insets.left,
131                bounds.y + insets.top,
132                bounds.width - insets.left - insets.right,
133                bounds.height - insets.top - insets.bottom);
134    }
135}
136