Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JFrame/4962534/bug4962534.java
38854 views
/*1* Copyright (c) 2012, 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/*24test25@bug 4962534 710459426@summary JFrame dances very badly27@author [email protected] area=28@run applet bug4962534.html29*/30import java.applet.Applet;31import java.awt.*;32import java.awt.event.*;33import java.util.Random;34import javax.swing.*;3536public class bug4962534 extends Applet {3738Robot robot;39volatile Point framePosition;40volatile Point newFrameLocation;41JFrame frame;42Rectangle gcBounds;43Component titleComponent;44JLayeredPane lPane;45volatile boolean titleFound = false;46public static Object LOCK = new Object();4748@Override49public void init() {50try {51SwingUtilities.invokeAndWait(new Runnable() {52@Override53public void run() {54createAndShowGUI();55}56});57} catch (Exception ex) {58throw new RuntimeException("Init failed. " + ex.getMessage());59}60}//End init()6162@Override63public void start() {64validate();6566try {67setJLayeredPaneEDT();68setTitleComponentEDT();69} catch (Exception ex) {70ex.printStackTrace();71throw new RuntimeException("Test failed. " + ex.getMessage());72}7374if (!titleFound) {75throw new RuntimeException("Test Failed. Unable to determine title's size.");76}7778Random r = new Random();7980for (int iteration = 0; iteration < 10; iteration++) {81try {82setFramePosEDT();83} catch (Exception ex) {84ex.printStackTrace();85throw new RuntimeException("Test failed.");86}87try {88robot = new Robot();89robot.setAutoDelay(70);9091robot.waitForIdle();9293robot.mouseMove(framePosition.x + getJFrameWidthEDT() / 2,94framePosition.y + titleComponent.getHeight() / 2);95robot.mousePress(InputEvent.BUTTON1_MASK);9697robot.waitForIdle();9899gcBounds =100GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getConfigurations()[0].getBounds();101102robot.mouseMove(framePosition.x + getJFrameWidthEDT() / 2,103framePosition.y + titleComponent.getHeight() / 2);104105robot.waitForIdle();106107int multier = gcBounds.height / 2 - 10; //we will not go out the borders108for (int i = 0; i < 10; i++) {109robot.mouseMove(gcBounds.width / 2 - (int) (r.nextDouble() * multier), gcBounds.height / 2 - (int) (r.nextDouble() * multier));110}111robot.mouseRelease(InputEvent.BUTTON1_MASK);112113robot.waitForIdle();114115} catch (AWTException e) {116throw new RuntimeException("Test Failed. AWTException thrown." + e.getMessage());117} catch (Exception e) {118e.printStackTrace();119throw new RuntimeException("Test Failed.");120}121System.out.println("Mouse lies in " + MouseInfo.getPointerInfo().getLocation());122boolean frameIsOutOfScreen = false;123try {124setNewFrameLocationEDT();125System.out.println("Now Frame lies in " + newFrameLocation);126frameIsOutOfScreen = checkFrameIsOutOfScreenEDT();127} catch (Exception ex) {128ex.printStackTrace();129throw new RuntimeException("Test Failed.");130}131132if (frameIsOutOfScreen) {133throw new RuntimeException("Test failed. JFrame is out of screen.");134}135136} //for iteration137System.out.println("Test passed.");138}// start()139140private void createAndShowGUI() {141try {142UIManager.setLookAndFeel(143"javax.swing.plaf.metal.MetalLookAndFeel");144} catch (Exception ex) {145throw new RuntimeException(ex.getMessage());146}147JFrame.setDefaultLookAndFeelDecorated(true);148frame = new JFrame("JFrame Dance Test");149frame.pack();150frame.setSize(450, 260);151frame.setVisible(true);152}153154private void setJLayeredPaneEDT() throws Exception {155156SwingUtilities.invokeAndWait(new Runnable() {157@Override158public void run() {159lPane = frame.getLayeredPane();160System.out.println("JFrame's LayeredPane " + lPane);161}162});163}164165private void setTitleComponentEDT() throws Exception {166167SwingUtilities.invokeAndWait(new Runnable() {168@Override169public void run() {170for (int j = 0; j < lPane.getComponentsInLayer(JLayeredPane.FRAME_CONTENT_LAYER.intValue()).length; j++) {171titleComponent = lPane.getComponentsInLayer(JLayeredPane.FRAME_CONTENT_LAYER.intValue())[j];172if (titleComponent.getClass().getName().equals("javax.swing.plaf.metal.MetalTitlePane")) {173titleFound = true;174break;175}176}177}178});179}180181private void setFramePosEDT() throws Exception {182183SwingUtilities.invokeAndWait(new Runnable() {184@Override185public void run() {186framePosition = frame.getLocationOnScreen();187}188});189}190191private boolean checkFrameIsOutOfScreenEDT() throws Exception {192193final boolean[] result = new boolean[1];194195SwingUtilities.invokeAndWait(new Runnable() {196@Override197public void run() {198if (newFrameLocation.x > gcBounds.width || newFrameLocation.x < 0199|| newFrameLocation.y > gcBounds.height || newFrameLocation.y200< 0) {201result[0] = true;202}203}204});205return result[0];206}207208private void setNewFrameLocationEDT() throws Exception {209210SwingUtilities.invokeAndWait(new Runnable() {211@Override212public void run() {213newFrameLocation = new Point(frame.getLocationOnScreen().x214+ frame.getWidth() / 2, frame.getLocationOnScreen().y + titleComponent.getHeight() / 2);215}216});217}218219private int getJFrameWidthEDT() throws Exception {220221final int[] result = new int[1];222223SwingUtilities.invokeAndWait(new Runnable() {224@Override225public void run() {226result[0] = frame.getWidth();227}228});229230return result[0];231}232}// class233234235