Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Menu/OpensWithNoGrab/OpensWithNoGrab.java
38828 views
/*1* Copyright (c) 2005, 2013, 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 635472126@summary REG: Menu does not disappear when clicked, keeping Choice's drop-down open, XToolkit27@author andrei.dmitriev: area=awt.menu28@library ../../regtesthelpers29@build Util30@run main OpensWithNoGrab31*/3233import java.awt.*;34import java.awt.event.*;3536import sun.awt.OSInfo;37import test.java.awt.regtesthelpers.Util;3839public class OpensWithNoGrab40{41final static int delay = 50;42private static void init()43{44String[] instructions =45{46"This is an AUTOMATIC test, simply wait until it is done.",47"The result (passed or failed) will be shown in the",48"message window below."49};50Sysout.createDialog( );51Sysout.printInstructions( instructions );5253if (!(OSInfo.getOSType().equals(OSInfo.OSType.LINUX)54|| OSInfo.getOSType().equals(OSInfo.OSType.SOLARIS))) {55System.out.println("This test is for XAWT/Motif only");56OpensWithNoGrab.pass();57}5859Choice ch = new Choice ();60Frame f = new Frame ("OpensWithNoGrab");61Robot robot;62Point framePt, choicePt;6364ch.add("line 1");65ch.add("line 2");66ch.add("line 3");67ch.add("line 4");68ch.setBackground(Color.red);69f.add(ch);7071Menu file = new Menu ("file");72MenuBar mb = new MenuBar();73mb.add(file);7475file.add(new MenuItem (" "));76file.add(new MenuItem (" "));77file.add(new MenuItem (" "));78file.add(new MenuItem (" "));79file.add(new MenuItem (" "));80file.add(new MenuItem (" "));81file.add(new MenuItem (" "));8283f.setMenuBar(mb);8485f.setBackground(Color.green);86f.setForeground(Color.green);87f.setSize(300, 200);88f.setVisible(true);89try {90robot = new Robot();91robot.setAutoWaitForIdle(true);92robot.setAutoDelay(50);9394Util.waitForIdle(robot);95// press on Choice96choicePt = ch.getLocationOnScreen();97robot.mouseMove(choicePt.x + ch.getWidth()/2, choicePt.y + ch.getHeight()/2);98robot.delay(delay);99robot.mousePress(InputEvent.BUTTON1_MASK);100robot.delay(delay);101robot.mouseRelease(InputEvent.BUTTON1_MASK);102robot.delay(delay);103104// press on Menu105framePt = f.getLocationOnScreen();106robot.mouseMove(choicePt.x + 10, choicePt.y - 15);107robot.delay(10*delay);108robot.mousePress(InputEvent.BUTTON1_MASK);109robot.delay(delay);110robot.mouseRelease(InputEvent.BUTTON1_MASK);111robot.delay(delay);112113robot.mouseMove(choicePt.x + 15, choicePt.y + 15);114Util.waitForIdle(robot);115116Color c = robot.getPixelColor(choicePt.x + 15, choicePt.y + 15);117System.out.println("Color obtained under opened menu is: "+c );118if (!c.equals(Color.red)){119OpensWithNoGrab.fail("Failed: menu was opened by first click after opened Choice.");120}121}catch(Exception e){122e.printStackTrace();123OpensWithNoGrab.fail("Failed: exception occur "+e);124}125OpensWithNoGrab.pass();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{203Sysout.println( "The test passed." );204Sysout.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{227Sysout.println( "The test failed: " + whyFailed );228Sysout.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 OpensWithNoGrab242243//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}249250//*********** End Standard Test Machinery Section **********251252253//************ Begin classes defined for the test ****************254255// if want to make listeners, here is the recommended place for them, then instantiate256// them in init()257258/* Example of a class which may be written as part of a test259class NewClass implements anInterface260{261static int newVar = 0;262263public void eventDispatched(AWTEvent e)264{265//Counting events to see if we get enough266eventCount++;267268if( eventCount == 20 )269{270//got enough events, so pass271272OpensWithNoGrab.pass();273}274else if( tries == 20 )275{276//tried too many times without getting enough events so fail277278OpensWithNoGrab.fail();279}280281}// eventDispatched()282283}// NewClass class284285*/286287288//************** End classes defined for the test *******************289290291292293/****************************************************294Standard Test Machinery295DO NOT modify anything below -- it's a standard296chunk of code whose purpose is to make user297interaction uniform, and thereby make it simpler298to read and understand someone else's test.299****************************************************/300301/**302This is part of the standard test machinery.303It creates a dialog (with the instructions), and is the interface304for sending text messages to the user.305To print the instructions, send an array of strings to Sysout.createDialog306WithInstructions method. Put one line of instructions per array entry.307To display a message for the tester to see, simply call Sysout.println308with the string to be displayed.309This mimics System.out.println but works within the test harness as well310as standalone.311*/312313class Sysout314{315private static TestDialog dialog;316317public static void createDialogWithInstructions( String[] instructions )318{319dialog = new TestDialog( new Frame(), "Instructions" );320dialog.printInstructions( instructions );321dialog.setVisible(true);322println( "Any messages for the tester will display here." );323}324325public static void createDialog( )326{327dialog = new TestDialog( new Frame(), "Instructions" );328String[] defInstr = { "Instructions will appear here. ", "" } ;329dialog.printInstructions( defInstr );330dialog.setVisible(true);331println( "Any messages for the tester will display here." );332}333334335public static void printInstructions( String[] instructions )336{337dialog.printInstructions( instructions );338}339340341public static void println( String messageIn )342{343dialog.displayMessage( messageIn );344System.out.println(messageIn);345}346347}// Sysout class348349/**350This is part of the standard test machinery. It provides a place for the351test instructions to be displayed, and a place for interactive messages352to the user to be displayed.353To have the test instructions displayed, see Sysout.354To have a message to the user be displayed, see Sysout.355Do not call anything in this dialog directly.356*/357class TestDialog extends Dialog358{359360TextArea instructionsText;361TextArea messageText;362int maxStringLength = 80;363364//DO NOT call this directly, go through Sysout365public TestDialog( Frame frame, String name )366{367super( frame, name );368int scrollBoth = TextArea.SCROLLBARS_BOTH;369instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );370add( "North", instructionsText );371372messageText = new TextArea( "", 5, maxStringLength, scrollBoth );373add("Center", messageText);374375pack();376377setVisible(true);378}// TestDialog()379380//DO NOT call this directly, go through Sysout381public void printInstructions( String[] instructions )382{383//Clear out any current instructions384instructionsText.setText( "" );385386//Go down array of instruction strings387388String printStr, remainingStr;389for( int i=0; i < instructions.length; i++ )390{391//chop up each into pieces maxSringLength long392remainingStr = instructions[ i ];393while( remainingStr.length() > 0 )394{395//if longer than max then chop off first max chars to print396if( remainingStr.length() >= maxStringLength )397{398//Try to chop on a word boundary399int posOfSpace = remainingStr.400lastIndexOf( ' ', maxStringLength - 1 );401402if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;403404printStr = remainingStr.substring( 0, posOfSpace + 1 );405remainingStr = remainingStr.substring( posOfSpace + 1 );406}407//else just print408else409{410printStr = remainingStr;411remainingStr = "";412}413414instructionsText.append( printStr + "\n" );415416}// while417418}// for419420}//printInstructions()421422//DO NOT call this directly, go through Sysout423public void displayMessage( String messageIn )424{425messageText.append( messageIn + "\n" );426System.out.println(messageIn);427}428429}// TestDialog class430431432