1 /*
2  * Copyright (c) 2009, 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 %W% %E%
26   @key headful
27   @bug 6779670
28   @summary Tests if a LW components in the glass pane affects HW in the content pane
29   @author anthony.petrov@...: area=awt.mixing
30   @library ../regtesthelpers
31   @build Util
32   @run main JButtonInGlassPane
33 */
34 
35 
36 /**
37  * JButtonInGlassPane.java
38  *
39  * summary:  Tests whether a LW menu correctly overlaps a HW button
40  */
41 
42 import java.awt.*;
43 import java.awt.event.*;
44 import javax.swing.*;
45 import test.java.awt.regtesthelpers.Util;
46 
47 
48 
49 public class JButtonInGlassPane
50 {
51     static volatile boolean failed = false;
52 
init()53     private static void init()
54     {
55         JFrame frame = new JFrame("Glass Pane children test");
56         frame.setLayout(null);
57 
58         final Button button = new Button("AWT Button");
59         button.setBounds(100,100,100,100);
60         frame.add(button);
61 
62         button.addActionListener(new ActionListener() {
63             public void actionPerformed(ActionEvent e) {
64                 failed = true;
65             }
66         });
67 
68         frame.getGlassPane().setVisible(true);
69         Container glassPane = (Container) frame.getGlassPane();
70         glassPane.setLayout(null);
71 
72         final JButton jbutton = new JButton("JButton");
73         jbutton.setBounds(50,50,100,100);
74         glassPane.add(jbutton);
75 
76         jbutton.setVisible(false);
77 
78         frame.setSize(400, 400);
79         frame.setLocationRelativeTo(null);
80         frame.setVisible(true);
81 
82         Robot robot = Util.createRobot();
83         robot.setAutoDelay(20);
84 
85         Util.waitForIdle(robot);
86 
87         jbutton.setVisible(true);
88         Util.waitForIdle(robot);
89 
90         // Click the LW button - in the area that intersects with
91         // the HW button.
92         Point lLoc = jbutton.getLocationOnScreen();
93         robot.mouseMove(lLoc.x + jbutton.getWidth() - 5, lLoc.y + jbutton.getHeight() - 5);
94 
95         robot.mousePress(InputEvent.BUTTON1_MASK);
96         robot.mouseRelease(InputEvent.BUTTON1_MASK);
97         Util.waitForIdle(robot);
98 
99         jbutton.setBounds(50,50,120,120);
100         Util.waitForIdle(robot);
101 
102         // Now click on the 'added' area of the LW button that again
103         // intersects with the HW.
104         robot.mouseMove(lLoc.x + jbutton.getWidth() - 5, lLoc.y + jbutton.getHeight() - 5);
105 
106         robot.mousePress(InputEvent.BUTTON1_MASK);
107         robot.mouseRelease(InputEvent.BUTTON1_MASK);
108         Util.waitForIdle(robot);
109 
110         if (failed) {
111             JButtonInGlassPane.fail("The LW button did not receive the click.");
112         } else {
113             JButtonInGlassPane.pass();
114         }
115     }//End  init()
116 
117 
118 
119     /*****************************************************
120      * Standard Test Machinery Section
121      * DO NOT modify anything in this section -- it's a
122      * standard chunk of code which has all of the
123      * synchronisation necessary for the test harness.
124      * By keeping it the same in all tests, it is easier
125      * to read and understand someone else's test, as
126      * well as insuring that all tests behave correctly
127      * with the test harness.
128      * There is a section following this for test-
129      * classes
130      ******************************************************/
131     private static boolean theTestPassed = false;
132     private static boolean testGeneratedInterrupt = false;
133     private static String failureMessage = "";
134 
135     private static Thread mainThread = null;
136 
137     private static int sleepTime = 300000;
138 
139     // Not sure about what happens if multiple of this test are
140     //  instantiated in the same VM.  Being static (and using
141     //  static vars), it aint gonna work.  Not worrying about
142     //  it for now.
main( String args[] )143     public static void main( String args[] ) throws InterruptedException
144     {
145         mainThread = Thread.currentThread();
146         try
147         {
148             init();
149         }
150         catch( TestPassedException e )
151         {
152             //The test passed, so just return from main and harness will
153             // interepret this return as a pass
154             return;
155         }
156         //At this point, neither test pass nor test fail has been
157         // called -- either would have thrown an exception and ended the
158         // test, so we know we have multiple threads.
159 
160         //Test involves other threads, so sleep and wait for them to
161         // called pass() or fail()
162         try
163         {
164             Thread.sleep( sleepTime );
165             //Timed out, so fail the test
166             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
167         }
168         catch (InterruptedException e)
169         {
170             //The test harness may have interrupted the test.  If so, rethrow the exception
171             // so that the harness gets it and deals with it.
172             if( ! testGeneratedInterrupt ) throw e;
173 
174             //reset flag in case hit this code more than once for some reason (just safety)
175             testGeneratedInterrupt = false;
176 
177             if ( theTestPassed == false )
178             {
179                 throw new RuntimeException( failureMessage );
180             }
181         }
182 
183     }//main
184 
setTimeoutTo( int seconds )185     public static synchronized void setTimeoutTo( int seconds )
186     {
187         sleepTime = seconds * 1000;
188     }
189 
pass()190     public static synchronized void pass()
191     {
192         System.out.println( "The test passed." );
193         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
194         //first check if this is executing in main thread
195         if ( mainThread == Thread.currentThread() )
196         {
197             //Still in the main thread, so set the flag just for kicks,
198             // and throw a test passed exception which will be caught
199             // and end the test.
200             theTestPassed = true;
201             throw new TestPassedException();
202         }
203         theTestPassed = true;
204         testGeneratedInterrupt = true;
205         mainThread.interrupt();
206     }//pass()
207 
fail()208     public static synchronized void fail()
209     {
210         //test writer didn't specify why test failed, so give generic
211         fail( "it just plain failed! :-)" );
212     }
213 
fail( String whyFailed )214     public static synchronized void fail( String whyFailed )
215     {
216         System.out.println( "The test failed: " + whyFailed );
217         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
218         //check if this called from main thread
219         if ( mainThread == Thread.currentThread() )
220         {
221             //If main thread, fail now 'cause not sleeping
222             throw new RuntimeException( whyFailed );
223         }
224         theTestPassed = false;
225         testGeneratedInterrupt = true;
226         failureMessage = whyFailed;
227         mainThread.interrupt();
228     }//fail()
229 
230 }// class JButtonInGlassPane
231 
232 //This exception is used to exit from any level of call nesting
233 // when it's determined that the test has passed, and immediately
234 // end the test.
235 class TestPassedException extends RuntimeException
236 {
237 }
238