Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mixing/OverlappingButtons.java
47626 views
/*1* Copyright (c) 2007, 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@bug 481109626@summary Tests whether overlapping buttons mix correctly27@author anthony.petrov@...: area=awt.mixing28@library ../regtesthelpers29@build Util30@run main OverlappingButtons31*/323334/**35* OverlappingButtons.java36*37* summary: Tests whether awt.Button and swing.JButton mix correctly38*/3940import java.awt.*;41import java.awt.event.*;42import javax.swing.*;43import test.java.awt.regtesthelpers.Util;44454647public class OverlappingButtons48{4950//*** test-writer defined static variables go here ***5152static volatile String testSeq = "";53final static String checkSeq = new String("010101");5455private static void init()56{57//*** Create instructions for the user here ***5859String[] instructions =60{61"This is an AUTOMATIC test, simply wait until it is done.",62"The result (passed or failed) will be shown in the",63"message window below."64};65Sysout.createDialog( );66Sysout.printInstructions( instructions );676869// Create components70final Frame f = new Frame("Button-JButton mix test");71final Panel p = new Panel();72final Button heavy = new Button(" Heavyweight Button ");73final JButton light = new JButton(" LW Button ");7475// Actions for the buttons add appropriate number to the test sequence76heavy.addActionListener(new java.awt.event.ActionListener()77{78public void actionPerformed(java.awt.event.ActionEvent e) {79p.setComponentZOrder(light, 0);80f.validate();81testSeq = testSeq + "0";82}83}84);8586light.addActionListener(new java.awt.event.ActionListener()87{88public void actionPerformed(java.awt.event.ActionEvent e) {89p.setComponentZOrder(heavy, 0);90f.validate();91testSeq = testSeq + "1";92}93}94);9596// Overlap the buttons97heavy.setBounds(30, 30, 200, 200);98light.setBounds(10, 10, 50, 50);99100// Put the components into the frame101p.setLayout(null);102p.add(heavy);103p.add(light);104f.add(p);105f.setBounds(50, 50, 400, 400);106f.show();107108109Robot robot = Util.createRobot();110robot.setAutoDelay(20);111112Util.waitForIdle(robot);113114// Move the mouse pointer to the position where both115// buttons overlap116Point heavyLoc = heavy.getLocationOnScreen();117robot.mouseMove(heavyLoc.x + 5, heavyLoc.y + 5);118119// Now perform the click at this point for 6 times120for (int i = 0; i < 6; ++i) {121robot.mousePress(InputEvent.BUTTON1_MASK);122robot.mouseRelease(InputEvent.BUTTON1_MASK);123Util.waitForIdle(robot);124}125126Util.waitForIdle(robot);127128// If the buttons are correctly mixed, the test sequence129// is equal to the check sequence.130if (testSeq.equals(checkSeq)) {131OverlappingButtons.pass();132} else {133OverlappingButtons.fail("The components changed their visible Z-order in a wrong sequence: '" + testSeq + "' instead of '" + checkSeq + "'");134}135}//End init()136137138139/*****************************************************140* Standard Test Machinery Section141* DO NOT modify anything in this section -- it's a142* standard chunk of code which has all of the143* synchronisation necessary for the test harness.144* By keeping it the same in all tests, it is easier145* to read and understand someone else's test, as146* well as insuring that all tests behave correctly147* with the test harness.148* There is a section following this for test-149* classes150******************************************************/151private static boolean theTestPassed = false;152private static boolean testGeneratedInterrupt = false;153private static String failureMessage = "";154155private static Thread mainThread = null;156157private static int sleepTime = 300000;158159// Not sure about what happens if multiple of this test are160// instantiated in the same VM. Being static (and using161// static vars), it aint gonna work. Not worrying about162// it for now.163public static void main( String args[] ) throws InterruptedException164{165mainThread = Thread.currentThread();166try167{168init();169}170catch( TestPassedException e )171{172//The test passed, so just return from main and harness will173// interepret this return as a pass174return;175}176//At this point, neither test pass nor test fail has been177// called -- either would have thrown an exception and ended the178// test, so we know we have multiple threads.179180//Test involves other threads, so sleep and wait for them to181// called pass() or fail()182try183{184Thread.sleep( sleepTime );185//Timed out, so fail the test186throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );187}188catch (InterruptedException e)189{190//The test harness may have interrupted the test. If so, rethrow the exception191// so that the harness gets it and deals with it.192if( ! testGeneratedInterrupt ) throw e;193194//reset flag in case hit this code more than once for some reason (just safety)195testGeneratedInterrupt = false;196197if ( theTestPassed == false )198{199throw new RuntimeException( failureMessage );200}201}202203}//main204205public static synchronized void setTimeoutTo( int seconds )206{207sleepTime = seconds * 1000;208}209210public static synchronized void pass()211{212Sysout.println( "The test passed." );213Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );214//first check if this is executing in main thread215if ( mainThread == Thread.currentThread() )216{217//Still in the main thread, so set the flag just for kicks,218// and throw a test passed exception which will be caught219// and end the test.220theTestPassed = true;221throw new TestPassedException();222}223theTestPassed = true;224testGeneratedInterrupt = true;225mainThread.interrupt();226}//pass()227228public static synchronized void fail()229{230//test writer didn't specify why test failed, so give generic231fail( "it just plain failed! :-)" );232}233234public static synchronized void fail( String whyFailed )235{236Sysout.println( "The test failed: " + whyFailed );237Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );238//check if this called from main thread239if ( mainThread == Thread.currentThread() )240{241//If main thread, fail now 'cause not sleeping242throw new RuntimeException( whyFailed );243}244theTestPassed = false;245testGeneratedInterrupt = true;246failureMessage = whyFailed;247mainThread.interrupt();248}//fail()249250}// class OverlappingButtons251252//This exception is used to exit from any level of call nesting253// when it's determined that the test has passed, and immediately254// end the test.255class TestPassedException extends RuntimeException256{257}258259//*********** End Standard Test Machinery Section **********260261262//************ Begin classes defined for the test ****************263264// if want to make listeners, here is the recommended place for them, then instantiate265// them in init()266267/* Example of a class which may be written as part of a test268class NewClass implements anInterface269{270static int newVar = 0;271272public void eventDispatched(AWTEvent e)273{274//Counting events to see if we get enough275eventCount++;276277if( eventCount == 20 )278{279//got enough events, so pass280281OverlappingButtons.pass();282}283else if( tries == 20 )284{285//tried too many times without getting enough events so fail286287OverlappingButtons.fail();288}289290}// eventDispatched()291292}// NewClass class293294*/295296297//************** End classes defined for the test *******************298299300301302/****************************************************303Standard Test Machinery304DO NOT modify anything below -- it's a standard305chunk of code whose purpose is to make user306interaction uniform, and thereby make it simpler307to read and understand someone else's test.308****************************************************/309310/**311This is part of the standard test machinery.312It creates a dialog (with the instructions), and is the interface313for sending text messages to the user.314To print the instructions, send an array of strings to Sysout.createDialog315WithInstructions method. Put one line of instructions per array entry.316To display a message for the tester to see, simply call Sysout.println317with the string to be displayed.318This mimics System.out.println but works within the test harness as well319as standalone.320*/321322class Sysout323{324private static TestDialog dialog;325326public static void createDialogWithInstructions( String[] instructions )327{328dialog = new TestDialog( new Frame(), "Instructions" );329dialog.printInstructions( instructions );330dialog.setVisible(true);331println( "Any messages for the tester will display here." );332}333334public static void createDialog( )335{336dialog = new TestDialog( new Frame(), "Instructions" );337String[] defInstr = { "Instructions will appear here. ", "" } ;338dialog.printInstructions( defInstr );339dialog.setVisible(true);340println( "Any messages for the tester will display here." );341}342343344public static void printInstructions( String[] instructions )345{346dialog.printInstructions( instructions );347}348349350public static void println( String messageIn )351{352dialog.displayMessage( messageIn );353System.out.println(messageIn);354}355356}// Sysout class357358/**359This is part of the standard test machinery. It provides a place for the360test instructions to be displayed, and a place for interactive messages361to the user to be displayed.362To have the test instructions displayed, see Sysout.363To have a message to the user be displayed, see Sysout.364Do not call anything in this dialog directly.365*/366class TestDialog extends Dialog367{368369TextArea instructionsText;370TextArea messageText;371int maxStringLength = 80;372373//DO NOT call this directly, go through Sysout374public TestDialog( Frame frame, String name )375{376super( frame, name );377int scrollBoth = TextArea.SCROLLBARS_BOTH;378instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );379add( "North", instructionsText );380381messageText = new TextArea( "", 5, maxStringLength, scrollBoth );382add("Center", messageText);383384pack();385386setVisible(true);387}// TestDialog()388389//DO NOT call this directly, go through Sysout390public void printInstructions( String[] instructions )391{392//Clear out any current instructions393instructionsText.setText( "" );394395//Go down array of instruction strings396397String printStr, remainingStr;398for( int i=0; i < instructions.length; i++ )399{400//chop up each into pieces maxSringLength long401remainingStr = instructions[ i ];402while( remainingStr.length() > 0 )403{404//if longer than max then chop off first max chars to print405if( remainingStr.length() >= maxStringLength )406{407//Try to chop on a word boundary408int posOfSpace = remainingStr.409lastIndexOf( ' ', maxStringLength - 1 );410411if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;412413printStr = remainingStr.substring( 0, posOfSpace + 1 );414remainingStr = remainingStr.substring( posOfSpace + 1 );415}416//else just print417else418{419printStr = remainingStr;420remainingStr = "";421}422423instructionsText.append( printStr + "\n" );424425}// while426427}// for428429}//printInstructions()430431//DO NOT call this directly, go through Sysout432public void displayMessage( String messageIn )433{434messageText.append( messageIn + "\n" );435System.out.println(messageIn);436}437438}// TestDialog class439440441