use of com.google.android.exoplayer2.testutil.FakeDataSet in project ExoPlayer by google.
the class CacheWriterTest method cache_afterFailureOnClose_succeeds.
@Test
public void cache_afterFailureOnClose_succeeds() throws Exception {
FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100);
FakeDataSource upstreamDataSource = new FakeDataSource(fakeDataSet);
AtomicBoolean failOnClose = new AtomicBoolean(/* initialValue= */
true);
FailOnCloseDataSink dataSink = new FailOnCloseDataSink(cache, failOnClose);
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstreamDataSource, new FileDataSource(), dataSink, /* flags= */
0, /* eventListener= */
null);
CachingCounters counters = new CachingCounters();
CacheWriter cacheWriter = new CacheWriter(cacheDataSource, new DataSpec(Uri.parse("test_data")), /* temporaryBuffer= */
null, counters);
// DataSink.close failing must cause the operation to fail rather than succeed.
assertThrows(IOException.class, cacheWriter::cache);
// Since all of the bytes were read through the DataSource chain successfully before the sink
// was closed, the progress listener will have seen all of the bytes being cached, even though
// this may not really be the case.
counters.assertValues(/* bytesAlreadyCached= */
0, /* bytesNewlyCached= */
100, /* contentLength= */
100);
failOnClose.set(false);
// The bytes will be downloaded again, but cached successfully this time.
cacheWriter.cache();
counters.assertValues(/* bytesAlreadyCached= */
0, /* bytesNewlyCached= */
100, /* contentLength= */
100);
assertCachedData(cache, fakeDataSet);
}
use of com.google.android.exoplayer2.testutil.FakeDataSet in project ExoPlayer by google.
the class CacheWriterTest method cacheUnknownLengthPartialCaching.
@Test
public void cacheUnknownLengthPartialCaching() throws Exception {
FakeDataSet fakeDataSet = new FakeDataSet().newData("test_data").setSimulateUnknownLength(true).appendReadData(TestUtil.buildTestData(100)).endData();
FakeDataSource dataSource = new FakeDataSource(fakeDataSet);
Uri testUri = Uri.parse("test_data");
DataSpec dataSpec = new DataSpec(testUri, /* position= */
10, /* length= */
20);
CachingCounters counters = new CachingCounters();
CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), dataSpec, /* temporaryBuffer= */
null, counters);
cacheWriter.cache();
counters.assertValues(0, 20, 20);
counters.reset();
cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), new DataSpec(testUri), /* temporaryBuffer= */
null, counters);
cacheWriter.cache();
counters.assertValues(20, 80, 100);
assertCachedData(cache, fakeDataSet);
}
use of com.google.android.exoplayer2.testutil.FakeDataSet in project ExoPlayer by google.
the class DownloadServiceDashTest method setUp.
@Before
public void setUp() throws IOException {
testThread = new DummyMainThread();
context = ApplicationProvider.getApplicationContext();
tempFolder = Util.createTempDirectory(context, "ExoPlayerTest");
cache = new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
Runnable pauseAction = () -> {
if (pauseDownloadCondition != null) {
try {
pauseDownloadCondition.block();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
fakeDataSet = new FakeDataSet().setData(TEST_MPD_URI, TEST_MPD).newData("audio_init_data").appendReadAction(pauseAction).appendReadData(TestUtil.buildTestData(10)).endData().setRandomData("audio_segment_1", 4).setRandomData("audio_segment_2", 5).setRandomData("audio_segment_3", 6).setRandomData("text_segment_1", 1).setRandomData("text_segment_2", 2).setRandomData("text_segment_3", 3);
final DataSource.Factory fakeDataSourceFactory = new FakeDataSource.Factory().setFakeDataSet(fakeDataSet);
fakeStreamKey1 = new StreamKey(0, 0, 0);
fakeStreamKey2 = new StreamKey(0, 1, 0);
testThread.runTestOnMainThread(() -> {
DefaultDownloadIndex downloadIndex = new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider());
DefaultDownloaderFactory downloaderFactory = new DefaultDownloaderFactory(new CacheDataSource.Factory().setCache(cache).setUpstreamDataSourceFactory(fakeDataSourceFactory), /* executor= */
Runnable::run);
final DownloadManager dashDownloadManager = new DownloadManager(ApplicationProvider.getApplicationContext(), downloadIndex, downloaderFactory);
downloadManagerListener = new TestDownloadManagerListener(dashDownloadManager);
dashDownloadManager.resumeDownloads();
dashDownloadService = new DownloadService(DownloadService.FOREGROUND_NOTIFICATION_ID_NONE) {
@Override
protected DownloadManager getDownloadManager() {
return dashDownloadManager;
}
@Override
@Nullable
protected Scheduler getScheduler() {
return null;
}
@Override
protected Notification getForegroundNotification(List<Download> downloads, @Requirements.RequirementFlags int notMetRequirements) {
throw new UnsupportedOperationException();
}
};
dashDownloadService.onCreate();
});
}
use of com.google.android.exoplayer2.testutil.FakeDataSet in project ExoPlayer by google.
the class DashDownloaderTest method progressiveDownloadSeparatePeriods.
@Test
public void progressiveDownloadSeparatePeriods() throws Exception {
FakeDataSet fakeDataSet = new FakeDataSet().setData(TEST_MPD_URI, TEST_MPD).setRandomData("audio_init_data", 10).setRandomData("audio_segment_1", 4).setRandomData("audio_segment_2", 5).setRandomData("audio_segment_3", 6).setRandomData("period_2_segment_1", 1).setRandomData("period_2_segment_2", 2).setRandomData("period_2_segment_3", 3);
FakeDataSource fakeDataSource = new FakeDataSource(fakeDataSet);
FakeDataSource.Factory factory = mock(FakeDataSource.Factory.class);
when(factory.createDataSource()).thenReturn(fakeDataSource);
DashDownloader dashDownloader = getDashDownloader(factory, new StreamKey(0, 0, 0), new StreamKey(1, 0, 0));
dashDownloader.download(progressListener);
DataSpec[] openedDataSpecs = fakeDataSource.getAndClearOpenedDataSpecs();
assertThat(openedDataSpecs.length).isEqualTo(8);
assertThat(openedDataSpecs[0].uri).isEqualTo(TEST_MPD_URI);
assertThat(openedDataSpecs[1].uri.getPath()).isEqualTo("audio_init_data");
assertThat(openedDataSpecs[2].uri.getPath()).isEqualTo("audio_segment_1");
assertThat(openedDataSpecs[3].uri.getPath()).isEqualTo("audio_segment_2");
assertThat(openedDataSpecs[4].uri.getPath()).isEqualTo("audio_segment_3");
assertThat(openedDataSpecs[5].uri.getPath()).isEqualTo("period_2_segment_1");
assertThat(openedDataSpecs[6].uri.getPath()).isEqualTo("period_2_segment_2");
assertThat(openedDataSpecs[7].uri.getPath()).isEqualTo("period_2_segment_3");
}
use of com.google.android.exoplayer2.testutil.FakeDataSet in project ExoPlayer by google.
the class DashDownloaderTest method counters.
@Test
public void counters() throws Exception {
FakeDataSet fakeDataSet = new FakeDataSet().setData(TEST_MPD_URI, TEST_MPD).setRandomData("audio_init_data", 10).setRandomData("audio_segment_1", 4).newData("audio_segment_2").appendReadData(TestUtil.buildTestData(2)).appendReadError(new IOException()).appendReadData(TestUtil.buildTestData(3)).endData().setRandomData("audio_segment_3", 6);
DashDownloader dashDownloader = getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0));
try {
dashDownloader.download(progressListener);
fail();
} catch (IOException e) {
// Failure expected after downloading init data, segment 1 and 2 bytes in segment 2.
}
progressListener.assertBytesDownloaded(10 + 4 + 2);
dashDownloader.download(progressListener);
progressListener.assertBytesDownloaded(10 + 4 + 5 + 6);
}
Aggregations