Path: blob/master/test/jdk/javax/sound/sampled/Mixers/DirectSoundRepeatingBuffer/Test4997635.java
41161 views
/*1* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Button;24import java.awt.Dialog;25import java.awt.Frame;26import java.awt.Panel;27import java.awt.TextArea;28import java.awt.event.ActionEvent;29import java.awt.event.ActionListener;3031/**32* @test33* @bug 499763534* @summary Win: SourceDataLine playback loops endlessly unless you manually35* stop()36* @build DirectSoundRepeatingBuffer37* @run main/manual Test499763538*/39public class Test4997635 {4041private static void init() throws Exception {42//*** Create instructions for the user here ***4344String[] 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 DirectSoundRepeatingBuffer\".",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};5455Sysout.createDialog( );56Sysout.printInstructions( instructions );5758}5960/*****************************************************61Standard Test Machinery Section62DO NOT modify anything in this section -- it's a63standard chunk of code which has all of the64synchronisation necessary for the test harness.65By keeping it the same in all tests, it is easier66to read and understand someone else's test, as67well as insuring that all tests behave correctly68with the test harness.69There is a section following this for test-defined70classes71******************************************************/72private static boolean theTestPassed = false;73private static boolean testGeneratedInterrupt = false;74private static String failureMessage = "";7576private static Thread mainThread = null;7778private static int sleepTime = 300000;7980public static void main( String args[] ) throws Exception81{82mainThread = Thread.currentThread();83try84{85init();86}87catch( TestPassedException e )88{89//The test passed, so just return from main and harness will90// interepret this return as a pass91return;92}93//At this point, neither test passed nor test failed has been94// called -- either would have thrown an exception and ended the95// test, so we know we have multiple threads.9697//Test involves other threads, so sleep and wait for them to98// called pass() or fail()99try100{101Thread.sleep( sleepTime );102//Timed out, so fail the test103throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );104}105catch (InterruptedException e)106{107if( ! testGeneratedInterrupt ) throw e;108109//reset flag in case hit this code more than once for some reason (just safety)110testGeneratedInterrupt = false;111if ( theTestPassed == false )112{113throw new RuntimeException( failureMessage );114}115}116117}//main118119public static synchronized void setTimeoutTo( int seconds )120{121sleepTime = seconds * 1000;122}123124public static synchronized void pass()125{126Sysout.println( "The test passed." );127Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );128//first check if this is executing in main thread129if ( 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 caught133// and end the test.134theTestPassed = true;135throw new TestPassedException();136}137//pass was called from a different thread, so set the flag and interrupt138// the main thead.139theTestPassed = true;140testGeneratedInterrupt = true;141mainThread.interrupt();142}//pass()143144public static synchronized void fail()145{146//test writer didn't specify why test failed, so give generic147fail( "it just plain failed! :-)" );148}149150public static synchronized void fail( String whyFailed )151{152Sysout.println( "The test failed: " + whyFailed );153Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );154//check if this called from main thread155if ( mainThread == Thread.currentThread() )156{157//If main thread, fail now 'cause not sleeping158throw new RuntimeException( whyFailed );159}160theTestPassed = false;161testGeneratedInterrupt = true;162failureMessage = whyFailed;163mainThread.interrupt();164}//fail()165166}// class Orient167168//This exception is used to exit from any level of call nesting169// when it's determined that the test has passed, and immediately170// end the test.171class TestPassedException extends RuntimeException172{173}174175//*********** End Standard Test Machinery Section **********176177178//************ Begin classes defined for the test ****************179180// make listeners in a class defined here, and instantiate them in init()181182/* Example of a class which may be written as part of a test183class NewClass implements anInterface184{185static int newVar = 0;186187public void eventDispatched(AWTEvent e)188{189//Counting events to see if we get enough190eventCount++;191192if( eventCount == 20 )193{194//got enough events, so pass195196Orient.pass();197}198else if( tries == 20 )199{200//tried too many times without getting enough events so fail201202Orient.fail();203}204205}// eventDispatched()206207}// NewClass class208209*/210211212//************** End classes defined for the test *******************213214215216217/****************************************************218Standard Test Machinery219DO NOT modify anything below -- it's a standard220chunk of code whose purpose is to make user221interaction uniform, and thereby make it simpler222to read and understand someone else's test.223****************************************************/224225/**226This is part of the standard test machinery.227It creates a dialog (with the instructions), and is the interface228for sending text messages to the user.229To print the instructions, send an array of strings to Sysout.createDialog230WithInstructions method. Put one line of instructions per array entry.231To display a message for the tester to see, simply call Sysout.println232with the string to be displayed.233This mimics System.out.println but works within the test harness as well234as standalone.235*/236237class Sysout238{239private static TestDialog dialog;240241public static void createDialogWithInstructions( String[] instructions )242{243dialog = new TestDialog( new Frame(), "Instructions" );244dialog.printInstructions( instructions );245dialog.show();246println( "Any messages for the tester will display here." );247}248249public static void createDialog( )250{251dialog = new TestDialog( new Frame(), "Instructions" );252String[] defInstr = { "Instructions will appear here. ", "" } ;253dialog.printInstructions( defInstr );254dialog.show();255println( "Any messages for the tester will display here." );256}257258259public static void printInstructions( String[] instructions )260{261dialog.printInstructions( instructions );262}263264265public static void println( String messageIn )266{267dialog.displayMessage( messageIn );268}269270}// Sysout class271272/**273This is part of the standard test machinery. It provides a place for the274test instructions to be displayed, and a place for interactive messages275to the user to be displayed.276To have the test instructions displayed, see Sysout.277To have a message to the user be displayed, see Sysout.278Do not call anything in this dialog directly.279*/280class TestDialog extends Dialog implements ActionListener281{282283TextArea instructionsText;284TextArea messageText;285int maxStringLength = 80;286Panel buttonP = new Panel();287Button passB = new Button( "pass" );288Button failB = new Button( "fail" );289290//DO NOT call this directly, go through Sysout291public TestDialog( Frame frame, String name )292{293super( frame, name );294int scrollBoth = TextArea.SCROLLBARS_BOTH;295instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );296add( "North", instructionsText );297298messageText = new TextArea( "", 5, maxStringLength, scrollBoth );299add("Center", messageText);300301passB = new Button( "pass" );302passB.setActionCommand( "pass" );303passB.addActionListener( this );304buttonP.add( "East", passB );305306failB = new Button( "fail" );307failB.setActionCommand( "fail" );308failB.addActionListener( this );309buttonP.add( "West", failB );310311add( "South", buttonP );312pack();313314show();315}// TestDialog()316317//DO NOT call this directly, go through Sysout318public void printInstructions( String[] instructions )319{320//Clear out any current instructions321instructionsText.setText( "" );322323//Go down array of instruction strings324325String printStr, remainingStr;326for( int i=0; i < instructions.length; i++ )327{328//chop up each into pieces maxSringLength long329remainingStr = instructions[ i ];330while( remainingStr.length() > 0 )331{332//if longer than max then chop off first max chars to print333if( remainingStr.length() >= maxStringLength )334{335//Try to chop on a word boundary336int posOfSpace = remainingStr.337lastIndexOf( ' ', maxStringLength - 1 );338339if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;340341printStr = remainingStr.substring( 0, posOfSpace + 1 );342remainingStr = remainingStr.substring( posOfSpace + 1 );343}344//else just print345else346{347printStr = remainingStr;348remainingStr = "";349}350351instructionsText.append( printStr + "\n" );352353}// while354355}// for356357}//printInstructions()358359//DO NOT call this directly, go through Sysout360public void displayMessage( String messageIn )361{362messageText.append( messageIn + "\n" );363}364365//catch presses of the passed and failed buttons.366//simply call the standard pass() or fail() static methods of367//DialogOrient368public void actionPerformed( ActionEvent e )369{370if( e.getActionCommand() == "pass" )371{372Test4997635.pass();373}374else375{376Test4997635.fail();377}378}379380}// TestDialog class381382383