1 /* 2 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* 24 @test 25 @key headful 26 @bug 6322625 27 @summary REG:Choice does not trigger MouseReleased when dragging and releasing the mouse outside choice, XAWT 28 @author andrei.dmitriev area=awt.choice 29 @run main DragMouseOutAndRelease 30 */ 31 32 import java.awt.*; 33 import java.awt.event.*; 34 35 public class DragMouseOutAndRelease 36 { 37 static Frame frame = new Frame("Test Frame"); 38 static Choice choice1 = new Choice(); 39 static Robot robot; 40 static Point pt; 41 static volatile boolean mousePressed = false; 42 static volatile boolean mouseReleased = false; 43 44 private static void init() 45 { 46 String[] instructions = 47 { 48 "This is an AUTOMATIC test, simply wait until it is done.", 49 "The result (passed or failed) will be shown in the", 50 "message window below." 51 }; 52 Sysout.createDialog( ); 53 Sysout.printInstructions( instructions ); 54 55 frame.setLayout (new FlowLayout ()); 56 for (int i = 1; i<10;i++){ 57 choice1.add("item "+i); 58 } 59 frame.add(choice1); 60 61 choice1.addMouseListener(new MouseAdapter() { 62 public void mousePressed(MouseEvent me) { 63 mousePressed = true; 64 System.out.println(me); 65 } 66 public void mouseReleased(MouseEvent me) { 67 mouseReleased = true; 68 System.out.println(me); 69 } 70 }); 71 72 frame.pack(); 73 frame.setVisible(true); 74 frame.validate(); 75 76 try { 77 robot = new Robot(); 78 robot.setAutoDelay(50); 79 robot.waitForIdle(); 80 testMouseDrag(); 81 } catch (Throwable e) { 82 new RuntimeException("Test failed. Exception thrown: "+e); 83 } 84 DragMouseOutAndRelease.pass(); 85 }//End init() 86 87 public static void testMouseDrag(){ 88 mousePressed = false; 89 mouseReleased = false; 90 91 pt = choice1.getLocationOnScreen(); 92 robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2); 93 robot.waitForIdle(); 94 robot.mousePress(InputEvent.BUTTON1_MASK); 95 robot.waitForIdle(); 96 97 98 //move mouse outside Choice 99 robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y - choice1.getHeight()); 100 robot.waitForIdle(); 101 robot.mouseRelease(InputEvent.BUTTON1_MASK); 102 robot.waitForIdle(); 103 104 if (!mousePressed || !mouseReleased) 105 { 106 System.out.println("ERROR: "+ mousePressed+","+mouseReleased); 107 // close the choice 108 robot.keyPress(KeyEvent.VK_ESCAPE); 109 robot.keyRelease(KeyEvent.VK_ESCAPE); 110 robot.waitForIdle(); 111 DragMouseOutAndRelease.fail("Test failed. Choice should generate PRESSED, RELEASED events outside if pressed on Choice "); 112 } else{ 113 // close the choice 114 robot.keyPress(KeyEvent.VK_ESCAPE); 115 robot.keyRelease(KeyEvent.VK_ESCAPE); 116 robot.waitForIdle(); 117 System.out.println("Choice did generated PRESSED and RELEASED after Drag outside the Choice "); 118 } 119 } 120 121 122 123 /***************************************************** 124 * Standard Test Machinery Section 125 * DO NOT modify anything in this section -- it's a 126 * standard chunk of code which has all of the 127 * synchronisation necessary for the test harness. 128 * By keeping it the same in all tests, it is easier 129 * to read and understand someone else's test, as 130 * well as insuring that all tests behave correctly 131 * with the test harness. 132 * There is a section following this for test- 133 * classes 134 ******************************************************/ 135 private static boolean theTestPassed = false; 136 private static boolean testGeneratedInterrupt = false; 137 private static String failureMessage = ""; 138 139 private static Thread mainThread = null; 140 141 private static int sleepTime = 300000; 142 143 // Not sure about what happens if multiple of this test are 144 // instantiated in the same VM. Being static (and using 145 // static vars), it aint gonna work. Not worrying about 146 // it for now. 147 public static void main( String args[] ) throws InterruptedException 148 { 149 mainThread = Thread.currentThread(); 150 try 151 { 152 init(); 153 } 154 catch( TestPassedException e ) 155 { 156 //The test passed, so just return from main and harness will 157 // interepret this return as a pass 158 return; 159 } 160 //At this point, neither test pass nor test fail has been 161 // called -- either would have thrown an exception and ended the 162 // test, so we know we have multiple threads. 163 164 //Test involves other threads, so sleep and wait for them to 165 // called pass() or fail() 166 try 167 { 168 Thread.sleep( sleepTime ); 169 //Timed out, so fail the test 170 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); 171 } 172 catch (InterruptedException e) 173 { 174 //The test harness may have interrupted the test. If so, rethrow the exception 175 // so that the harness gets it and deals with it. 176 if( ! testGeneratedInterrupt ) throw e; 177 178 //reset flag in case hit this code more than once for some reason (just safety) 179 testGeneratedInterrupt = false; 180 181 if ( theTestPassed == false ) 182 { 183 throw new RuntimeException( failureMessage ); 184 } 185 } 186 187 }//main 188 189 public static synchronized void setTimeoutTo( int seconds ) 190 { 191 sleepTime = seconds * 1000; 192 } 193 194 public static synchronized void pass() 195 { 196 Sysout.println( "The test passed." ); 197 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 198 //first check if this is executing in main thread 199 if ( mainThread == Thread.currentThread() ) 200 { 201 //Still in the main thread, so set the flag just for kicks, 202 // and throw a test passed exception which will be caught 203 // and end the test. 204 theTestPassed = true; 205 throw new TestPassedException(); 206 } 207 theTestPassed = true; 208 testGeneratedInterrupt = true; 209 mainThread.interrupt(); 210 }//pass() 211 212 public static synchronized void fail() 213 { 214 //test writer didn't specify why test failed, so give generic 215 fail( "it just plain failed! :-)" ); 216 } 217 218 public static synchronized void fail( String whyFailed ) 219 { 220 Sysout.println( "The test failed: " + whyFailed ); 221 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 222 //check if this called from main thread 223 if ( mainThread == Thread.currentThread() ) 224 { 225 //If main thread, fail now 'cause not sleeping 226 throw new RuntimeException( whyFailed ); 227 } 228 theTestPassed = false; 229 testGeneratedInterrupt = true; 230 failureMessage = whyFailed; 231 mainThread.interrupt(); 232 }//fail() 233 234 }// class DragMouseOutAndRelease 235 236 //This exception is used to exit from any level of call nesting 237 // when it's determined that the test has passed, and immediately 238 // end the test. 239 class TestPassedException extends RuntimeException 240 { 241 } 242 243 //*********** End Standard Test Machinery Section ********** 244 245 246 //************ Begin classes defined for the test **************** 247 248 // if want to make listeners, here is the recommended place for them, then instantiate 249 // them in init() 250 251 /* Example of a class which may be written as part of a test 252 class NewClass implements anInterface 253 { 254 static int newVar = 0; 255 256 public void eventDispatched(AWTEvent e) 257 { 258 //Counting events to see if we get enough 259 eventCount++; 260 261 if( eventCount == 20 ) 262 { 263 //got enough events, so pass 264 265 DragMouseOutAndRelease.pass(); 266 } 267 else if( tries == 20 ) 268 { 269 //tried too many times without getting enough events so fail 270 271 DragMouseOutAndRelease.fail(); 272 } 273 274 }// eventDispatched() 275 276 }// NewClass class 277 278 */ 279 280 281 //************** End classes defined for the test ******************* 282 283 284 285 286 /**************************************************** 287 Standard Test Machinery 288 DO NOT modify anything below -- it's a standard 289 chunk of code whose purpose is to make user 290 interaction uniform, and thereby make it simpler 291 to read and understand someone else's test. 292 ****************************************************/ 293 294 /** 295 This is part of the standard test machinery. 296 It creates a dialog (with the instructions), and is the interface 297 for sending text messages to the user. 298 To print the instructions, send an array of strings to Sysout.createDialog 299 WithInstructions method. Put one line of instructions per array entry. 300 To display a message for the tester to see, simply call Sysout.println 301 with the string to be displayed. 302 This mimics System.out.println but works within the test harness as well 303 as standalone. 304 */ 305 306 class Sysout 307 { 308 private static TestDialog dialog; 309 310 public static void createDialogWithInstructions( String[] instructions ) 311 { 312 dialog = new TestDialog( new Frame(), "Instructions" ); 313 dialog.printInstructions( instructions ); 314 dialog.setVisible(true); 315 println( "Any messages for the tester will display here." ); 316 } 317 318 public static void createDialog( ) 319 { 320 dialog = new TestDialog( new Frame(), "Instructions" ); 321 String[] defInstr = { "Instructions will appear here. ", "" } ; 322 dialog.printInstructions( defInstr ); 323 dialog.setVisible(true); 324 println( "Any messages for the tester will display here." ); 325 } 326 327 328 public static void printInstructions( String[] instructions ) 329 { 330 dialog.printInstructions( instructions ); 331 } 332 333 334 public static void println( String messageIn ) 335 { 336 dialog.displayMessage( messageIn ); 337 System.out.println(messageIn); 338 } 339 340 }// Sysout class 341 342 /** 343 This is part of the standard test machinery. It provides a place for the 344 test instructions to be displayed, and a place for interactive messages 345 to the user to be displayed. 346 To have the test instructions displayed, see Sysout. 347 To have a message to the user be displayed, see Sysout. 348 Do not call anything in this dialog directly. 349 */ 350 class TestDialog extends Dialog 351 { 352 353 TextArea instructionsText; 354 TextArea messageText; 355 int maxStringLength = 80; 356 357 //DO NOT call this directly, go through Sysout 358 public TestDialog( Frame frame, String name ) 359 { 360 super( frame, name ); 361 int scrollBoth = TextArea.SCROLLBARS_BOTH; 362 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 363 add( "North", instructionsText ); 364 365 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 366 add("Center", messageText); 367 368 pack(); 369 370 setVisible(true); 371 }// TestDialog() 372 373 //DO NOT call this directly, go through Sysout 374 public void printInstructions( String[] instructions ) 375 { 376 //Clear out any current instructions 377 instructionsText.setText( "" ); 378 379 //Go down array of instruction strings 380 381 String printStr, remainingStr; 382 for( int i=0; i < instructions.length; i++ ) 383 { 384 //chop up each into pieces maxSringLength long 385 remainingStr = instructions[ i ]; 386 while( remainingStr.length() > 0 ) 387 { 388 //if longer than max then chop off first max chars to print 389 if( remainingStr.length() >= maxStringLength ) 390 { 391 //Try to chop on a word boundary 392 int posOfSpace = remainingStr. 393 lastIndexOf( ' ', maxStringLength - 1 ); 394 395 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 396 397 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 398 remainingStr = remainingStr.substring( posOfSpace + 1 ); 399 } 400 //else just print 401 else 402 { 403 printStr = remainingStr; 404 remainingStr = ""; 405 } 406 407 instructionsText.append( printStr + "\n" ); 408 409 }// while 410 411 }// for 412 413 }//printInstructions() 414 415 //DO NOT call this directly, go through Sysout 416 public void displayMessage( String messageIn ) 417 { 418 messageText.append( messageIn + "\n" ); 419 System.out.println(messageIn); 420 } 421 422 }// TestDialog class