Path: blob/master/test/jdk/java/awt/Mixing/OverlappingButtons.java
41149 views
/*1* Copyright (c) 2007, 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@test25@key headful26@bug 481109627@summary Tests whether overlapping buttons mix correctly28@author anthony.petrov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main OverlappingButtons32*/333435/**36* OverlappingButtons.java37*38* summary: Tests whether awt.Button and swing.JButton mix correctly39*/4041import java.awt.*;42import java.awt.event.*;43import javax.swing.*;44import test.java.awt.regtesthelpers.Util;45464748public class OverlappingButtons49{5051//*** test-writer defined static variables go here ***5253static volatile String testSeq = "";54final static String checkSeq = new String("010101");5556private static void init()57{58//*** Create instructions for the user here ***5960// Create components61final Frame f = new Frame("Button-JButton mix test");62final Panel p = new Panel();63final Button heavy = new Button(" Heavyweight Button ");64final JButton light = new JButton(" LW Button ");6566// Actions for the buttons add appropriate number to the test sequence67heavy.addActionListener(new java.awt.event.ActionListener()68{69public void actionPerformed(java.awt.event.ActionEvent e) {70p.setComponentZOrder(light, 0);71f.validate();72testSeq = testSeq + "0";73}74}75);7677light.addActionListener(new java.awt.event.ActionListener()78{79public void actionPerformed(java.awt.event.ActionEvent e) {80p.setComponentZOrder(heavy, 0);81f.validate();82testSeq = testSeq + "1";83}84}85);8687// Overlap the buttons88heavy.setBounds(30, 30, 200, 200);89light.setBounds(10, 10, 50, 50);9091// Put the components into the frame92p.setLayout(null);93p.add(heavy);94p.add(light);95f.add(p);96f.setBounds(50, 50, 400, 400);97f.show();9899100Robot robot = Util.createRobot();101robot.setAutoDelay(20);102103Util.waitForIdle(robot);104105// Move the mouse pointer to the position where both106// buttons overlap107Point heavyLoc = heavy.getLocationOnScreen();108robot.mouseMove(heavyLoc.x + 5, heavyLoc.y + 5);109110// Now perform the click at this point for 6 times111for (int i = 0; i < 6; ++i) {112robot.mousePress(InputEvent.BUTTON1_MASK);113robot.mouseRelease(InputEvent.BUTTON1_MASK);114Util.waitForIdle(robot);115}116117Util.waitForIdle(robot);118119// If the buttons are correctly mixed, the test sequence120// is equal to the check sequence.121if (testSeq.equals(checkSeq)) {122OverlappingButtons.pass();123} else {124OverlappingButtons.fail("The components changed their visible Z-order in a wrong sequence: '" + testSeq + "' instead of '" + checkSeq + "'");125}126}//End init()127128129130/*****************************************************131* Standard Test Machinery Section132* DO NOT modify anything in this section -- it's a133* standard chunk of code which has all of the134* synchronisation necessary for the test harness.135* By keeping it the same in all tests, it is easier136* to read and understand someone else's test, as137* well as insuring that all tests behave correctly138* with the test harness.139* There is a section following this for test-140* classes141******************************************************/142private static boolean theTestPassed = false;143private static boolean testGeneratedInterrupt = false;144private static String failureMessage = "";145146private static Thread mainThread = null;147148private static int sleepTime = 300000;149150// Not sure about what happens if multiple of this test are151// instantiated in the same VM. Being static (and using152// static vars), it aint gonna work. Not worrying about153// it for now.154public static void main( String args[] ) throws InterruptedException155{156mainThread = Thread.currentThread();157try158{159init();160}161catch( TestPassedException e )162{163//The test passed, so just return from main and harness will164// interepret this return as a pass165return;166}167//At this point, neither test pass nor test fail has been168// called -- either would have thrown an exception and ended the169// test, so we know we have multiple threads.170171//Test involves other threads, so sleep and wait for them to172// called pass() or fail()173try174{175Thread.sleep( sleepTime );176//Timed out, so fail the test177throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );178}179catch (InterruptedException e)180{181//The test harness may have interrupted the test. If so, rethrow the exception182// so that the harness gets it and deals with it.183if( ! testGeneratedInterrupt ) throw e;184185//reset flag in case hit this code more than once for some reason (just safety)186testGeneratedInterrupt = false;187188if ( theTestPassed == false )189{190throw new RuntimeException( failureMessage );191}192}193194}//main195196public static synchronized void setTimeoutTo( int seconds )197{198sleepTime = seconds * 1000;199}200201public static synchronized void pass()202{203System.out.println( "The test passed." );204System.out.println( "The test is over, hit Ctl-C to stop Java VM" );205//first check if this is executing in main thread206if ( mainThread == Thread.currentThread() )207{208//Still in the main thread, so set the flag just for kicks,209// and throw a test passed exception which will be caught210// and end the test.211theTestPassed = true;212throw new TestPassedException();213}214theTestPassed = true;215testGeneratedInterrupt = true;216mainThread.interrupt();217}//pass()218219public static synchronized void fail()220{221//test writer didn't specify why test failed, so give generic222fail( "it just plain failed! :-)" );223}224225public static synchronized void fail( String whyFailed )226{227System.out.println( "The test failed: " + whyFailed );228System.out.println( "The test is over, hit Ctl-C to stop Java VM" );229//check if this called from main thread230if ( mainThread == Thread.currentThread() )231{232//If main thread, fail now 'cause not sleeping233throw new RuntimeException( whyFailed );234}235theTestPassed = false;236testGeneratedInterrupt = true;237failureMessage = whyFailed;238mainThread.interrupt();239}//fail()240241}// class OverlappingButtons242243//This exception is used to exit from any level of call nesting244// when it's determined that the test has passed, and immediately245// end the test.246class TestPassedException extends RuntimeException247{248}249250251