5647N/A/*
5647N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5647N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5647N/A *
5647N/A * This code is free software; you can redistribute it and/or modify it
5647N/A * under the terms of the GNU General Public License version 2 only, as
5647N/A * published by the Free Software Foundation.
5647N/A *
5647N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5647N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5647N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5647N/A * version 2 for more details (a copy is included in the LICENSE file that
5647N/A * accompanied this code).
5647N/A *
5647N/A * You should have received a copy of the GNU General Public License version
5647N/A * 2 along with this work; if not, write to the Free Software Foundation,
5647N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5647N/A *
5647N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5647N/A * or visit www.oracle.com if you need additional information or have any
5647N/A * questions.
5647N/A */
5647N/A
5647N/A/*
5647N/A test
5647N/A @bug 4962534 7104594
5647N/A @summary JFrame dances very badly
5647N/A @author dav@sparc.spb.su area=
5647N/A @run applet bug4962534.html
5647N/A */
5647N/Aimport java.applet.Applet;
5647N/Aimport java.awt.*;
5647N/Aimport java.awt.event.*;
5647N/Aimport java.util.Random;
5647N/Aimport javax.swing.*;
5647N/Aimport sun.awt.SunToolkit;
5647N/A
5647N/Apublic class bug4962534 extends Applet {
5647N/A
5647N/A Robot robot;
5647N/A volatile Point framePosition;
5647N/A volatile Point newFrameLocation;
5647N/A JFrame frame;
5647N/A Rectangle gcBounds;
5647N/A Component titleComponent;
5647N/A JLayeredPane lPane;
5647N/A volatile boolean titleFound = false;
5647N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
5647N/A public static Object LOCK = new Object();
5647N/A
5647N/A @Override
5647N/A public void init() {
5647N/A try {
5647N/A SwingUtilities.invokeAndWait(new Runnable() {
5647N/A @Override
5647N/A public void run() {
5647N/A createAndShowGUI();
5647N/A }
5647N/A });
5647N/A } catch (Exception ex) {
5647N/A throw new RuntimeException("Init failed. " + ex.getMessage());
5647N/A }
5647N/A }//End init()
5647N/A
5647N/A @Override
5647N/A public void start() {
5647N/A validate();
5647N/A
5647N/A try {
5647N/A setJLayeredPaneEDT();
5647N/A setTitleComponentEDT();
5647N/A } catch (Exception ex) {
5647N/A ex.printStackTrace();
5647N/A throw new RuntimeException("Test failed. " + ex.getMessage());
5647N/A }
5647N/A
5647N/A if (!titleFound) {
5647N/A throw new RuntimeException("Test Failed. Unable to determine title's size.");
5647N/A }
5647N/A
5647N/A Random r = new Random();
5647N/A
5647N/A for (int iteration = 0; iteration < 10; iteration++) {
5647N/A try {
5647N/A setFramePosEDT();
5647N/A } catch (Exception ex) {
5647N/A ex.printStackTrace();
5647N/A throw new RuntimeException("Test failed.");
5647N/A }
5647N/A try {
5647N/A robot = new Robot();
5647N/A robot.setAutoDelay(70);
5647N/A
5647N/A toolkit.realSync();
5647N/A
5647N/A robot.mouseMove(framePosition.x + getJFrameWidthEDT() / 2,
5647N/A framePosition.y + titleComponent.getHeight() / 2);
5647N/A robot.mousePress(InputEvent.BUTTON1_MASK);
5647N/A
5647N/A toolkit.realSync();
5647N/A
5647N/A gcBounds =
5647N/A GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getConfigurations()[0].getBounds();
5647N/A
5647N/A robot.mouseMove(framePosition.x + getJFrameWidthEDT() / 2,
5647N/A framePosition.y + titleComponent.getHeight() / 2);
5647N/A
5647N/A toolkit.realSync();
5647N/A
5647N/A int multier = gcBounds.height / 2 - 10; //we will not go out the borders
5647N/A for (int i = 0; i < 10; i++) {
5647N/A robot.mouseMove(gcBounds.width / 2 - (int) (r.nextDouble() * multier), gcBounds.height / 2 - (int) (r.nextDouble() * multier));
5647N/A }
5647N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
5647N/A
5647N/A toolkit.realSync();
5647N/A
5647N/A } catch (AWTException e) {
5647N/A throw new RuntimeException("Test Failed. AWTException thrown." + e.getMessage());
5647N/A } catch (Exception e) {
5647N/A e.printStackTrace();
5647N/A throw new RuntimeException("Test Failed.");
5647N/A }
5647N/A System.out.println("Mouse lies in " + MouseInfo.getPointerInfo().getLocation());
5647N/A boolean frameIsOutOfScreen = false;
5647N/A try {
5647N/A setNewFrameLocationEDT();
5647N/A System.out.println("Now Frame lies in " + newFrameLocation);
5647N/A frameIsOutOfScreen = checkFrameIsOutOfScreenEDT();
5647N/A } catch (Exception ex) {
5647N/A ex.printStackTrace();
5647N/A throw new RuntimeException("Test Failed.");
5647N/A }
5647N/A
5647N/A if (frameIsOutOfScreen) {
5647N/A throw new RuntimeException("Test failed. JFrame is out of screen.");
5647N/A }
5647N/A
5647N/A } //for iteration
5647N/A System.out.println("Test passed.");
5647N/A }// start()
5647N/A
5647N/A private void createAndShowGUI() {
5647N/A try {
5647N/A UIManager.setLookAndFeel(
5647N/A "javax.swing.plaf.metal.MetalLookAndFeel");
5647N/A } catch (Exception ex) {
5647N/A throw new RuntimeException(ex.getMessage());
5647N/A }
5647N/A JFrame.setDefaultLookAndFeelDecorated(true);
5647N/A frame = new JFrame("JFrame Dance Test");
5647N/A frame.pack();
5647N/A frame.setSize(450, 260);
5647N/A frame.setVisible(true);
5647N/A }
5647N/A
5647N/A private void setJLayeredPaneEDT() throws Exception {
5647N/A
5647N/A SwingUtilities.invokeAndWait(new Runnable() {
5647N/A @Override
5647N/A public void run() {
5647N/A lPane = frame.getLayeredPane();
5647N/A System.out.println("JFrame's LayeredPane " + lPane);
5647N/A }
5647N/A });
5647N/A }
5647N/A
5647N/A private void setTitleComponentEDT() throws Exception {
5647N/A
5647N/A SwingUtilities.invokeAndWait(new Runnable() {
5647N/A @Override
5647N/A public void run() {
5647N/A for (int j = 0; j < lPane.getComponentsInLayer(JLayeredPane.FRAME_CONTENT_LAYER.intValue()).length; j++) {
5647N/A titleComponent = lPane.getComponentsInLayer(JLayeredPane.FRAME_CONTENT_LAYER.intValue())[j];
5647N/A if (titleComponent.getClass().getName().equals("javax.swing.plaf.metal.MetalTitlePane")) {
5647N/A titleFound = true;
5647N/A break;
5647N/A }
5647N/A }
5647N/A }
5647N/A });
5647N/A }
5647N/A
5647N/A private void setFramePosEDT() throws Exception {
5647N/A
5647N/A SwingUtilities.invokeAndWait(new Runnable() {
5647N/A @Override
5647N/A public void run() {
5647N/A framePosition = frame.getLocationOnScreen();
5647N/A }
5647N/A });
5647N/A }
5647N/A
5647N/A private boolean checkFrameIsOutOfScreenEDT() throws Exception {
5647N/A
5647N/A final boolean[] result = new boolean[1];
5647N/A
5647N/A SwingUtilities.invokeAndWait(new Runnable() {
5647N/A @Override
5647N/A public void run() {
5647N/A if (newFrameLocation.x > gcBounds.width || newFrameLocation.x < 0
5647N/A || newFrameLocation.y > gcBounds.height || newFrameLocation.y
5647N/A < 0) {
5647N/A result[0] = true;
5647N/A }
5647N/A }
5647N/A });
5647N/A return result[0];
5647N/A }
5647N/A
5647N/A private void setNewFrameLocationEDT() throws Exception {
5647N/A
5647N/A SwingUtilities.invokeAndWait(new Runnable() {
5647N/A @Override
5647N/A public void run() {
5647N/A newFrameLocation = new Point(frame.getLocationOnScreen().x
5647N/A + frame.getWidth() / 2, frame.getLocationOnScreen().y + titleComponent.getHeight() / 2);
5647N/A }
5647N/A });
5647N/A }
5647N/A
5647N/A private int getJFrameWidthEDT() throws Exception {
5647N/A
5647N/A final int[] result = new int[1];
5647N/A
5647N/A SwingUtilities.invokeAndWait(new Runnable() {
5647N/A @Override
5647N/A public void run() {
5647N/A result[0] = frame.getWidth();
5647N/A }
5647N/A });
5647N/A
5647N/A return result[0];
5647N/A }
5647N/A}// class