< prev index next >

test/jdk/java/awt/Focus/6382144/EndlessLoopTest.java

Print this page


   1 /*
   2  * Copyright (c) 2006, 2016, 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  */


  52 import java.awt.event.KeyEvent;
  53 
  54 import javax.swing.InputVerifier;
  55 import javax.swing.JButton;
  56 import javax.swing.JComponent;
  57 import javax.swing.JDialog;
  58 import javax.swing.JFrame;
  59 import javax.swing.JTextField;
  60 
  61 public class EndlessLoopTest
  62 {
  63 
  64     //*** test-writer defined static variables go here ***
  65     static volatile int n_iv_calls;
  66 
  67 
  68     private static void init()
  69     {
  70         //*** Create instructions for the user here ***
  71 
  72         String[] instructions =
  73         {
  74             "This is an AUTOMATIC test, simply wait until it is done.",
  75             "The result (passed or failed) will be shown in the",
  76             "message window below."
  77         };
  78         Sysout.createDialog( );
  79         Sysout.printInstructions( instructions );
  80 
  81         JFrame frame = new JFrame();
  82         final JDialog dialog = new JDialog(frame, true);
  83         JButton button = new JButton("press me");
  84         button.addActionListener(new ActionListener() {
  85                 public void actionPerformed(ActionEvent ae) {
  86                     dialog.dispose();
  87                 }
  88             });
  89         dialog.getContentPane().add(button);
  90         dialog.pack();
  91 
  92         JTextField t1 = new JTextField();
  93         t1.setInputVerifier(new InputVerifier() {
  94             public boolean verify(JComponent input) {
  95                 n_iv_calls++;
  96                 if (n_iv_calls == 1) {
  97                     dialog.setVisible(true);
  98                 }
  99                 return true;
 100             }


 211             if( ! testGeneratedInterrupt ) throw e;
 212 
 213             //reset flag in case hit this code more than once for some reason (just safety)
 214             testGeneratedInterrupt = false;
 215 
 216             if ( theTestPassed == false )
 217             {
 218                 throw new RuntimeException( failureMessage );
 219             }
 220         }
 221 
 222     }//main
 223 
 224     public static synchronized void setTimeoutTo( int seconds )
 225     {
 226         sleepTime = seconds * 1000;
 227     }
 228 
 229     public static synchronized void pass()
 230     {
 231         Sysout.println( "The test passed." );
 232         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 233         //first check if this is executing in main thread
 234         if ( mainThread == Thread.currentThread() )
 235         {
 236             //Still in the main thread, so set the flag just for kicks,
 237             // and throw a test passed exception which will be caught
 238             // and end the test.
 239             theTestPassed = true;
 240             throw new TestPassedException();
 241         }
 242         theTestPassed = true;
 243         testGeneratedInterrupt = true;
 244         mainThread.interrupt();
 245     }//pass()
 246 
 247     public static synchronized void fail( Exception whyFailed )
 248     {
 249         Sysout.println( "The test failed: " + whyFailed );
 250         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 251         //check if this called from main thread
 252         if ( mainThread == Thread.currentThread() )
 253         {
 254             //If main thread, fail now 'cause not sleeping
 255             throw new RuntimeException( whyFailed );
 256         }
 257         theTestPassed = false;
 258         testGeneratedInterrupt = true;
 259         failureMessage = whyFailed.toString();
 260         mainThread.interrupt();
 261     }//fail()
 262 
 263 }// class EndlessLoopTest
 264 
 265 //This exception is used to exit from any level of call nesting
 266 // when it's determined that the test has passed, and immediately
 267 // end the test.
 268 class TestPassedException extends RuntimeException
 269 {
 270 }
 271 
 272 //*********** End Standard Test Machinery Section **********
 273 
 274 /****************************************************
 275  Standard Test Machinery
 276  DO NOT modify anything below -- it's a standard
 277   chunk of code whose purpose is to make user
 278   interaction uniform, and thereby make it simpler
 279   to read and understand someone else's test.
 280  ****************************************************/
 281 
 282 /**
 283  This is part of the standard test machinery.
 284  It creates a dialog (with the instructions), and is the interface
 285   for sending text messages to the user.
 286  To print the instructions, send an array of strings to Sysout.createDialog
 287   WithInstructions method.  Put one line of instructions per array entry.
 288  To display a message for the tester to see, simply call Sysout.println
 289   with the string to be displayed.
 290  This mimics System.out.println but works within the test harness as well
 291   as standalone.
 292  */
 293 
 294 class Sysout
 295 {
 296     private static TestDialog dialog;
 297 
 298     public static void createDialogWithInstructions( String[] instructions )
 299     {
 300         dialog = new TestDialog( new Frame(), "Instructions" );
 301         dialog.printInstructions( instructions );
 302         dialog.setVisible(true);
 303         println( "Any messages for the tester will display here." );
 304     }
 305 
 306     public static void createDialog( )
 307     {
 308         dialog = new TestDialog( new Frame(), "Instructions" );
 309         String[] defInstr = { "Instructions will appear here. ", "" } ;
 310         dialog.printInstructions( defInstr );
 311         dialog.setVisible(true);
 312         println( "Any messages for the tester will display here." );
 313     }
 314 
 315 
 316     public static void printInstructions( String[] instructions )
 317     {
 318         dialog.printInstructions( instructions );
 319     }
 320 
 321 
 322     public static void println( String messageIn )
 323     {
 324         dialog.displayMessage( messageIn );
 325         System.out.println(messageIn);
 326     }
 327 
 328 }// Sysout  class
 329 
 330 /**
 331   This is part of the standard test machinery.  It provides a place for the
 332    test instructions to be displayed, and a place for interactive messages
 333    to the user to be displayed.
 334   To have the test instructions displayed, see Sysout.
 335   To have a message to the user be displayed, see Sysout.
 336   Do not call anything in this dialog directly.
 337   */
 338 class TestDialog extends Dialog
 339 {
 340 
 341     TextArea instructionsText;
 342     TextArea messageText;
 343     int maxStringLength = 80;
 344 
 345     //DO NOT call this directly, go through Sysout
 346     public TestDialog( Frame frame, String name )
 347     {
 348         super( frame, name );
 349         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 350         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 351         add( "North", instructionsText );
 352 
 353         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 354         add("Center", messageText);
 355 
 356         pack();
 357 
 358         setVisible(true);
 359     }// TestDialog()
 360 
 361     //DO NOT call this directly, go through Sysout
 362     public void printInstructions( String[] instructions )
 363     {
 364         //Clear out any current instructions
 365         instructionsText.setText( "" );
 366 
 367         //Go down array of instruction strings
 368 
 369         String printStr, remainingStr;
 370         for( int i=0; i < instructions.length; i++ )
 371         {
 372             //chop up each into pieces maxSringLength long
 373             remainingStr = instructions[ i ];
 374             while( remainingStr.length() > 0 )
 375             {
 376                 //if longer than max then chop off first max chars to print
 377                 if( remainingStr.length() >= maxStringLength )
 378                 {
 379                     //Try to chop on a word boundary
 380                     int posOfSpace = remainingStr.
 381                         lastIndexOf( ' ', maxStringLength - 1 );
 382 
 383                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 384 
 385                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 386                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 387                 }
 388                 //else just print
 389                 else
 390                 {
 391                     printStr = remainingStr;
 392                     remainingStr = "";
 393                 }
 394 
 395                 instructionsText.append( printStr + "\n" );
 396 
 397             }// while
 398 
 399         }// for
 400 
 401     }//printInstructions()
 402 
 403     //DO NOT call this directly, go through Sysout
 404     public void displayMessage( String messageIn )
 405     {
 406         messageText.append( messageIn + "\n" );
 407         System.out.println(messageIn);
 408     }
 409 
 410 }// TestDialog  class
   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  */


  52 import java.awt.event.KeyEvent;
  53 
  54 import javax.swing.InputVerifier;
  55 import javax.swing.JButton;
  56 import javax.swing.JComponent;
  57 import javax.swing.JDialog;
  58 import javax.swing.JFrame;
  59 import javax.swing.JTextField;
  60 
  61 public class EndlessLoopTest
  62 {
  63 
  64     //*** test-writer defined static variables go here ***
  65     static volatile int n_iv_calls;
  66 
  67 
  68     private static void init()
  69     {
  70         //*** Create instructions for the user here ***
  71 









  72         JFrame frame = new JFrame();
  73         final JDialog dialog = new JDialog(frame, true);
  74         JButton button = new JButton("press me");
  75         button.addActionListener(new ActionListener() {
  76                 public void actionPerformed(ActionEvent ae) {
  77                     dialog.dispose();
  78                 }
  79             });
  80         dialog.getContentPane().add(button);
  81         dialog.pack();
  82 
  83         JTextField t1 = new JTextField();
  84         t1.setInputVerifier(new InputVerifier() {
  85             public boolean verify(JComponent input) {
  86                 n_iv_calls++;
  87                 if (n_iv_calls == 1) {
  88                     dialog.setVisible(true);
  89                 }
  90                 return true;
  91             }


 202             if( ! testGeneratedInterrupt ) throw e;
 203 
 204             //reset flag in case hit this code more than once for some reason (just safety)
 205             testGeneratedInterrupt = false;
 206 
 207             if ( theTestPassed == false )
 208             {
 209                 throw new RuntimeException( failureMessage );
 210             }
 211         }
 212 
 213     }//main
 214 
 215     public static synchronized void setTimeoutTo( int seconds )
 216     {
 217         sleepTime = seconds * 1000;
 218     }
 219 
 220     public static synchronized void pass()
 221     {
 222         System.out.println( "The test passed." );
 223         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 224         //first check if this is executing in main thread
 225         if ( mainThread == Thread.currentThread() )
 226         {
 227             //Still in the main thread, so set the flag just for kicks,
 228             // and throw a test passed exception which will be caught
 229             // and end the test.
 230             theTestPassed = true;
 231             throw new TestPassedException();
 232         }
 233         theTestPassed = true;
 234         testGeneratedInterrupt = true;
 235         mainThread.interrupt();
 236     }//pass()
 237 
 238     public static synchronized void fail( Exception whyFailed )
 239     {
 240         System.out.println( "The test failed: " + whyFailed );
 241         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 242         //check if this called from main thread
 243         if ( mainThread == Thread.currentThread() )
 244         {
 245             //If main thread, fail now 'cause not sleeping
 246             throw new RuntimeException( whyFailed );
 247         }
 248         theTestPassed = false;
 249         testGeneratedInterrupt = true;
 250         failureMessage = whyFailed.toString();
 251         mainThread.interrupt();
 252     }//fail()
 253 
 254 }// class EndlessLoopTest
 255 
 256 //This exception is used to exit from any level of call nesting
 257 // when it's determined that the test has passed, and immediately
 258 // end the test.
 259 class TestPassedException extends RuntimeException
 260 {
 261 }












































































































































< prev index next >