### Question:
DownloadManager { public List<Download> getCurrentDownloads() { return downloads; } DownloadManager(
      Context context, DatabaseProvider databaseProvider, Cache cache, Factory upstreamFactory);  DownloadManager(
      Context context, WritableDownloadIndex downloadIndex, DownloaderFactory downloaderFactory); boolean isInitialized(); boolean isIdle(); boolean isWaitingForRequirements(); void addListener(Listener listener); void removeListener(Listener listener); Requirements getRequirements(); @Requirements.RequirementFlags int getNotMetRequirements(); void setRequirements(Requirements requirements); int getMaxParallelDownloads(); void setMaxParallelDownloads(int maxParallelDownloads); int getMinRetryCount(); void setMinRetryCount(int minRetryCount); DownloadIndex getDownloadIndex(); List<Download> getCurrentDownloads(); boolean getDownloadsPaused(); void resumeDownloads(); void pauseDownloads(); void setStopReason(@Nullable String id, int stopReason); void addDownload(DownloadRequest request); void addDownload(DownloadRequest request, int stopReason); void removeDownload(String id); void release(); static final int DEFAULT_MAX_PARALLEL_DOWNLOADS; static final int DEFAULT_MIN_RETRY_COUNT; static final Requirements DEFAULT_REQUIREMENTS; }### Answer:
@Test public void postDownloadRequest_downloads() throws Throwable { DownloadRunner runner = new DownloadRunner(uri1); TaskWrapper task = runner.postDownloadRequest().getTask(); task.assertDownloading(); runner.getDownloader(0).unblock().assertReleased().assertStartCount(1); task.assertCompleted(); runner.assertCreatedDownloaderCount(1); downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); assertThat(downloadManager.getCurrentDownloads()).isEmpty(); }
@Test public void postRemoveRequest_removes() throws Throwable { DownloadRunner runner = new DownloadRunner(uri1); TaskWrapper task = runner.postDownloadRequest().postRemoveRequest().getTask(); task.assertRemoving(); runner.getDownloader(1).unblock().assertReleased().assertStartCount(1); task.assertRemoved(); runner.assertCreatedDownloaderCount(2); downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); assertThat(downloadManager.getCurrentDownloads()).isEmpty(); }
@Test public void downloadFails_retriesThenTaskFails() throws Throwable { DownloadRunner runner = new DownloadRunner(uri1); runner.postDownloadRequest(); FakeDownloader downloader = runner.getDownloader(0); for (int i = 0; i <= MIN_RETRY_COUNT; i++) { downloader.assertStarted(MAX_RETRY_DELAY).fail(); } downloader.assertReleased().assertStartCount(MIN_RETRY_COUNT + 1); runner.getTask().assertFailed(); downloadManagerListener.blockUntilTasksComplete(); assertThat(downloadManager.getCurrentDownloads()).isEmpty(); }
@Test public void downloadFails_retries() throws Throwable { DownloadRunner runner = new DownloadRunner(uri1); runner.postDownloadRequest(); FakeDownloader downloader = runner.getDownloader(0); for (int i = 0; i < MIN_RETRY_COUNT; i++) { downloader.assertStarted(MAX_RETRY_DELAY).fail(); } downloader.assertStarted(MAX_RETRY_DELAY).unblock(); downloader.assertReleased().assertStartCount(MIN_RETRY_COUNT + 1); runner.getTask().assertCompleted(); downloadManagerListener.blockUntilTasksComplete(); assertThat(downloadManager.getCurrentDownloads()).isEmpty(); }
@Test public void getCurrentDownloads_returnsCurrentDownloads() { TaskWrapper task1 = new DownloadRunner(uri1).postDownloadRequest().getTask(); TaskWrapper task2 = new DownloadRunner(uri2).postDownloadRequest().getTask(); TaskWrapper task3 = new DownloadRunner(uri3).postDownloadRequest().postRemoveRequest().getTask(); task3.assertRemoving(); List<Download> downloads = downloadManager.getCurrentDownloads(); assertThat(downloads).hasSize(3); String[] taskIds = {task1.taskId, task2.taskId, task3.taskId}; String[] downloadIds = { downloads.get(0).request.id, downloads.get(1).request.id, downloads.get(2).request.id }; assertThat(downloadIds).isEqualTo(taskIds); }