com.google.firebase.storage.FileDownloadTask Java Examples

The following examples show how to use com.google.firebase.storage.FileDownloadTask. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StorageTest.java    From gdx-fireapp with Apache License 2.0 7 votes vote down vote up
@Test
public void download_file() {
    // Given
    Storage storage = new Storage();
    FileHandle file = Mockito.mock(FileHandle.class);
    when(file.file()).thenReturn(Mockito.mock(File.class));
    FileDownloadTask task = Mockito.mock(FileDownloadTask.class);
    when(storageReference.getFile(Mockito.any(File.class))).thenReturn(task);
    when(task.addOnFailureListener(Mockito.any(OnFailureListener.class))).thenReturn(task);
    when(task.addOnSuccessListener(Mockito.any(OnSuccessListener.class))).then(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) {
            ((OnSuccessListener) invocation.getArgument(0)).onSuccess(Mockito.mock(FileDownloadTask.TaskSnapshot.class));
            return null;
        }
    });

    // When
    storage.download("test", file).silentFail().subscribe();

    // Then
    Mockito.verify(storageReference, VerificationModeFactory.times(1)).getFile(Mockito.any(File.class));
    Mockito.verify(task, VerificationModeFactory.times(1)).addOnFailureListener(Mockito.any(OnFailureListener.class));
    Mockito.verify(task, VerificationModeFactory.times(1)).addOnSuccessListener(Mockito.any(OnSuccessListener.class));
}
 
Example #2
Source File: StorageTest.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
@Test
public void download_file_failure() {
    // Given
    Storage storage = new Storage();
    FileHandle file = Mockito.mock(FileHandle.class);
    when(file.file()).thenReturn(Mockito.mock(File.class));
    final FileDownloadTask task = Mockito.mock(FileDownloadTask.class);
    when(storageReference.getFile(Mockito.any(File.class))).thenReturn(task);
    when(task.addOnFailureListener(Mockito.any(OnFailureListener.class))).then(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) {
            ((OnFailureListener) invocation.getArgument(0)).onFailure(new Exception());
            return task;
        }
    });

    // When
    storage.download("test", file).silentFail().subscribe();

    // Then
    Mockito.verify(storageReference, VerificationModeFactory.times(1)).getFile(Mockito.any(File.class));
    Mockito.verify(task, VerificationModeFactory.times(1)).addOnFailureListener(Mockito.any(OnFailureListener.class));
    Mockito.verify(task, VerificationModeFactory.times(1)).addOnSuccessListener(Mockito.any(OnSuccessListener.class));
}
 
Example #3
Source File: StorageTest.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
@Test
public void download_nullFile() {
    // Given
    Storage storage = new Storage();
    PowerMockito.mockStatic(File.class);
    FileHandle file = null;
    FileDownloadTask task = Mockito.mock(FileDownloadTask.class);
    when(storageReference.getFile(Mockito.any(File.class))).thenReturn(task);
    when(task.addOnFailureListener(Mockito.any(OnFailureListener.class))).thenReturn(task);

    // When
    storage.download("test", file).subscribe();

    // Then
    Mockito.verify(storageReference, VerificationModeFactory.times(1)).getFile(Mockito.any(File.class));
    Mockito.verify(task, VerificationModeFactory.times(1)).addOnFailureListener(Mockito.any(OnFailureListener.class));
    Mockito.verify(task, VerificationModeFactory.times(1)).addOnSuccessListener(Mockito.any(OnSuccessListener.class));
}
 
Example #4
Source File: RxFirebaseStorageTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void getFile() {

    TestSubscriber<FileDownloadTask.TaskSnapshot> testSubscriber = new TestSubscriber<>();
    RxFirebaseStorage.getFile(mockStorageRef, file)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(fileSnapshot);

    verify(mockStorageRef).getFile(file);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(fileSnapshot));
    testSubscriber.assertNotCompleted();
    testSubscriber.unsubscribe();
}
 
Example #5
Source File: RxFirebaseStorageTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void getFileUri() {

    TestSubscriber<FileDownloadTask.TaskSnapshot> testSubscriber = new TestSubscriber<>();
    RxFirebaseStorage.getFile(mockStorageRef, uri)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(fileSnapshot);

    verify(mockStorageRef).getFile(uri);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(fileSnapshot));
    testSubscriber.assertNotCompleted();
    testSubscriber.unsubscribe();
}
 
Example #6
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("Duplicates") @Test public void testGetFileUri() {
  mockSuccessfulResultForTask(mockFileDownloadTask, mockFileDownloadTaskSnapshot);
  when(mockStorageReference.getFile(mockUri)).thenReturn(mockFileDownloadTask);
  when(mockFileDownloadTaskSnapshot.getBytesTransferred()).thenReturn(1000L);
  when(mockFileDownloadTaskSnapshot.getTotalByteCount()).thenReturn(1000L);

  TestObserver<FileDownloadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.getFile(mockStorageReference, mockUri).subscribe(obs);

  verifyAddOnCompleteListenerForTask(mockFileDownloadTask);

  callOnComplete(mockFileDownloadTask);
  obs.dispose();

  callOnComplete(mockFileDownloadTask);

  obs.assertNoErrors();
  obs.assertComplete();
  obs.assertValue(new Predicate<FileDownloadTask.TaskSnapshot>() {
    @Override public boolean test(FileDownloadTask.TaskSnapshot taskSnapshot) throws Exception {
      return taskSnapshot.getBytesTransferred() == taskSnapshot.getTotalByteCount()
          && taskSnapshot.getTotalByteCount() == 1000;
    }
  });
}
 
