Path: blob/master/test/jdk/javax/swing/JFrame/4962534/bug4962534.java
41154 views
/*1* Copyright (c) 2012, 2018, 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*/2223/*24@test25@key headful26@bug 496253427@summary JFrame dances very badly28@run main bug496253429*/3031import java.awt.*;32import java.awt.event.*;33import java.util.Random;34import javax.swing.*;3536public class bug4962534 {3738Robot robot;39volatile Point framePosition;40volatile Point newFrameLocation;41static JFrame frame;42Rectangle gcBounds;43Component titleComponent;44JLayeredPane lPane;45volatile boolean titleFound = false;46public static Object LOCK = new Object();4748public static void main(final String[] args) throws Exception {49try {50bug4962534 app = new bug4962534();51app.init();52app.start();53} finally {54if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());55}56}5758public void init() {59try {60SwingUtilities.invokeAndWait(new Runnable() {61@Override62public void run() {63createAndShowGUI();64}65});66} catch (Exception ex) {67throw new RuntimeException("Init failed. " + ex.getMessage());68}69}//End init()7071public void start() {72try {73setJLayeredPaneEDT();74setTitleComponentEDT();75} catch (Exception ex) {76ex.printStackTrace();77throw new RuntimeException("Test failed. " + ex.getMessage());78}7980if (!titleFound) {81throw new RuntimeException("Test Failed. Unable to determine title's size.");82}8384Random r = new Random();8586for (int iteration = 0; iteration < 10; iteration++) {87try {88setFramePosEDT();89} catch (Exception ex) {90ex.printStackTrace();91throw new RuntimeException("Test failed.");92}93try {94robot = new Robot();95robot.setAutoDelay(70);9697robot.waitForIdle();9899robot.mouseMove(framePosition.x + getJFrameWidthEDT() / 2,100framePosition.y + titleComponent.getHeight() / 2);101robot.mousePress(InputEvent.BUTTON1_MASK);102103robot.waitForIdle();104105gcBounds =106GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getConfigurations()[0].getBounds();107108robot.mouseMove(framePosition.x + getJFrameWidthEDT() / 2,109framePosition.y + titleComponent.getHeight() / 2);110111robot.waitForIdle();112113int multier = gcBounds.height / 2 - 10; //we will not go out the borders114for (int i = 0; i < 10; i++) {115robot.mouseMove(gcBounds.width / 2 - (int) (r.nextDouble() * multier), gcBounds.height / 2 - (int) (r.nextDouble() * multier));116}117robot.mouseRelease(InputEvent.BUTTON1_MASK);118119robot.waitForIdle();120121} catch (AWTException e) {122throw new RuntimeException("Test Failed. AWTException thrown." + e.getMessage());123} catch (Exception e) {124e.printStackTrace();125throw new RuntimeException("Test Failed.");126}127System.out.println("Mouse lies in " + MouseInfo.getPointerInfo().getLocation());128boolean frameIsOutOfScreen = false;129try {130setNewFrameLocationEDT();131System.out.println("Now Frame lies in " + newFrameLocation);132frameIsOutOfScreen = checkFrameIsOutOfScreenEDT();133} catch (Exception ex) {134ex.printStackTrace();135throw new RuntimeException("Test Failed.");136}137138if (frameIsOutOfScreen) {139throw new RuntimeException("Test failed. JFrame is out of screen.");140}141142} //for iteration143System.out.println("Test passed.");144}// start()145146private void createAndShowGUI() {147try {148UIManager.setLookAndFeel(149"javax.swing.plaf.metal.MetalLookAndFeel");150} catch (Exception ex) {151throw new RuntimeException(ex.getMessage());152}153JFrame.setDefaultLookAndFeelDecorated(true);154frame = new JFrame("JFrame Dance Test");155frame.pack();156frame.setSize(450, 260);157frame.setLocationRelativeTo(null);158frame.setVisible(true);159}160161private void setJLayeredPaneEDT() throws Exception {162163SwingUtilities.invokeAndWait(new Runnable() {164@Override165public void run() {166lPane = frame.getLayeredPane();167System.out.println("JFrame's LayeredPane " + lPane);168}169});170}171172private void setTitleComponentEDT() throws Exception {173174SwingUtilities.invokeAndWait(new Runnable() {175@Override176public void run() {177for (int j = 0; j < lPane.getComponentsInLayer(JLayeredPane.FRAME_CONTENT_LAYER.intValue()).length; j++) {178titleComponent = lPane.getComponentsInLayer(JLayeredPane.FRAME_CONTENT_LAYER.intValue())[j];179if (titleComponent.getClass().getName().equals("javax.swing.plaf.metal.MetalTitlePane")) {180titleFound = true;181break;182}183}184}185});186}187188private void setFramePosEDT() throws Exception {189190SwingUtilities.invokeAndWait(new Runnable() {191@Override192public void run() {193framePosition = frame.getLocationOnScreen();194}195});196}197198private boolean checkFrameIsOutOfScreenEDT() throws Exception {199200final boolean[] result = new boolean[1];201202SwingUtilities.invokeAndWait(new Runnable() {203@Override204public void run() {205if (newFrameLocation.x > gcBounds.width || newFrameLocation.x < 0206|| newFrameLocation.y > gcBounds.height || newFrameLocation.y207< 0) {208result[0] = true;209}210}211});212return result[0];213}214215private void setNewFrameLocationEDT() throws Exception {216217SwingUtilities.invokeAndWait(new Runnable() {218@Override219public void run() {220newFrameLocation = new Point(frame.getLocationOnScreen().x221+ frame.getWidth() / 2, frame.getLocationOnScreen().y + titleComponent.getHeight() / 2);222}223});224}225226private int getJFrameWidthEDT() throws Exception {227228final int[] result = new int[1];229230SwingUtilities.invokeAndWait(new Runnable() {231@Override232public void run() {233result[0] = frame.getWidth();234}235});236237return result[0];238}239}// class240241242