HttpConnectionDownloadFileTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { TrafficStats.setThreadStatsTag(THREAD_STATS_TAG); try { long enqueueTime = System.currentTimeMillis(); mDownloader.onDownloadStarted(enqueueTime); URLConnection connection = createConnection(); long totalBytesDownloaded = downloadFile(connection); if (totalBytesDownloaded > 0) { mDownloader.onDownloadComplete(mTargetFile); } else { throw new IOException("The content of downloaded file is empty"); } } catch (IOException e) { mDownloader.onDownloadError(e.getMessage()); } finally { TrafficStats.clearThreadStatsTag(); } return null; } HttpConnectionDownloadFileTask(HttpConnectionReleaseDownloader downloader, Uri downloadUri, File targetFile);   }
@Test public void reportProgressBasedOnDownloadTime() throws IOException { long totalSize = UPDATE_PROGRESS_BYTES_THRESHOLD * 3; when(mUrlConnection.getContentLength()).thenReturn((int) totalSize); String apk = generateString((int) totalSize, '*'); mockConnectionContent(apk); mDownloadFileTask.doInBackground(); verifyDownloadedContent(apk); verify(mDownloader).onDownloadStarted(anyLong()); verify(mDownloader).onDownloadProgress(WRITE_BUFFER_SIZE, totalSize); verify(mDownloader).onDownloadProgress(totalSize, totalSize); verify(mDownloader).onDownloadComplete(eq(mTargetFile)); verify(mDownloader, never()).onDownloadError(anyString()); }
@Test public void cancelDuringDownloading() throws IOException { long totalSize = UPDATE_PROGRESS_BYTES_THRESHOLD * 3; when(mUrlConnection.getContentLength()).thenReturn((int) totalSize); String apk = generateString((int) totalSize, '*'); mockConnectionContent(apk); mDownloadFileTask = spy(mDownloadFileTask); when(mDownloadFileTask.isCancelled()).thenReturn(true); mDownloadFileTask.doInBackground(); assertEquals(WRITE_BUFFER_SIZE, mTargetFile.length()); verify(mDownloader).onDownloadStarted(anyLong()); verify(mDownloader).onDownloadProgress(WRITE_BUFFER_SIZE, totalSize); verify(mDownloader).onDownloadComplete(eq(mTargetFile)); verify(mDownloader, never()).onDownloadError(anyString()); }
@Test public void errorDuringStreamCopy() throws IOException { InputStream inputStream = mock(InputStream.class); when(inputStream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(new IOException()); when(mUrlConnection.getInputStream()).thenReturn(inputStream); mDownloadFileTask.doInBackground(); verify(mDownloader).onDownloadStarted(anyLong()); verify(mDownloader, never()).onDownloadComplete(any(File.class)); verify(mDownloader).onDownloadError(anyString()); }
@Test(expected = NullPointerException.class) public void somethingGoingWrong() { doThrow(new NullPointerException()).when(mDownloader).onDownloadStarted(anyLong()); mDownloadFileTask.doInBackground(); }
@Test public void downloadSuccessful() throws IOException { String apk = "I'm an APK file"; mockConnectionContent(apk); mDownloadFileTask.doInBackground(); verifyDownloadedContent(apk); verify(mDownloader).onDownloadStarted(anyLong()); verify(mDownloader).onDownloadComplete(eq(mTargetFile)); verify(mDownloader, never()).onDownloadError(anyString()); }
@Test public void wrongContentType() throws IOException { mockStatic(AppCenterLog.class); String apk = "I'm an APK file"; mockConnectionContent(apk); when(mUrlConnection.getContentType()).thenReturn("text/html"); mDownloadFileTask.doInBackground(); verifyStatic(); AppCenterLog.warn(eq(LOG_TAG), anyString()); verifyDownloadedContent(apk); verify(mDownloader).onDownloadStarted(anyLong()); verify(mDownloader).onDownloadComplete(eq(mTargetFile)); verify(mDownloader, never()).onDownloadError(anyString()); }
@Test public void errorResponseCode() throws IOException { when(mUrlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); mDownloadFileTask.doInBackground(); verify(mDownloader).onDownloadStarted(anyLong()); verify(mDownloader, never()).onDownloadComplete(any(File.class)); verify(mDownloader).onDownloadError(anyString()); }
@Test public void errorResponseCodeInvalid() throws IOException { when(mUrlConnection.getResponseCode()).thenReturn(-1); mDownloadFileTask.doInBackground(); verify(mDownloader).onDownloadStarted(anyLong()); verify(mDownloader, never()).onDownloadComplete(any(File.class)); verify(mDownloader).onDownloadError(anyString()); }
@Test public void nothingIsDownloaded() throws IOException { when(mUrlConnection.getInputStream()).thenReturn(new ByteArrayInputStream(new byte[0])); mDownloadFileTask.doInBackground(); verify(mDownloader).onDownloadStarted(anyLong()); verify(mDownloader, never()).onDownloadComplete(any(File.class)); verify(mDownloader).onDownloadError(anyString()); }
@Test public void readResponseError() throws IOException { when(mUrlConnection.getInputStream()).thenThrow(new IOException()); mDownloadFileTask.doInBackground(); verify(mDownloader).onDownloadStarted(anyLong()); verify(mDownloader, never()).onDownloadComplete(any(File.class)); verify(mDownloader).onDownloadError(anyString()); }
@Test public void reportProgressBasedOnDownloadCount() throws IOException { long totalSize = UPDATE_PROGRESS_BYTES_THRESHOLD * 3; when(mUrlConnection.getContentLength()).thenReturn((int) totalSize); String apk = generateString((int) totalSize, '*'); mockConnectionContent(apk); mockStatic(System.class); when(System.currentTimeMillis()).thenAnswer(new Answer<Long>() { private long currentTime = 0; @Override public Long answer(InvocationOnMock invocation) { return currentTime += UPDATE_PROGRESS_TIME_THRESHOLD; } }); mDownloadFileTask.doInBackground(); verifyDownloadedContent(apk); verify(mDownloader).onDownloadStarted(anyLong()); verify(mDownloader).onDownloadProgress(WRITE_BUFFER_SIZE, totalSize); verify(mDownloader).onDownloadProgress(WRITE_BUFFER_SIZE + UPDATE_PROGRESS_BYTES_THRESHOLD, totalSize); verify(mDownloader).onDownloadProgress(WRITE_BUFFER_SIZE + UPDATE_PROGRESS_BYTES_THRESHOLD * 2, totalSize); verify(mDownloader).onDownloadProgress(totalSize, totalSize); verify(mDownloader).onDownloadComplete(eq(mTargetFile)); verify(mDownloader, never()).onDownloadError(anyString()); }