FileDocCategorySizeDatePackage
MediaAudioManagerTest.javaAPI DocAndroid 5.1 API10130Thu Mar 12 22:22:30 GMT 2015com.android.mediaframeworktest.functional.audio

MediaAudioManagerTest

public class MediaAudioManagerTest extends android.test.ActivityInstrumentationTestCase2
Junit / Instrumentation test case for the media AudioManager api

Fields Summary
private static final String
TAG
private android.media.AudioManager
mAudioManager
private android.os.Looper
mAudioManagerLooper
private final Object
mLooperLock
private static final int
WAIT_FOR_LOOPER_TO_INITIALIZE_MS
private int[]
ringtoneMode
private boolean
mUseFixedVolume
private static AudioFocusListener
mAudioFocusListener
private static final int
INVALID_FOCUS
private static final int
WAIT_FOR_AUDIOFOCUS_LOSS_MS
Constructors Summary
public MediaAudioManagerTest()


      
        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
    
Methods Summary
private voidcleanupAudioFocusListener()

        // clean up
        mAudioManager.abandonAudioFocus(mAudioFocusListener);
        mAudioManager.unregisterAudioFocusListener(mAudioFocusListener);
    
private voidinitializeAudioManagerWithLooper()

        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                mAudioManagerLooper = Looper.myLooper();
                mAudioManager = (AudioManager)getActivity().getSystemService(Context.AUDIO_SERVICE);
                synchronized (mLooperLock) {
                    mLooperLock.notify();
                }
                Looper.loop();
            }
        }.start();
    
protected voidsetUp()

        super.setUp();

        mUseFixedVolume = getActivity().getResources().getBoolean(
                com.android.internal.R.bool.config_useFixedVolume);

        synchronized(mLooperLock) {
            initializeAudioManagerWithLooper();
            try {
                mLooperLock.wait(WAIT_FOR_LOOPER_TO_INITIALIZE_MS);
            } catch (Exception e) {
                assertTrue("initializeAudioManagerWithLooper() failed to complete in time", false);
            }
        }
     
private voidsetupAudioFocusListener()

        mAudioFocusListener = new AudioFocusListener();
        mAudioManager.registerAudioFocusListener(mAudioFocusListener);
    
protected voidtearDown()

         super.tearDown();
         synchronized(mLooperLock) {
             if (mAudioManagerLooper != null) {
                 mAudioManagerLooper.quit();
             }
         }
     
public voidtestAudioFocusLoss()

        setupAudioFocusListener();

        verifyAudioFocusLoss(AudioManager.AUDIOFOCUS_GAIN, AudioManager.AUDIOFOCUS_LOSS);

        cleanupAudioFocusListener();
    
public voidtestAudioFocusLossTransient()

        setupAudioFocusListener();

        verifyAudioFocusLoss(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,
                AudioManager.AUDIOFOCUS_LOSS_TRANSIENT);

        cleanupAudioFocusListener();
    
public voidtestAudioFocusLossTransientDuck()

        setupAudioFocusListener();

        verifyAudioFocusLoss(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
                AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK);

        cleanupAudioFocusListener();
    
public voidtestAudioFocusStressListenerRequestAbandon()

        final int ITERATIONS = 3000;
        // here we only test the life cycle of a focus listener, and make sure we don't crash
        // when doing it many times without waiting
        for (int i = 0 ; i < ITERATIONS ; i++) {
            setupAudioFocusListener();
            int result = mAudioManager.requestAudioFocus(mAudioFocusListener,
                    AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
            assertTrue("audio focus request was not granted",
                    result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
            cleanupAudioFocusListener();
        }
        assertTrue("testAudioFocusListenerLifeCycle : tested" + ITERATIONS +" iterations", true);
    
public voidtestAudioFocusStressNoListenerRequestAbandon()

        final int ITERATIONS = 1000;
        // make sure we have a listener in the stack
        setupAudioFocusListener();
        mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        // keep making the current owner lose and gain audio focus repeatedly
        for (int i = 0 ; i < ITERATIONS ; i++) {
            mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            mAudioManager.abandonAudioFocus(null);
            // the audio focus request is async, so wait a bit to verify it had the expected effect
            java.lang.Thread.sleep(WAIT_FOR_AUDIOFOCUS_LOSS_MS);
        }
        // verify there were 2 audio focus changes per iteration (one loss + one gain)
        assertTrue("testAudioFocusListenerLifeCycle : observed " +
                mAudioFocusListener.mFocusChangeCounter + " AudioFocus changes",
                mAudioFocusListener.mFocusChangeCounter == ITERATIONS * 2);
        mAudioManager.abandonAudioFocus(mAudioFocusListener);
        mAudioManager.unregisterAudioFocusListener(mAudioFocusListener);
    
public voidtestSetRingtoneMode()

         boolean result = false;

         for (int i = 0; i < ringtoneMode.length; i++) {
             mAudioManager.setRingerMode(ringtoneMode[i]);
             result = validateSetRingTone(ringtoneMode[i]);
             assertTrue("SetRingtoneMode : " + ringtoneMode[i], result);
         }
     
public booleanvalidateSetRingTone(int i)

         int getRingtone = mAudioManager.getRingerMode();

         if (mUseFixedVolume) {
             return (getRingtone == AudioManager.RINGER_MODE_NORMAL);
         } else {
             return (getRingtone == i);
         }
     
private voidverifyAudioFocusLoss(int focusGainMode, int expectedFocusLossMode)
Fails the test if expectedFocusLossMode != mAudioFocusListener.mLastFocusChange

        // request AudioFocus so we can test that mAudioFocusListener loses it when another
        //     request comes in
        int result = mAudioManager.requestAudioFocus(mAudioFocusListener,
                AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        assertTrue("requestAudioFocus returned " + result,
                result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
        // cause mAudioFocusListener to lose AudioFocus
        result = mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
                focusGainMode);
        assertTrue("requestAudioFocus returned " + result,
                result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
        // the audio focus request is async, so wait a bit to verify it had the expected effect
        java.lang.Thread.sleep(WAIT_FOR_AUDIOFOCUS_LOSS_MS);
        // test successful if the expected focus loss was recorded
        assertEquals("listener lost focus",
                mAudioFocusListener.mLastFocusChange, expectedFocusLossMode);