886N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
886N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
886N/A *
886N/A * This code is free software; you can redistribute it and/or modify it
886N/A * under the terms of the GNU General Public License version 2 only, as
886N/A * published by the Free Software Foundation.
886N/A *
886N/A * This code is distributed in the hope that it will be useful, but WITHOUT
886N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
886N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
886N/A * version 2 for more details (a copy is included in the LICENSE file that
886N/A * accompanied this code).
886N/A *
886N/A * You should have received a copy of the GNU General Public License version
886N/A * 2 along with this work; if not, write to the Free Software Foundation,
886N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
886N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
886N/A */
886N/A
886N/A/*
886N/A @test %W% %E%
886N/A @bug 6779670
886N/A @summary Tests if a LW components in the glass pane affects HW in the content pane
886N/A @author anthony.petrov@...: area=awt.mixing
886N/A @library ../regtesthelpers
886N/A @build Util
886N/A @run main JButtonInGlassPane
886N/A*/
886N/A
886N/A
886N/A/**
886N/A * JButtonInGlassPane.java
886N/A *
886N/A * summary: Tests whether a LW menu correctly overlaps a HW button
886N/A */
886N/A
886N/Aimport java.awt.*;
886N/Aimport java.awt.event.*;
886N/Aimport javax.swing.*;
886N/Aimport test.java.awt.regtesthelpers.Util;
886N/A
886N/A
886N/A
886N/Apublic class JButtonInGlassPane
886N/A{
886N/A static volatile boolean failed = false;
886N/A
886N/A private static void init()
886N/A {
886N/A //*** Create instructions for the user here ***
886N/A
886N/A String[] instructions =
886N/A {
886N/A "This is an AUTOMATIC test, simply wait until it is done.",
886N/A "The result (passed or failed) will be shown in the",
886N/A "message window below."
886N/A };
886N/A Sysout.createDialog( );
886N/A Sysout.printInstructions( instructions );
886N/A
886N/A JFrame frame = new JFrame("Glass Pane children test");
886N/A frame.setLayout(null);
886N/A
886N/A final Button button = new Button("AWT Button");
886N/A button.setBounds(100,100,100,100);
886N/A frame.add(button);
886N/A
886N/A button.addActionListener(new ActionListener() {
886N/A public void actionPerformed(ActionEvent e) {
886N/A failed = true;
886N/A }
886N/A });
886N/A
886N/A frame.getGlassPane().setVisible(true);
886N/A Container glassPane = (Container) frame.getGlassPane();
886N/A glassPane.setLayout(null);
886N/A
886N/A final JButton jbutton = new JButton("JButton");
886N/A jbutton.setBounds(50,50,100,100);
886N/A glassPane.add(jbutton);
886N/A
886N/A jbutton.setVisible(false);
886N/A
886N/A frame.setSize(400, 400);
886N/A frame.setLocationRelativeTo(null);
886N/A frame.setVisible(true);
886N/A
886N/A Robot robot = Util.createRobot();
886N/A robot.setAutoDelay(20);
886N/A
886N/A Util.waitForIdle(robot);
886N/A
886N/A jbutton.setVisible(true);
886N/A Util.waitForIdle(robot);
886N/A
886N/A // Click the LW button - in the area that intersects with
886N/A // the HW button.
886N/A Point lLoc = jbutton.getLocationOnScreen();
886N/A robot.mouseMove(lLoc.x + jbutton.getWidth() - 5, lLoc.y + jbutton.getHeight() - 5);
886N/A
886N/A robot.mousePress(InputEvent.BUTTON1_MASK);
886N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
886N/A Util.waitForIdle(robot);
886N/A
886N/A jbutton.setBounds(50,50,120,120);
886N/A Util.waitForIdle(robot);
886N/A
886N/A // Now click on the 'added' area of the LW button that again
886N/A // intersects with the HW.
886N/A robot.mouseMove(lLoc.x + jbutton.getWidth() - 5, lLoc.y + jbutton.getHeight() - 5);
886N/A
886N/A robot.mousePress(InputEvent.BUTTON1_MASK);
886N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
886N/A Util.waitForIdle(robot);
886N/A
886N/A if (failed) {
886N/A JButtonInGlassPane.fail("The LW button did not receive the click.");
886N/A } else {
886N/A JButtonInGlassPane.pass();
886N/A }
886N/A }//End init()
886N/A
886N/A
886N/A
886N/A /*****************************************************
886N/A * Standard Test Machinery Section
886N/A * DO NOT modify anything in this section -- it's a
886N/A * standard chunk of code which has all of the
886N/A * synchronisation necessary for the test harness.
886N/A * By keeping it the same in all tests, it is easier
886N/A * to read and understand someone else's test, as
886N/A * well as insuring that all tests behave correctly
886N/A * with the test harness.
886N/A * There is a section following this for test-
886N/A * classes
886N/A ******************************************************/
886N/A private static boolean theTestPassed = false;
886N/A private static boolean testGeneratedInterrupt = false;
886N/A private static String failureMessage = "";
886N/A
886N/A private static Thread mainThread = null;
886N/A
886N/A private static int sleepTime = 300000;
886N/A
886N/A // Not sure about what happens if multiple of this test are
886N/A // instantiated in the same VM. Being static (and using
886N/A // static vars), it aint gonna work. Not worrying about
886N/A // it for now.
886N/A public static void main( String args[] ) throws InterruptedException
886N/A {
886N/A mainThread = Thread.currentThread();
886N/A try
886N/A {
886N/A init();
886N/A }
886N/A catch( TestPassedException e )
886N/A {
886N/A //The test passed, so just return from main and harness will
886N/A // interepret this return as a pass
886N/A return;
886N/A }
886N/A //At this point, neither test pass nor test fail has been
886N/A // called -- either would have thrown an exception and ended the
886N/A // test, so we know we have multiple threads.
886N/A
886N/A //Test involves other threads, so sleep and wait for them to
886N/A // called pass() or fail()
886N/A try
886N/A {
886N/A Thread.sleep( sleepTime );
886N/A //Timed out, so fail the test
886N/A throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
886N/A }
886N/A catch (InterruptedException e)
886N/A {
886N/A //The test harness may have interrupted the test. If so, rethrow the exception
886N/A // so that the harness gets it and deals with it.
886N/A if( ! testGeneratedInterrupt ) throw e;
886N/A
886N/A //reset flag in case hit this code more than once for some reason (just safety)
886N/A testGeneratedInterrupt = false;
886N/A
886N/A if ( theTestPassed == false )
886N/A {
886N/A throw new RuntimeException( failureMessage );
886N/A }
886N/A }
886N/A
886N/A }//main
886N/A
886N/A public static synchronized void setTimeoutTo( int seconds )
886N/A {
886N/A sleepTime = seconds * 1000;
886N/A }
886N/A
886N/A public static synchronized void pass()
886N/A {
886N/A Sysout.println( "The test passed." );
886N/A Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
886N/A //first check if this is executing in main thread
886N/A if ( mainThread == Thread.currentThread() )
886N/A {
886N/A //Still in the main thread, so set the flag just for kicks,
886N/A // and throw a test passed exception which will be caught
886N/A // and end the test.
886N/A theTestPassed = true;
886N/A throw new TestPassedException();
886N/A }
886N/A theTestPassed = true;
886N/A testGeneratedInterrupt = true;
886N/A mainThread.interrupt();
886N/A }//pass()
886N/A
886N/A public static synchronized void fail()
886N/A {
886N/A //test writer didn't specify why test failed, so give generic
886N/A fail( "it just plain failed! :-)" );
886N/A }
886N/A
886N/A public static synchronized void fail( String whyFailed )
886N/A {
886N/A Sysout.println( "The test failed: " + whyFailed );
886N/A Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
886N/A //check if this called from main thread
886N/A if ( mainThread == Thread.currentThread() )
886N/A {
886N/A //If main thread, fail now 'cause not sleeping
886N/A throw new RuntimeException( whyFailed );
886N/A }
886N/A theTestPassed = false;
886N/A testGeneratedInterrupt = true;
886N/A failureMessage = whyFailed;
886N/A mainThread.interrupt();
886N/A }//fail()
886N/A
886N/A}// class JButtonInGlassPane
886N/A
886N/A//This exception is used to exit from any level of call nesting
886N/A// when it's determined that the test has passed, and immediately
886N/A// end the test.
886N/Aclass TestPassedException extends RuntimeException
886N/A{
886N/A}
886N/A
886N/A//*********** End Standard Test Machinery Section **********
886N/A
886N/A
886N/A//************ Begin classes defined for the test ****************
886N/A
886N/A// if want to make listeners, here is the recommended place for them, then instantiate
886N/A// them in init()
886N/A
886N/A/* Example of a class which may be written as part of a test
886N/Aclass NewClass implements anInterface
886N/A {
886N/A static int newVar = 0;
886N/A
886N/A public void eventDispatched(AWTEvent e)
886N/A {
886N/A //Counting events to see if we get enough
886N/A eventCount++;
886N/A
886N/A if( eventCount == 20 )
886N/A {
886N/A //got enough events, so pass
886N/A
886N/A JButtonInGlassPane.pass();
886N/A }
886N/A else if( tries == 20 )
886N/A {
886N/A //tried too many times without getting enough events so fail
886N/A
886N/A JButtonInGlassPane.fail();
886N/A }
886N/A
886N/A }// eventDispatched()
886N/A
886N/A }// NewClass class
886N/A
886N/A*/
886N/A
886N/A
886N/A//************** End classes defined for the test *******************
886N/A
886N/A
886N/A
886N/A
886N/A/****************************************************
886N/A Standard Test Machinery
886N/A DO NOT modify anything below -- it's a standard
886N/A chunk of code whose purpose is to make user
886N/A interaction uniform, and thereby make it simpler
886N/A to read and understand someone else's test.
886N/A ****************************************************/
886N/A
886N/A/**
886N/A This is part of the standard test machinery.
886N/A It creates a dialog (with the instructions), and is the interface
886N/A for sending text messages to the user.
886N/A To print the instructions, send an array of strings to Sysout.createDialog
886N/A WithInstructions method. Put one line of instructions per array entry.
886N/A To display a message for the tester to see, simply call Sysout.println
886N/A with the string to be displayed.
886N/A This mimics System.out.println but works within the test harness as well
886N/A as standalone.
886N/A */
886N/A
886N/Aclass Sysout
886N/A{
886N/A private static TestDialog dialog;
886N/A
886N/A public static void createDialogWithInstructions( String[] instructions )
886N/A {
886N/A dialog = new TestDialog( new Frame(), "Instructions" );
886N/A dialog.printInstructions( instructions );
886N/A dialog.setVisible(true);
886N/A println( "Any messages for the tester will display here." );
886N/A }
886N/A
886N/A public static void createDialog( )
886N/A {
886N/A dialog = new TestDialog( new Frame(), "Instructions" );
886N/A String[] defInstr = { "Instructions will appear here. ", "" } ;
886N/A dialog.printInstructions( defInstr );
886N/A dialog.setVisible(true);
886N/A println( "Any messages for the tester will display here." );
886N/A }
886N/A
886N/A
886N/A public static void printInstructions( String[] instructions )
886N/A {
886N/A dialog.printInstructions( instructions );
886N/A }
886N/A
886N/A
886N/A public static void println( String messageIn )
886N/A {
886N/A dialog.displayMessage( messageIn );
886N/A System.out.println(messageIn);
886N/A }
886N/A
886N/A}// Sysout class
886N/A
886N/A/**
886N/A This is part of the standard test machinery. It provides a place for the
886N/A test instructions to be displayed, and a place for interactive messages
886N/A to the user to be displayed.
886N/A To have the test instructions displayed, see Sysout.
886N/A To have a message to the user be displayed, see Sysout.
886N/A Do not call anything in this dialog directly.
886N/A */
886N/Aclass TestDialog extends Dialog
886N/A{
886N/A
886N/A TextArea instructionsText;
886N/A TextArea messageText;
886N/A int maxStringLength = 80;
886N/A
886N/A //DO NOT call this directly, go through Sysout
886N/A public TestDialog( Frame frame, String name )
886N/A {
886N/A super( frame, name );
886N/A int scrollBoth = TextArea.SCROLLBARS_BOTH;
886N/A instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
886N/A add( "North", instructionsText );
886N/A
886N/A messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
886N/A add("Center", messageText);
886N/A
886N/A pack();
886N/A
886N/A setVisible(true);
886N/A }// TestDialog()
886N/A
886N/A //DO NOT call this directly, go through Sysout
886N/A public void printInstructions( String[] instructions )
886N/A {
886N/A //Clear out any current instructions
886N/A instructionsText.setText( "" );
886N/A
886N/A //Go down array of instruction strings
886N/A
886N/A String printStr, remainingStr;
886N/A for( int i=0; i < instructions.length; i++ )
886N/A {
886N/A //chop up each into pieces maxSringLength long
886N/A remainingStr = instructions[ i ];
886N/A while( remainingStr.length() > 0 )
886N/A {
886N/A //if longer than max then chop off first max chars to print
886N/A if( remainingStr.length() >= maxStringLength )
886N/A {
886N/A //Try to chop on a word boundary
886N/A int posOfSpace = remainingStr.
886N/A lastIndexOf( ' ', maxStringLength - 1 );
886N/A
886N/A if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
886N/A
886N/A printStr = remainingStr.substring( 0, posOfSpace + 1 );
886N/A remainingStr = remainingStr.substring( posOfSpace + 1 );
886N/A }
886N/A //else just print
886N/A else
886N/A {
886N/A printStr = remainingStr;
886N/A remainingStr = "";
886N/A }
886N/A
886N/A instructionsText.append( printStr + "\n" );
886N/A
886N/A }// while
886N/A
886N/A }// for
886N/A
886N/A }//printInstructions()
886N/A
886N/A //DO NOT call this directly, go through Sysout
886N/A public void displayMessage( String messageIn )
886N/A {
886N/A messageText.append( messageIn + "\n" );
886N/A System.out.println(messageIn);
886N/A }
886N/A
886N/A}// TestDialog class
886N/A
886N/A