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