jdk/test/com/sun/awt/SecurityWarning/GetSizeShouldNotReturnZero.java
changeset 2476 95a94517f011
child 5506 202f599c92aa
equal deleted inserted replaced
2475:a55ae5159045 2476:95a94517f011
       
     1 /*
       
     2  * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25   @test %W% %E%
       
    26   @bug 6818312
       
    27   @summary The size returned by SecurityWarning.getSize() should not be zero
       
    28   @author anthony.petrov@sun.com: area=awt.toplevel
       
    29   @library ../../../../java/awt/regtesthelpers
       
    30   @build Util
       
    31   @run main GetSizeShouldNotReturnZero
       
    32 */
       
    33 
       
    34 /**
       
    35  * GetSizeShouldNotReturnZero.java
       
    36  *
       
    37  * summary: The size returned by SecurityWarning.getSize() should not be zero
       
    38  */
       
    39 
       
    40 import java.awt.*;
       
    41 import java.awt.event.*;
       
    42 import java.security.Permission;
       
    43 import test.java.awt.regtesthelpers.Util;
       
    44 import com.sun.awt.SecurityWarning;
       
    45 
       
    46 public class GetSizeShouldNotReturnZero
       
    47 {
       
    48     private static void init()
       
    49     {
       
    50         String[] instructions =
       
    51         {
       
    52             "This is an AUTOMATIC test, simply wait until it is done.",
       
    53             "The result (passed or failed) will be shown in the",
       
    54             "message window below."
       
    55         };
       
    56         Sysout.createDialog( );
       
    57         Sysout.printInstructions( instructions );
       
    58 
       
    59 
       
    60         // Install the security manager so that all subsequently created
       
    61         // windows display the security warning.
       
    62         System.setSecurityManager(new SecurityManager() {
       
    63 
       
    64             @Override
       
    65             public void checkPermission(Permission perm) {
       
    66             }
       
    67 
       
    68             @Override
       
    69             public boolean checkTopLevelWindow(Object window) {
       
    70                 return false;
       
    71             }
       
    72         });
       
    73 
       
    74         Frame f = new Frame();
       
    75         f.setSize(100, 100);
       
    76         f.setVisible(true);
       
    77 
       
    78         Robot robot = Util.createRobot();
       
    79         Util.waitForIdle(robot);
       
    80 
       
    81         Dimension size = SecurityWarning.getSize(f);
       
    82         if (size.width == 0 || size.height == 0) {
       
    83             fail("Reported security warning size: " + size);
       
    84             return;
       
    85         }
       
    86         pass();
       
    87     }//End  init()
       
    88 
       
    89 
       
    90 
       
    91     /*****************************************************
       
    92      * Standard Test Machinery Section
       
    93      * DO NOT modify anything in this section -- it's a
       
    94      * standard chunk of code which has all of the
       
    95      * synchronisation necessary for the test harness.
       
    96      * By keeping it the same in all tests, it is easier
       
    97      * to read and understand someone else's test, as
       
    98      * well as insuring that all tests behave correctly
       
    99      * with the test harness.
       
   100      * There is a section following this for test-
       
   101      * classes
       
   102      ******************************************************/
       
   103     private static boolean theTestPassed = false;
       
   104     private static boolean testGeneratedInterrupt = false;
       
   105     private static String failureMessage = "";
       
   106 
       
   107     private static Thread mainThread = null;
       
   108 
       
   109     private static int sleepTime = 300000;
       
   110 
       
   111     // Not sure about what happens if multiple of this test are
       
   112     //  instantiated in the same VM.  Being static (and using
       
   113     //  static vars), it aint gonna work.  Not worrying about
       
   114     //  it for now.
       
   115     public static void main( String args[] ) throws InterruptedException
       
   116     {
       
   117         mainThread = Thread.currentThread();
       
   118         try
       
   119         {
       
   120             init();
       
   121         }
       
   122         catch( TestPassedException e )
       
   123         {
       
   124             //The test passed, so just return from main and harness will
       
   125             // interepret this return as a pass
       
   126             return;
       
   127         }
       
   128         //At this point, neither test pass nor test fail has been
       
   129         // called -- either would have thrown an exception and ended the
       
   130         // test, so we know we have multiple threads.
       
   131 
       
   132         //Test involves other threads, so sleep and wait for them to
       
   133         // called pass() or fail()
       
   134         try
       
   135         {
       
   136             Thread.sleep( sleepTime );
       
   137             //Timed out, so fail the test
       
   138             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
       
   139         }
       
   140         catch (InterruptedException e)
       
   141         {
       
   142             //The test harness may have interrupted the test.  If so, rethrow the exception
       
   143             // so that the harness gets it and deals with it.
       
   144             if( ! testGeneratedInterrupt ) throw e;
       
   145 
       
   146             //reset flag in case hit this code more than once for some reason (just safety)
       
   147             testGeneratedInterrupt = false;
       
   148 
       
   149             if ( theTestPassed == false )
       
   150             {
       
   151                 throw new RuntimeException( failureMessage );
       
   152             }
       
   153         }
       
   154 
       
   155     }//main
       
   156 
       
   157     public static synchronized void setTimeoutTo( int seconds )
       
   158     {
       
   159         sleepTime = seconds * 1000;
       
   160     }
       
   161 
       
   162     public static synchronized void pass()
       
   163     {
       
   164         Sysout.println( "The test passed." );
       
   165         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
       
   166         //first check if this is executing in main thread
       
   167         if ( mainThread == Thread.currentThread() )
       
   168         {
       
   169             //Still in the main thread, so set the flag just for kicks,
       
   170             // and throw a test passed exception which will be caught
       
   171             // and end the test.
       
   172             theTestPassed = true;
       
   173             throw new TestPassedException();
       
   174         }
       
   175         theTestPassed = true;
       
   176         testGeneratedInterrupt = true;
       
   177         mainThread.interrupt();
       
   178     }//pass()
       
   179 
       
   180     public static synchronized void fail()
       
   181     {
       
   182         //test writer didn't specify why test failed, so give generic
       
   183         fail( "it just plain failed! :-)" );
       
   184     }
       
   185 
       
   186     public static synchronized void fail( String whyFailed )
       
   187     {
       
   188         Sysout.println( "The test failed: " + whyFailed );
       
   189         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
       
   190         //check if this called from main thread
       
   191         if ( mainThread == Thread.currentThread() )
       
   192         {
       
   193             //If main thread, fail now 'cause not sleeping
       
   194             throw new RuntimeException( whyFailed );
       
   195         }
       
   196         theTestPassed = false;
       
   197         testGeneratedInterrupt = true;
       
   198         failureMessage = whyFailed;
       
   199         mainThread.interrupt();
       
   200     }//fail()
       
   201 
       
   202 }// class GetSizeShouldNotReturnZero
       
   203 
       
   204 //This exception is used to exit from any level of call nesting
       
   205 // when it's determined that the test has passed, and immediately
       
   206 // end the test.
       
   207 class TestPassedException extends RuntimeException
       
   208 {
       
   209 }
       
   210 
       
   211 //*********** End Standard Test Machinery Section **********
       
   212 
       
   213 
       
   214 //************ Begin classes defined for the test ****************
       
   215 
       
   216 // if want to make listeners, here is the recommended place for them, then instantiate
       
   217 //  them in init()
       
   218 
       
   219 /* Example of a class which may be written as part of a test
       
   220 class NewClass implements anInterface
       
   221  {
       
   222    static int newVar = 0;
       
   223 
       
   224    public void eventDispatched(AWTEvent e)
       
   225     {
       
   226       //Counting events to see if we get enough
       
   227       eventCount++;
       
   228 
       
   229       if( eventCount == 20 )
       
   230        {
       
   231          //got enough events, so pass
       
   232 
       
   233          GetSizeShouldNotReturnZero.pass();
       
   234        }
       
   235       else if( tries == 20 )
       
   236        {
       
   237          //tried too many times without getting enough events so fail
       
   238 
       
   239          GetSizeShouldNotReturnZero.fail();
       
   240        }
       
   241 
       
   242     }// eventDispatched()
       
   243 
       
   244  }// NewClass class
       
   245 
       
   246 */
       
   247 
       
   248 
       
   249 //************** End classes defined for the test *******************
       
   250 
       
   251 
       
   252 
       
   253 
       
   254 /****************************************************
       
   255  Standard Test Machinery
       
   256  DO NOT modify anything below -- it's a standard
       
   257   chunk of code whose purpose is to make user
       
   258   interaction uniform, and thereby make it simpler
       
   259   to read and understand someone else's test.
       
   260  ****************************************************/
       
   261 
       
   262 /**
       
   263  This is part of the standard test machinery.
       
   264  It creates a dialog (with the instructions), and is the interface
       
   265   for sending text messages to the user.
       
   266  To print the instructions, send an array of strings to Sysout.createDialog
       
   267   WithInstructions method.  Put one line of instructions per array entry.
       
   268  To display a message for the tester to see, simply call Sysout.println
       
   269   with the string to be displayed.
       
   270  This mimics System.out.println but works within the test harness as well
       
   271   as standalone.
       
   272  */
       
   273 
       
   274 class Sysout
       
   275 {
       
   276     private static TestDialog dialog;
       
   277 
       
   278     public static void createDialogWithInstructions( String[] instructions )
       
   279     {
       
   280         dialog = new TestDialog( new Frame(), "Instructions" );
       
   281         dialog.printInstructions( instructions );
       
   282         dialog.setVisible(true);
       
   283         println( "Any messages for the tester will display here." );
       
   284     }
       
   285 
       
   286     public static void createDialog( )
       
   287     {
       
   288         dialog = new TestDialog( new Frame(), "Instructions" );
       
   289         String[] defInstr = { "Instructions will appear here. ", "" } ;
       
   290         dialog.printInstructions( defInstr );
       
   291         dialog.setVisible(true);
       
   292         println( "Any messages for the tester will display here." );
       
   293     }
       
   294 
       
   295 
       
   296     public static void printInstructions( String[] instructions )
       
   297     {
       
   298         dialog.printInstructions( instructions );
       
   299     }
       
   300 
       
   301 
       
   302     public static void println( String messageIn )
       
   303     {
       
   304         dialog.displayMessage( messageIn );
       
   305         System.out.println(messageIn);
       
   306     }
       
   307 
       
   308 }// Sysout  class
       
   309 
       
   310 /**
       
   311   This is part of the standard test machinery.  It provides a place for the
       
   312    test instructions to be displayed, and a place for interactive messages
       
   313    to the user to be displayed.
       
   314   To have the test instructions displayed, see Sysout.
       
   315   To have a message to the user be displayed, see Sysout.
       
   316   Do not call anything in this dialog directly.
       
   317   */
       
   318 class TestDialog extends Dialog
       
   319 {
       
   320 
       
   321     TextArea instructionsText;
       
   322     TextArea messageText;
       
   323     int maxStringLength = 80;
       
   324 
       
   325     //DO NOT call this directly, go through Sysout
       
   326     public TestDialog( Frame frame, String name )
       
   327     {
       
   328         super( frame, name );
       
   329         int scrollBoth = TextArea.SCROLLBARS_BOTH;
       
   330         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
       
   331         add( "North", instructionsText );
       
   332 
       
   333         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
       
   334         add("Center", messageText);
       
   335 
       
   336         pack();
       
   337 
       
   338         setVisible(true);
       
   339     }// TestDialog()
       
   340 
       
   341     //DO NOT call this directly, go through Sysout
       
   342     public void printInstructions( String[] instructions )
       
   343     {
       
   344         //Clear out any current instructions
       
   345         instructionsText.setText( "" );
       
   346 
       
   347         //Go down array of instruction strings
       
   348 
       
   349         String printStr, remainingStr;
       
   350         for( int i=0; i < instructions.length; i++ )
       
   351         {
       
   352             //chop up each into pieces maxSringLength long
       
   353             remainingStr = instructions[ i ];
       
   354             while( remainingStr.length() > 0 )
       
   355             {
       
   356                 //if longer than max then chop off first max chars to print
       
   357                 if( remainingStr.length() >= maxStringLength )
       
   358                 {
       
   359                     //Try to chop on a word boundary
       
   360                     int posOfSpace = remainingStr.
       
   361                         lastIndexOf( ' ', maxStringLength - 1 );
       
   362 
       
   363                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
       
   364 
       
   365                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
       
   366                     remainingStr = remainingStr.substring( posOfSpace + 1 );
       
   367                 }
       
   368                 //else just print
       
   369                 else
       
   370                 {
       
   371                     printStr = remainingStr;
       
   372                     remainingStr = "";
       
   373                 }
       
   374 
       
   375                 instructionsText.append( printStr + "\n" );
       
   376 
       
   377             }// while
       
   378 
       
   379         }// for
       
   380 
       
   381     }//printInstructions()
       
   382 
       
   383     //DO NOT call this directly, go through Sysout
       
   384     public void displayMessage( String messageIn )
       
   385     {
       
   386         messageText.append( messageIn + "\n" );
       
   387         System.out.println(messageIn);
       
   388     }
       
   389 
       
   390 }// TestDialog  class
       
   391 
       
   392