1 /*
   2  * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   @test
  26   @key headful
  27   @bug 6477497
  28   @summary Windows drawn off-screen on Win98 if locationByPlatform is true
  29   @author anthony.petrov@...: area=awt.toplevel
  30   @library ../../regtesthelpers
  31   @build Util
  32   @run main ShownOffScreenOnWin98Test
  33 */
  34 
  35 /**
  36  * ShownOffScreenOnWin98Test.java
  37  *
  38  * summary:  Tests whether a frame is located inside the screen boundaries
  39  */
  40 
  41 import java.awt.*;
  42 import java.awt.event.*;
  43 import javax.swing.*;
  44 import test.java.awt.regtesthelpers.Util;
  45 
  46 
  47 public class ShownOffScreenOnWin98Test
  48 {
  49 
  50     //*** test-writer defined static variables go here ***
  51 
  52 
  53     private static void init()
  54     {
  55         boolean passed = false;
  56 
  57         try {
  58             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  59 
  60             JFrame frame = new JFrame();
  61             frame.setLocationByPlatform(true);
  62             frame.setVisible(true);
  63 
  64             Util.waitForIdle(null);
  65 
  66             GraphicsConfiguration gc = frame.getGraphicsConfiguration();
  67 
  68             Point loc = frame.getLocation();
  69             Rectangle scrBnd = gc.getBounds();
  70             Insets scrIns = Toolkit.getDefaultToolkit().getScreenInsets(gc);
  71 
  72             System.out.println("The frame location: " + loc);
  73             System.out.println("The screen bound: " + scrBnd);
  74             System.out.println("The screen insets: " + scrIns);
  75 
  76             passed =  (loc.x >= scrBnd.x + scrIns.left
  77                     && loc.x <= scrBnd.x + scrBnd.getWidth() - scrIns.right
  78                     && loc.y >= scrBnd.y + scrIns.top
  79                     && loc.y <= scrBnd.y + scrBnd.getHeight() - scrIns.bottom);
  80         } catch (Exception e) {
  81             e.printStackTrace();
  82             ShownOffScreenOnWin98Test.fail("Unexpected exception caught: " + e);
  83         }
  84 
  85         if (passed)
  86         {
  87             ShownOffScreenOnWin98Test.pass();
  88         } else {
  89             ShownOffScreenOnWin98Test.fail("The frame is located off screen");
  90         }
  91 
  92     }//End  init()
  93 
  94 
  95 
  96     /*****************************************************
  97      * Standard Test Machinery Section
  98      * DO NOT modify anything in this section -- it's a
  99      * standard chunk of code which has all of the
 100      * synchronisation necessary for the test harness.
 101      * By keeping it the same in all tests, it is easier
 102      * to read and understand someone else's test, as
 103      * well as insuring that all tests behave correctly
 104      * with the test harness.
 105      * There is a section following this for test-
 106      * classes
 107      ******************************************************/
 108     private static boolean theTestPassed = false;
 109     private static boolean testGeneratedInterrupt = false;
 110     private static String failureMessage = "";
 111 
 112     private static Thread mainThread = null;
 113 
 114     private static int sleepTime = 300000;
 115 
 116     // Not sure about what happens if multiple of this test are
 117     //  instantiated in the same VM.  Being static (and using
 118     //  static vars), it aint gonna work.  Not worrying about
 119     //  it for now.
 120     public static void main( String args[] ) throws InterruptedException
 121     {
 122         mainThread = Thread.currentThread();
 123         try
 124         {
 125             init();
 126         }
 127         catch( TestPassedException e )
 128         {
 129             //The test passed, so just return from main and harness will
 130             // interepret this return as a pass
 131             return;
 132         }
 133         //At this point, neither test pass nor test fail has been
 134         // called -- either would have thrown an exception and ended the
 135         // test, so we know we have multiple threads.
 136 
 137         //Test involves other threads, so sleep and wait for them to
 138         // called pass() or fail()
 139         try
 140         {
 141             Thread.sleep( sleepTime );
 142             //Timed out, so fail the test
 143             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 144         }
 145         catch (InterruptedException e)
 146         {
 147             //The test harness may have interrupted the test.  If so, rethrow the exception
 148             // so that the harness gets it and deals with it.
 149             if( ! testGeneratedInterrupt ) throw e;
 150 
 151             //reset flag in case hit this code more than once for some reason (just safety)
 152             testGeneratedInterrupt = false;
 153 
 154             if ( theTestPassed == false )
 155             {
 156                 throw new RuntimeException( failureMessage );
 157             }
 158         }
 159 
 160     }//main
 161 
 162     public static synchronized void setTimeoutTo( int seconds )
 163     {
 164         sleepTime = seconds * 1000;
 165     }
 166 
 167     public static synchronized void pass()
 168     {
 169         System.out.println( "The test passed." );
 170         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 171         //first check if this is executing in main thread
 172         if ( mainThread == Thread.currentThread() )
 173         {
 174             //Still in the main thread, so set the flag just for kicks,
 175             // and throw a test passed exception which will be caught
 176             // and end the test.
 177             theTestPassed = true;
 178             throw new TestPassedException();
 179         }
 180         theTestPassed = true;
 181         testGeneratedInterrupt = true;
 182         mainThread.interrupt();
 183     }//pass()
 184 
 185     public static synchronized void fail()
 186     {
 187         //test writer didn't specify why test failed, so give generic
 188         fail( "it just plain failed! :-)" );
 189     }
 190 
 191     public static synchronized void fail( String whyFailed )
 192     {
 193         System.out.println( "The test failed: " + whyFailed );
 194         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 195         //check if this called from main thread
 196         if ( mainThread == Thread.currentThread() )
 197         {
 198             //If main thread, fail now 'cause not sleeping
 199             throw new RuntimeException( whyFailed );
 200         }
 201         theTestPassed = false;
 202         testGeneratedInterrupt = true;
 203         failureMessage = whyFailed;
 204         mainThread.interrupt();
 205     }//fail()
 206 
 207 }// class ShownOffScreenOnWin98Test
 208 
 209 //This exception is used to exit from any level of call nesting
 210 // when it's determined that the test has passed, and immediately
 211 // end the test.
 212 class TestPassedException extends RuntimeException
 213 {
 214 }