/* * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.awt.*; import java.lang.Math; /** * @test * @key headful * @bug 8065739 8131339 8196006 * @requires (os.family == "windows" | os.family == "mac") * @summary When Frame.setExtendedState(Frame.MAXIMIZED_BOTH) * is called for a Frame after been called setMaximizedBounds() with * certain value, Frame bounds must equal to this value. * * @run main SetMaximizedBounds */ public class SetMaximizedBounds { public static void main(String[] args) throws Exception { if (!Toolkit.getDefaultToolkit(). isFrameStateSupported(Frame.MAXIMIZED_BOTH)) { return; } GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice().getDefaultConfiguration(); testMaximizedBounds(gc, false); testMaximizedBounds(gc, true); } static void testMaximizedBounds(GraphicsConfiguration gc, boolean undecorated) throws Exception { Frame frame = null; try { Rectangle maxArea = getMaximizedScreenArea(gc); Robot robot = new Robot(); robot.setAutoDelay(50); frame = new Frame(); frame.setUndecorated(undecorated); Rectangle maximizedBounds = new Rectangle( maxArea.x + maxArea.width / 6, maxArea.y + maxArea.height / 6, maxArea.width / 3, maxArea.height / 3); frame.setMaximizedBounds(maximizedBounds); frame.setSize(maxArea.width / 8, maxArea.height / 8); frame.setVisible(true); robot.waitForIdle(); frame.setExtendedState(Frame.MAXIMIZED_BOTH); robot.waitForIdle(); robot.delay(1000); Rectangle bounds = frame.getBounds(); applyScale(bounds, gc.getDefaultTransform().getScaleX(), gc.getDefaultTransform().getScaleY()); if (!compare(bounds, maximizedBounds)) { throw new RuntimeException("The bounds of the Frame do not equal to what" + " is specified when the frame is in Frame.MAXIMIZED_BOTH"); } frame.setExtendedState(Frame.NORMAL); robot.waitForIdle(); robot.delay(1000); maximizedBounds = new Rectangle( maxArea.x + maxArea.width / 10, maxArea.y + maxArea.height / 10, maxArea.width / 5, maxArea.height / 5); frame.setMaximizedBounds(maximizedBounds); frame.setExtendedState(Frame.MAXIMIZED_BOTH); robot.waitForIdle(); robot.delay(1000); bounds = frame.getBounds(); applyScale(bounds, gc.getDefaultTransform().getScaleX(), gc.getDefaultTransform().getScaleY()); if (!compare(bounds, maximizedBounds)) { throw new RuntimeException("The bounds of the Frame do not equal to what" + " is specified when the frame is in Frame.MAXIMIZED_BOTH"); } } finally { if (frame != null) { frame.dispose(); } } } static Rectangle getMaximizedScreenArea(GraphicsConfiguration gc) { Rectangle bounds = gc.getBounds(); Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc); return new Rectangle( bounds.x + insets.left, bounds.y + insets.top, bounds.width - insets.left - insets.right, bounds.height - insets.top - insets.bottom); } static void applyScale(Rectangle rect, double scaleX, double scaleY) { rect.x = (int)Math.ceil(((double)rect.x * scaleX)); rect.y = (int)Math.ceil(((double)rect.y * scaleY)); rect.width = (int)Math.ceil(((double)rect.width * scaleX)); rect.height = (int)Math.ceil(((double)rect.height * scaleY)); } static boolean compare(Rectangle r1, Rectangle r2) { return (Math.abs(r1.x - r2.x) <= 2 && Math.abs(r1.y - r2.y) <= 2 && Math.abs(r1.width - r2.width) <= 2 && Math.abs(r1.height - r2.height) <= 2); } }