@Test
@Config(maxSdk = 25)
public void onAudioFocusChange_withAudioFocusLost_sendsDoNotPlayAndAbandondsFocus() {

  // Ensure that AUDIOFOCUS_LOSS causes AudioFocusManager to pause playback and abandon audio
  // focus.
  AudioAttributes media =
      new AudioAttributes.Builder()
          .setUsage(C.USAGE_MEDIA)
          .setContentType(C.CONTENT_TYPE_SPEECH)
          .build();
  Shadows.shadowOf(audioManager)
      .setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
  assertThat(
          audioFocusManager.setAudioAttributes(
              media, /* playWhenReady= */ true, Player.STATE_READY))
      .isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY);
  assertThat(Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener()).isNull();
  ShadowAudioManager.AudioFocusRequest request =
      Shadows.shadowOf(audioManager).getLastAudioFocusRequest();
  request.listener.onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS);
  assertThat(testPlayerControl.lastPlayerCommand).isEqualTo(PLAYER_COMMAND_DO_NOT_PLAY);
  assertThat(Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener())
      .isEqualTo(request.listener);
}