Path: blob/master/test/jdk/java/awt/Mixing/OpaqueTest.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 4811096 817340927@summary Tests whether opaque and non-opaque components mix correctly28@author anthony.petrov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main OpaqueTest32*/333435/**36* OpaqueTest.java37*38* summary: OpaqueTest39*/4041import java.awt.*;42import java.awt.event.*;43import javax.swing.*;44import test.java.awt.regtesthelpers.Util;45464748public class OpaqueTest49{5051//*** test-writer defined static variables go here ***5253static String testSeq = new String("");54final static String checkSeq = new String("010000101");5556private static void init()57{58// Create components59final Frame f = new Frame("Button-JButton mix test");60final Panel p = new Panel();61final Button heavy = new Button(" Heavyweight Button ");62final JButton light = new JButton(" LW Button ");6364// Actions for the buttons add appropriate number to the test sequence65heavy.addActionListener(new java.awt.event.ActionListener()66{67public void actionPerformed(java.awt.event.ActionEvent e) {68p.setComponentZOrder(light, 0);69f.validate();70testSeq = testSeq + "0";71}72}73);7475light.addActionListener(new java.awt.event.ActionListener()76{77public void actionPerformed(java.awt.event.ActionEvent e) {78p.setComponentZOrder(heavy, 0);79f.validate();80testSeq = testSeq + "1";81}82}83);8485// Overlap the buttons86heavy.setBounds(30, 30, 200, 200);87light.setBounds(10, 10, 50, 50);8889// Put the components into the frame90p.setLayout(null);91p.add(heavy);92p.add(light);93f.add(p);94f.setBounds(50, 50, 400, 400);95f.show();969798Robot robot = Util.createRobot();99robot.setAutoDelay(20);100101Util.waitForIdle(robot);102103// Move the mouse pointer to the position where both104// buttons overlap105Point heavyLoc = heavy.getLocationOnScreen();106robot.mouseMove(heavyLoc.x + 5, heavyLoc.y + 5);107108// Now perform the click at this point for 9 times109// In the middle of the process toggle the opaque110// flag value.111for (int i = 0; i < 9; ++i) {112if (i == 3) {113light.setMixingCutoutShape(new Rectangle());114}115if (i == 6) {116light.setMixingCutoutShape(null);117}118119robot.mousePress(InputEvent.BUTTON1_MASK);120robot.mouseRelease(InputEvent.BUTTON1_MASK);121Util.waitForIdle(robot);122}123124Util.waitForIdle(robot);125126// If the buttons are correctly mixed, the test sequence127// is equal to the check sequence.128if (testSeq.equals(checkSeq)) {129OpaqueTest.pass();130} else {131OpaqueTest.fail("The components changed their visible Z-order in a wrong sequence: '" + testSeq + "' instead of '" + checkSeq + "'");132}133}//End init()134135136137/*****************************************************138* Standard Test Machinery Section139* DO NOT modify anything in this section -- it's a140* standard chunk of code which has all of the141* synchronisation necessary for the test harness.142* By keeping it the same in all tests, it is easier143* to read and understand someone else's test, as144* well as insuring that all tests behave correctly145* with the test harness.146* There is a section following this for test-147* classes148******************************************************/149private static boolean theTestPassed = false;150private static boolean testGeneratedInterrupt = false;151private static String failureMessage = "";152153private static Thread mainThread = null;154155private static int sleepTime = 300000;156157// Not sure about what happens if multiple of this test are158// instantiated in the same VM. Being static (and using159// static vars), it aint gonna work. Not worrying about160// it for now.161public static void main( String args[] ) throws InterruptedException162{163mainThread = Thread.currentThread();164try165{166init();167}168catch( TestPassedException e )169{170//The test passed, so just return from main and harness will171// interepret this return as a pass172return;173}174//At this point, neither test pass nor test fail has been175// called -- either would have thrown an exception and ended the176// test, so we know we have multiple threads.177178//Test involves other threads, so sleep and wait for them to179// called pass() or fail()180try181{182Thread.sleep( sleepTime );183//Timed out, so fail the test184throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );185}186catch (InterruptedException e)187{188//The test harness may have interrupted the test. If so, rethrow the exception189// so that the harness gets it and deals with it.190if( ! testGeneratedInterrupt ) throw e;191192//reset flag in case hit this code more than once for some reason (just safety)193testGeneratedInterrupt = false;194195if ( theTestPassed == false )196{197throw new RuntimeException( failureMessage );198}199}200201}//main202203public static synchronized void setTimeoutTo( int seconds )204{205sleepTime = seconds * 1000;206}207208public static synchronized void pass()209{210System.out.println( "The test passed." );211System.out.println( "The test is over, hit Ctl-C to stop Java VM" );212//first check if this is executing in main thread213if ( mainThread == Thread.currentThread() )214{215//Still in the main thread, so set the flag just for kicks,216// and throw a test passed exception which will be caught217// and end the test.218theTestPassed = true;219throw new TestPassedException();220}221theTestPassed = true;222testGeneratedInterrupt = true;223mainThread.interrupt();224}//pass()225226public static synchronized void fail()227{228//test writer didn't specify why test failed, so give generic229fail( "it just plain failed! :-)" );230}231232public static synchronized void fail( String whyFailed )233{234System.out.println( "The test failed: " + whyFailed );235System.out.println( "The test is over, hit Ctl-C to stop Java VM" );236//check if this called from main thread237if ( mainThread == Thread.currentThread() )238{239//If main thread, fail now 'cause not sleeping240throw new RuntimeException( whyFailed );241}242theTestPassed = false;243testGeneratedInterrupt = true;244failureMessage = whyFailed;245mainThread.interrupt();246}//fail()247248}// class OpaqueTest249250//This exception is used to exit from any level of call nesting251// when it's determined that the test has passed, and immediately252// end the test.253class TestPassedException extends RuntimeException254{255}256257258