WebvttSubtitle implements Subtitle { @Override public List<Cue> getCues(long timeUs) { List<Cue> list = new ArrayList<>(); WebvttCue firstNormalCue = null; SpannableStringBuilder normalCueTextBuilder = null; for (int i = 0; i < numCues; i++) { if ((cueTimesUs[i * 2] <= timeUs) && (timeUs < cueTimesUs[i * 2 + 1])) { WebvttCue cue = cues.get(i); if (cue.isNormalCue()) { if (firstNormalCue == null) { firstNormalCue = cue; } else if (normalCueTextBuilder == null) { normalCueTextBuilder = new SpannableStringBuilder(); normalCueTextBuilder .append(Assertions.checkNotNull(firstNormalCue.text)) .append("\n") .append(Assertions.checkNotNull(cue.text)); } else { normalCueTextBuilder.append("\n").append(Assertions.checkNotNull(cue.text)); } } else { list.add(cue); } } } if (normalCueTextBuilder != null) { list.add(new WebvttCue.Builder().setText(normalCueTextBuilder).build()); } else if (firstNormalCue != null) { list.add(firstNormalCue); } return list; } WebvttSubtitle(List<WebvttCue> cues); @Override int getNextEventTimeIndex(long timeUs); @Override int getEventTimeCount(); @Override long getEventTime(int index); @Override List<Cue> getCues(long timeUs);  }
@Test public void testSimpleSubtitleText() { assertSingleCueEmpty(simpleSubtitle.getCues(0)); assertSingleCueEmpty(simpleSubtitle.getCues(500000)); assertSingleCueEmpty(simpleSubtitle.getCues(999999)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, simpleSubtitle.getCues(1000000)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, simpleSubtitle.getCues(1500000)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, simpleSubtitle.getCues(1999999)); assertSingleCueEmpty(simpleSubtitle.getCues(2000000)); assertSingleCueEmpty(simpleSubtitle.getCues(2500000)); assertSingleCueEmpty(simpleSubtitle.getCues(2999999)); assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, simpleSubtitle.getCues(3000000)); assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, simpleSubtitle.getCues(3500000)); assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, simpleSubtitle.getCues(3999999)); assertSingleCueEmpty(simpleSubtitle.getCues(4000000)); assertSingleCueEmpty(simpleSubtitle.getCues(4500000)); assertSingleCueEmpty(simpleSubtitle.getCues(Long.MAX_VALUE)); }
@Test public void testOverlappingSubtitleText() { assertSingleCueEmpty(overlappingSubtitle.getCues(0)); assertSingleCueEmpty(overlappingSubtitle.getCues(500000)); assertSingleCueEmpty(overlappingSubtitle.getCues(999999)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, overlappingSubtitle.getCues(1000000)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, overlappingSubtitle.getCues(1500000)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, overlappingSubtitle.getCues(1999999)); assertSingleCueTextEquals(FIRST_AND_SECOND_SUBTITLE_STRING, overlappingSubtitle.getCues(2000000)); assertSingleCueTextEquals(FIRST_AND_SECOND_SUBTITLE_STRING, overlappingSubtitle.getCues(2500000)); assertSingleCueTextEquals(FIRST_AND_SECOND_SUBTITLE_STRING, overlappingSubtitle.getCues(2999999)); assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, overlappingSubtitle.getCues(3000000)); assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, overlappingSubtitle.getCues(3500000)); assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, overlappingSubtitle.getCues(3999999)); assertSingleCueEmpty(overlappingSubtitle.getCues(4000000)); assertSingleCueEmpty(overlappingSubtitle.getCues(4500000)); assertSingleCueEmpty(overlappingSubtitle.getCues(Long.MAX_VALUE)); }
@Test public void testNestedSubtitleText() { assertSingleCueEmpty(nestedSubtitle.getCues(0)); assertSingleCueEmpty(nestedSubtitle.getCues(500000)); assertSingleCueEmpty(nestedSubtitle.getCues(999999)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(1000000)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(1500000)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(1999999)); assertSingleCueTextEquals(FIRST_AND_SECOND_SUBTITLE_STRING, nestedSubtitle.getCues(2000000)); assertSingleCueTextEquals(FIRST_AND_SECOND_SUBTITLE_STRING, nestedSubtitle.getCues(2500000)); assertSingleCueTextEquals(FIRST_AND_SECOND_SUBTITLE_STRING, nestedSubtitle.getCues(2999999)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(3000000)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(3500000)); assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(3999999)); assertSingleCueEmpty(nestedSubtitle.getCues(4000000)); assertSingleCueEmpty(nestedSubtitle.getCues(4500000)); assertSingleCueEmpty(nestedSubtitle.getCues(Long.MAX_VALUE)); }