FileDocCategorySizeDatePackage
MediaRecorderStateUnitTestTemplate.javaAPI DocAndroid 1.5 API13350Wed May 06 22:42:00 BST 2009com.android.mediaframeworktest.unit

MediaRecorderStateUnitTestTemplate

public class MediaRecorderStateUnitTestTemplate extends android.test.AndroidTestCase
A template class for running a method under test in all possible states of a MediaRecorder object.
see
com.android.mediaframeworktest.unit.MediaRecorderStopStateUnitTest for an example of using this class. A typical concrete unit test class would implement the MediaRecorderMethodUnderTest interface and have a reference to an object of this class. Then it calls runTestOnMethod() to actually perform the unit tests. It is recommended that the toString() method of the concrete unit test class be overridden to use the actual method name under test for logging purpose.

Fields Summary
public static final String
RECORD_OUTPUT_PATH
public static final int
OUTPUT_FORMAT
public static final int
AUDIO_ENCODER
public static final int
AUDIO_SOURCE
private static final String
TAG
private MediaRecorderStateErrors
mStateErrors
private android.media.MediaRecorder
mMediaRecorder
private MediaRecorderStateErrors.MediaRecorderState
mMediaRecorderState
private MediaRecorderMethodUnderTest
mMethodUnderTest
Constructors Summary
Methods Summary
private voidcallMediaRecorderMethodUnderTestInState(MediaRecorderStateErrors.MediaRecorderState state)

        Log.v(TAG, "call " + mMethodUnderTest + ": started in state " + state);
        setMediaRecorderToState(state);
        try {
            mMethodUnderTest.invokeMethodUnderTest(mMediaRecorder);
        } catch(Exception e) {
            setStateError(mMediaRecorderState, true);
        }
        Log.v(TAG, "call " + mMethodUnderTest + ": ended in state " + state);
    
private voidcheckDataSourceConfiguredState()

        callMediaRecorderMethodUnderTestInState(MediaRecorderStateErrors.MediaRecorderState.DATASOURCECONFIGURED);
    
private voidcheckErrorState()

        callMediaRecorderMethodUnderTestInState(MediaRecorderStateErrors.MediaRecorderState.ERROR);
    
private voidcheckInitialState()

        callMediaRecorderMethodUnderTestInState(MediaRecorderStateErrors.MediaRecorderState.INITIAL);
    
private voidcheckInitialStateAfterReset()

        callMediaRecorderMethodUnderTestInState(MediaRecorderStateErrors.MediaRecorderState.INITIAL_AFTER_RESET);
    
private voidcheckInitialStateAfterStop()

        callMediaRecorderMethodUnderTestInState(MediaRecorderStateErrors.MediaRecorderState.INITIAL_AFTER_STOP);
    
private voidcheckInitializedState()

        callMediaRecorderMethodUnderTestInState(MediaRecorderStateErrors.MediaRecorderState.INITIALIZED);
    
private voidcheckMethodUnderTestInAllPossibleStates()

        // Must be called first.
        checkInitialState(); 
        
        // The sequence of the following method calls should not 
        // affect the test results.
        checkErrorState();
        checkInitialStateAfterReset();
        checkInitialStateAfterStop();
        checkInitializedState();
        checkRecordingState();
        checkDataSourceConfiguredState();
        checkPreparedState();
    
private voidcheckPreparedState()

        callMediaRecorderMethodUnderTestInState(MediaRecorderStateErrors.MediaRecorderState.PREPARED);
    
private voidcheckRecordingState()

        callMediaRecorderMethodUnderTestInState(MediaRecorderStateErrors.MediaRecorderState.RECORDING);
    
private voidcleanUp()

        mMediaRecorder.release();
        mMediaRecorder = null;
        mMediaRecorderState = null;
        mStateErrors = null;
        mMethodUnderTest = null;
    
public voidrunTestOnMethod(MediaRecorderMethodUnderTest testMethod)
Runs the given method under test in all possible states of a MediaRecorder object.

param
testMethod the method under test.

    
                              
        
        mMethodUnderTest = testMethod;
        if (mMethodUnderTest != null) {  // Method under test has been set?
            checkMethodUnderTestInAllPossibleStates();
            mMethodUnderTest.checkStateErrors(mStateErrors);
            cleanUp();
        }
    
private voidsetMediaRecorderToDataSourceConfiguredState()

        try {
            mMediaRecorder.reset();
            mMediaRecorder.setAudioSource(AUDIO_SOURCE);
            mMediaRecorder.setOutputFormat(OUTPUT_FORMAT);
            
            /* Skip setAudioEncoder() and setOutputFile() calls if
             * the method under test is setAudioEncoder() since this
             * method can only be called once even in the DATASOURCECONFIGURED state
             */
            if (mMethodUnderTest.toString() != "setAudioEncoder()") {
                mMediaRecorder.setAudioEncoder(AUDIO_ENCODER);
            }
            
            if (mMethodUnderTest.toString() != "setOutputFile()") {
                mMediaRecorder.setOutputFile(RECORD_OUTPUT_PATH);
            }
        } catch(Exception e) {
            fail("setMediaRecorderToDataSourceConfiguredState: Exception " + e.getClass().getName() + " was thrown.");
        }
    
