HttpConnectionCheckTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { File targetFile = mDownloader.getTargetFile(); if (targetFile == null) { mDownloader.onDownloadError("Cannot access to downloads folder. Shared storage is not currently available."); return null; } String downloadedReleaseFilePath = mDownloader.getDownloadedReleaseFilePath(); if (downloadedReleaseFilePath != null) { File downloadedReleaseFile = new File(downloadedReleaseFilePath); if (downloadedReleaseFilePath.equals(targetFile.getAbsolutePath())) { if (downloadedReleaseFile.exists()) { mDownloader.onDownloadComplete(targetFile); return null; } } else { downloadedReleaseFile.delete(); mDownloader.setDownloadedReleaseFilePath(null); } } if (isCancelled()) { return null; } mDownloader.onStart(targetFile); return null; } HttpConnectionCheckTask(HttpConnectionReleaseDownloader downloader);   }
@Test public void errorIfTargetFileIsNull() { when(mDownloader.getTargetFile()).thenReturn(null); mCheckTask.doInBackground(); verify(mDownloader).onDownloadError(anyString()); verifyNoMoreInteractions(ignoreStubs(mDownloader)); }
@Test public void downloadedFileExists() throws Exception { File targetFile = mTemporaryFolder.newFile(); when(mDownloader.getTargetFile()).thenReturn(targetFile); when(mDownloader.getDownloadedReleaseFilePath()).thenReturn(targetFile.getAbsolutePath()); mCheckTask.doInBackground(); verify(mDownloader).onDownloadComplete(targetFile); verifyNoMoreInteractions(ignoreStubs(mDownloader)); }
@Test public void downloadedFilePathDoesNotExist() { File targetFile = new File("/this/file/does/not/exist"); when(mDownloader.getTargetFile()).thenReturn(targetFile); when(mDownloader.getDownloadedReleaseFilePath()).thenReturn(targetFile.getAbsolutePath()); mCheckTask.doInBackground(); verify(mDownloader).onStart(targetFile); verifyNoMoreInteractions(ignoreStubs(mDownloader)); }
@Test public void downloadedFileIsNotEqualToTargetFile() throws Exception { File downloadedFile = mTemporaryFolder.newFile(); File targetFile = mTemporaryFolder.newFile(); when(mDownloader.getTargetFile()).thenReturn(targetFile); when(mDownloader.getDownloadedReleaseFilePath()).thenReturn(downloadedFile.getAbsolutePath()); mCheckTask.doInBackground(); assertFalse(downloadedFile.exists()); verify(mDownloader).setDownloadedReleaseFilePath(isNull(String.class)); verify(mDownloader).onStart(targetFile); verifyNoMoreInteractions(ignoreStubs(mDownloader)); }
@Test public void downloadedFilePathIsNull() throws Exception { File targetFile = mTemporaryFolder.newFile(); when(mDownloader.getTargetFile()).thenReturn(targetFile); when(mDownloader.getDownloadedReleaseFilePath()).thenReturn(null); mCheckTask.doInBackground(); verify(mDownloader).onStart(targetFile); verifyNoMoreInteractions(ignoreStubs(mDownloader)); }
@Test public void connectionCheckTaskIsCancelled() throws Exception { File targetFile = mTemporaryFolder.newFile(); when(mDownloader.getTargetFile()).thenReturn(targetFile); when(mDownloader.getDownloadedReleaseFilePath()).thenReturn(null); mCheckTask = spy(mCheckTask); when(mCheckTask.isCancelled()).thenReturn(true); mCheckTask.doInBackground(); verifyZeroInteractions(ignoreStubs(mDownloader)); }