@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()); }
@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 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 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); }
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);  }
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);   }