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