Example #7
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("Duplicates") @Test public void testGetFileUri_notSuccessful() {
  mockNotSuccessfulResultForTask(mockFileDownloadTask, new IllegalStateException());
  when(mockStorageReference.getFile(mockUri)).thenReturn(mockFileDownloadTask);

  TestObserver<FileDownloadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.getFile(mockStorageReference, mockUri).subscribe(obs);
  verifyAddOnCompleteListenerForTask(mockFileDownloadTask);

  callOnComplete(mockFileDownloadTask);
  obs.dispose();

  callOnComplete(mockFileDownloadTask);

  obs.assertError(IllegalStateException.class);
  obs.assertNoValues();
}
 
Example #8
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("Duplicates") @Test public void testGetFile() {
  mockSuccessfulResultForTask(mockFileDownloadTask, mockFileDownloadTaskSnapshot);
  when(mockStorageReference.getFile(mockFile)).thenReturn(mockFileDownloadTask);
  when(mockFileDownloadTaskSnapshot.getBytesTransferred()).thenReturn(1000L);
  when(mockFileDownloadTaskSnapshot.getTotalByteCount()).thenReturn(1000L);

  TestObserver<FileDownloadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.getFile(mockStorageReference, mockFile).subscribe(obs);

  verifyAddOnCompleteListenerForTask(mockFileDownloadTask);

  callOnComplete(mockFileDownloadTask);
  obs.dispose();

  callOnComplete(mockFileDownloadTask);

  obs.assertNoErrors();
  obs.assertComplete();
  obs.assertValue(new Predicate<FileDownloadTask.TaskSnapshot>() {
    @Override public boolean test(FileDownloadTask.TaskSnapshot taskSnapshot) throws Exception {
      return taskSnapshot.getBytesTransferred() == taskSnapshot.getTotalByteCount()
          && taskSnapshot.getTotalByteCount() == 1000;
    }
  });
}
 
Example #9
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("Duplicates") @Test public void testGetFile_notSuccessful() {
  mockNotSuccessfulResultForTask(mockFileDownloadTask, new IllegalStateException());
  when(mockStorageReference.getFile(mockFile)).thenReturn(mockFileDownloadTask);

  TestObserver<FileDownloadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.getFile(mockStorageReference, mockFile).subscribe(obs);
  verifyAddOnCompleteListenerForTask(mockFileDownloadTask);

  callOnComplete(mockFileDownloadTask);
  obs.dispose();

  callOnComplete(mockFileDownloadTask);

  obs.assertError(IllegalStateException.class);
  obs.assertNoValues();
}
 
Example #10
Source File: DownloadActivity.java    From snippets-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // If there was a download in progress, get its reference and create a new StorageReference
    final String stringRef = savedInstanceState.getString("reference");
    if (stringRef == null) {
        return;
    }
    mStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl(stringRef);

    // Find all DownloadTasks under this StorageReference (in this example, there should be one)
    List<FileDownloadTask> tasks = mStorageRef.getActiveDownloadTasks();
    if (tasks.size() > 0) {
        // Get the task monitoring the download
        FileDownloadTask task = tasks.get(0);

        // Add new listeners to the task using an Activity scope
        task.addOnSuccessListener(this, new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot state) {
                // Success!
                // ...
            }
        });
    }
}
 
Example #11
Source File: RxFirebaseStorage.java    From RxFirebase with Apache License 2.0 5 votes vote down vote up
@NonNull
public static Observable<FileDownloadTask.TaskSnapshot> getFile(@NonNull final StorageReference storageRef,
                                                                @NonNull final File destinationFile) {
    return Observable.unsafeCreate(new Observable.OnSubscribe<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void call(final Subscriber<? super FileDownloadTask.TaskSnapshot> subscriber) {
            RxTask.assignOnTask(subscriber, storageRef.getFile(destinationFile));
        }
    });
}
 
Example #12
Source File: RxFirebaseStorage.java    From RxFirebase with Apache License 2.0 5 votes vote down vote up
@NonNull
public static Observable<FileDownloadTask.TaskSnapshot> getFile(@NonNull final StorageReference storageRef,
                                                                @NonNull final Uri destinationUri) {
    return Observable.unsafeCreate(new Observable.OnSubscribe<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void call(final Subscriber<? super FileDownloadTask.TaskSnapshot> subscriber) {
            RxTask.assignOnTask(subscriber, storageRef.getFile(destinationUri));
        }
    });
}
 
Example #13
Source File: RxFirebaseStorage.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
/**
 * @see StorageReference#getFile(Uri)
 */
@CheckReturnValue
@NonNull
public static Single<FileDownloadTask.TaskSnapshot> getFile(@NonNull final StorageReference ref,
                                                            @NonNull final Uri uri) {
    return RxTask.single(new Callable<Task<FileDownloadTask.TaskSnapshot>>() {
        @Override
        public Task<FileDownloadTask.TaskSnapshot> call() throws Exception {
            return ref.getFile(uri);
        }
    });
}
 
Example #14
Source File: RxFirebaseStorage.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
/**
 * @see StorageReference#getFile(File)
 */
@CheckReturnValue
@NonNull
public static Single<FileDownloadTask.TaskSnapshot> getFile(@NonNull final StorageReference ref,
                                                            @NonNull final File file) {
    return RxTask.single(new Callable<Task<FileDownloadTask.TaskSnapshot>>() {
        @Override
        public Task<FileDownloadTask.TaskSnapshot> call() throws Exception {
            return ref.getFile(file);
        }
    });
}