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