Path: blob/master/test/jdk/java/awt/Mixing/LWComboBox.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 663765527@summary Tests whether a LW combobox correctly overlaps a HW button28@author anthony.petrov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main LWComboBox32*/333435/**36* LWComboBox.java37*38* summary: Tests whether a LW combobox correctly overlaps a HW button39*/4041import java.awt.*;42import java.awt.event.*;43import javax.swing.*;44import java.util.Vector;45import test.java.awt.regtesthelpers.Util;46474849public class LWComboBox50{51static volatile boolean failed = false;5253private static void init()54{55JFrame f = new JFrame("LW menu test");5657JComboBox ch;58Button b;5960Vector v = new Vector();61for(int i = 1 ; i <=20;i++){62v.add("Item # "+i);63}64ch = new JComboBox(v);656667b = new Button("AWT Button");68b.addActionListener(new ActionListener() {69public void actionPerformed(ActionEvent e) {70failed = true;71}72});7374f.add(ch,BorderLayout.NORTH);75f.add(b,BorderLayout.CENTER);76f.setSize(300,300);77f.setVisible(true);7879Robot robot = Util.createRobot();80robot.setAutoDelay(20);8182Util.waitForIdle(robot);8384// Pop up the combobox85Point lLoc = ch.getLocationOnScreen();86System.err.println("lLoc: " + lLoc);87robot.mouseMove(lLoc.x + 5, lLoc.y + 5);8889robot.mousePress(InputEvent.BUTTON1_MASK);90robot.mouseRelease(InputEvent.BUTTON1_MASK);91Util.waitForIdle(robot);9293// Click on the combo popup.94// It's assumed that the popup item is located95// above the heavyweight button.96Point bLoc = b.getLocationOnScreen();97System.err.println("bLoc: " + bLoc);98robot.mouseMove(bLoc.x + 10, bLoc.y + 10);99100robot.mousePress(InputEvent.BUTTON1_MASK);101robot.mouseRelease(InputEvent.BUTTON1_MASK);102Util.waitForIdle(robot);103104if (failed) {105fail("The LW popup did not received the click.");106} else {107pass();108}109}//End init()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 LWComboBox225226//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