Path: blob/master/test/jdk/java/awt/MouseAdapter/MouseAdapterUnitTest/MouseAdapterUnitTest.java
41153 views
/*1* Copyright (c) 2006, 2018, 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@key headful26@bug 445316227@summary MouseAdapter should implement MouseMotionListener and MouseWheelListener28@author andrei.dmitriev: area=29@library ../../regtesthelpers30@build Util31@run main MouseAdapterUnitTest32*/3334import java.awt.*;35import java.awt.event.*;36import test.java.awt.regtesthelpers.Util;3738public class MouseAdapterUnitTest39{40static Point pt;41static Frame frame = new Frame("Test Frame");42static Button b = new Button("Test Button");43static Robot robot;44static boolean clicked = false;45static boolean pressed = false;46static boolean released = false;47static boolean entered = false;48static boolean exited = false;49static boolean rotated = false;50static boolean dragged = false;51static boolean moved = false;5253private static void init()54{55MouseAdapter ma = new MouseAdapter(){56public void mouseClicked(MouseEvent e) {clicked = true;}5758public void mousePressed(MouseEvent e) { pressed = true;}5960public void mouseReleased(MouseEvent e) {released = true;}6162public void mouseEntered(MouseEvent e) { entered = true;}6364public void mouseExited(MouseEvent e) {exited = true;}6566public void mouseWheelMoved(MouseWheelEvent e){rotated = true;}6768public void mouseDragged(MouseEvent e){dragged = true;}6970public void mouseMoved(MouseEvent e){moved = true;}7172};7374b.addMouseListener(ma);75b.addMouseWheelListener(ma);76b.addMouseMotionListener(ma);7778frame.add(b);79frame.pack();80frame.setVisible(true);8182try{83robot = new Robot();84robot.setAutoWaitForIdle(true);85robot.setAutoDelay(50);8687Util.waitForIdle(robot);8889pt = b.getLocationOnScreen();90testPressMouseButton(InputEvent.BUTTON1_MASK);91testDragMouseButton(InputEvent.BUTTON1_MASK);92testMoveMouseButton();93testCrossingMouseButton();94testWheelMouseButton();95} catch (Throwable e) {96throw new RuntimeException("Test failed. Exception thrown: "+e);97}9899MouseAdapterUnitTest.pass();100101}//End init()102103public static void testPressMouseButton(int button){104robot.mouseMove(pt.x + b.getWidth()/2, pt.y + b.getHeight()/2);105robot.delay(100);106robot.mousePress(button);107robot.mouseRelease(button);108robot.delay(300);109110111if ( !pressed || !released || !clicked ){112dumpListenerState();113fail("press, release or click hasn't come");114}115}116117public static void testWheelMouseButton(){118robot.mouseMove(pt.x + b.getWidth()/2, pt.y + b.getHeight()/2);119robot.mouseWheel(10);120if ( !rotated){121dumpListenerState();122fail("Wheel event hasn't come");123}124}125126public static void testDragMouseButton(int button) {127robot.mouseMove(pt.x + b.getWidth()/2, pt.y + b.getHeight()/2);128robot.mousePress(button);129moveMouse(pt.x + b.getWidth()/2, pt.y +130b.getHeight()/2,131pt.x + b.getWidth()/2,132pt.y + 2 * b.getHeight());133robot.mouseRelease(button);134135if ( !dragged){136dumpListenerState();137fail("dragged hasn't come");138}139140}141142public static void testMoveMouseButton() {143moveMouse(pt.x + b.getWidth()/2, pt.y +144b.getHeight()/2,145pt.x + b.getWidth()/2,146pt.y + 2 * b.getHeight());147148if ( !moved){149dumpListenerState();150fail("dragged hasn't come");151}152153}154155public static void moveMouse(int x0, int y0, int x1, int y1){156int curX = x0;157int curY = y0;158int dx = x0 < x1 ? 1 : -1;159int dy = y0 < y1 ? 1 : -1;160161while (curX != x1){162curX += dx;163robot.mouseMove(curX, curY);164}165while (curY != y1 ){166curY += dy;167robot.mouseMove(curX, curY);168}169}170171public static void testCrossingMouseButton() {172//exit173moveMouse(pt.x + b.getWidth()/2,174pt.y + b.getHeight()/2,175pt.x + b.getWidth()/2,176pt.y + 2 * b.getHeight());177//enter178moveMouse(pt.x + b.getWidth()/2,179pt.y + 2 * b.getHeight()/2,180pt.x + b.getWidth()/2,181pt.y + b.getHeight());182183if ( !entered || !exited){184dumpListenerState();185fail("enter or exit hasn't come");186}187188}189190public static void dumpListenerState(){191System.out.println("pressed = "+pressed);192System.out.println("released = "+released);193System.out.println("clicked = "+clicked);194System.out.println("entered = "+exited);195System.out.println("rotated = "+rotated);196System.out.println("dragged = "+dragged);197System.out.println("moved = "+moved);198}199200/*****************************************************201* Standard Test Machinery Section202* DO NOT modify anything in this section -- it's a203* standard chunk of code which has all of the204* synchronisation necessary for the test harness.205* By keeping it the same in all tests, it is easier206* to read and understand someone else's test, as207* well as insuring that all tests behave correctly208* with the test harness.209* There is a section following this for test-210* classes211******************************************************/212private static boolean theTestPassed = false;213private static boolean testGeneratedInterrupt = false;214private static String failureMessage = "";215216private static Thread mainThread = null;217218private static int sleepTime = 300000;219220// Not sure about what happens if multiple of this test are221// instantiated in the same VM. Being static (and using222// static vars), it aint gonna work. Not worrying about223// it for now.224public static void main( String args[] ) throws InterruptedException225{226mainThread = Thread.currentThread();227try228{229init();230}231catch( TestPassedException e )232{233//The test passed, so just return from main and harness will234// interepret this return as a pass235return;236}237//At this point, neither test pass nor test fail has been238// called -- either would have thrown an exception and ended the239// test, so we know we have multiple threads.240241//Test involves other threads, so sleep and wait for them to242// called pass() or fail()243try244{245Thread.sleep( sleepTime );246//Timed out, so fail the test247throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );248}249catch (InterruptedException e)250{251//The test harness may have interrupted the test. If so, rethrow the exception252// so that the harness gets it and deals with it.253if( ! testGeneratedInterrupt ) throw e;254255//reset flag in case hit this code more than once for some reason (just safety)256testGeneratedInterrupt = false;257258if ( theTestPassed == false )259{260throw new RuntimeException( failureMessage );261}262}263264}//main265266public static synchronized void setTimeoutTo( int seconds )267{268sleepTime = seconds * 1000;269}270271public static synchronized void pass()272{273System.out.println( "The test passed." );274System.out.println( "The test is over, hit Ctl-C to stop Java VM" );275//first check if this is executing in main thread276if ( mainThread == Thread.currentThread() )277{278//Still in the main thread, so set the flag just for kicks,279// and throw a test passed exception which will be caught280// and end the test.281theTestPassed = true;282throw new TestPassedException();283}284theTestPassed = true;285testGeneratedInterrupt = true;286mainThread.interrupt();287}//pass()288289public static synchronized void fail()290{291//test writer didn't specify why test failed, so give generic292fail( "it just plain failed! :-)" );293}294295public static synchronized void fail( String whyFailed )296{297System.out.println( "The test failed: " + whyFailed );298System.out.println( "The test is over, hit Ctl-C to stop Java VM" );299//check if this called from main thread300if ( mainThread == Thread.currentThread() )301{302//If main thread, fail now 'cause not sleeping303throw new RuntimeException( whyFailed );304}305theTestPassed = false;306testGeneratedInterrupt = true;307failureMessage = whyFailed;308mainThread.interrupt();309}//fail()310311}// class MouseAdapterUnitTest312313//This exception is used to exit from any level of call nesting314// when it's determined that the test has passed, and immediately315// end the test.316class TestPassedException extends RuntimeException317{318}319320321