1 /*
   2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   @test
  26   @key headful
  27   @bug 4962534
  28   @summary JFrame dances very badly
  29   @run main bug4962534
  30  */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import java.util.Random;
  35 import javax.swing.*;
  36 
  37 public class bug4962534 {
  38 
  39     Robot robot;
  40     volatile Point framePosition;
  41     volatile Point newFrameLocation;
  42     JFrame frame;
  43     Rectangle gcBounds;
  44     Component titleComponent;
  45     JLayeredPane lPane;
  46     volatile boolean titleFound = false;
  47     public static Object LOCK = new Object();
  48 
  49     public static void main(final String[] args) {
  50         bug4962534 app = new bug4962534();
  51         app.init();
  52         app.start();
  53     }
  54 
  55     public void init() {
  56         try {
  57             SwingUtilities.invokeAndWait(new Runnable() {
  58                 @Override
  59                 public void run() {
  60                     createAndShowGUI();
  61                 }
  62             });
  63         } catch (Exception ex) {
  64             throw new RuntimeException("Init failed. " + ex.getMessage());
  65         }
  66     }//End  init()
  67 
  68     public void start() {
  69         try {
  70             setJLayeredPaneEDT();
  71             setTitleComponentEDT();
  72         } catch (Exception ex) {
  73             ex.printStackTrace();
  74             throw new RuntimeException("Test failed. " + ex.getMessage());
  75         }
  76 
  77         if (!titleFound) {
  78             throw new RuntimeException("Test Failed. Unable to determine title's size.");
  79         }
  80 
  81         Random r = new Random();
  82 
  83         for (int iteration = 0; iteration < 10; iteration++) {
  84             try {
  85                 setFramePosEDT();
  86             } catch (Exception ex) {
  87                 ex.printStackTrace();
  88                 throw new RuntimeException("Test failed.");
  89             }
  90             try {
  91                 robot = new Robot();
  92                 robot.setAutoDelay(70);
  93 
  94                 robot.waitForIdle();
  95 
  96                 robot.mouseMove(framePosition.x + getJFrameWidthEDT() / 2,
  97                         framePosition.y + titleComponent.getHeight() / 2);
  98                 robot.mousePress(InputEvent.BUTTON1_MASK);
  99 
 100                 robot.waitForIdle();
 101 
 102                 gcBounds =
 103                         GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getConfigurations()[0].getBounds();
 104 
 105                 robot.mouseMove(framePosition.x + getJFrameWidthEDT() / 2,
 106                         framePosition.y + titleComponent.getHeight() / 2);
 107 
 108                 robot.waitForIdle();
 109 
 110                 int multier = gcBounds.height / 2 - 10; //we will not go out the borders
 111                 for (int i = 0; i < 10; i++) {
 112                     robot.mouseMove(gcBounds.width / 2 - (int) (r.nextDouble() * multier), gcBounds.height / 2 - (int) (r.nextDouble() * multier));
 113                 }
 114                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
 115 
 116                 robot.waitForIdle();
 117 
 118             } catch (AWTException e) {
 119                 throw new RuntimeException("Test Failed. AWTException thrown." + e.getMessage());
 120             } catch (Exception e) {
 121                 e.printStackTrace();
 122                 throw new RuntimeException("Test Failed.");
 123             }
 124             System.out.println("Mouse  lies in " + MouseInfo.getPointerInfo().getLocation());
 125             boolean frameIsOutOfScreen = false;
 126             try {
 127                 setNewFrameLocationEDT();
 128                 System.out.println("Now Frame lies in " + newFrameLocation);
 129                 frameIsOutOfScreen = checkFrameIsOutOfScreenEDT();
 130             } catch (Exception ex) {
 131                 ex.printStackTrace();
 132                 throw new RuntimeException("Test Failed.");
 133             }
 134 
 135             if (frameIsOutOfScreen) {
 136                 throw new RuntimeException("Test failed. JFrame is out of screen.");
 137             }
 138 
 139         } //for iteration
 140         System.out.println("Test passed.");
 141     }// start()
 142 
 143     private void createAndShowGUI() {
 144         try {
 145             UIManager.setLookAndFeel(
 146                     "javax.swing.plaf.metal.MetalLookAndFeel");
 147         } catch (Exception ex) {
 148             throw new RuntimeException(ex.getMessage());
 149         }
 150         JFrame.setDefaultLookAndFeelDecorated(true);
 151         frame = new JFrame("JFrame Dance Test");
 152         frame.pack();
 153         frame.setSize(450, 260);
 154         frame.setLocationRelativeTo(null);
 155         frame.setVisible(true);
 156     }
 157 
 158     private void setJLayeredPaneEDT() throws Exception {
 159 
 160         SwingUtilities.invokeAndWait(new Runnable() {
 161             @Override
 162             public void run() {
 163                 lPane = frame.getLayeredPane();
 164                 System.out.println("JFrame's LayeredPane " + lPane);
 165             }
 166         });
 167     }
 168 
 169     private void setTitleComponentEDT() throws Exception {
 170 
 171         SwingUtilities.invokeAndWait(new Runnable() {
 172             @Override
 173             public void run() {
 174                 for (int j = 0; j < lPane.getComponentsInLayer(JLayeredPane.FRAME_CONTENT_LAYER.intValue()).length; j++) {
 175                     titleComponent = lPane.getComponentsInLayer(JLayeredPane.FRAME_CONTENT_LAYER.intValue())[j];
 176                     if (titleComponent.getClass().getName().equals("javax.swing.plaf.metal.MetalTitlePane")) {
 177                         titleFound = true;
 178                         break;
 179                     }
 180                 }
 181             }
 182         });
 183     }
 184 
 185     private void setFramePosEDT() throws Exception {
 186 
 187         SwingUtilities.invokeAndWait(new Runnable() {
 188             @Override
 189             public void run() {
 190                 framePosition = frame.getLocationOnScreen();
 191             }
 192         });
 193     }
 194 
 195     private boolean checkFrameIsOutOfScreenEDT() throws Exception {
 196 
 197         final boolean[] result = new boolean[1];
 198 
 199         SwingUtilities.invokeAndWait(new Runnable() {
 200             @Override
 201             public void run() {
 202                 if (newFrameLocation.x > gcBounds.width || newFrameLocation.x < 0
 203                     || newFrameLocation.y > gcBounds.height || newFrameLocation.y
 204                     < 0) {
 205                 result[0] = true;
 206             }
 207             }
 208         });
 209         return result[0];
 210     }
 211 
 212     private void setNewFrameLocationEDT() throws Exception {
 213 
 214         SwingUtilities.invokeAndWait(new Runnable() {
 215             @Override
 216             public void run() {
 217                 newFrameLocation = new Point(frame.getLocationOnScreen().x
 218                         + frame.getWidth() / 2, frame.getLocationOnScreen().y + titleComponent.getHeight() / 2);
 219             }
 220         });
 221     }
 222 
 223     private int getJFrameWidthEDT() throws Exception {
 224 
 225         final int[] result = new int[1];
 226 
 227         SwingUtilities.invokeAndWait(new Runnable() {
 228             @Override
 229             public void run() {
 230                 result[0] = frame.getWidth();
 231             }
 232         });
 233 
 234         return result[0];
 235     }
 236 }// class