@Test
public void testSelectingForcedTextTrackMatchesAudioLanguage() throws ExoPlaybackException {

  Format forcedEnglish =
      buildTextFormat(/* id= */ "forcedEnglish", /* language= */ "eng", C.SELECTION_FLAG_FORCED);
  Format forcedGerman =
      buildTextFormat(/* id= */ "forcedGerman", /* language= */ "deu", C.SELECTION_FLAG_FORCED);
  Format forcedNoLanguage =
      buildTextFormat(
          /* id= */ "forcedNoLanguage",
          /* language= */ C.LANGUAGE_UNDETERMINED,
          C.SELECTION_FLAG_FORCED);
  Format audio = buildAudioFormat(/* id= */ "audio");
  Format germanAudio =
      buildAudioFormat(
          /* id= */ "germanAudio",
          MimeTypes.AUDIO_AAC,
          /* bitrate= */ Format.NO_VALUE,
          "deu",
          /* selectionFlags= */ 0,
          /* channelCount= */ Format.NO_VALUE,
          /* sampleRate= */ Format.NO_VALUE);
  RendererCapabilities[] rendererCapabilities =
      new RendererCapabilities[] {
        ALL_AUDIO_FORMAT_SUPPORTED_RENDERER_CAPABILITIES,
        ALL_TEXT_FORMAT_SUPPORTED_RENDERER_CAPABILITIES
      };
  // Neither the audio nor the forced text track define a language. We select them both under the
  // assumption that they have matching language.
  TrackGroupArray trackGroups = wrapFormats(audio, forcedNoLanguage);
  TrackSelectorResult result =
      trackSelector.selectTracks(rendererCapabilities, trackGroups, periodId, TIMELINE);
  assertFixedSelection(result.selections.get(1), trackGroups, forcedNoLanguage);
  // No forced text track should be selected because none of the forced text tracks' languages
  // matches the selected audio language.
  trackGroups = wrapFormats(audio, forcedEnglish, forcedGerman);
  result = trackSelector.selectTracks(rendererCapabilities, trackGroups, periodId, TIMELINE);
  assertNoSelection(result.selections.get(1));
  // The audio declares german. The german forced track should be selected.
  trackGroups = wrapFormats(germanAudio, forcedGerman, forcedEnglish);
  result = trackSelector.selectTracks(rendererCapabilities, trackGroups, periodId, TIMELINE);
  assertFixedSelection(result.selections.get(1), trackGroups, forcedGerman);
  // Ditto
  trackGroups = wrapFormats(germanAudio, forcedEnglish, forcedGerman);
  result = trackSelector.selectTracks(rendererCapabilities, trackGroups, periodId, TIMELINE);
  assertFixedSelection(result.selections.get(1), trackGroups, forcedGerman);
}