CastTimelineTracker { public CastTimeline getCastTimeline(RemoteMediaClient remoteMediaClient) { int[] itemIds = remoteMediaClient.getMediaQueue().getItemIds(); if (itemIds.length > 0) { removeUnusedItemDataEntries(itemIds); } MediaStatus mediaStatus = remoteMediaClient.getMediaStatus(); if (mediaStatus == null) { return CastTimeline.EMPTY_CAST_TIMELINE; } int currentItemId = mediaStatus.getCurrentItemId(); updateItemData( currentItemId, mediaStatus.getMediaInfo(), C.TIME_UNSET); for (MediaQueueItem item : mediaStatus.getQueueItems()) { long defaultPositionUs = (long) (item.getStartTime() * C.MICROS_PER_SECOND); updateItemData(item.getItemId(), item.getMedia(), defaultPositionUs); } return new CastTimeline(itemIds, itemIdToData); } CastTimelineTracker(); CastTimeline getCastTimeline(RemoteMediaClient remoteMediaClient);  }
@Test public void testGetCastTimelinePersistsDuration() { CastTimelineTracker tracker = new CastTimelineTracker(); RemoteMediaClient remoteMediaClient = mockRemoteMediaClient( new int[] {1, 2, 3, 4, 5}, 2, DURATION_2_MS); TimelineAsserts.assertPeriodDurations( tracker.getCastTimeline(remoteMediaClient), C.TIME_UNSET, C.msToUs(DURATION_2_MS), C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET); remoteMediaClient = mockRemoteMediaClient( new int[] {1, 2, 3}, 3, DURATION_3_MS); TimelineAsserts.assertPeriodDurations( tracker.getCastTimeline(remoteMediaClient), C.TIME_UNSET, C.msToUs(DURATION_2_MS), C.msToUs(DURATION_3_MS)); remoteMediaClient = mockRemoteMediaClient( new int[] {1, 3}, 3, DURATION_3_MS); TimelineAsserts.assertPeriodDurations( tracker.getCastTimeline(remoteMediaClient), C.TIME_UNSET, C.msToUs(DURATION_3_MS)); remoteMediaClient = mockRemoteMediaClient( new int[] {1, 2, 3, 4, 5}, 4, DURATION_4_MS); TimelineAsserts.assertPeriodDurations( tracker.getCastTimeline(remoteMediaClient), C.TIME_UNSET, C.TIME_UNSET, C.msToUs(DURATION_3_MS), C.msToUs(DURATION_4_MS), C.TIME_UNSET); remoteMediaClient = mockRemoteMediaClient( new int[] {1, 2, 3, 4, 5}, 5, DURATION_5_MS); TimelineAsserts.assertPeriodDurations( tracker.getCastTimeline(remoteMediaClient), C.TIME_UNSET, C.TIME_UNSET, C.msToUs(DURATION_3_MS), C.msToUs(DURATION_4_MS), C.msToUs(DURATION_5_MS)); }