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