@Test public void completeDownloading() { when(mCursor.getInt(eq(DownloadManager.COLUMN_STATUS.hashCode()))).thenReturn(DownloadManager.STATUS_SUCCESSFUL); mUpdateTask.doInBackground(); verify(mDownloader).onDownloadComplete(eq(mCursor)); verify(mDownloader, never()).onDownloadError(any(RuntimeException.class)); verify(mCursor).close(); }
@Override protected Void doInBackground(Void... params) { DownloadManager downloadManager = mDownloader.getDownloadManager(); long downloadId = mDownloader.getDownloadId(); if (downloadId == INVALID_DOWNLOAD_IDENTIFIER) { mDownloader.onStart(); return null; } try { Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId)); if (cursor == null) { throw new NoSuchElementException("Cannot find download with id=" + downloadId); } try { if (!cursor.moveToFirst()) { throw new NoSuchElementException("Cannot find download with id=" + downloadId); } if (isCancelled()) { return null; } int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)); if (status == DownloadManager.STATUS_FAILED) { int reason = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON)); throw new IllegalStateException("The download has failed with reason code: " + reason); } if (status != DownloadManager.STATUS_SUCCESSFUL) { mDownloader.onDownloadProgress(cursor); return null; } mDownloader.onDownloadComplete(cursor); } finally { cursor.close(); } } catch (RuntimeException e) { mDownloader.onDownloadError(e); } return null; }
DownloadManagerUpdateTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { DownloadManager downloadManager = mDownloader.getDownloadManager(); long downloadId = mDownloader.getDownloadId(); if (downloadId == INVALID_DOWNLOAD_IDENTIFIER) { mDownloader.onStart(); return null; } try { Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId)); if (cursor == null) { throw new NoSuchElementException("Cannot find download with id=" + downloadId); } try { if (!cursor.moveToFirst()) { throw new NoSuchElementException("Cannot find download with id=" + downloadId); } if (isCancelled()) { return null; } int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)); if (status == DownloadManager.STATUS_FAILED) { int reason = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON)); throw new IllegalStateException("The download has failed with reason code: " + reason); } if (status != DownloadManager.STATUS_SUCCESSFUL) { mDownloader.onDownloadProgress(cursor); return null; } mDownloader.onDownloadComplete(cursor); } finally { cursor.close(); } } catch (RuntimeException e) { mDownloader.onDownloadError(e); } return null; } }
DownloadManagerUpdateTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { DownloadManager downloadManager = mDownloader.getDownloadManager(); long downloadId = mDownloader.getDownloadId(); if (downloadId == INVALID_DOWNLOAD_IDENTIFIER) { mDownloader.onStart(); return null; } try { Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId)); if (cursor == null) { throw new NoSuchElementException("Cannot find download with id=" + downloadId); } try { if (!cursor.moveToFirst()) { throw new NoSuchElementException("Cannot find download with id=" + downloadId); } if (isCancelled()) { return null; } int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)); if (status == DownloadManager.STATUS_FAILED) { int reason = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON)); throw new IllegalStateException("The download has failed with reason code: " + reason); } if (status != DownloadManager.STATUS_SUCCESSFUL) { mDownloader.onDownloadProgress(cursor); return null; } mDownloader.onDownloadComplete(cursor); } finally { cursor.close(); } } catch (RuntimeException e) { mDownloader.onDownloadError(e); } return null; } DownloadManagerUpdateTask(DownloadManagerReleaseDownloader downloader); }
DownloadManagerUpdateTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { DownloadManager downloadManager = mDownloader.getDownloadManager(); long downloadId = mDownloader.getDownloadId(); if (downloadId == INVALID_DOWNLOAD_IDENTIFIER) { mDownloader.onStart(); return null; } try { Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId)); if (cursor == null) { throw new NoSuchElementException("Cannot find download with id=" + downloadId); } try { if (!cursor.moveToFirst()) { throw new NoSuchElementException("Cannot find download with id=" + downloadId); } if (isCancelled()) { return null; } int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)); if (status == DownloadManager.STATUS_FAILED) { int reason = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON)); throw new IllegalStateException("The download has failed with reason code: " + reason); } if (status != DownloadManager.STATUS_SUCCESSFUL) { mDownloader.onDownloadProgress(cursor); return null; } mDownloader.onDownloadComplete(cursor); } finally { cursor.close(); } } catch (RuntimeException e) { mDownloader.onDownloadError(e); } return null; } DownloadManagerUpdateTask(DownloadManagerReleaseDownloader downloader);  }
DownloadManagerUpdateTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { DownloadManager downloadManager = mDownloader.getDownloadManager(); long downloadId = mDownloader.getDownloadId(); if (downloadId == INVALID_DOWNLOAD_IDENTIFIER) { mDownloader.onStart(); return null; } try { Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId)); if (cursor == null) { throw new NoSuchElementException("Cannot find download with id=" + downloadId); } try { if (!cursor.moveToFirst()) { throw new NoSuchElementException("Cannot find download with id=" + downloadId); } if (isCancelled()) { return null; } int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)); if (status == DownloadManager.STATUS_FAILED) { int reason = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON)); throw new IllegalStateException("The download has failed with reason code: " + reason); } if (status != DownloadManager.STATUS_SUCCESSFUL) { mDownloader.onDownloadProgress(cursor); return null; } mDownloader.onDownloadComplete(cursor); } finally { cursor.close(); } } catch (RuntimeException e) { mDownloader.onDownloadError(e); } return null; } DownloadManagerUpdateTask(DownloadManagerReleaseDownloader downloader);   }