private voidsetMediaRecorderToErrorState()

        try {
            mMediaRecorder.reset();
            
            /* Skip setAudioSource() if the method under test is setAudioEncoder()
             * Because, otherwise, it is valid to call setAudioEncoder() after
             * start() since start() will fail, and then the mMediaRecorder 
             * won't be set to the Error state
             */ 
            if (mMethodUnderTest.toString() != "setAudioEncoder()") {
                mMediaRecorder.setAudioSource(AUDIO_SOURCE);
            }
            
            /* Skip setOutputFormat if the method under test is setOutputFile()
             *  Because, otherwise, it is valid to call setOutputFile() after
             * start() since start() will fail, and then the mMediaRecorder 
             * won't be set to the Error state
             */ 
            if (mMethodUnderTest.toString() != "setOutputFile()") {
                mMediaRecorder.setOutputFormat(OUTPUT_FORMAT);
            }
            
            mMediaRecorder.start();
        } catch(Exception e) {
            if (!(e instanceof IllegalStateException)) {
                fail("setMediaRecorderToErrorState: Exception " + e.getClass().getName() + " was thrown.");
            }
        }
        Log.v(TAG, "setMediaRecorderToErrorState: done.");
    
private voidsetMediaRecorderToInitialStateAfterReset()

        try {
            mMediaRecorder.reset();
        } catch(Exception e) {
            fail("setMediaRecorderToInitialStateAfterReset: Exception " + e.getClass().getName() + " was thrown.");
        }
    
private voidsetMediaRecorderToInitialStateAfterStop()

        try {
            mMediaRecorder.reset();
/*
            mMediaRecorder.setAudioSource(AUDIO_SOURCE);
            mMediaRecorder.setOutputFormat(OUTPUT_FORMAT);
            mMediaRecorder.setAudioEncoder(AUDIO_ENCODER);
            mMediaRecorder.setOutputFile(RECORD_OUTPUT_PATH);
            mMediaRecorder.prepare();
            mMediaRecorder.start();
            mMediaRecorder.stop();
*/
        } catch(Exception e) {
            fail("setMediaRecorderToInitialStateAfterReset: Exception " + e.getClass().getName() + " was thrown.");
        }
    
private voidsetMediaRecorderToInitializedState()

        try {
            mMediaRecorder.reset();
            if (mMethodUnderTest.toString() != "setAudioSource()") {
                mMediaRecorder.setAudioSource(AUDIO_SOURCE);
            }
        } catch(Exception e) {
            fail("setMediaRecorderToInitializedState: Exception " + e.getClass().getName() + " was thrown.");
        }
    
private voidsetMediaRecorderToPreparedState()

        try {
            mMediaRecorder.reset();
            mMediaRecorder.setAudioSource(AUDIO_SOURCE);
            mMediaRecorder.setOutputFormat(OUTPUT_FORMAT);
            mMediaRecorder.setAudioEncoder(AUDIO_ENCODER);
            mMediaRecorder.setOutputFile(RECORD_OUTPUT_PATH);
            mMediaRecorder.prepare();
        } catch(Exception e) {
            fail("setMediaRecorderToPreparedState: Exception " + e.getClass().getName() + " was thrown.");
        }
    
private voidsetMediaRecorderToRecordingState()

        try {
            mMediaRecorder.reset();
            mMediaRecorder.setAudioSource(AUDIO_SOURCE);
            mMediaRecorder.setOutputFormat(OUTPUT_FORMAT);
            mMediaRecorder.setAudioEncoder(AUDIO_ENCODER);
            mMediaRecorder.setOutputFile(RECORD_OUTPUT_PATH);
            mMediaRecorder.prepare();
            mMediaRecorder.start();
        } catch(Exception e) {
            fail("setMediaRecorderToRecordingState: Exception " + e.getClass().getName() + " was thrown.");
        }
    
private voidsetMediaRecorderToState(MediaRecorderStateErrors.MediaRecorderState state)

        mMediaRecorderState = state;
        switch(state) {
            case INITIAL:
                // Does nothing.
                break;
            case INITIAL_AFTER_RESET:
                setMediaRecorderToInitialStateAfterReset();
                break;
            case INITIAL_AFTER_STOP:
                setMediaRecorderToInitialStateAfterStop();
                break;
            case INITIALIZED:
                setMediaRecorderToInitializedState();
                break;
            case DATASOURCECONFIGURED:
                setMediaRecorderToDataSourceConfiguredState();
                break;
            case PREPARED:
                setMediaRecorderToPreparedState();
                break;
            case RECORDING:
                setMediaRecorderToRecordingState();
                break;
            case ERROR:
                setMediaRecorderToErrorState();
                break;
        }
    
private voidsetStateError(MediaRecorderStateErrors.MediaRecorderState state, boolean error)

        switch(state) {
            case INITIAL:
                mStateErrors.errorInInitialState = error;
                break;
            case INITIAL_AFTER_RESET:
                mStateErrors.errorInInitialStateAfterReset = error;
                break;
            case INITIAL_AFTER_STOP:
                mStateErrors.errorInInitialStateAfterStop = error;
                break;
            case INITIALIZED:
                mStateErrors.errorInInitializedState = error;
                break;
            case DATASOURCECONFIGURED:
                mStateErrors.errorInDataSourceConfiguredState = error;
                break;
            case PREPARED:
                mStateErrors.errorInPreparedState = error;
                break;
            case RECORDING:
                mStateErrors.errorInRecordingState = error;
                break;
            case ERROR:
                mStateErrors.errorInErrorState = error;
                break;
        }