1 /*
   2  * Copyright (c) 2012, 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  @bug 4962534 7104594
  27  @summary JFrame dances very badly
  28  @author dav@sparc.spb.su area=
  29  @run applet bug4962534.html
  30  @key randomness
  31  */
  32 import java.applet.Applet;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.util.Random;
  36 import javax.swing.*;
  37 
  38 public class bug4962534 extends Applet {
  39 
  40     Robot robot;
  41     volatile Point framePosition;
  42     volatile Point newFrameLocation;
  43     JFrame frame;
  44     Rectangle gcBounds;
  45     Component titleComponent;
  46     JLayeredPane lPane;
  47     volatile boolean titleFound = false;
  48     public static Object LOCK = new Object();
  49 
  50     @Override
  51     public void init() {
  52         try {
  53             SwingUtilities.invokeAndWait(new Runnable() {
  54                 @Override
  55                 public void run() {
  56                     createAndShowGUI();
  57                 }
  58             });
  59         } catch (Exception ex) {
  60             throw new RuntimeException("Init failed. " + ex.getMessage());
  61         }
  62     }//End  init()
  63 
  64     @Override
  65     public void start() {
  66         validate();
  67 
  68         try {
  69             setJLayeredPaneEDT();
  70             setTitleComponentEDT();
  71         } catch (Exception ex) {
  72             ex.printStackTrace();
  73             throw new RuntimeException("Test failed. " + ex.getMessage());
  74         }
  75 
  76         if (!titleFound) {
  77             throw new RuntimeException("Test Failed. Unable to determine title's size.");
  78         }
  79 
  80         Random r = new Random();
  81 
  82         for (int iteration = 0; iteration < 10; iteration++) {
  83             try {
  84                 setFramePosEDT();
  85             } catch (Exception ex) {
  86                 ex.printStackTrace();
  87                 throw new RuntimeException("Test failed.");
  88             }
  89             try {
  90                 robot = new Robot();
  91                 robot.setAutoDelay(70);
  92 
  93                 robot.waitForIdle();
  94 
  95                 robot.mouseMove(framePosition.x + getJFrameWidthEDT() / 2,
  96                         framePosition.y + titleComponent.getHeight() / 2);
  97                 robot.mousePress(InputEvent.BUTTON1_MASK);
  98 
  99                 robot.waitForIdle();
 100 
 101                 gcBounds =
 102                         GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getConfigurations()[0].getBounds();
 103 
 104                 robot.mouseMove(framePosition.x + getJFrameWidthEDT() / 2,
 105                         framePosition.y + titleComponent.getHeight() / 2);
 106 
 107                 robot.waitForIdle();
 108 
 109                 int multier = gcBounds.height / 2 - 10; //we will not go out the borders
 110                 for (int i = 0; i < 10; i++) {
 111                     robot.mouseMove(gcBounds.width / 2 - (int) (r.nextDouble() * multier), gcBounds.height / 2 - (int) (r.nextDouble() * multier));
 112                 }
 113                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
 114 
 115                 robot.waitForIdle();
 116 
 117             } catch (AWTException e) {
 118                 throw new RuntimeException("Test Failed. AWTException thrown." + e.getMessage());
 119             } catch (Exception e) {
 120                 e.printStackTrace();
 121                 throw new RuntimeException("Test Failed.");
 122             }
 123             System.out.println("Mouse  lies in " + MouseInfo.getPointerInfo().getLocation());
 124             boolean frameIsOutOfScreen = false;
 125             try {
 126                 setNewFrameLocationEDT();
 127                 System.out.println("Now Frame lies in " + newFrameLocation);
 128                 frameIsOutOfScreen = checkFrameIsOutOfScreenEDT();
 129             } catch (Exception ex) {
 130                 ex.printStackTrace();
 131                 throw new RuntimeException("Test Failed.");
 132             }
 133 
 134             if (frameIsOutOfScreen) {
 135                 throw new RuntimeException("Test failed. JFrame is out of screen.");
 136             }
 137 
 138         } //for iteration
 139         System.out.println("Test passed.");
 140     }// start()
 141 
 142     private void createAndShowGUI() {
 143         try {
 144             UIManager.setLookAndFeel(
 145                     "javax.swing.plaf.metal.MetalLookAndFeel");
 146         } catch (Exception ex) {
 147             throw new RuntimeException(ex.getMessage());
 148         }
 149         JFrame.setDefaultLookAndFeelDecorated(true);
 150         frame = new JFrame("JFrame Dance Test");
 151         frame.pack();
 152         frame.setSize(450, 260);
 153         frame.setVisible(true);
 154     }
 155 
 156     private void setJLayeredPaneEDT() throws Exception {
 157 
 158         SwingUtilities.invokeAndWait(new Runnable() {
 159             @Override
 160             public void run() {
 161                 lPane = frame.getLayeredPane();
 162                 System.out.println("JFrame's LayeredPane " + lPane);
 163             }
 164         });
 165     }
 166 
 167     private void setTitleComponentEDT() throws Exception {
 168 
 169         SwingUtilities.invokeAndWait(new Runnable() {
 170             @Override
 171             public void run() {
 172                 for (int j = 0; j < lPane.getComponentsInLayer(JLayeredPane.FRAME_CONTENT_LAYER.intValue()).length; j++) {
 173                     titleComponent = lPane.getComponentsInLayer(JLayeredPane.FRAME_CONTENT_LAYER.intValue())[j];
 174                     if (titleComponent.getClass().getName().equals("javax.swing.plaf.metal.MetalTitlePane")) {
 175                         titleFound = true;
 176                         break;
 177                     }
 178                 }
 179             }
 180         });
 181     }
 182 
 183     private void setFramePosEDT() throws Exception {
 184 
 185         SwingUtilities.invokeAndWait(new Runnable() {
 186             @Override
 187             public void run() {
 188                 framePosition = frame.getLocationOnScreen();
 189             }
 190         });
 191     }
 192 
 193     private boolean checkFrameIsOutOfScreenEDT() throws Exception {
 194 
 195         final boolean[] result = new boolean[1];
 196 
 197         SwingUtilities.invokeAndWait(new Runnable() {
 198             @Override
 199             public void run() {
 200                 if (newFrameLocation.x > gcBounds.width || newFrameLocation.x < 0
 201                     || newFrameLocation.y > gcBounds.height || newFrameLocation.y
 202                     < 0) {
 203                 result[0] = true;
 204             }
 205             }
 206         });
 207         return result[0];
 208     }
 209 
 210     private void setNewFrameLocationEDT() throws Exception {
 211 
 212         SwingUtilities.invokeAndWait(new Runnable() {
 213             @Override
 214             public void run() {
 215                 newFrameLocation = new Point(frame.getLocationOnScreen().x
 216                         + frame.getWidth() / 2, frame.getLocationOnScreen().y + titleComponent.getHeight() / 2);
 217             }
 218         });
 219     }
 220 
 221     private int getJFrameWidthEDT() throws Exception {
 222 
 223         final int[] result = new int[1];
 224 
 225         SwingUtilities.invokeAndWait(new Runnable() {
 226             @Override
 227             public void run() {
 228                 result[0] = frame.getWidth();
 229             }
 230         });
 231 
 232         return result[0];
 233     }
 234 }// class