Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Choice/DragMouseOutAndRelease/DragMouseOutAndRelease.java
47490 views
/*1* Copyright (c) 2006, 2014, 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*/22/*23@test24@bug 632262525@summary REG:Choice does not trigger MouseReleased when dragging and releasing the mouse outside choice, XAWT26@author andrei.dmitriev area=awt.choice27@run main DragMouseOutAndRelease28*/2930import java.awt.*;31import java.awt.event.*;3233public class DragMouseOutAndRelease34{35static Frame frame = new Frame("Test Frame");36static Choice choice1 = new Choice();37static Robot robot;38static Point pt;39static volatile boolean mousePressed = false;40static volatile boolean mouseReleased = false;4142private 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 );5253frame.setLayout (new FlowLayout ());54for (int i = 1; i<10;i++){55choice1.add("item "+i);56}57frame.add(choice1);5859choice1.addMouseListener(new MouseAdapter() {60public void mousePressed(MouseEvent me) {61mousePressed = true;62System.out.println(me);63}64public void mouseReleased(MouseEvent me) {65mouseReleased = true;66System.out.println(me);67}68});6970frame.pack();71frame.setVisible(true);72frame.validate();7374try {75robot = new Robot();76robot.setAutoDelay(50);77robot.waitForIdle();78testMouseDrag();79} catch (Throwable e) {80new RuntimeException("Test failed. Exception thrown: "+e);81}82DragMouseOutAndRelease.pass();83}//End init()8485public static void testMouseDrag(){86mousePressed = false;87mouseReleased = false;8889pt = choice1.getLocationOnScreen();90robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2);91robot.waitForIdle();92robot.mousePress(InputEvent.BUTTON1_MASK);93robot.waitForIdle();949596//move mouse outside Choice97robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y - choice1.getHeight());98robot.waitForIdle();99robot.mouseRelease(InputEvent.BUTTON1_MASK);100robot.waitForIdle();101102if (!mousePressed || !mouseReleased)103{104System.out.println("ERROR: "+ mousePressed+","+mouseReleased);105// close the choice106robot.keyPress(KeyEvent.VK_ESCAPE);107robot.keyRelease(KeyEvent.VK_ESCAPE);108robot.waitForIdle();109DragMouseOutAndRelease.fail("Test failed. Choice should generate PRESSED, RELEASED events outside if pressed on Choice ");110} else{111// close the choice112robot.keyPress(KeyEvent.VK_ESCAPE);113robot.keyRelease(KeyEvent.VK_ESCAPE);114robot.waitForIdle();115System.out.println("Choice did generated PRESSED and RELEASED after Drag outside the Choice ");116}117}118119120121/*****************************************************122* Standard Test Machinery Section123* DO NOT modify anything in this section -- it's a124* standard chunk of code which has all of the125* synchronisation necessary for the test harness.126* By keeping it the same in all tests, it is easier127* to read and understand someone else's test, as128* well as insuring that all tests behave correctly129* with the test harness.130* There is a section following this for test-131* classes132******************************************************/133private static boolean theTestPassed = false;134private static boolean testGeneratedInterrupt = false;135private static String failureMessage = "";136137private static Thread mainThread = null;138139private static int sleepTime = 300000;140141// Not sure about what happens if multiple of this test are142// instantiated in the same VM. Being static (and using143// static vars), it aint gonna work. Not worrying about144// it for now.145public static void main( String args[] ) throws InterruptedException146{147mainThread = Thread.currentThread();148try149{150init();151}152catch( TestPassedException e )153{154//The test passed, so just return from main and harness will155// interepret this return as a pass156return;157}158//At this point, neither test pass nor test fail has been159// called -- either would have thrown an exception and ended the160// test, so we know we have multiple threads.161162//Test involves other threads, so sleep and wait for them to163// called pass() or fail()164try165{166Thread.sleep( sleepTime );167//Timed out, so fail the test168throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );169}170catch (InterruptedException e)171{172//The test harness may have interrupted the test. If so, rethrow the exception173// so that the harness gets it and deals with it.174if( ! testGeneratedInterrupt ) throw e;175176//reset flag in case hit this code more than once for some reason (just safety)177testGeneratedInterrupt = false;178179if ( theTestPassed == false )180{181throw new RuntimeException( failureMessage );182}183}184185}//main186187public static synchronized void setTimeoutTo( int seconds )188{189sleepTime = seconds * 1000;190}191192public static synchronized void pass()193{194Sysout.println( "The test passed." );195Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );196//first check if this is executing in main thread197if ( mainThread == Thread.currentThread() )198{199//Still in the main thread, so set the flag just for kicks,200// and throw a test passed exception which will be caught201// and end the test.202theTestPassed = true;203throw new TestPassedException();204}205theTestPassed = true;206testGeneratedInterrupt = true;207mainThread.interrupt();208}//pass()209210public static synchronized void fail()211{212//test writer didn't specify why test failed, so give generic213fail( "it just plain failed! :-)" );214}215216public static synchronized void fail( String whyFailed )217{218Sysout.println( "The test failed: " + whyFailed );219Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );220//check if this called from main thread221if ( mainThread == Thread.currentThread() )222{223//If main thread, fail now 'cause not sleeping224throw new RuntimeException( whyFailed );225}226theTestPassed = false;227testGeneratedInterrupt = true;228failureMessage = whyFailed;229mainThread.interrupt();230}//fail()231232}// class DragMouseOutAndRelease233234//This exception is used to exit from any level of call nesting235// when it's determined that the test has passed, and immediately236// end the test.237class TestPassedException extends RuntimeException238{239}240241//*********** End Standard Test Machinery Section **********242243244//************ Begin classes defined for the test ****************245246// if want to make listeners, here is the recommended place for them, then instantiate247// them in init()248249/* Example of a class which may be written as part of a test250class NewClass implements anInterface251{252static int newVar = 0;253254public void eventDispatched(AWTEvent e)255{256//Counting events to see if we get enough257eventCount++;258259if( eventCount == 20 )260{261//got enough events, so pass262263DragMouseOutAndRelease.pass();264}265else if( tries == 20 )266{267//tried too many times without getting enough events so fail268269DragMouseOutAndRelease.fail();270}271272}// eventDispatched()273274}// NewClass class275276*/277278279//************** End classes defined for the test *******************280281282283284/****************************************************285Standard Test Machinery286DO NOT modify anything below -- it's a standard287chunk of code whose purpose is to make user288interaction uniform, and thereby make it simpler289to read and understand someone else's test.290****************************************************/291292/**293This is part of the standard test machinery.294It creates a dialog (with the instructions), and is the interface295for sending text messages to the user.296To print the instructions, send an array of strings to Sysout.createDialog297WithInstructions method. Put one line of instructions per array entry.298To display a message for the tester to see, simply call Sysout.println299with the string to be displayed.300This mimics System.out.println but works within the test harness as well301as standalone.302*/303304class Sysout305{306private static TestDialog dialog;307308public static void createDialogWithInstructions( String[] instructions )309{310dialog = new TestDialog( new Frame(), "Instructions" );311dialog.printInstructions( instructions );312dialog.setVisible(true);313println( "Any messages for the tester will display here." );314}315316public static void createDialog( )317{318dialog = new TestDialog( new Frame(), "Instructions" );319String[] defInstr = { "Instructions will appear here. ", "" } ;320dialog.printInstructions( defInstr );321dialog.setVisible(true);322println( "Any messages for the tester will display here." );323}324325326public static void printInstructions( String[] instructions )327{328dialog.printInstructions( instructions );329}330331332public static void println( String messageIn )333{334dialog.displayMessage( messageIn );335System.out.println(messageIn);336}337338}// Sysout class339340/**341This is part of the standard test machinery. It provides a place for the342test instructions to be displayed, and a place for interactive messages343to the user to be displayed.344To have the test instructions displayed, see Sysout.345To have a message to the user be displayed, see Sysout.346Do not call anything in this dialog directly.347*/348class TestDialog extends Dialog349{350351TextArea instructionsText;352TextArea messageText;353int maxStringLength = 80;354355//DO NOT call this directly, go through Sysout356public TestDialog( Frame frame, String name )357{358super( frame, name );359int scrollBoth = TextArea.SCROLLBARS_BOTH;360instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );361add( "North", instructionsText );362363messageText = new TextArea( "", 5, maxStringLength, scrollBoth );364add("Center", messageText);365366pack();367368setVisible(true);369}// TestDialog()370371//DO NOT call this directly, go through Sysout372public void printInstructions( String[] instructions )373{374//Clear out any current instructions375instructionsText.setText( "" );376377//Go down array of instruction strings378379String printStr, remainingStr;380for( int i=0; i < instructions.length; i++ )381{382//chop up each into pieces maxSringLength long383remainingStr = instructions[ i ];384while( remainingStr.length() > 0 )385{386//if longer than max then chop off first max chars to print387if( remainingStr.length() >= maxStringLength )388{389//Try to chop on a word boundary390int posOfSpace = remainingStr.391lastIndexOf( ' ', maxStringLength - 1 );392393if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;394395printStr = remainingStr.substring( 0, posOfSpace + 1 );396remainingStr = remainingStr.substring( posOfSpace + 1 );397}398//else just print399else400{401printStr = remainingStr;402remainingStr = "";403}404405instructionsText.append( printStr + "\n" );406407}// while408409}// for410411}//printInstructions()412413//DO NOT call this directly, go through Sysout414public void displayMessage( String messageIn )415{416messageText.append( messageIn + "\n" );417System.out.println(messageIn);418}419420}// TestDialog class421422423