Path: blob/master/test/jdk/java/awt/Mixing/NonOpaqueInternalFrame.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 676833227@summary Tests whether internal frames are always considered opaque28@author anthony.petrov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main NonOpaqueInternalFrame32*/333435/**36* NonOpaqueInternalFrame.java37*38* summary: Tests whether internal frames are always considered opaque39*/4041import java.awt.*;42import java.awt.event.*;43import java.beans.PropertyVetoException;44import javax.swing.*;45import java.util.Vector;46import test.java.awt.regtesthelpers.Util;47484950public class NonOpaqueInternalFrame51{52static volatile boolean failed = false;5354private static final class MyButton extends Button55implements ActionListener56{57public MyButton() {58setPreferredSize(new Dimension(100, 100));59addActionListener(this);60}6162public void actionPerformed(ActionEvent e) {63failed = true;64}65}6667private static void init()68{69// Create a frame with two non-opaque JInternalFrame's containing70// heavyweight buttons.71JFrame jframe = new JFrame("mixing test");72JDesktopPane desktop = new JDesktopPane();73jframe.setContentPane(desktop);74JInternalFrame iframe1 = new JInternalFrame("iframe 1");75iframe1.setIconifiable(true);76iframe1.add(new MyButton());77iframe1.setBounds(10, 10, 100, 100);78iframe1.setOpaque(false);79iframe1.setVisible(true);80desktop.add(iframe1);81JInternalFrame iframe2 = new JInternalFrame("iframe 2");82iframe2.setIconifiable(true);83iframe2.add(new MyButton());84iframe2.setBounds(50, 50, 100, 100);85iframe2.setOpaque(false);86iframe2.setVisible(true);87desktop.add(iframe2);88jframe.setSize(300, 300);89jframe.setVisible(true);9091Robot robot = Util.createRobot();92robot.setAutoDelay(20);9394Util.waitForIdle(robot);9596// Try selecting the bottommost frame97try {98iframe2.setSelected(true);99} catch (PropertyVetoException ex) {100ex.printStackTrace();101}102103// Click the title bar of the internal frame104Point lLoc = iframe2.getLocationOnScreen();105System.err.println("lLoc: " + lLoc);106robot.mouseMove(lLoc.x + 10, lLoc.y + 10);107Util.waitForIdle(robot);108109robot.mousePress(InputEvent.BUTTON1_MASK);110robot.mouseRelease(InputEvent.BUTTON1_MASK);111Util.waitForIdle(robot);112113114if (failed) {115fail("The JInternalFrame is considered non-opaque.");116} else {117pass();118}119}//End init()120121122123/*****************************************************124* Standard Test Machinery Section125* DO NOT modify anything in this section -- it's a126* standard chunk of code which has all of the127* synchronisation necessary for the test harness.128* By keeping it the same in all tests, it is easier129* to read and understand someone else's test, as130* well as insuring that all tests behave correctly131* with the test harness.132* There is a section following this for test-133* classes134******************************************************/135private static boolean theTestPassed = false;136private static boolean testGeneratedInterrupt = false;137private static String failureMessage = "";138139private static Thread mainThread = null;140141private static int sleepTime = 300000;142143// Not sure about what happens if multiple of this test are144// instantiated in the same VM. Being static (and using145// static vars), it aint gonna work. Not worrying about146// it for now.147public static void main( String args[] ) throws InterruptedException148{149mainThread = Thread.currentThread();150try151{152init();153}154catch( TestPassedException e )155{156//The test passed, so just return from main and harness will157// interepret this return as a pass158return;159}160//At this point, neither test pass nor test fail has been161// called -- either would have thrown an exception and ended the162// test, so we know we have multiple threads.163164//Test involves other threads, so sleep and wait for them to165// called pass() or fail()166try167{168Thread.sleep( sleepTime );169//Timed out, so fail the test170throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );171}172catch (InterruptedException e)173{174//The test harness may have interrupted the test. If so, rethrow the exception175// so that the harness gets it and deals with it.176if( ! testGeneratedInterrupt ) throw e;177178//reset flag in case hit this code more than once for some reason (just safety)179testGeneratedInterrupt = false;180181if ( theTestPassed == false )182{183throw new RuntimeException( failureMessage );184}185}186187}//main188189public static synchronized void setTimeoutTo( int seconds )190{191sleepTime = seconds * 1000;192}193194public static synchronized void pass()195{196System.out.println( "The test passed." );197System.out.println( "The test is over, hit Ctl-C to stop Java VM" );198//first check if this is executing in main thread199if ( mainThread == Thread.currentThread() )200{201//Still in the main thread, so set the flag just for kicks,202// and throw a test passed exception which will be caught203// and end the test.204theTestPassed = true;205throw new TestPassedException();206}207theTestPassed = true;208testGeneratedInterrupt = true;209mainThread.interrupt();210}//pass()211212public static synchronized void fail()213{214//test writer didn't specify why test failed, so give generic215fail( "it just plain failed! :-)" );216}217218public static synchronized void fail( String whyFailed )219{220System.out.println( "The test failed: " + whyFailed );221System.out.println( "The test is over, hit Ctl-C to stop Java VM" );222//check if this called from main thread223if ( mainThread == Thread.currentThread() )224{225//If main thread, fail now 'cause not sleeping226throw new RuntimeException( whyFailed );227}228theTestPassed = false;229testGeneratedInterrupt = true;230failureMessage = whyFailed;231mainThread.interrupt();232}//fail()233234}// class NonOpaqueInternalFrame235236//This exception is used to exit from any level of call nesting237// when it's determined that the test has passed, and immediately238// end the test.239class TestPassedException extends RuntimeException240{241}242243244