ImageDownloadTaskManager extends TaskManager<DownloadResponse> { @Override void execute() { if (mDownloadTasks.isEmpty()) { mImageTaskManagerListener.onSuccess(mResults); } for (final Entry<HttpUriRequest, DownloadTask> entry : mDownloadTasks.entrySet()) { final HttpUriRequest httpUriRequest = entry.getKey(); final DownloadTask downloadTask = entry.getValue(); try { AsyncTasks.safeExecuteOnExecutor(downloadTask, httpUriRequest); } catch (Exception e) { MoPubLog.d("Failed to download image", e); mImageTaskManagerListener.onFail(); } } } ImageDownloadTaskManager(@NonNull final List<String> urls,
                             @NonNull final TaskManagerListener<DownloadResponse> imageTaskManagerListener,
                             final int requestedWidth);   }
@Test public void execute_withValidUrlListAndListenerAndHttpResponses_shouldReturnMapOfUrlToDownloadResponse() throws Exception { subject = new ImageDownloadTaskManager( Arrays.asList(url1, url2), mMockImageTaskManagerListener, testMaxWidth ); String imageData1 = "image_data_1"; String imageData2 = "image_data_2"; fakeHttpLayer.addPendingHttpResponse(200, imageData1); fakeHttpLayer.addPendingHttpResponse(200, imageData2); subject.execute(); semaphore.acquire(); assertThat(networkImages.keySet()).containsOnly(url1, url2); DownloadResponse bitmap1 = networkImages.get(url1); DownloadResponse bitmap2 = networkImages.get(url2); verify(mMockImageTaskManagerListener).onSuccess(anyMap()); verify(mMockImageTaskManagerListener, never()).onFail(); }
@Test public void execute_withEmptyUrlList_shouldReturnEmptyMap() throws Exception { subject = new ImageDownloadTaskManager( new ArrayList<String>(), mMockImageTaskManagerListener, testMaxWidth ); subject.execute(); semaphore.acquire(); assertThat(networkImages.isEmpty()).isTrue(); verify(mMockImageTaskManagerListener).onSuccess(anyMap()); verify(mMockImageTaskManagerListener, never()).onFail(); }
@Test public void execute_withSingleNon200Response_shouldFailAllTasks() throws Exception { subject = new ImageDownloadTaskManager( Arrays.asList(url1, url1, url1, url1, url1), mMockImageTaskManagerListener, testMaxWidth ); fakeHttpLayer.addPendingHttpResponse(200, ""); fakeHttpLayer.addPendingHttpResponse(200, ""); fakeHttpLayer.addPendingHttpResponse(200, ""); fakeHttpLayer.addPendingHttpResponse(200, ""); fakeHttpLayer.addPendingHttpResponse(599, ""); subject.execute(); semaphore.acquire(); verify(mMockImageTaskManagerListener, never()).onSuccess(anyMap()); verify(mMockImageTaskManagerListener).onFail(); }
@Test public void execute_withMultipleNon200Response_shouldFailAllTasks() throws Exception { subject = new ImageDownloadTaskManager( Arrays.asList(url1, url1, url1, url1, url1), mMockImageTaskManagerListener, testMaxWidth ); fakeHttpLayer.addPendingHttpResponse(599, ""); fakeHttpLayer.addPendingHttpResponse(599, ""); fakeHttpLayer.addPendingHttpResponse(599, ""); fakeHttpLayer.addPendingHttpResponse(599, ""); fakeHttpLayer.addPendingHttpResponse(599, ""); subject.execute(); semaphore.acquire(); verify(mMockImageTaskManagerListener, never()).onSuccess(anyMap()); verify(mMockImageTaskManagerListener).onFail(); }
@Ignore("need to fix concurrency logic") @Test public void execute_withSingleInvalidHttpResponse_shouldFailAllTasks() throws Exception { subject = new ImageDownloadTaskManager( Arrays.asList(url1, url1, url1, url1, url1), mMockImageTaskManagerListener, testMaxWidth ); fakeHttpLayer.addPendingHttpResponse(200, ""); fakeHttpLayer.addPendingHttpResponse(200, ""); fakeHttpLayer.addPendingHttpResponse(200, ""); fakeHttpLayer.addPendingHttpResponse(200, ""); fakeHttpLayer.addPendingHttpResponse(createMockHttpResponseThatThrowsOnGetContent()); subject.execute(); semaphore.acquire(); verify(mMockImageTaskManagerListener, never()).onSuccess(anyMap()); verify(mMockImageTaskManagerListener).onFail(); }