@Test
public void testPeriodCreationWithAds() throws IOException, InterruptedException {

  // Create concatenated media source with ad child source.
  Timeline timelineContentOnly =
      new FakeTimeline(
          new TimelineWindowDefinition(2, 111, true, false, 10 * C.MICROS_PER_SECOND));
  Timeline timelineWithAds =
      new FakeTimeline(
          new TimelineWindowDefinition(
              2,
              222,
              true,
              false,
              10 * C.MICROS_PER_SECOND,
              FakeTimeline.createAdPlaybackState(
                  /* adsPerAdGroup= */ 1, /* adGroupTimesUs= */ 0)));
  FakeMediaSource mediaSourceContentOnly = new FakeMediaSource(timelineContentOnly);
  FakeMediaSource mediaSourceWithAds = new FakeMediaSource(timelineWithAds);
  mediaSource.addMediaSource(mediaSourceContentOnly);
  mediaSource.addMediaSource(mediaSourceWithAds);
  Timeline timeline = testRunner.prepareSource();
  // Assert the timeline contains ad groups.
  TimelineAsserts.assertAdGroupCounts(timeline, 0, 0, 1, 1);
  // Create all periods and assert period creation of child media sources has been called.
  testRunner.assertPrepareAndReleaseAllPeriods();
  Object timelineContentOnlyPeriodUid0 = timelineContentOnly.getUidOfPeriod(/* periodIndex= */ 0);
  Object timelineContentOnlyPeriodUid1 = timelineContentOnly.getUidOfPeriod(/* periodIndex= */ 1);
  Object timelineWithAdsPeriodUid0 = timelineWithAds.getUidOfPeriod(/* periodIndex= */ 0);
  Object timelineWithAdsPeriodUid1 = timelineWithAds.getUidOfPeriod(/* periodIndex= */ 1);
  mediaSourceContentOnly.assertMediaPeriodCreated(
      new MediaPeriodId(timelineContentOnlyPeriodUid0, /* windowSequenceNumber= */ 0));
  mediaSourceContentOnly.assertMediaPeriodCreated(
      new MediaPeriodId(timelineContentOnlyPeriodUid1, /* windowSequenceNumber= */ 0));
  mediaSourceWithAds.assertMediaPeriodCreated(
      new MediaPeriodId(timelineWithAdsPeriodUid0, /* windowSequenceNumber= */ 1));
  mediaSourceWithAds.assertMediaPeriodCreated(
      new MediaPeriodId(timelineWithAdsPeriodUid1, /* windowSequenceNumber= */ 1));
  mediaSourceWithAds.assertMediaPeriodCreated(
      new MediaPeriodId(
          timelineWithAdsPeriodUid0,
          /* adGroupIndex= */ 0,
          /* adIndexInAdGroup= */ 0,
          /* windowSequenceNumber= */ 1));
  mediaSourceWithAds.assertMediaPeriodCreated(
      new MediaPeriodId(
          timelineWithAdsPeriodUid1,
          /* adGroupIndex= */ 0,
          /* adIndexInAdGroup= */ 0,
          /* windowSequenceNumber= */ 1));
  testRunner.assertCompletedManifestLoads(0, 1);
  assertCompletedAllMediaPeriodLoads(timeline);
}