Path: blob/master/test/jdk/java/awt/Mixing/LWPopupMenu.java
41149 views
/*1* Copyright (c) 2007, 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*/2223/*24@test25@key headful26@bug 481109627@summary Tests whether a LW menu correctly overlaps a HW button28@author anthony.petrov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main LWPopupMenu32*/333435/**36* LWPopupMenu.java37*38* summary: Tests whether a LW menu correctly overlaps a HW button39*/4041import java.awt.*;42import java.awt.event.*;43import javax.swing.*;44import test.java.awt.regtesthelpers.Util;45464748public class LWPopupMenu49{5051//*** test-writer defined static variables go here ***5253static volatile boolean failed = true;5455private static void init()56{57JFrame f = new JFrame("LW menu test");5859JMenuBar menubar = new JMenuBar();60f.setJMenuBar(menubar);6162// Create lightweight-enabled menu63JMenu lmenu = new JMenu("Lite Menu");64lmenu.add("Salad");65lmenu.add( new AbstractAction("Fruit Plate") {66public void actionPerformed(ActionEvent e) {67failed = false;68}69});70lmenu.add("Water");71menubar.add(lmenu);7273// Create Heavyweight AWT Button74Button heavy = new Button(" Heavyweight Button ");7576// Add heavy button to box77Box box = Box.createVerticalBox();78box.add(Box.createVerticalStrut(20));79box.add(heavy);80box.add(Box.createVerticalStrut(20));8182f.getContentPane().add("Center", box);8384f.pack();85f.show();8687Robot robot = Util.createRobot();88robot.setAutoDelay(20);8990Util.waitForIdle(robot);9192// Activate the menu93Point lLoc = lmenu.getLocationOnScreen();94robot.mouseMove(lLoc.x + 5, lLoc.y + 5);9596robot.mousePress(InputEvent.BUTTON1_MASK);97robot.mouseRelease(InputEvent.BUTTON1_MASK);98Util.waitForIdle(robot);99100101// Click on the "Fruit Plate" menu item.102// It's assumed that the menu item is located103// above the heavyweight button.104Point bLoc = heavy.getLocationOnScreen();105robot.mouseMove(bLoc.x + 10, bLoc.y + 5);106107robot.mousePress(InputEvent.BUTTON1_MASK);108robot.mouseRelease(InputEvent.BUTTON1_MASK);109Util.waitForIdle(robot);110111if (failed) {112LWPopupMenu.fail("The LW menu item did not received the click.");113} else {114LWPopupMenu.pass();115}116}//End init()117118119120/*****************************************************121* Standard Test Machinery Section122* DO NOT modify anything in this section -- it's a123* standard chunk of code which has all of the124* synchronisation necessary for the test harness.125* By keeping it the same in all tests, it is easier126* to read and understand someone else's test, as127* well as insuring that all tests behave correctly128* with the test harness.129* There is a section following this for test-130* classes131******************************************************/132private static boolean theTestPassed = false;133private static boolean testGeneratedInterrupt = false;134private static String failureMessage = "";135136private static Thread mainThread = null;137138private static int sleepTime = 300000;139140// Not sure about what happens if multiple of this test are141// instantiated in the same VM. Being static (and using142// static vars), it aint gonna work. Not worrying about143// it for now.144public static void main( String args[] ) throws InterruptedException145{146mainThread = Thread.currentThread();147try148{149init();150}151catch( TestPassedException e )152{153//The test passed, so just return from main and harness will154// interepret this return as a pass155return;156}157//At this point, neither test pass nor test fail has been158// called -- either would have thrown an exception and ended the159// test, so we know we have multiple threads.160161//Test involves other threads, so sleep and wait for them to162// called pass() or fail()163try164{165Thread.sleep( sleepTime );166//Timed out, so fail the test167throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );168}169catch (InterruptedException e)170{171//The test harness may have interrupted the test. If so, rethrow the exception172// so that the harness gets it and deals with it.173if( ! testGeneratedInterrupt ) throw e;174175//reset flag in case hit this code more than once for some reason (just safety)176testGeneratedInterrupt = false;177178if ( theTestPassed == false )179{180throw new RuntimeException( failureMessage );181}182}183184}//main185186public static synchronized void setTimeoutTo( int seconds )187{188sleepTime = seconds * 1000;189}190191public static synchronized void pass()192{193System.out.println( "The test passed." );194System.out.println( "The test is over, hit Ctl-C to stop Java VM" );195//first check if this is executing in main thread196if ( mainThread == Thread.currentThread() )197{198//Still in the main thread, so set the flag just for kicks,199// and throw a test passed exception which will be caught200// and end the test.201theTestPassed = true;202throw new TestPassedException();203}204theTestPassed = true;205testGeneratedInterrupt = true;206mainThread.interrupt();207}//pass()208209public static synchronized void fail()210{211//test writer didn't specify why test failed, so give generic212fail( "it just plain failed! :-)" );213}214215public static synchronized void fail( String whyFailed )216{217System.out.println( "The test failed: " + whyFailed );218System.out.println( "The test is over, hit Ctl-C to stop Java VM" );219//check if this called from main thread220if ( mainThread == Thread.currentThread() )221{222//If main thread, fail now 'cause not sleeping223throw new RuntimeException( whyFailed );224}225theTestPassed = false;226testGeneratedInterrupt = true;227failureMessage = whyFailed;228mainThread.interrupt();229}//fail()230231}// class LWPopupMenu232233//This exception is used to exit from any level of call nesting234// when it's determined that the test has passed, and immediately235// end the test.236class TestPassedException extends RuntimeException237{238}239240241