Path: blob/master/test/jdk/java/awt/Frame/ShownOffScreenOnWin98/ShownOffScreenOnWin98Test.java
41154 views
/*1* Copyright (c) 2006, 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@test25@key headful26@bug 647749727@summary Windows drawn off-screen on Win98 if locationByPlatform is true28@author anthony.petrov@...: area=awt.toplevel29@library ../../regtesthelpers30@build Util31@run main ShownOffScreenOnWin98Test32*/3334/**35* ShownOffScreenOnWin98Test.java36*37* summary: Tests whether a frame is located inside the screen boundaries38*/3940import java.awt.*;41import java.awt.event.*;42import javax.swing.*;43import test.java.awt.regtesthelpers.Util;444546public class ShownOffScreenOnWin98Test47{4849//*** test-writer defined static variables go here ***505152private static void init()53{54boolean passed = false;5556try {57UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());5859JFrame frame = new JFrame();60frame.setLocationByPlatform(true);61frame.setVisible(true);6263Util.waitForIdle(null);6465GraphicsConfiguration gc = frame.getGraphicsConfiguration();6667Point loc = frame.getLocation();68Rectangle scrBnd = gc.getBounds();69Insets scrIns = Toolkit.getDefaultToolkit().getScreenInsets(gc);7071System.out.println("The frame location: " + loc);72System.out.println("The screen bound: " + scrBnd);73System.out.println("The screen insets: " + scrIns);7475passed = (loc.x >= scrBnd.x + scrIns.left76&& loc.x <= scrBnd.x + scrBnd.getWidth() - scrIns.right77&& loc.y >= scrBnd.y + scrIns.top78&& loc.y <= scrBnd.y + scrBnd.getHeight() - scrIns.bottom);79} catch (Exception e) {80e.printStackTrace();81ShownOffScreenOnWin98Test.fail("Unexpected exception caught: " + e);82}8384if (passed)85{86ShownOffScreenOnWin98Test.pass();87} else {88ShownOffScreenOnWin98Test.fail("The frame is located off screen");89}9091}//End init()92939495/*****************************************************96* Standard Test Machinery Section97* DO NOT modify anything in this section -- it's a98* standard chunk of code which has all of the99* synchronisation necessary for the test harness.100* By keeping it the same in all tests, it is easier101* to read and understand someone else's test, as102* well as insuring that all tests behave correctly103* with the test harness.104* There is a section following this for test-105* classes106******************************************************/107private static boolean theTestPassed = false;108private static boolean testGeneratedInterrupt = false;109private static String failureMessage = "";110111private static Thread mainThread = null;112113private static int sleepTime = 300000;114115// Not sure about what happens if multiple of this test are116// instantiated in the same VM. Being static (and using117// static vars), it aint gonna work. Not worrying about118// it for now.119public static void main( String args[] ) throws InterruptedException120{121mainThread = Thread.currentThread();122try123{124init();125}126catch( TestPassedException e )127{128//The test passed, so just return from main and harness will129// interepret this return as a pass130return;131}132//At this point, neither test pass nor test fail has been133// called -- either would have thrown an exception and ended the134// test, so we know we have multiple threads.135136//Test involves other threads, so sleep and wait for them to137// called pass() or fail()138try139{140Thread.sleep( sleepTime );141//Timed out, so fail the test142throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );143}144catch (InterruptedException e)145{146//The test harness may have interrupted the test. If so, rethrow the exception147// so that the harness gets it and deals with it.148if( ! testGeneratedInterrupt ) throw e;149150//reset flag in case hit this code more than once for some reason (just safety)151testGeneratedInterrupt = false;152153if ( theTestPassed == false )154{155throw new RuntimeException( failureMessage );156}157}158159}//main160161public static synchronized void setTimeoutTo( int seconds )162{163sleepTime = seconds * 1000;164}165166public static synchronized void pass()167{168System.out.println( "The test passed." );169System.out.println( "The test is over, hit Ctl-C to stop Java VM" );170//first check if this is executing in main thread171if ( mainThread == Thread.currentThread() )172{173//Still in the main thread, so set the flag just for kicks,174// and throw a test passed exception which will be caught175// and end the test.176theTestPassed = true;177throw new TestPassedException();178}179theTestPassed = true;180testGeneratedInterrupt = true;181mainThread.interrupt();182}//pass()183184public static synchronized void fail()185{186//test writer didn't specify why test failed, so give generic187fail( "it just plain failed! :-)" );188}189190public static synchronized void fail( String whyFailed )191{192System.out.println( "The test failed: " + whyFailed );193System.out.println( "The test is over, hit Ctl-C to stop Java VM" );194//check if this called from main thread195if ( mainThread == Thread.currentThread() )196{197//If main thread, fail now 'cause not sleeping198throw new RuntimeException( whyFailed );199}200theTestPassed = false;201testGeneratedInterrupt = true;202failureMessage = whyFailed;203mainThread.interrupt();204}//fail()205206}// class ShownOffScreenOnWin98Test207208//This exception is used to exit from any level of call nesting209// when it's determined that the test has passed, and immediately210// end the test.211class TestPassedException extends RuntimeException212{213}214215216