Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Frame/ShownOffScreenOnWin98/ShownOffScreenOnWin98Test.java
38828 views
/*1* Copyright (c) 2006, 2007, 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@bug 647749726@summary Windows drawn off-screen on Win98 if locationByPlatform is true27@author anthony.petrov@...: area=awt.toplevel28@library ../../regtesthelpers29@build Util30@run main ShownOffScreenOnWin98Test31*/3233/**34* ShownOffScreenOnWin98Test.java35*36* summary: Tests whether a frame is located inside the screen boundaries37*/3839import java.awt.*;40import java.awt.event.*;41import javax.swing.*;42import test.java.awt.regtesthelpers.Util;434445public class ShownOffScreenOnWin98Test46{4748//*** test-writer defined static variables go here ***495051private static void init()52{53//*** Create instructions for the user here ***5455String[] instructions =56{57"This is an AUTOMATIC test, simply wait until it is done.",58"The result (passed or failed) will be shown in the",59"message window below."60};61Sysout.createDialog( );62Sysout.printInstructions( instructions );636465boolean passed = false;6667try {68UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());6970JFrame frame = new JFrame();71frame.setLocationByPlatform(true);72frame.setVisible(true);7374Util.waitForIdle(null);7576GraphicsConfiguration gc = frame.getGraphicsConfiguration();7778Point loc = frame.getLocation();79Rectangle scrBnd = gc.getBounds();80Insets scrIns = Toolkit.getDefaultToolkit().getScreenInsets(gc);8182System.out.println("The frame location: " + loc);83System.out.println("The screen bound: " + scrBnd);84System.out.println("The screen insets: " + scrIns);8586passed = (loc.x >= scrBnd.x + scrIns.left87&& loc.x <= scrBnd.x + scrBnd.getWidth() - scrIns.right88&& loc.y >= scrBnd.y + scrIns.top89&& loc.y <= scrBnd.y + scrBnd.getHeight() - scrIns.bottom);90} catch (Exception e) {91e.printStackTrace();92ShownOffScreenOnWin98Test.fail("Unexpected exception caught: " + e);93}9495if (passed)96{97ShownOffScreenOnWin98Test.pass();98} else {99ShownOffScreenOnWin98Test.fail("The frame is located off screen");100}101102}//End init()103104105106/*****************************************************107* Standard Test Machinery Section108* DO NOT modify anything in this section -- it's a109* standard chunk of code which has all of the110* synchronisation necessary for the test harness.111* By keeping it the same in all tests, it is easier112* to read and understand someone else's test, as113* well as insuring that all tests behave correctly114* with the test harness.115* There is a section following this for test-116* classes117******************************************************/118private static boolean theTestPassed = false;119private static boolean testGeneratedInterrupt = false;120private static String failureMessage = "";121122private static Thread mainThread = null;123124private static int sleepTime = 300000;125126// Not sure about what happens if multiple of this test are127// instantiated in the same VM. Being static (and using128// static vars), it aint gonna work. Not worrying about129// it for now.130public static void main( String args[] ) throws InterruptedException131{132mainThread = Thread.currentThread();133try134{135init();136}137catch( TestPassedException e )138{139//The test passed, so just return from main and harness will140// interepret this return as a pass141return;142}143//At this point, neither test pass nor test fail has been144// called -- either would have thrown an exception and ended the145// test, so we know we have multiple threads.146147//Test involves other threads, so sleep and wait for them to148// called pass() or fail()149try150{151Thread.sleep( sleepTime );152//Timed out, so fail the test153throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );154}155catch (InterruptedException e)156{157//The test harness may have interrupted the test. If so, rethrow the exception158// so that the harness gets it and deals with it.159if( ! testGeneratedInterrupt ) throw e;160161//reset flag in case hit this code more than once for some reason (just safety)162testGeneratedInterrupt = false;163164if ( theTestPassed == false )165{166throw new RuntimeException( failureMessage );167}168}169170}//main171172public static synchronized void setTimeoutTo( int seconds )173{174sleepTime = seconds * 1000;175}176177public static synchronized void pass()178{179Sysout.println( "The test passed." );180Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );181//first check if this is executing in main thread182if ( mainThread == Thread.currentThread() )183{184//Still in the main thread, so set the flag just for kicks,185// and throw a test passed exception which will be caught186// and end the test.187theTestPassed = true;188throw new TestPassedException();189}190theTestPassed = true;191testGeneratedInterrupt = true;192mainThread.interrupt();193}//pass()194195public static synchronized void fail()196{197//test writer didn't specify why test failed, so give generic198fail( "it just plain failed! :-)" );199}200201public static synchronized void fail( String whyFailed )202{203Sysout.println( "The test failed: " + whyFailed );204Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );205//check if this called from main thread206if ( mainThread == Thread.currentThread() )207{208//If main thread, fail now 'cause not sleeping209throw new RuntimeException( whyFailed );210}211theTestPassed = false;212testGeneratedInterrupt = true;213failureMessage = whyFailed;214mainThread.interrupt();215}//fail()216217}// class ShownOffScreenOnWin98Test218219//This exception is used to exit from any level of call nesting220// when it's determined that the test has passed, and immediately221// end the test.222class TestPassedException extends RuntimeException223{224}225226//*********** End Standard Test Machinery Section **********227228229//************ Begin classes defined for the test ****************230231// if want to make listeners, here is the recommended place for them, then instantiate232// them in init()233234/* Example of a class which may be written as part of a test235class NewClass implements anInterface236{237static int newVar = 0;238239public void eventDispatched(AWTEvent e)240{241//Counting events to see if we get enough242eventCount++;243244if( eventCount == 20 )245{246//got enough events, so pass247248ShownOffScreenOnWin98Test.pass();249}250else if( tries == 20 )251{252//tried too many times without getting enough events so fail253254ShownOffScreenOnWin98Test.fail();255}256257}// eventDispatched()258259}// NewClass class260261*/262263264//************** End classes defined for the test *******************265266267268269/****************************************************270Standard Test Machinery271DO NOT modify anything below -- it's a standard272chunk of code whose purpose is to make user273interaction uniform, and thereby make it simpler274to read and understand someone else's test.275****************************************************/276277/**278This is part of the standard test machinery.279It creates a dialog (with the instructions), and is the interface280for sending text messages to the user.281To print the instructions, send an array of strings to Sysout.createDialog282WithInstructions method. Put one line of instructions per array entry.283To display a message for the tester to see, simply call Sysout.println284with the string to be displayed.285This mimics System.out.println but works within the test harness as well286as standalone.287*/288289class Sysout290{291private static TestDialog dialog;292293public static void createDialogWithInstructions( String[] instructions )294{295dialog = new TestDialog( new Frame(), "Instructions" );296dialog.printInstructions( instructions );297dialog.setVisible(true);298println( "Any messages for the tester will display here." );299}300301public static void createDialog( )302{303dialog = new TestDialog( new Frame(), "Instructions" );304String[] defInstr = { "Instructions will appear here. ", "" } ;305dialog.printInstructions( defInstr );306dialog.setVisible(true);307println( "Any messages for the tester will display here." );308}309310311public static void printInstructions( String[] instructions )312{313dialog.printInstructions( instructions );314}315316317public static void println( String messageIn )318{319dialog.displayMessage( messageIn );320System.out.println(messageIn);321}322323}// Sysout class324325/**326This is part of the standard test machinery. It provides a place for the327test instructions to be displayed, and a place for interactive messages328to the user to be displayed.329To have the test instructions displayed, see Sysout.330To have a message to the user be displayed, see Sysout.331Do not call anything in this dialog directly.332*/333class TestDialog extends Dialog334{335336TextArea instructionsText;337TextArea messageText;338int maxStringLength = 80;339340//DO NOT call this directly, go through Sysout341public TestDialog( Frame frame, String name )342{343super( frame, name );344int scrollBoth = TextArea.SCROLLBARS_BOTH;345instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );346add( "North", instructionsText );347348messageText = new TextArea( "", 5, maxStringLength, scrollBoth );349add("Center", messageText);350351pack();352353setVisible(true);354}// TestDialog()355356//DO NOT call this directly, go through Sysout357public void printInstructions( String[] instructions )358{359//Clear out any current instructions360instructionsText.setText( "" );361362//Go down array of instruction strings363364String printStr, remainingStr;365for( int i=0; i < instructions.length; i++ )366{367//chop up each into pieces maxSringLength long368remainingStr = instructions[ i ];369while( remainingStr.length() > 0 )370{371//if longer than max then chop off first max chars to print372if( remainingStr.length() >= maxStringLength )373{374//Try to chop on a word boundary375int posOfSpace = remainingStr.376lastIndexOf( ' ', maxStringLength - 1 );377378if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;379380printStr = remainingStr.substring( 0, posOfSpace + 1 );381remainingStr = remainingStr.substring( posOfSpace + 1 );382}383//else just print384else385{386printStr = remainingStr;387remainingStr = "";388}389390instructionsText.append( printStr + "\n" );391392}// while393394}// for395396}//printInstructions()397398//DO NOT call this directly, go through Sysout399public void displayMessage( String messageIn )400{401messageText.append( messageIn + "\n" );402System.out.println(messageIn);403}404405}// TestDialog class406407408