1 /*
   2  * Copyright (c) 2009, 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 %W% %E%
  26   @key headful
  27   @bug 6818312
  28   @summary The size returned by SecurityWarning.getSize() should not be zero
  29   @author anthony.petrov@sun.com: area=awt.toplevel
  30   @library ../../../../java/awt/regtesthelpers
  31   @modules java.desktop/com.sun.awt
  32            java.desktop/sun.awt
  33   @build Util CustomSecurityManager CopyClassFile
  34   @run main CopyClassFile CustomSecurityManager bootcp/
  35   @run main/othervm/secure=CustomSecurityManager -Xbootclasspath/a:bootcp GetSizeShouldNotReturnZero
  36 */
  37 
  38 /**
  39  * GetSizeShouldNotReturnZero.java
  40  *
  41  * summary: The size returned by SecurityWarning.getSize() should not be zero
  42  */
  43 
  44 import com.sun.awt.SecurityWarning;
  45 import test.java.awt.regtesthelpers.Util;
  46 
  47 import java.awt.*;
  48 
  49 public class GetSizeShouldNotReturnZero
  50 {
  51     private static void init()
  52     {
  53         Frame f = new Frame();
  54         f.setSize(100, 100);
  55         f.setVisible(true);
  56 
  57         Robot robot = Util.createRobot();
  58         Util.waitForIdle(robot);
  59 
  60         Dimension size = SecurityWarning.getSize(f);
  61         if (size.width == 0 || size.height == 0) {
  62             fail("Reported security warning size: " + size);
  63             return;
  64         }
  65         pass();
  66     }//End  init()
  67 
  68 
  69     /*****************************************************
  70      * Standard Test Machinery Section
  71      * DO NOT modify anything in this section -- it's a
  72      * standard chunk of code which has all of the
  73      * synchronisation necessary for the test harness.
  74      * By keeping it the same in all tests, it is easier
  75      * to read and understand someone else's test, as
  76      * well as insuring that all tests behave correctly
  77      * with the test harness.
  78      * There is a section following this for test-
  79      * classes
  80      ******************************************************/
  81     private static boolean theTestPassed = false;
  82     private static boolean testGeneratedInterrupt = false;
  83     private static String failureMessage = "";
  84 
  85     private static Thread mainThread = null;
  86 
  87     private static int sleepTime = 300000;
  88 
  89     // Not sure about what happens if multiple of this test are
  90     //  instantiated in the same VM.  Being static (and using
  91     //  static vars), it aint gonna work.  Not worrying about
  92     //  it for now.
  93     public static void main( String args[] ) throws InterruptedException
  94     {
  95         mainThread = Thread.currentThread();
  96         try
  97         {
  98             init();
  99         }
 100         catch( TestPassedException e )
 101         {
 102             //The test passed, so just return from main and harness will
 103             // interepret this return as a pass
 104             return;
 105         }
 106         //At this point, neither test pass nor test fail has been
 107         // called -- either would have thrown an exception and ended the
 108         // test, so we know we have multiple threads.
 109 
 110         //Test involves other threads, so sleep and wait for them to
 111         // called pass() or fail()
 112         try
 113         {
 114             Thread.sleep( sleepTime );
 115             //Timed out, so fail the test
 116             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 117         }
 118         catch (InterruptedException e)
 119         {
 120             //The test harness may have interrupted the test.  If so, rethrow the exception
 121             // so that the harness gets it and deals with it.
 122             if( ! testGeneratedInterrupt ) throw e;
 123 
 124             //reset flag in case hit this code more than once for some reason (just safety)
 125             testGeneratedInterrupt = false;
 126 
 127             if ( theTestPassed == false )
 128             {
 129                 throw new RuntimeException( failureMessage );
 130             }
 131         }
 132 
 133     }//main
 134 
 135     public static synchronized void setTimeoutTo( int seconds )
 136     {
 137         sleepTime = seconds * 1000;
 138     }
 139 
 140     public static synchronized void pass()
 141     {
 142         System.out.println( "The test passed." );
 143         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 144         //first check if this is executing in main thread
 145         if ( mainThread == Thread.currentThread() )
 146         {
 147             //Still in the main thread, so set the flag just for kicks,
 148             // and throw a test passed exception which will be caught
 149             // and end the test.
 150             theTestPassed = true;
 151             throw new TestPassedException();
 152         }
 153         theTestPassed = true;
 154         testGeneratedInterrupt = true;
 155         mainThread.interrupt();
 156     }//pass()
 157 
 158     public static synchronized void fail()
 159     {
 160         //test writer didn't specify why test failed, so give generic
 161         fail( "it just plain failed! :-)" );
 162     }
 163 
 164     public static synchronized void fail( String whyFailed )
 165     {
 166         System.out.println( "The test failed: " + whyFailed );
 167         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 168         //check if this called from main thread
 169         if ( mainThread == Thread.currentThread() )
 170         {
 171             //If main thread, fail now 'cause not sleeping
 172             throw new RuntimeException( whyFailed );
 173         }
 174         theTestPassed = false;
 175         testGeneratedInterrupt = true;
 176         failureMessage = whyFailed;
 177         mainThread.interrupt();
 178     }//fail()
 179 
 180 }// class GetSizeShouldNotReturnZero
 181 
 182 //This exception is used to exit from any level of call nesting
 183 // when it's determined that the test has passed, and immediately
 184 // end the test.
 185 class TestPassedException extends RuntimeException
 186 {
 187 }