Path: blob/master/test/jdk/java/awt/Mixing/JButtonInGlassPane.java
41149 views
/*1* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test %W% %E%25@key headful26@bug 677967027@summary Tests if a LW components in the glass pane affects HW in the content pane28@author anthony.petrov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main JButtonInGlassPane32*/333435/**36* JButtonInGlassPane.java37*38* summary: Tests whether a LW menu correctly overlaps a HW button39*/4041import java.awt.*;42import java.awt.event.*;43import javax.swing.*;44import test.java.awt.regtesthelpers.Util;45464748public class JButtonInGlassPane49{50static volatile boolean failed = false;5152private static void init()53{54JFrame frame = new JFrame("Glass Pane children test");55frame.setLayout(null);5657final Button button = new Button("AWT Button");58button.setBounds(100,100,100,100);59frame.add(button);6061button.addActionListener(new ActionListener() {62public void actionPerformed(ActionEvent e) {63failed = true;64}65});6667frame.getGlassPane().setVisible(true);68Container glassPane = (Container) frame.getGlassPane();69glassPane.setLayout(null);7071final JButton jbutton = new JButton("JButton");72jbutton.setBounds(50,50,100,100);73glassPane.add(jbutton);7475jbutton.setVisible(false);7677frame.setSize(400, 400);78frame.setLocationRelativeTo(null);79frame.setVisible(true);8081Robot robot = Util.createRobot();82robot.setAutoDelay(20);8384Util.waitForIdle(robot);8586jbutton.setVisible(true);87Util.waitForIdle(robot);8889// Click the LW button - in the area that intersects with90// the HW button.91Point lLoc = jbutton.getLocationOnScreen();92robot.mouseMove(lLoc.x + jbutton.getWidth() - 5, lLoc.y + jbutton.getHeight() - 5);9394robot.mousePress(InputEvent.BUTTON1_MASK);95robot.mouseRelease(InputEvent.BUTTON1_MASK);96Util.waitForIdle(robot);9798jbutton.setBounds(50,50,120,120);99Util.waitForIdle(robot);100101// Now click on the 'added' area of the LW button that again102// intersects with the HW.103robot.mouseMove(lLoc.x + jbutton.getWidth() - 5, lLoc.y + jbutton.getHeight() - 5);104105robot.mousePress(InputEvent.BUTTON1_MASK);106robot.mouseRelease(InputEvent.BUTTON1_MASK);107Util.waitForIdle(robot);108109if (failed) {110JButtonInGlassPane.fail("The LW button did not receive the click.");111} else {112JButtonInGlassPane.pass();113}114}//End init()115116117118/*****************************************************119* Standard Test Machinery Section120* DO NOT modify anything in this section -- it's a121* standard chunk of code which has all of the122* synchronisation necessary for the test harness.123* By keeping it the same in all tests, it is easier124* to read and understand someone else's test, as125* well as insuring that all tests behave correctly126* with the test harness.127* There is a section following this for test-128* classes129******************************************************/130private static boolean theTestPassed = false;131private static boolean testGeneratedInterrupt = false;132private static String failureMessage = "";133134private static Thread mainThread = null;135136private static int sleepTime = 300000;137138// Not sure about what happens if multiple of this test are139// instantiated in the same VM. Being static (and using140// static vars), it aint gonna work. Not worrying about141// it for now.142public static void main( String args[] ) throws InterruptedException143{144mainThread = Thread.currentThread();145try146{147init();148}149catch( TestPassedException e )150{151//The test passed, so just return from main and harness will152// interepret this return as a pass153return;154}155//At this point, neither test pass nor test fail has been156// called -- either would have thrown an exception and ended the157// test, so we know we have multiple threads.158159//Test involves other threads, so sleep and wait for them to160// called pass() or fail()161try162{163Thread.sleep( sleepTime );164//Timed out, so fail the test165throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );166}167catch (InterruptedException e)168{169//The test harness may have interrupted the test. If so, rethrow the exception170// so that the harness gets it and deals with it.171if( ! testGeneratedInterrupt ) throw e;172173//reset flag in case hit this code more than once for some reason (just safety)174testGeneratedInterrupt = false;175176if ( theTestPassed == false )177{178throw new RuntimeException( failureMessage );179}180}181182}//main183184public static synchronized void setTimeoutTo( int seconds )185{186sleepTime = seconds * 1000;187}188189public static synchronized void pass()190{191System.out.println( "The test passed." );192System.out.println( "The test is over, hit Ctl-C to stop Java VM" );193//first check if this is executing in main thread194if ( mainThread == Thread.currentThread() )195{196//Still in the main thread, so set the flag just for kicks,197// and throw a test passed exception which will be caught198// and end the test.199theTestPassed = true;200throw new TestPassedException();201}202theTestPassed = true;203testGeneratedInterrupt = true;204mainThread.interrupt();205}//pass()206207public static synchronized void fail()208{209//test writer didn't specify why test failed, so give generic210fail( "it just plain failed! :-)" );211}212213public static synchronized void fail( String whyFailed )214{215System.out.println( "The test failed: " + whyFailed );216System.out.println( "The test is over, hit Ctl-C to stop Java VM" );217//check if this called from main thread218if ( mainThread == Thread.currentThread() )219{220//If main thread, fail now 'cause not sleeping221throw new RuntimeException( whyFailed );222}223theTestPassed = false;224testGeneratedInterrupt = true;225failureMessage = whyFailed;226mainThread.interrupt();227}//fail()228229}// class JButtonInGlassPane230231//This exception is used to exit from any level of call nesting232// when it's determined that the test has passed, and immediately233// end the test.234class TestPassedException extends RuntimeException235{236}237238239