Path: blob/master/test/jdk/java/awt/Frame/RestoreToOppositeScreen/RestoreToOppositeScreen.java
41153 views
/*1* Copyright (c) 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.GraphicsDevice;25import java.awt.GraphicsEnvironment;26import java.awt.Rectangle;27import java.awt.Toolkit;2829/**30* @test31* @bug 825637332* @key headful33* @summary setBounds() should work if the frame is minimized34*/35public final class RestoreToOppositeScreen {3637public static void main(String[] args) throws Exception {38Toolkit toolkit = Toolkit.getDefaultToolkit();39if (!toolkit.isFrameStateSupported(Frame.ICONIFIED)) {40return;41}4243var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();44GraphicsDevice[] gds = ge.getScreenDevices();45for (GraphicsDevice gd1 : gds) {46Rectangle screen1 = gd1.getDefaultConfiguration().getBounds();47int x1 = (int) screen1.getCenterX();48int y1 = (int) screen1.getCenterY();49for (GraphicsDevice gd2 : gds) {50Rectangle screen2 = gd2.getDefaultConfiguration().getBounds();51// tweak the (x2, y2) point so even if the screen1 and screen252// are the same, we will use different bounds, otherwise53// setBounds() will be ignored54int x2 = (int) screen2.getCenterX() - 50;55int y2 = (int) screen2.getCenterY() - 50;56Frame frame = new Frame();57try {58// show the frame on one monitor, and then move it to59// another while the frame minimized60frame.setBounds(x1, y1, 400, 400);61frame.setVisible(true);62Thread.sleep(2000);63frame.setExtendedState(Frame.ICONIFIED);64Thread.sleep(2000);65Rectangle before = new Rectangle(x2, y2, 380, 380);66frame.setBounds(before);67Thread.sleep(2000);68frame.setExtendedState(Frame.NORMAL);69Thread.sleep(2000);70Rectangle after = frame.getBounds();71checkSize(after.x, before.x, "x");72checkSize(after.y, before.y, "y");73checkSize(after.width, before.width, "width");74checkSize(after.height, before.height, "height");75} finally {76frame.dispose();77}78}79}80}8182private static void checkSize(int actual, int expected, String prop) {83if (Math.abs(actual - expected) > 10) { // let's allow size variation,84// the bug is reproduced anyway85System.err.println("Expected: " + expected);86System.err.println("Actual: " + actual);87throw new RuntimeException(prop + " is wrong");88}89}90}919293