1 /*
2  * Copyright (c) 2013, 2018, 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 /**
25  * @test
26  * @key headful
27  * @bug 8026143
28  * @summary [macosx] Maximized state could be inconsistent between peer and frame
29  * @author Petr Pchelko
30  * @library /test/lib
31  * @build jdk.test.lib.Platform
32  * @run main MaximizedByPlatform
33  */
34 
35 import jdk.test.lib.Platform;
36 
37 import java.awt.*;
38 
39 public class MaximizedByPlatform {
40     private static Frame frame;
41     private static Rectangle availableScreenBounds;
42 
main(String[] args)43     public static void main(String[] args) {
44         if (!Platform.isOSX()) {
45             // Test only for macosx. Pass
46             return;
47         }
48 
49         Robot robot;
50         try {
51             robot = new Robot();
52         }catch(Exception ex) {
53             ex.printStackTrace();
54             throw new RuntimeException("Unexpected failure");
55         }
56         availableScreenBounds = getAvailableScreenBounds();
57 
58         // Test 1. The maximized state is set in setBounds
59         try {
60             frame = new Frame();
61             frame.setBounds(100, 100, 100, 100);
62             frame.setVisible(true);
63 
64             robot.waitForIdle();
65 
66             frame.setBounds(availableScreenBounds.x, availableScreenBounds.y,
67                     availableScreenBounds.width, availableScreenBounds.height);
68 
69             robot.waitForIdle();
70 
71             if (frame.getExtendedState() != Frame.MAXIMIZED_BOTH) {
72                 throw new RuntimeException("Maximized state was not set for frame in setBounds");
73             }
74         } finally {
75             if (frame != null) frame.dispose();
76         }
77 
78 
79         // Test 2. The maximized state is set in setVisible
80         try {
81             frame = new Frame();
82             frame.setBounds(availableScreenBounds.x, availableScreenBounds.y,
83                     availableScreenBounds.width + 100, availableScreenBounds.height);
84             frame.setVisible(true);
85 
86             robot.waitForIdle();
87 
88             if (frame.getExtendedState() != Frame.MAXIMIZED_BOTH) {
89                 throw new RuntimeException("Maximized state was not set for frame in setVisible");
90             }
91         } finally {
92             if (frame != null) frame.dispose();
93         }
94     }
95 
getAvailableScreenBounds()96     private static Rectangle getAvailableScreenBounds() {
97         final Toolkit toolkit = Toolkit.getDefaultToolkit();
98         final GraphicsEnvironment graphicsEnvironment =
99                 GraphicsEnvironment.getLocalGraphicsEnvironment();
100         final GraphicsDevice graphicsDevice =
101                 graphicsEnvironment.getDefaultScreenDevice();
102 
103         final Dimension screenSize = toolkit.getScreenSize();
104         final Insets screenInsets = toolkit.getScreenInsets(
105                 graphicsDevice.getDefaultConfiguration());
106 
107         final Rectangle availableScreenBounds = new Rectangle(screenSize);
108 
109         availableScreenBounds.x += screenInsets.left;
110         availableScreenBounds.y += screenInsets.top;
111         availableScreenBounds.width -= (screenInsets.left + screenInsets.right);
112         availableScreenBounds.height -= (screenInsets.top + screenInsets.bottom);
113         return availableScreenBounds;
114     }
115 }
116