Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/MouseAdapter/MouseAdapterUnitTest/MouseAdapterUnitTest.java
47791 views
/*1* Copyright (c) 2006, 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 445316226@summary MouseAdapter should implement MouseMotionListener and MouseWheelListener27@author andrei.dmitriev: area=28@library ../../regtesthelpers29@build Util30@run main MouseAdapterUnitTest31*/3233import java.awt.*;34import java.awt.event.*;35import test.java.awt.regtesthelpers.Util;3637public class MouseAdapterUnitTest38{39static Point pt;40static Frame frame = new Frame("Test Frame");41static Button b = new Button("Test Button");42static Robot robot;43static boolean clicked = false;44static boolean pressed = false;45static boolean released = false;46static boolean entered = false;47static boolean exited = false;48static boolean rotated = false;49static boolean dragged = false;50static boolean moved = false;5152private static void init()53{54String[] instructions =55{56"This is an AUTOMATIC test, simply wait until it is done.",57"The result (passed or failed) will be shown in the",58"message window below."59};60Sysout.createDialog( );61Sysout.printInstructions( instructions );6263MouseAdapter ma = new MouseAdapter(){64public void mouseClicked(MouseEvent e) {clicked = true;}6566public void mousePressed(MouseEvent e) { pressed = true;}6768public void mouseReleased(MouseEvent e) {released = true;}6970public void mouseEntered(MouseEvent e) { entered = true;}7172public void mouseExited(MouseEvent e) {exited = true;}7374public void mouseWheelMoved(MouseWheelEvent e){rotated = true;}7576public void mouseDragged(MouseEvent e){dragged = true;}7778public void mouseMoved(MouseEvent e){moved = true;}7980};8182b.addMouseListener(ma);83b.addMouseWheelListener(ma);84b.addMouseMotionListener(ma);8586frame.add(b);87frame.pack();88frame.setVisible(true);8990try{91robot = new Robot();92robot.setAutoWaitForIdle(true);93robot.setAutoDelay(50);9495Util.waitForIdle(robot);9697pt = b.getLocationOnScreen();98testPressMouseButton(InputEvent.BUTTON1_MASK);99testDragMouseButton(InputEvent.BUTTON1_MASK);100testMoveMouseButton();101testCrossingMouseButton();102testWheelMouseButton();103} catch (Throwable e) {104throw new RuntimeException("Test failed. Exception thrown: "+e);105}106107MouseAdapterUnitTest.pass();108109}//End init()110111public static void testPressMouseButton(int button){112robot.mouseMove(pt.x + b.getWidth()/2, pt.y + b.getHeight()/2);113robot.delay(100);114robot.mousePress(button);115robot.mouseRelease(button);116robot.delay(300);117118119if ( !pressed || !released || !clicked ){120dumpListenerState();121fail("press, release or click hasn't come");122}123}124125public static void testWheelMouseButton(){126robot.mouseMove(pt.x + b.getWidth()/2, pt.y + b.getHeight()/2);127robot.mouseWheel(10);128if ( !rotated){129dumpListenerState();130fail("Wheel event hasn't come");131}132}133134public static void testDragMouseButton(int button) {135robot.mouseMove(pt.x + b.getWidth()/2, pt.y + b.getHeight()/2);136robot.mousePress(button);137moveMouse(pt.x + b.getWidth()/2, pt.y +138b.getHeight()/2,139pt.x + b.getWidth()/2,140pt.y + 2 * b.getHeight());141robot.mouseRelease(button);142143if ( !dragged){144dumpListenerState();145fail("dragged hasn't come");146}147148}149150public static void testMoveMouseButton() {151moveMouse(pt.x + b.getWidth()/2, pt.y +152b.getHeight()/2,153pt.x + b.getWidth()/2,154pt.y + 2 * b.getHeight());155156if ( !moved){157dumpListenerState();158fail("dragged hasn't come");159}160161}162163public static void moveMouse(int x0, int y0, int x1, int y1){164int curX = x0;165int curY = y0;166int dx = x0 < x1 ? 1 : -1;167int dy = y0 < y1 ? 1 : -1;168169while (curX != x1){170curX += dx;171robot.mouseMove(curX, curY);172}173while (curY != y1 ){174curY += dy;175robot.mouseMove(curX, curY);176}177}178179public static void testCrossingMouseButton() {180//exit181moveMouse(pt.x + b.getWidth()/2,182pt.y + b.getHeight()/2,183pt.x + b.getWidth()/2,184pt.y + 2 * b.getHeight());185//enter186moveMouse(pt.x + b.getWidth()/2,187pt.y + 2 * b.getHeight()/2,188pt.x + b.getWidth()/2,189pt.y + b.getHeight());190191if ( !entered || !exited){192dumpListenerState();193fail("enter or exit hasn't come");194}195196}197198public static void dumpListenerState(){199System.out.println("pressed = "+pressed);200System.out.println("released = "+released);201System.out.println("clicked = "+clicked);202System.out.println("entered = "+exited);203System.out.println("rotated = "+rotated);204System.out.println("dragged = "+dragged);205System.out.println("moved = "+moved);206}207208/*****************************************************209* Standard Test Machinery Section210* DO NOT modify anything in this section -- it's a211* standard chunk of code which has all of the212* synchronisation necessary for the test harness.213* By keeping it the same in all tests, it is easier214* to read and understand someone else's test, as215* well as insuring that all tests behave correctly216* with the test harness.217* There is a section following this for test-218* classes219******************************************************/220private static boolean theTestPassed = false;221private static boolean testGeneratedInterrupt = false;222private static String failureMessage = "";223224private static Thread mainThread = null;225226private static int sleepTime = 300000;227228// Not sure about what happens if multiple of this test are229// instantiated in the same VM. Being static (and using230// static vars), it aint gonna work. Not worrying about231// it for now.232public static void main( String args[] ) throws InterruptedException233{234mainThread = Thread.currentThread();235try236{237init();238}239catch( TestPassedException e )240{241//The test passed, so just return from main and harness will242// interepret this return as a pass243return;244}245//At this point, neither test pass nor test fail has been246// called -- either would have thrown an exception and ended the247// test, so we know we have multiple threads.248249//Test involves other threads, so sleep and wait for them to250// called pass() or fail()251try252{253Thread.sleep( sleepTime );254//Timed out, so fail the test255throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );256}257catch (InterruptedException e)258{259//The test harness may have interrupted the test. If so, rethrow the exception260// so that the harness gets it and deals with it.261if( ! testGeneratedInterrupt ) throw e;262263//reset flag in case hit this code more than once for some reason (just safety)264testGeneratedInterrupt = false;265266if ( theTestPassed == false )267{268throw new RuntimeException( failureMessage );269}270}271272}//main273274public static synchronized void setTimeoutTo( int seconds )275{276sleepTime = seconds * 1000;277}278279public static synchronized void pass()280{281Sysout.println( "The test passed." );282Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );283//first check if this is executing in main thread284if ( mainThread == Thread.currentThread() )285{286//Still in the main thread, so set the flag just for kicks,287// and throw a test passed exception which will be caught288// and end the test.289theTestPassed = true;290throw new TestPassedException();291}292theTestPassed = true;293testGeneratedInterrupt = true;294mainThread.interrupt();295}//pass()296297public static synchronized void fail()298{299//test writer didn't specify why test failed, so give generic300fail( "it just plain failed! :-)" );301}302303public static synchronized void fail( String whyFailed )304{305Sysout.println( "The test failed: " + whyFailed );306Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );307//check if this called from main thread308if ( mainThread == Thread.currentThread() )309{310//If main thread, fail now 'cause not sleeping311throw new RuntimeException( whyFailed );312}313theTestPassed = false;314testGeneratedInterrupt = true;315failureMessage = whyFailed;316mainThread.interrupt();317}//fail()318319}// class MouseAdapterUnitTest320321//This exception is used to exit from any level of call nesting322// when it's determined that the test has passed, and immediately323// end the test.324class TestPassedException extends RuntimeException325{326}327328//*********** End Standard Test Machinery Section **********329330331//************ Begin classes defined for the test ****************332333// if want to make listeners, here is the recommended place for them, then instantiate334// them in init()335336/* Example of a class which may be written as part of a test337class NewClass implements anInterface338{339static int newVar = 0;340341public void eventDispatched(AWTEvent e)342{343//Counting events to see if we get enough344eventCount++;345346if( eventCount == 20 )347{348//got enough events, so pass349350MouseAdapterUnitTest.pass();351}352else if( tries == 20 )353{354//tried too many times without getting enough events so fail355356MouseAdapterUnitTest.fail();357}358359}// eventDispatched()360361}// NewClass class362363*/364365366//************** End classes defined for the test *******************367368369370371/****************************************************372Standard Test Machinery373DO NOT modify anything below -- it's a standard374chunk of code whose purpose is to make user375interaction uniform, and thereby make it simpler376to read and understand someone else's test.377****************************************************/378379/**380This is part of the standard test machinery.381It creates a dialog (with the instructions), and is the interface382for sending text messages to the user.383To print the instructions, send an array of strings to Sysout.createDialog384WithInstructions method. Put one line of instructions per array entry.385To display a message for the tester to see, simply call Sysout.println386with the string to be displayed.387This mimics System.out.println but works within the test harness as well388as standalone.389*/390391class Sysout392{393private static TestDialog dialog;394395public static void createDialogWithInstructions( String[] instructions )396{397dialog = new TestDialog( new Frame(), "Instructions" );398dialog.printInstructions( instructions );399dialog.setVisible(true);400println( "Any messages for the tester will display here." );401}402403public static void createDialog( )404{405dialog = new TestDialog( new Frame(), "Instructions" );406String[] defInstr = { "Instructions will appear here. ", "" } ;407dialog.printInstructions( defInstr );408dialog.setVisible(true);409println( "Any messages for the tester will display here." );410}411412413public static void printInstructions( String[] instructions )414{415dialog.printInstructions( instructions );416}417418419public static void println( String messageIn )420{421dialog.displayMessage( messageIn );422System.out.println(messageIn);423}424425}// Sysout class426427/**428This is part of the standard test machinery. It provides a place for the429test instructions to be displayed, and a place for interactive messages430to the user to be displayed.431To have the test instructions displayed, see Sysout.432To have a message to the user be displayed, see Sysout.433Do not call anything in this dialog directly.434*/435class TestDialog extends Dialog436{437438TextArea instructionsText;439TextArea messageText;440int maxStringLength = 80;441442//DO NOT call this directly, go through Sysout443public TestDialog( Frame frame, String name )444{445super( frame, name );446int scrollBoth = TextArea.SCROLLBARS_BOTH;447instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );448add( "North", instructionsText );449450messageText = new TextArea( "", 5, maxStringLength, scrollBoth );451add("Center", messageText);452453pack();454455setVisible(true);456}// TestDialog()457458//DO NOT call this directly, go through Sysout459public void printInstructions( String[] instructions )460{461//Clear out any current instructions462instructionsText.setText( "" );463464//Go down array of instruction strings465466String printStr, remainingStr;467for( int i=0; i < instructions.length; i++ )468{469//chop up each into pieces maxSringLength long470remainingStr = instructions[ i ];471while( remainingStr.length() > 0 )472{473//if longer than max then chop off first max chars to print474if( remainingStr.length() >= maxStringLength )475{476//Try to chop on a word boundary477int posOfSpace = remainingStr.478lastIndexOf( ' ', maxStringLength - 1 );479480if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;481482printStr = remainingStr.substring( 0, posOfSpace + 1 );483remainingStr = remainingStr.substring( posOfSpace + 1 );484}485//else just print486else487{488printStr = remainingStr;489remainingStr = "";490}491492instructionsText.append( printStr + "\n" );493494}// while495496}// for497498}//printInstructions()499500//DO NOT call this directly, go through Sysout501public void displayMessage( String messageIn )502{503messageText.append( messageIn + "\n" );504System.out.println(messageIn);505}506507}// TestDialog class508509510