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