Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mixing/LWComboBox.java
47867 views
/*1* Copyright (c) 2009, 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@bug 663765526@summary Tests whether a LW combobox correctly overlaps a HW button27@author anthony.petrov@...: area=awt.mixing28@library ../regtesthelpers29@build Util30@run main LWComboBox31*/323334/**35* LWComboBox.java36*37* summary: Tests whether a LW combobox correctly overlaps a HW button38*/3940import java.awt.*;41import java.awt.event.*;42import javax.swing.*;43import java.util.Vector;44import test.java.awt.regtesthelpers.Util;45464748public class LWComboBox49{50static volatile boolean failed = false;5152private static void init()53{54//*** Create instructions for the user here ***5556String[] instructions =57{58"This is an AUTOMATIC test, simply wait until it is done.",59"The result (passed or failed) will be shown in the",60"message window below."61};62Sysout.createDialog( );63Sysout.printInstructions( instructions );6465JFrame f = new JFrame("LW menu test");6667JComboBox ch;68Button b;6970Vector v = new Vector();71for(int i = 1 ; i <=20;i++){72v.add("Item # "+i);73}74ch = new JComboBox(v);757677b = new Button("AWT Button");78b.addActionListener(new ActionListener() {79public void actionPerformed(ActionEvent e) {80failed = true;81}82});8384f.add(ch,BorderLayout.NORTH);85f.add(b,BorderLayout.CENTER);86f.setSize(300,300);87f.setVisible(true);8889Robot robot = Util.createRobot();90robot.setAutoDelay(20);9192Util.waitForIdle(robot);9394// Pop up the combobox95Point lLoc = ch.getLocationOnScreen();96System.err.println("lLoc: " + lLoc);97robot.mouseMove(lLoc.x + 5, lLoc.y + 5);9899robot.mousePress(InputEvent.BUTTON1_MASK);100robot.mouseRelease(InputEvent.BUTTON1_MASK);101Util.waitForIdle(robot);102103// Click on the combo popup.104// It's assumed that the popup item is located105// above the heavyweight button.106Point bLoc = b.getLocationOnScreen();107System.err.println("bLoc: " + bLoc);108robot.mouseMove(bLoc.x + 10, bLoc.y + 10);109110robot.mousePress(InputEvent.BUTTON1_MASK);111robot.mouseRelease(InputEvent.BUTTON1_MASK);112Util.waitForIdle(robot);113114if (failed) {115fail("The LW popup did not received the click.");116} else {117pass();118}119}//End init()120121122123/*****************************************************124* Standard Test Machinery Section125* DO NOT modify anything in this section -- it's a126* standard chunk of code which has all of the127* synchronisation necessary for the test harness.128* By keeping it the same in all tests, it is easier129* to read and understand someone else's test, as130* well as insuring that all tests behave correctly131* with the test harness.132* There is a section following this for test-133* classes134******************************************************/135private static boolean theTestPassed = false;136private static boolean testGeneratedInterrupt = false;137private static String failureMessage = "";138139private static Thread mainThread = null;140141private static int sleepTime = 300000;142143// Not sure about what happens if multiple of this test are144// instantiated in the same VM. Being static (and using145// static vars), it aint gonna work. Not worrying about146// it for now.147public static void main( String args[] ) throws InterruptedException148{149mainThread = Thread.currentThread();150try151{152init();153}154catch( TestPassedException e )155{156//The test passed, so just return from main and harness will157// interepret this return as a pass158return;159}160//At this point, neither test pass nor test fail has been161// called -- either would have thrown an exception and ended the162// test, so we know we have multiple threads.163164//Test involves other threads, so sleep and wait for them to165// called pass() or fail()166try167{168Thread.sleep( sleepTime );169//Timed out, so fail the test170throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );171}172catch (InterruptedException e)173{174//The test harness may have interrupted the test. If so, rethrow the exception175// so that the harness gets it and deals with it.176if( ! testGeneratedInterrupt ) throw e;177178//reset flag in case hit this code more than once for some reason (just safety)179testGeneratedInterrupt = false;180181if ( theTestPassed == false )182{183throw new RuntimeException( failureMessage );184}185}186187}//main188189public static synchronized void setTimeoutTo( int seconds )190{191sleepTime = seconds * 1000;192}193194public static synchronized void pass()195{196Sysout.println( "The test passed." );197Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );198//first check if this is executing in main thread199if ( mainThread == Thread.currentThread() )200{201//Still in the main thread, so set the flag just for kicks,202// and throw a test passed exception which will be caught203// and end the test.204theTestPassed = true;205throw new TestPassedException();206}207theTestPassed = true;208testGeneratedInterrupt = true;209mainThread.interrupt();210}//pass()211212public static synchronized void fail()213{214//test writer didn't specify why test failed, so give generic215fail( "it just plain failed! :-)" );216}217218public static synchronized void fail( String whyFailed )219{220Sysout.println( "The test failed: " + whyFailed );221Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );222//check if this called from main thread223if ( mainThread == Thread.currentThread() )224{225//If main thread, fail now 'cause not sleeping226throw new RuntimeException( whyFailed );227}228theTestPassed = false;229testGeneratedInterrupt = true;230failureMessage = whyFailed;231mainThread.interrupt();232}//fail()233234}// class LWComboBox235236//This exception is used to exit from any level of call nesting237// when it's determined that the test has passed, and immediately238// end the test.239class TestPassedException extends RuntimeException240{241}242243//*********** End Standard Test Machinery Section **********244245246//************ Begin classes defined for the test ****************247248// if want to make listeners, here is the recommended place for them, then instantiate249// them in init()250251/* Example of a class which may be written as part of a test252class NewClass implements anInterface253{254static int newVar = 0;255256public void eventDispatched(AWTEvent e)257{258//Counting events to see if we get enough259eventCount++;260261if( eventCount == 20 )262{263//got enough events, so pass264265LWComboBox.pass();266}267else if( tries == 20 )268{269//tried too many times without getting enough events so fail270271LWComboBox.fail();272}273274}// eventDispatched()275276}// NewClass class277278*/279280281//************** End classes defined for the test *******************282283284285286/****************************************************287Standard Test Machinery288DO NOT modify anything below -- it's a standard289chunk of code whose purpose is to make user290interaction uniform, and thereby make it simpler291to read and understand someone else's test.292****************************************************/293294/**295This is part of the standard test machinery.296It creates a dialog (with the instructions), and is the interface297for sending text messages to the user.298To print the instructions, send an array of strings to Sysout.createDialog299WithInstructions method. Put one line of instructions per array entry.300To display a message for the tester to see, simply call Sysout.println301with the string to be displayed.302This mimics System.out.println but works within the test harness as well303as standalone.304*/305306class Sysout307{308private static TestDialog dialog;309310public static void createDialogWithInstructions( String[] instructions )311{312dialog = new TestDialog( new Frame(), "Instructions" );313dialog.printInstructions( instructions );314dialog.setVisible(true);315println( "Any messages for the tester will display here." );316}317318public static void createDialog( )319{320dialog = new TestDialog( new Frame(), "Instructions" );321String[] defInstr = { "Instructions will appear here. ", "" } ;322dialog.printInstructions( defInstr );323dialog.setVisible(true);324println( "Any messages for the tester will display here." );325}326327328public static void printInstructions( String[] instructions )329{330dialog.printInstructions( instructions );331}332333334public static void println( String messageIn )335{336dialog.displayMessage( messageIn );337System.out.println(messageIn);338}339340}// Sysout class341342/**343This is part of the standard test machinery. It provides a place for the344test instructions to be displayed, and a place for interactive messages345to the user to be displayed.346To have the test instructions displayed, see Sysout.347To have a message to the user be displayed, see Sysout.348Do not call anything in this dialog directly.349*/350class TestDialog extends Dialog351{352353TextArea instructionsText;354TextArea messageText;355int maxStringLength = 80;356357//DO NOT call this directly, go through Sysout358public TestDialog( Frame frame, String name )359{360super( frame, name );361int scrollBoth = TextArea.SCROLLBARS_BOTH;362instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );363add( "North", instructionsText );364365messageText = new TextArea( "", 5, maxStringLength, scrollBoth );366add("Center", messageText);367368pack();369370setVisible(true);371}// TestDialog()372373//DO NOT call this directly, go through Sysout374public void printInstructions( String[] instructions )375{376//Clear out any current instructions377instructionsText.setText( "" );378379//Go down array of instruction strings380381String printStr, remainingStr;382for( int i=0; i < instructions.length; i++ )383{384//chop up each into pieces maxSringLength long385remainingStr = instructions[ i ];386while( remainingStr.length() > 0 )387{388//if longer than max then chop off first max chars to print389if( remainingStr.length() >= maxStringLength )390{391//Try to chop on a word boundary392int posOfSpace = remainingStr.393lastIndexOf( ' ', maxStringLength - 1 );394395if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;396397printStr = remainingStr.substring( 0, posOfSpace + 1 );398remainingStr = remainingStr.substring( posOfSpace + 1 );399}400//else just print401else402{403printStr = remainingStr;404remainingStr = "";405}406407instructionsText.append( printStr + "\n" );408409}// while410411}// for412413}//printInstructions()414415//DO NOT call this directly, go through Sysout416public void displayMessage( String messageIn )417{418messageText.append( messageIn + "\n" );419System.out.println(messageIn);420}421422}// TestDialog class423424425426427