Path: blob/master/test/jdk/java/awt/Choice/DragMouseOutAndRelease/DragMouseOutAndRelease.java
41152 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*/22/*23@test24@key headful25@bug 632262526@summary REG:Choice does not trigger MouseReleased when dragging and releasing the mouse outside choice, XAWT27@author andrei.dmitriev area=awt.choice28@run main DragMouseOutAndRelease29*/3031import java.awt.*;32import java.awt.event.*;3334public class DragMouseOutAndRelease35{36static Frame frame = new Frame("Test Frame");37static Choice choice1 = new Choice();38static Robot robot;39static Point pt;40static volatile boolean mousePressed = false;41static volatile boolean mouseReleased = false;4243private static void init()44{45frame.setLayout (new FlowLayout ());46for (int i = 1; i<10;i++){47choice1.add("item "+i);48}49frame.add(choice1);5051choice1.addMouseListener(new MouseAdapter() {52public void mousePressed(MouseEvent me) {53mousePressed = true;54System.out.println(me);55}56public void mouseReleased(MouseEvent me) {57mouseReleased = true;58System.out.println(me);59}60});6162frame.pack();63frame.setVisible(true);64frame.validate();6566try {67robot = new Robot();68robot.setAutoDelay(50);69robot.waitForIdle();70testMouseDrag();71} catch (Throwable e) {72new RuntimeException("Test failed. Exception thrown: "+e);73}74DragMouseOutAndRelease.pass();75}//End init()7677public static void testMouseDrag(){78mousePressed = false;79mouseReleased = false;8081pt = choice1.getLocationOnScreen();82robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2);83robot.waitForIdle();84robot.mousePress(InputEvent.BUTTON1_MASK);85robot.waitForIdle();868788//move mouse outside Choice89robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y - choice1.getHeight());90robot.waitForIdle();91robot.mouseRelease(InputEvent.BUTTON1_MASK);92robot.waitForIdle();9394if (!mousePressed || !mouseReleased)95{96System.out.println("ERROR: "+ mousePressed+","+mouseReleased);97// close the choice98robot.keyPress(KeyEvent.VK_ESCAPE);99robot.keyRelease(KeyEvent.VK_ESCAPE);100robot.waitForIdle();101DragMouseOutAndRelease.fail("Test failed. Choice should generate PRESSED, RELEASED events outside if pressed on Choice ");102} else{103// close the choice104robot.keyPress(KeyEvent.VK_ESCAPE);105robot.keyRelease(KeyEvent.VK_ESCAPE);106robot.waitForIdle();107System.out.println("Choice did generated PRESSED and RELEASED after Drag outside the Choice ");108}109}110111112113/*****************************************************114* Standard Test Machinery Section115* DO NOT modify anything in this section -- it's a116* standard chunk of code which has all of the117* synchronisation necessary for the test harness.118* By keeping it the same in all tests, it is easier119* to read and understand someone else's test, as120* well as insuring that all tests behave correctly121* with the test harness.122* There is a section following this for test-123* classes124******************************************************/125private static boolean theTestPassed = false;126private static boolean testGeneratedInterrupt = false;127private static String failureMessage = "";128129private static Thread mainThread = null;130131private static int sleepTime = 300000;132133// Not sure about what happens if multiple of this test are134// instantiated in the same VM. Being static (and using135// static vars), it aint gonna work. Not worrying about136// it for now.137public static void main( String args[] ) throws InterruptedException138{139mainThread = Thread.currentThread();140try141{142init();143}144catch( TestPassedException e )145{146//The test passed, so just return from main and harness will147// interepret this return as a pass148return;149}150//At this point, neither test pass nor test fail has been151// called -- either would have thrown an exception and ended the152// test, so we know we have multiple threads.153154//Test involves other threads, so sleep and wait for them to155// called pass() or fail()156try157{158Thread.sleep( sleepTime );159//Timed out, so fail the test160throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );161}162catch (InterruptedException e)163{164//The test harness may have interrupted the test. If so, rethrow the exception165// so that the harness gets it and deals with it.166if( ! testGeneratedInterrupt ) throw e;167168//reset flag in case hit this code more than once for some reason (just safety)169testGeneratedInterrupt = false;170171if ( theTestPassed == false )172{173throw new RuntimeException( failureMessage );174}175}176177}//main178179public static synchronized void setTimeoutTo( int seconds )180{181sleepTime = seconds * 1000;182}183184public static synchronized void pass()185{186System.out.println( "The test passed." );187System.out.println( "The test is over, hit Ctl-C to stop Java VM" );188//first check if this is executing in main thread189if ( mainThread == Thread.currentThread() )190{191//Still in the main thread, so set the flag just for kicks,192// and throw a test passed exception which will be caught193// and end the test.194theTestPassed = true;195throw new TestPassedException();196}197theTestPassed = true;198testGeneratedInterrupt = true;199mainThread.interrupt();200}//pass()201202public static synchronized void fail()203{204//test writer didn't specify why test failed, so give generic205fail( "it just plain failed! :-)" );206}207208public static synchronized void fail( String whyFailed )209{210System.out.println( "The test failed: " + whyFailed );211System.out.println( "The test is over, hit Ctl-C to stop Java VM" );212//check if this called from main thread213if ( mainThread == Thread.currentThread() )214{215//If main thread, fail now 'cause not sleeping216throw new RuntimeException( whyFailed );217}218theTestPassed = false;219testGeneratedInterrupt = true;220failureMessage = whyFailed;221mainThread.interrupt();222}//fail()223224}// class DragMouseOutAndRelease225226//This exception is used to exit from any level of call nesting227// when it's determined that the test has passed, and immediately228// end the test.229class TestPassedException extends RuntimeException230{231}232233234