Path: blob/master/test/jdk/javax/sound/sampled/Lines/ClickInPlay/Test4218609.java
41155 views
/*1* Copyright (c) 2003, 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 421860934* @summary A soft audio click is heard when playing some audio file35* @build ClickInPlay36* @run main/manual Test421860937*/38public class Test4218609 {3940private static void init() throws Exception {41//*** Create instructions for the user here ***4243String[] instructions =44{45"To run the test follow these instructions:",46"1. Open a terminal window.",47"2. Type \"cd " + System.getProperty("test.classes") + "\".",48"3. Type \"" + System.getProperty("java.home") + "/bin/java ClickInPlay\".",49"4. Follow the instructions shown in the terminal window.",50"If no error occured during the test, and the java application ",51"in the termial exited successfully, press PASS, else press FAIL."52};5354Sysout.createDialog( );55Sysout.printInstructions( instructions );5657}5859/*****************************************************60Standard Test Machinery Section61DO NOT modify anything in this section -- it's a62standard chunk of code which has all of the63synchronisation necessary for the test harness.64By keeping it the same in all tests, it is easier65to read and understand someone else's test, as66well as insuring that all tests behave correctly67with the test harness.68There is a section following this for test-defined69classes70******************************************************/71private static boolean theTestPassed = false;72private static boolean testGeneratedInterrupt = false;73private static String failureMessage = "";7475private static Thread mainThread = null;7677private static int sleepTime = 300000;7879public static void main( String args[] ) throws Exception80{81mainThread = Thread.currentThread();82try83{84init();85}86catch( TestPassedException e )87{88//The test passed, so just return from main and harness will89// interepret this return as a pass90return;91}92//At this point, neither test passed nor test failed has been93// called -- either would have thrown an exception and ended the94// test, so we know we have multiple threads.9596//Test involves other threads, so sleep and wait for them to97// called pass() or fail()98try99{100Thread.sleep( sleepTime );101//Timed out, so fail the test102throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );103}104catch (InterruptedException e)105{106if( ! testGeneratedInterrupt ) throw e;107108//reset flag in case hit this code more than once for some reason (just safety)109testGeneratedInterrupt = false;110if ( theTestPassed == false )111{112throw new RuntimeException( failureMessage );113}114}115116}//main117118public static synchronized void setTimeoutTo( int seconds )119{120sleepTime = seconds * 1000;121}122123public static synchronized void pass()124{125Sysout.println( "The test passed." );126Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );127//first check if this is executing in main thread128if ( mainThread == Thread.currentThread() )129{130//Still in the main thread, so set the flag just for kicks,131// and throw a test passed exception which will be caught132// and end the test.133theTestPassed = true;134throw new TestPassedException();135}136//pass was called from a different thread, so set the flag and interrupt137// the main thead.138theTestPassed = true;139testGeneratedInterrupt = true;140mainThread.interrupt();141}//pass()142143public static synchronized void fail()144{145//test writer didn't specify why test failed, so give generic146fail( "it just plain failed! :-)" );147}148149public static synchronized void fail( String whyFailed )150{151Sysout.println( "The test failed: " + whyFailed );152Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );153//check if this called from main thread154if ( mainThread == Thread.currentThread() )155{156//If main thread, fail now 'cause not sleeping157throw new RuntimeException( whyFailed );158}159theTestPassed = false;160testGeneratedInterrupt = true;161failureMessage = whyFailed;162mainThread.interrupt();163}//fail()164165}// class Orient166167//This exception is used to exit from any level of call nesting168// when it's determined that the test has passed, and immediately169// end the test.170class TestPassedException extends RuntimeException171{172}173174//*********** End Standard Test Machinery Section **********175176177//************ Begin classes defined for the test ****************178179// make listeners in a class defined here, and instantiate them in init()180181/* Example of a class which may be written as part of a test182class NewClass implements anInterface183{184static int newVar = 0;185186public void eventDispatched(AWTEvent e)187{188//Counting events to see if we get enough189eventCount++;190191if( eventCount == 20 )192{193//got enough events, so pass194195Orient.pass();196}197else if( tries == 20 )198{199//tried too many times without getting enough events so fail200201Orient.fail();202}203204}// eventDispatched()205206}// NewClass class207208*/209210211//************** End classes defined for the test *******************212213214215216/****************************************************217Standard Test Machinery218DO NOT modify anything below -- it's a standard219chunk of code whose purpose is to make user220interaction uniform, and thereby make it simpler221to read and understand someone else's test.222****************************************************/223224/**225This is part of the standard test machinery.226It creates a dialog (with the instructions), and is the interface227for sending text messages to the user.228To print the instructions, send an array of strings to Sysout.createDialog229WithInstructions method. Put one line of instructions per array entry.230To display a message for the tester to see, simply call Sysout.println231with the string to be displayed.232This mimics System.out.println but works within the test harness as well233as standalone.234*/235236class Sysout237{238private static TestDialog dialog;239240public static void createDialogWithInstructions( String[] instructions )241{242dialog = new TestDialog( new Frame(), "Instructions" );243dialog.printInstructions( instructions );244dialog.show();245println( "Any messages for the tester will display here." );246}247248public static void createDialog( )249{250dialog = new TestDialog( new Frame(), "Instructions" );251String[] defInstr = { "Instructions will appear here. ", "" } ;252dialog.printInstructions( defInstr );253dialog.show();254println( "Any messages for the tester will display here." );255}256257258public static void printInstructions( String[] instructions )259{260dialog.printInstructions( instructions );261}262263264public static void println( String messageIn )265{266dialog.displayMessage( messageIn );267}268269}// Sysout class270271/**272This is part of the standard test machinery. It provides a place for the273test instructions to be displayed, and a place for interactive messages274to the user to be displayed.275To have the test instructions displayed, see Sysout.276To have a message to the user be displayed, see Sysout.277Do not call anything in this dialog directly.278*/279class TestDialog extends Dialog implements ActionListener280{281282TextArea instructionsText;283TextArea messageText;284int maxStringLength = 80;285Panel buttonP = new Panel();286Button passB = new Button( "pass" );287Button failB = new Button( "fail" );288289//DO NOT call this directly, go through Sysout290public TestDialog( Frame frame, String name )291{292super( frame, name );293int scrollBoth = TextArea.SCROLLBARS_BOTH;294instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );295add( "North", instructionsText );296297messageText = new TextArea( "", 5, maxStringLength, scrollBoth );298add("Center", messageText);299300passB = new Button( "pass" );301passB.setActionCommand( "pass" );302passB.addActionListener( this );303buttonP.add( "East", passB );304305failB = new Button( "fail" );306failB.setActionCommand( "fail" );307failB.addActionListener( this );308buttonP.add( "West", failB );309310add( "South", buttonP );311pack();312313show();314}// TestDialog()315316//DO NOT call this directly, go through Sysout317public void printInstructions( String[] instructions )318{319//Clear out any current instructions320instructionsText.setText( "" );321322//Go down array of instruction strings323324String printStr, remainingStr;325for( int i=0; i < instructions.length; i++ )326{327//chop up each into pieces maxSringLength long328remainingStr = instructions[ i ];329while( remainingStr.length() > 0 )330{331//if longer than max then chop off first max chars to print332if( remainingStr.length() >= maxStringLength )333{334//Try to chop on a word boundary335int posOfSpace = remainingStr.336lastIndexOf( ' ', maxStringLength - 1 );337338if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;339340printStr = remainingStr.substring( 0, posOfSpace + 1 );341remainingStr = remainingStr.substring( posOfSpace + 1 );342}343//else just print344else345{346printStr = remainingStr;347remainingStr = "";348}349350instructionsText.append( printStr + "\n" );351352}// while353354}// for355356}//printInstructions()357358//DO NOT call this directly, go through Sysout359public void displayMessage( String messageIn )360{361messageText.append( messageIn + "\n" );362}363364//catch presses of the passed and failed buttons.365//simply call the standard pass() or fail() static methods of366//DialogOrient367public void actionPerformed( ActionEvent e )368{369if( e.getActionCommand() == "pass" )370{371Test4218609.pass();372}373else374{375Test4218609.fail();376}377}378379}// TestDialog class380381382