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