Path: blob/master/test/jdk/java/awt/Frame/SetMaximizedBounds/SetMaximizedBounds.java
41153 views
/*1* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Frame;24import java.awt.GraphicsConfiguration;25import java.awt.GraphicsDevice;26import java.awt.GraphicsEnvironment;27import java.awt.Insets;28import java.awt.Rectangle;29import java.awt.Robot;30import java.awt.Toolkit;3132/**33* @test34* @key headful35* @bug 8065739 8131339 823156436* @requires (os.family == "windows" | os.family == "mac")37* @summary When Frame.setExtendedState(Frame.MAXIMIZED_BOTH)38* is called for a Frame after been called setMaximizedBounds() with39* certain value, Frame bounds must equal to this value.40*/41public class SetMaximizedBounds {4243public static void main(String[] args) throws Exception {44//Supported platforms are Windows and OS X.45String os = System.getProperty("os.name").toLowerCase();46if (!os.contains("windows") && !os.contains("os x")) {47return;48}4950if (!Toolkit.getDefaultToolkit().51isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {52return;53}5455GraphicsEnvironment ge = GraphicsEnvironment.56getLocalGraphicsEnvironment();5758for (GraphicsDevice gd : ge.getScreenDevices()) {59for (GraphicsConfiguration gc : gd.getConfigurations()) {60testMaximizedBounds(gc, false);61testMaximizedBounds(gc, true);62}63}64}6566static void testMaximizedBounds(GraphicsConfiguration gc, boolean undecorated)67throws Exception {6869Frame frame = null;70try {7172Rectangle maxArea = getMaximizedScreenArea(gc);7374Robot robot = new Robot();75robot.setAutoDelay(50);7677frame = new Frame();78frame.setUndecorated(undecorated);79Rectangle maximizedBounds = new Rectangle(80maxArea.x + maxArea.width / 5,81maxArea.y + maxArea.height / 5,82maxArea.width / 2,83maxArea.height / 2);84frame.setMaximizedBounds(maximizedBounds);85frame.setSize(maxArea.width / 8, maxArea.height / 8);86frame.setVisible(true);87robot.waitForIdle();8889frame.setExtendedState(Frame.MAXIMIZED_BOTH);90robot.waitForIdle();91robot.delay(1000);9293Rectangle bounds = frame.getBounds();94if (!bounds.equals(maximizedBounds)) {95System.err.println("Expected: " + maximizedBounds);96System.err.println("Actual: " + bounds);97throw new RuntimeException("The bounds of the Frame do not equal to what"98+ " is specified when the frame is in Frame.MAXIMIZED_BOTH state");99}100101frame.setExtendedState(Frame.NORMAL);102robot.waitForIdle();103robot.delay(1000);104105maximizedBounds = new Rectangle(106maxArea.x + maxArea.width / 6,107maxArea.y + maxArea.height / 6,108maxArea.width / 3,109maxArea.height / 3);110frame.setMaximizedBounds(maximizedBounds);111frame.setExtendedState(Frame.MAXIMIZED_BOTH);112robot.waitForIdle();113robot.delay(1000);114115bounds = frame.getBounds();116if (!bounds.equals(maximizedBounds)) {117System.err.println("Expected: " + maximizedBounds);118System.err.println("Actual: " + bounds);119throw new RuntimeException("The bounds of the Frame do not equal to what"120+ " is specified when the frame is in Frame.MAXIMIZED_BOTH state");121}122} finally {123if (frame != null) {124frame.dispose();125}126}127}128129static Rectangle getMaximizedScreenArea(GraphicsConfiguration gc) {130Rectangle bounds = gc.getBounds();131Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);132return new Rectangle(133bounds.x + insets.left,134bounds.y + insets.top,135bounds.width - insets.left - insets.right,136bounds.height - insets.top - insets.bottom);137}138}139140141