Path: blob/master/test/jdk/java/awt/Mixing/MixingOnDialog.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 mixing works on Dialogs28@author anthony.petrov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main MixingOnDialog32*/333435/**36* MixingOnDialog.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 MixingOnDialog49{50static volatile boolean heavyClicked = false;51static volatile boolean lightClicked = false;5253private static void init()54{55// Create components56final Dialog d = new Dialog((Frame)null, "Button-JButton mix test");57final Button heavy = new Button(" Heavyweight Button ");58final JButton light = new JButton(" LW Button ");5960// Actions for the buttons add appropriate number to the test sequence61heavy.addActionListener(new java.awt.event.ActionListener()62{63public void actionPerformed(java.awt.event.ActionEvent e) {64heavyClicked = true;65}66}67);6869light.addActionListener(new java.awt.event.ActionListener()70{71public void actionPerformed(java.awt.event.ActionEvent e) {72lightClicked = true;73}74}75);7677// Overlap the buttons78heavy.setBounds(30, 30, 200, 200);79light.setBounds(10, 10, 50, 50);8081// Put the components into the frame82d.setLayout(null);83d.add(light);84d.add(heavy);85d.setBounds(50, 50, 400, 400);86d.setVisible(true);878889Robot robot = Util.createRobot();90robot.setAutoDelay(20);9192Util.waitForIdle(robot);9394// Move the mouse pointer to the position where both95// buttons overlap96Point heavyLoc = heavy.getLocationOnScreen();97robot.mouseMove(heavyLoc.x + 5, heavyLoc.y + 5);9899// Now perform the click at this point100robot.mousePress(InputEvent.BUTTON1_MASK);101robot.mouseRelease(InputEvent.BUTTON1_MASK);102Util.waitForIdle(robot);103104// If the buttons are correctly mixed, the test sequence105// is equal to the check sequence.106if (lightClicked == true) {107MixingOnDialog.pass();108} else {109MixingOnDialog.fail("The lightweight component left behind the heavyweight one.");110}111}//End init()112113114115/*****************************************************116* Standard Test Machinery Section117* DO NOT modify anything in this section -- it's a118* standard chunk of code which has all of the119* synchronisation necessary for the test harness.120* By keeping it the same in all tests, it is easier121* to read and understand someone else's test, as122* well as insuring that all tests behave correctly123* with the test harness.124* There is a section following this for test-125* classes126******************************************************/127private static boolean theTestPassed = false;128private static boolean testGeneratedInterrupt = false;129private static String failureMessage = "";130131private static Thread mainThread = null;132133private static int sleepTime = 300000;134135// Not sure about what happens if multiple of this test are136// instantiated in the same VM. Being static (and using137// static vars), it aint gonna work. Not worrying about138// it for now.139public static void main( String args[] ) throws InterruptedException140{141mainThread = Thread.currentThread();142try143{144init();145}146catch( TestPassedException e )147{148//The test passed, so just return from main and harness will149// interepret this return as a pass150return;151}152//At this point, neither test pass nor test fail has been153// called -- either would have thrown an exception and ended the154// test, so we know we have multiple threads.155156//Test involves other threads, so sleep and wait for them to157// called pass() or fail()158try159{160Thread.sleep( sleepTime );161//Timed out, so fail the test162throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );163}164catch (InterruptedException e)165{166//The test harness may have interrupted the test. If so, rethrow the exception167// so that the harness gets it and deals with it.168if( ! testGeneratedInterrupt ) throw e;169170//reset flag in case hit this code more than once for some reason (just safety)171testGeneratedInterrupt = false;172173if ( theTestPassed == false )174{175throw new RuntimeException( failureMessage );176}177}178179}//main180181public static synchronized void setTimeoutTo( int seconds )182{183sleepTime = seconds * 1000;184}185186public static synchronized void pass()187{188System.out.println( "The test passed." );189System.out.println( "The test is over, hit Ctl-C to stop Java VM" );190//first check if this is executing in main thread191if ( mainThread == Thread.currentThread() )192{193//Still in the main thread, so set the flag just for kicks,194// and throw a test passed exception which will be caught195// and end the test.196theTestPassed = true;197throw new TestPassedException();198}199theTestPassed = true;200testGeneratedInterrupt = true;201mainThread.interrupt();202}//pass()203204public static synchronized void fail()205{206//test writer didn't specify why test failed, so give generic207fail( "it just plain failed! :-)" );208}209210public static synchronized void fail( String whyFailed )211{212System.out.println( "The test failed: " + whyFailed );213System.out.println( "The test is over, hit Ctl-C to stop Java VM" );214//check if this called from main thread215if ( mainThread == Thread.currentThread() )216{217//If main thread, fail now 'cause not sleeping218throw new RuntimeException( whyFailed );219}220theTestPassed = false;221testGeneratedInterrupt = true;222failureMessage = whyFailed;223mainThread.interrupt();224}//fail()225226}// class MixingOnDialog227228//This exception is used to exit from any level of call nesting229// when it's determined that the test has passed, and immediately230// end the test.231class TestPassedException extends RuntimeException232{233}234235236