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