Methods Summary |
---|
private void | cleanupAudioFocusListener()
// clean up
mAudioManager.abandonAudioFocus(mAudioFocusListener);
mAudioManager.unregisterAudioFocusListener(mAudioFocusListener);
|
private void | initializeAudioManagerWithLooper()
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 void | setUp()
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 void | setupAudioFocusListener()
mAudioFocusListener = new AudioFocusListener();
mAudioManager.registerAudioFocusListener(mAudioFocusListener);
|
protected void | tearDown()
super.tearDown();
synchronized(mLooperLock) {
if (mAudioManagerLooper != null) {
mAudioManagerLooper.quit();
}
}
|
public void | testAudioFocusLoss()
setupAudioFocusListener();
verifyAudioFocusLoss(AudioManager.AUDIOFOCUS_GAIN, AudioManager.AUDIOFOCUS_LOSS);
cleanupAudioFocusListener();
|
public void | testAudioFocusLossTransient()
setupAudioFocusListener();
verifyAudioFocusLoss(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT);
cleanupAudioFocusListener();
|
public void | testAudioFocusLossTransientDuck()
setupAudioFocusListener();
verifyAudioFocusLoss(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK);
cleanupAudioFocusListener();
|
public void | testAudioFocusStressListenerRequestAbandon()
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 void | testAudioFocusStressNoListenerRequestAbandon()
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 void | testSetRingtoneMode()
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 boolean | validateSetRingTone(int i)
int getRingtone = mAudioManager.getRingerMode();
if (mUseFixedVolume) {
return (getRingtone == AudioManager.RINGER_MODE_NORMAL);
} else {
return (getRingtone == i);
}
|
private void | verifyAudioFocusLoss(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);
|