Path: blob/master/test/jdk/java/awt/Mixing/HWDisappear.java
41149 views
/*1* Copyright (c) 2009, 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@test %W% %E%25@key headful26@bug 676951127@summary AWT components are invisible for a while after frame is moved & menu items are visible28@author anthony.petrov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main HWDisappear32*/3334/**35* HWDisappear.java36*37* summary: AWT components are invisible for a while after frame is moved & menu items are visible38*/3940import java.awt.*;41import java.awt.event.*;42import javax.swing.*;43import javax.swing.plaf.metal.MetalLookAndFeel;44import test.java.awt.regtesthelpers.Util;4546public class HWDisappear47{4849static volatile boolean clickPassed = false;5051private static void init()52{5354// Create the frame and the button55JFrame f = new JFrame();56f.setBounds(100, 100, 400, 300);5758JMenuBar menubar = new JMenuBar();59f.setJMenuBar(menubar);6061// Create lightweight-enabled menu62JMenu lmenu = new JMenu("Lite Menu");63lmenu.add("Salad");64lmenu.add("Fruit Plate");65lmenu.add("Water");66menubar.add(lmenu);6768Button b = new Button("OK");6970f.setLayout(null);71f.add(b);72b.setBounds(50, 50, 200, 50);7374b.addActionListener(new java.awt.event.ActionListener() {75public void actionPerformed(java.awt.event.ActionEvent e) {76clickPassed = true;77}78});7980f.setVisible(true);8182Robot robot = Util.createRobot();83robot.setAutoDelay(20);8485Util.waitForIdle(robot);8687// Move quite far to ensure the button is hidden completely88f.setLocation(500, 200);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);99100// Click on the button.101Point bLoc = b.getLocationOnScreen();102robot.mouseMove(bLoc.x + b.getWidth() / 2, bLoc.y + b.getHeight() / 2);103104robot.mousePress(InputEvent.BUTTON1_MASK);105robot.mouseRelease(InputEvent.BUTTON1_MASK);106Util.waitForIdle(robot);107108if (clickPassed) {109pass();110} else {111fail("The button cannot be clicked.");112}113}//End init()114115116117/*****************************************************118* Standard Test Machinery Section119* DO NOT modify anything in this section -- it's a120* standard chunk of code which has all of the121* synchronisation necessary for the test harness.122* By keeping it the same in all tests, it is easier123* to read and understand someone else's test, as124* well as insuring that all tests behave correctly125* with the test harness.126* There is a section following this for test-127* classes128******************************************************/129private static boolean theTestPassed = false;130private static boolean testGeneratedInterrupt = false;131private static String failureMessage = "";132133private static Thread mainThread = null;134135private static int sleepTime = 300000;136137// Not sure about what happens if multiple of this test are138// instantiated in the same VM. Being static (and using139// static vars), it aint gonna work. Not worrying about140// it for now.141public static void main( String args[] ) throws Exception142{143UIManager.setLookAndFeel(new MetalLookAndFeel());144mainThread = Thread.currentThread();145try146{147init();148}149catch( TestPassedException e )150{151//The test passed, so just return from main and harness will152// interepret this return as a pass153return;154}155//At this point, neither test pass nor test fail has been156// called -- either would have thrown an exception and ended the157// test, so we know we have multiple threads.158159//Test involves other threads, so sleep and wait for them to160// called pass() or fail()161try162{163Thread.sleep( sleepTime );164//Timed out, so fail the test165throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );166}167catch (InterruptedException e)168{169//The test harness may have interrupted the test. If so, rethrow the exception170// so that the harness gets it and deals with it.171if( ! testGeneratedInterrupt ) throw e;172173//reset flag in case hit this code more than once for some reason (just safety)174testGeneratedInterrupt = false;175176if ( theTestPassed == false )177{178throw new RuntimeException( failureMessage );179}180}181182}//main183184public static synchronized void setTimeoutTo( int seconds )185{186sleepTime = seconds * 1000;187}188189public static synchronized void pass()190{191System.out.println( "The test passed." );192System.out.println( "The test is over, hit Ctl-C to stop Java VM" );193//first check if this is executing in main thread194if ( mainThread == Thread.currentThread() )195{196//Still in the main thread, so set the flag just for kicks,197// and throw a test passed exception which will be caught198// and end the test.199theTestPassed = true;200throw new TestPassedException();201}202theTestPassed = true;203testGeneratedInterrupt = true;204mainThread.interrupt();205}//pass()206207public static synchronized void fail()208{209//test writer didn't specify why test failed, so give generic210fail( "it just plain failed! :-)" );211}212213public static synchronized void fail( String whyFailed )214{215System.out.println( "The test failed: " + whyFailed );216System.out.println( "The test is over, hit Ctl-C to stop Java VM" );217//check if this called from main thread218if ( mainThread == Thread.currentThread() )219{220//If main thread, fail now 'cause not sleeping221throw new RuntimeException( whyFailed );222}223theTestPassed = false;224testGeneratedInterrupt = true;225failureMessage = whyFailed;226mainThread.interrupt();227}//fail()228229}// class HWDisappear230231//This exception is used to exit from any level of call nesting232// when it's determined that the test has passed, and immediately233// end the test.234class TestPassedException extends RuntimeException235{236}237238239