1 /* 2 * Copyright (c) 2007, 2018, 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 JFrame f = new JFrame("LW menu test"); 59 60 JMenuBar menubar = new JMenuBar(); 61 f.setJMenuBar(menubar); 62 63 // Create lightweight-enabled menu 64 JMenu lmenu = new JMenu("Lite Menu"); 65 lmenu.add("Salad"); 66 lmenu.add( new AbstractAction("Fruit Plate") { 67 public void actionPerformed(ActionEvent e) { 68 failed = false; 69 } 70 }); 71 lmenu.add("Water"); 72 menubar.add(lmenu); 73 74 // Create Heavyweight AWT Button 75 Button heavy = new Button(" Heavyweight Button "); 76 77 // Add heavy button to box 78 Box box = Box.createVerticalBox(); 79 box.add(Box.createVerticalStrut(20)); 80 box.add(heavy); 81 box.add(Box.createVerticalStrut(20)); 82 83 f.getContentPane().add("Center", box); 84 85 f.pack(); 86 f.show(); 87 88 Robot robot = Util.createRobot(); 89 robot.setAutoDelay(20); 90 91 Util.waitForIdle(robot); 92 93 // Activate the menu 94 Point lLoc = lmenu.getLocationOnScreen(); 95 robot.mouseMove(lLoc.x + 5, lLoc.y + 5); 96 97 robot.mousePress(InputEvent.BUTTON1_MASK); 98 robot.mouseRelease(InputEvent.BUTTON1_MASK); 99 Util.waitForIdle(robot); 100 101 102 // Click on the "Fruit Plate" menu item. 103 // It's assumed that the menu item is located 104 // above the heavyweight button. 105 Point bLoc = heavy.getLocationOnScreen(); 106 robot.mouseMove(bLoc.x + 10, bLoc.y + 5); 107 108 robot.mousePress(InputEvent.BUTTON1_MASK); 109 robot.mouseRelease(InputEvent.BUTTON1_MASK); 110 Util.waitForIdle(robot); 111 112 if (failed) { 113 LWPopupMenu.fail("The LW menu item did not received the click."); 114 } else { 115 LWPopupMenu.pass(); 116 } 117 }//End init() 118 119 120 121 /***************************************************** 122 * Standard Test Machinery Section 123 * DO NOT modify anything in this section -- it's a 124 * standard chunk of code which has all of the 125 * synchronisation necessary for the test harness. 126 * By keeping it the same in all tests, it is easier 127 * to read and understand someone else's test, as 128 * well as insuring that all tests behave correctly 129 * with the test harness. 130 * There is a section following this for test- 131 * classes 132 ******************************************************/ 133 private static boolean theTestPassed = false; 134 private static boolean testGeneratedInterrupt = false; 135 private static String failureMessage = ""; 136 137 private static Thread mainThread = null; 138 139 private static int sleepTime = 300000; 140 141 // Not sure about what happens if multiple of this test are 142 // instantiated in the same VM. Being static (and using 143 // static vars), it aint gonna work. Not worrying about 144 // it for now. 145 public static void main( String args[] ) throws InterruptedException 146 { 147 mainThread = Thread.currentThread(); 148 try 149 { 150 init(); 151 } 152 catch( TestPassedException e ) 153 { 154 //The test passed, so just return from main and harness will 155 // interepret this return as a pass 156 return; 157 } 158 //At this point, neither test pass nor test fail has been 159 // called -- either would have thrown an exception and ended the 160 // test, so we know we have multiple threads. 161 162 //Test involves other threads, so sleep and wait for them to 163 // called pass() or fail() 164 try 165 { 166 Thread.sleep( sleepTime ); 167 //Timed out, so fail the test 168 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); 169 } 170 catch (InterruptedException e) 171 { 172 //The test harness may have interrupted the test. If so, rethrow the exception 173 // so that the harness gets it and deals with it. 174 if( ! testGeneratedInterrupt ) throw e; 175 176 //reset flag in case hit this code more than once for some reason (just safety) 177 testGeneratedInterrupt = false; 178 179 if ( theTestPassed == false ) 180 { 181 throw new RuntimeException( failureMessage ); 182 } 183 } 184 185 }//main 186 187 public static synchronized void setTimeoutTo( int seconds ) 188 { 189 sleepTime = seconds * 1000; 190 } 191 192 public static synchronized void pass() 193 { 194 System.out.println( "The test passed." ); 195 System.out.println( "The test is over, hit Ctl-C to stop Java VM" ); 196 //first check if this is executing in main thread 197 if ( mainThread == Thread.currentThread() ) 198 { 199 //Still in the main thread, so set the flag just for kicks, 200 // and throw a test passed exception which will be caught 201 // and end the test. 202 theTestPassed = true; 203 throw new TestPassedException(); 204 } 205 theTestPassed = true; 206 testGeneratedInterrupt = true; 207 mainThread.interrupt(); 208 }//pass() 209 210 public static synchronized void fail() 211 { 212 //test writer didn't specify why test failed, so give generic 213 fail( "it just plain failed! :-)" ); 214 } 215 216 public static synchronized void fail( String whyFailed ) 217 { 218 System.out.println( "The test failed: " + whyFailed ); 219 System.out.println( "The test is over, hit Ctl-C to stop Java VM" ); 220 //check if this called from main thread 221 if ( mainThread == Thread.currentThread() ) 222 { 223 //If main thread, fail now 'cause not sleeping 224 throw new RuntimeException( whyFailed ); 225 } 226 theTestPassed = false; 227 testGeneratedInterrupt = true; 228 failureMessage = whyFailed; 229 mainThread.interrupt(); 230 }//fail() 231 232 }// class LWPopupMenu 233 234 //This exception is used to exit from any level of call nesting 235 // when it's determined that the test has passed, and immediately 236 // end the test. 237 class TestPassedException extends RuntimeException 238 { 239 }