Path: blob/master/test/jdk/java/awt/Choice/UnfocusableCB_ERR/UnfocusableCB_ERR.java
41153 views
/*1* Copyright (c) 2006, 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*/22/*23@test24@key headful25@bug 639010326@summary Non-Focusable choice throws exception when selecting an item, Win3227@author andrei.dmitriev area=awt.choice28@run main UnfocusableCB_ERR29*/3031import java.awt.*;32import java.awt.event.*;3334public class UnfocusableCB_ERR35{36static final int delay = 100;37static Frame frame = new Frame("Test Frame");38static Choice choice1 = new Choice();39static Button button = new Button("Test");4041static Robot robot;42static Point pt;43static String failed = "";4445private static void init()46{47EventQueue.invokeLater(new Runnable() {48public void run() {49Thread.currentThread().setUncaughtExceptionHandler(50new Thread.UncaughtExceptionHandler(){51public void uncaughtException(Thread t, Throwable exc){52failed = exc.toString();53}54});55}56});5758frame.setLayout (new FlowLayout ());59for (int i = 1; i<10;i++){60choice1.add("item "+i);61}62frame.add(button);63frame.add(choice1);6465choice1.setFocusable(false);6667frame.pack();68frame.setVisible(true);69frame.validate();7071try {72robot = new Robot();73robot.setAutoDelay(50);74robot.waitForIdle();75testSpacePress();76} catch (Throwable e) {77UnfocusableCB_ERR.fail("Test failed. Exception thrown: "+e);78}79if (failed.equals("")){80UnfocusableCB_ERR.pass();81} else {82UnfocusableCB_ERR.fail("Test failed:");83}84}//End init()8586public static void testSpacePress(){8788pt = choice1.getLocationOnScreen();89robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2);90robot.waitForIdle();91robot.mousePress(InputEvent.BUTTON1_MASK);92robot.waitForIdle();93robot.mouseRelease(InputEvent.BUTTON1_MASK);949596robot.waitForIdle();9798//position mouse cursor over dropdown menu99robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + 2 * choice1.getHeight());100robot.waitForIdle();101102//move mouse outside Choice103robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y - choice1.getHeight());104robot.waitForIdle();105106robot.keyPress(KeyEvent.VK_SPACE);107robot.keyRelease(KeyEvent.VK_SPACE);108robot.waitForIdle();109}110111112113/*****************************************************114* Standard Test Machinery Section115* DO NOT modify anything in this section -- it's a116* standard chunk of code which has all of the117* synchronisation necessary for the test harness.118* By keeping it the same in all tests, it is easier119* to read and understand someone else's test, as120* well as insuring that all tests behave correctly121* with the test harness.122* There is a section following this for test-123* classes124******************************************************/125private static boolean theTestPassed = false;126private static boolean testGeneratedInterrupt = false;127private static String failureMessage = "";128129private static Thread mainThread = null;130131private static int sleepTime = 300000;132133// Not sure about what happens if multiple of this test are134// instantiated in the same VM. Being static (and using135// static vars), it aint gonna work. Not worrying about136// it for now.137public static void main( String args[] ) throws InterruptedException138{139mainThread = Thread.currentThread();140try141{142init();143}144catch( TestPassedException e )145{146//The test passed, so just return from main and harness will147// interepret this return as a pass148return;149}150//At this point, neither test pass nor test fail has been151// called -- either would have thrown an exception and ended the152// test, so we know we have multiple threads.153154//Test involves other threads, so sleep and wait for them to155// called pass() or fail()156try157{158Thread.sleep( sleepTime );159//Timed out, so fail the test160throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );161}162catch (InterruptedException e)163{164//The test harness may have interrupted the test. If so, rethrow the exception165// so that the harness gets it and deals with it.166if( ! testGeneratedInterrupt ) throw e;167168//reset flag in case hit this code more than once for some reason (just safety)169testGeneratedInterrupt = false;170171if ( theTestPassed == false )172{173throw new RuntimeException( failureMessage );174}175}176177}//main178179public static synchronized void setTimeoutTo( int seconds )180{181sleepTime = seconds * 1000;182}183184public static synchronized void pass()185{186System.out.println( "The test passed." );187System.out.println( "The test is over, hit Ctl-C to stop Java VM" );188//first check if this is executing in main thread189if ( mainThread == Thread.currentThread() )190{191//Still in the main thread, so set the flag just for kicks,192// and throw a test passed exception which will be caught193// and end the test.194theTestPassed = true;195throw new TestPassedException();196}197theTestPassed = true;198testGeneratedInterrupt = true;199mainThread.interrupt();200}//pass()201202public static synchronized void fail()203{204//test writer didn't specify why test failed, so give generic205fail( "it just plain failed! :-)" );206}207208public static synchronized void fail( String whyFailed )209{210System.out.println( "The test failed: " + whyFailed );211System.out.println( "The test is over, hit Ctl-C to stop Java VM" );212//check if this called from main thread213if ( mainThread == Thread.currentThread() )214{215//If main thread, fail now 'cause not sleeping216throw new RuntimeException( whyFailed );217}218theTestPassed = false;219testGeneratedInterrupt = true;220failureMessage = whyFailed;221mainThread.interrupt();222}//fail()223224}// class UnfocusableCB_ERR225226//This exception is used to exit from any level of call nesting227// when it's determined that the test has passed, and immediately228// end the test.229class TestPassedException extends RuntimeException230{231}232233234