Path: blob/master/test/jdk/java/awt/Menu/OpensWithNoGrab/OpensWithNoGrab.java
41154 views
/*1* Copyright (c) 2005, 2020, 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 635472127@summary REG: Menu does not disappear when clicked, keeping Choice's drop-down open, XToolkit28@author andrei.dmitriev: area=awt.menu29@library ../../regtesthelpers30@library /test/lib31@modules java.desktop/sun.awt32@build jdk.test.lib.Platform33@build Util34@run main OpensWithNoGrab35*/3637import java.awt.*;38import java.awt.event.*;3940import jdk.test.lib.Platform;41import test.java.awt.regtesthelpers.Util;4243public class OpensWithNoGrab44{45final static int delay = 50;46private static void init()47{48if (!Platform.isLinux()) {49System.out.println("This test is for XAWT/Motif only");50OpensWithNoGrab.pass();51}5253Choice ch = new Choice ();54Frame f = new Frame ("OpensWithNoGrab");55Robot robot;56Point framePt, choicePt;5758ch.add("line 1");59ch.add("line 2");60ch.add("line 3");61ch.add("line 4");62ch.setBackground(Color.red);63f.add(ch);6465Menu file = new Menu ("file");66MenuBar mb = new MenuBar();67mb.add(file);6869file.add(new MenuItem (" "));70file.add(new MenuItem (" "));71file.add(new MenuItem (" "));72file.add(new MenuItem (" "));73file.add(new MenuItem (" "));74file.add(new MenuItem (" "));75file.add(new MenuItem (" "));7677f.setMenuBar(mb);7879f.setBackground(Color.green);80f.setForeground(Color.green);81f.setSize(300, 200);82f.setVisible(true);83try {84robot = new Robot();85robot.setAutoWaitForIdle(true);86robot.setAutoDelay(50);8788Util.waitForIdle(robot);89// press on Choice90choicePt = ch.getLocationOnScreen();91robot.mouseMove(choicePt.x + ch.getWidth()/2, choicePt.y + ch.getHeight()/2);92robot.delay(delay);93robot.mousePress(InputEvent.BUTTON1_MASK);94robot.delay(delay);95robot.mouseRelease(InputEvent.BUTTON1_MASK);96robot.delay(delay);9798// press on Menu99framePt = f.getLocationOnScreen();100robot.mouseMove(choicePt.x + 10, choicePt.y - 15);101robot.delay(10*delay);102robot.mousePress(InputEvent.BUTTON1_MASK);103robot.delay(delay);104robot.mouseRelease(InputEvent.BUTTON1_MASK);105robot.delay(delay);106107robot.mouseMove(choicePt.x + 15, choicePt.y + 15);108Util.waitForIdle(robot);109110Color c = robot.getPixelColor(choicePt.x + 15, choicePt.y + 15);111System.out.println("Color obtained under opened menu is: "+c );112if (!c.equals(Color.red)){113OpensWithNoGrab.fail("Failed: menu was opened by first click after opened Choice.");114}115}catch(Exception e){116e.printStackTrace();117OpensWithNoGrab.fail("Failed: exception occur "+e);118}119OpensWithNoGrab.pass();120}//End init()121122123124/*****************************************************125* Standard Test Machinery Section126* DO NOT modify anything in this section -- it's a127* standard chunk of code which has all of the128* synchronisation necessary for the test harness.129* By keeping it the same in all tests, it is easier130* to read and understand someone else's test, as131* well as insuring that all tests behave correctly132* with the test harness.133* There is a section following this for test-134* classes135******************************************************/136private static boolean theTestPassed = false;137private static boolean testGeneratedInterrupt = false;138private static String failureMessage = "";139140private static Thread mainThread = null;141142private static int sleepTime = 300000;143144// Not sure about what happens if multiple of this test are145// instantiated in the same VM. Being static (and using146// static vars), it aint gonna work. Not worrying about147// it for now.148public static void main( String args[] ) throws InterruptedException149{150mainThread = Thread.currentThread();151try152{153init();154}155catch( TestPassedException e )156{157//The test passed, so just return from main and harness will158// interepret this return as a pass159return;160}161//At this point, neither test pass nor test fail has been162// called -- either would have thrown an exception and ended the163// test, so we know we have multiple threads.164165//Test involves other threads, so sleep and wait for them to166// called pass() or fail()167try168{169Thread.sleep( sleepTime );170//Timed out, so fail the test171throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );172}173catch (InterruptedException e)174{175//The test harness may have interrupted the test. If so, rethrow the exception176// so that the harness gets it and deals with it.177if( ! testGeneratedInterrupt ) throw e;178179//reset flag in case hit this code more than once for some reason (just safety)180testGeneratedInterrupt = false;181182if ( theTestPassed == false )183{184throw new RuntimeException( failureMessage );185}186}187188}//main189190public static synchronized void setTimeoutTo( int seconds )191{192sleepTime = seconds * 1000;193}194195public static synchronized void pass()196{197System.out.println( "The test passed." );198System.out.println( "The test is over, hit Ctl-C to stop Java VM" );199//first check if this is executing in main thread200if ( mainThread == Thread.currentThread() )201{202//Still in the main thread, so set the flag just for kicks,203// and throw a test passed exception which will be caught204// and end the test.205theTestPassed = true;206throw new TestPassedException();207}208theTestPassed = true;209testGeneratedInterrupt = true;210mainThread.interrupt();211}//pass()212213public static synchronized void fail()214{215//test writer didn't specify why test failed, so give generic216fail( "it just plain failed! :-)" );217}218219public static synchronized void fail( String whyFailed )220{221System.out.println( "The test failed: " + whyFailed );222System.out.println( "The test is over, hit Ctl-C to stop Java VM" );223//check if this called from main thread224if ( mainThread == Thread.currentThread() )225{226//If main thread, fail now 'cause not sleeping227throw new RuntimeException( whyFailed );228}229theTestPassed = false;230testGeneratedInterrupt = true;231failureMessage = whyFailed;232mainThread.interrupt();233}//fail()234235}// class OpensWithNoGrab236237//This exception is used to exit from any level of call nesting238// when it's determined that the test has passed, and immediately239// end the test.240class TestPassedException extends RuntimeException241{242}243244245