Skip to content

Commit 7badcb4

Browse files
fragarsiefragarsie
fragarsie
authored and
fragarsie
committed
Add Tests for offline methods
1 parent df67b22 commit 7badcb4

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

app/src/test/java/durdinapps/rxfirebase2/RxFirestoreTest.java

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import com.google.firebase.firestore.CollectionReference;
55
import com.google.firebase.firestore.DocumentReference;
66
import com.google.firebase.firestore.DocumentSnapshot;
7+
import com.google.firebase.firestore.ListenerRegistration;
78
import com.google.firebase.firestore.Query;
8-
import com.google.firebase.firestore.QueryDocumentSnapshot;
99
import com.google.firebase.firestore.QuerySnapshot;
1010

1111
import org.junit.Before;
@@ -17,12 +17,16 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.Collections;
20+
import java.util.HashMap;
2021
import java.util.Iterator;
2122
import java.util.List;
2223

2324
import io.reactivex.observers.TestObserver;
2425

26+
import static durdinapps.rxfirebase2.RxTestUtil.eventSnapshotListener;
27+
import static durdinapps.rxfirebase2.RxTestUtil.setupOfflineTask;
2528
import static durdinapps.rxfirebase2.RxTestUtil.setupTask;
29+
import static durdinapps.rxfirebase2.RxTestUtil.testOnCompleteListener;
2630
import static durdinapps.rxfirebase2.RxTestUtil.testOnSuccessListener;
2731
import static org.mockito.Mockito.verify;
2832
import static org.mockito.Mockito.when;
@@ -71,13 +75,21 @@ public class RxFirestoreTest {
7175
@Mock
7276
private Task<DocumentSnapshot> documentSnapshotTask;
7377

78+
@Mock
79+
private Task<DocumentReference> documentRefTask;
80+
7481
@Mock
7582
private Task<QuerySnapshot> queryResultTask;
7683

7784
@Mock
7885
private Task<QuerySnapshot> emptyQueryResultTask;
7986

87+
@Mock
88+
private ListenerRegistration registration;
89+
8090

91+
private HashMap<String, Object> updateMap = new HashMap<>();
92+
private ChildDocData setData = new ChildDocData();
8193
private ChildDocData childData = new ChildDocData();
8294
private List<ChildDocData> childDataList = new ArrayList<>();
8395

@@ -90,13 +102,17 @@ public void setup() {
90102
setupTask(queryResultTask);
91103
setupTask(emptyQueryResultTask);
92104
setupTask(mockVoidTask);
105+
setupOfflineTask(documentReference, registration);
93106

94107
when(documentReference.get()).thenReturn(documentSnapshotTask);
95108
when(emptyDocumentReference.get()).thenReturn(emptyDocumentSnapshotTask);
96109
when(collectionReference.get()).thenReturn(queryResultTask);
97110
when(emptyCollectionReference.get()).thenReturn(emptyQueryResultTask);
98111
when(queryReference.get()).thenReturn(queryResultTask);
99112
when(emptyQueryReference.get()).thenReturn(emptyQueryResultTask);
113+
when(documentReference.delete()).thenReturn(mockVoidTask);
114+
when(documentReference.update(updateMap)).thenReturn(mockVoidTask);
115+
when(collectionReference.add(setData)).thenReturn(documentRefTask);
100116
when(documentSnapshot.toObject(ChildDocData.class)).thenReturn(childData);
101117
when(documentSnapshot.exists()).thenReturn(true); //This snapshots exist
102118
when(documentSnapshot.exists()).thenReturn(true); //This snapshots exist
@@ -308,6 +324,85 @@ public void testMappedGetEmptyQuery() throws InterruptedException {
308324
.assertComplete();
309325
}
310326

327+
@Test
328+
public void testSetDocumentOffline() throws InterruptedException {
329+
TestObserver<Void> testObserver = RxFirestore
330+
.setDocumentOffline(documentReference, setData)
331+
.test();
332+
333+
eventSnapshotListener.getValue().onEvent(documentSnapshot, null);
334+
335+
verify(documentReference).set(setData);
336+
337+
testObserver
338+
.assertNoErrors()
339+
.assertComplete();
340+
}
341+
342+
@Test
343+
public void testUpdateDocument() throws InterruptedException {
344+
345+
TestObserver<Void> storageTestObserver =
346+
RxFirestore.updateDocument(documentReference, updateMap)
347+
.test();
348+
349+
testOnCompleteListener.getValue().onComplete(mockVoidTask);
350+
351+
verify(documentReference).update(updateMap);
352+
353+
storageTestObserver.assertNoErrors()
354+
.assertComplete()
355+
.dispose();
356+
}
357+
358+
359+
@Test
360+
public void testUpdateDocumentOffline() throws InterruptedException {
361+
TestObserver<Void> testObserver = RxFirestore
362+
.updateDocumentOffline(documentReference, updateMap)
363+
.test();
364+
365+
eventSnapshotListener.getValue().onEvent(documentSnapshot, null);
366+
367+
verify(documentReference).update(updateMap);
368+
369+
testObserver
370+
.assertNoErrors()
371+
.assertComplete();
372+
}
373+
374+
375+
@Test
376+
public void testDeleteDocument() throws InterruptedException {
377+
378+
TestObserver<Void> storageTestObserver =
379+
RxFirestore.deleteDocument(documentReference)
380+
.test();
381+
382+
testOnCompleteListener.getValue().onComplete(mockVoidTask);
383+
384+
verify(documentReference).delete();
385+
386+
storageTestObserver.assertNoErrors()
387+
.assertComplete()
388+
.dispose();
389+
}
390+
391+
@Test
392+
public void testDeleteDocumentOffline() throws InterruptedException {
393+
TestObserver<Void> testObserver = RxFirestore
394+
.deleteDocumentOffline(documentReference)
395+
.test();
396+
397+
eventSnapshotListener.getValue().onEvent(documentSnapshot, null);
398+
verify(documentReference).delete();
399+
400+
testObserver
401+
.assertNoErrors()
402+
.assertComplete();
403+
}
404+
405+
311406
class ChildDocData {
312407
int id;
313408
String str;

app/src/test/java/durdinapps/rxfirebase2/RxTestUtil.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import com.google.android.gms.tasks.OnFailureListener;
55
import com.google.android.gms.tasks.OnSuccessListener;
66
import com.google.android.gms.tasks.Task;
7+
import com.google.firebase.firestore.DocumentReference;
8+
import com.google.firebase.firestore.DocumentSnapshot;
9+
import com.google.firebase.firestore.EventListener;
10+
import com.google.firebase.firestore.ListenerRegistration;
711

812
import org.mockito.ArgumentCaptor;
913

@@ -25,10 +29,16 @@ public class RxTestUtil {
2529
public static ArgumentCaptor<OnCompleteListener> testOnCompleteListener = ArgumentCaptor.forClass(OnCompleteListener.class);
2630
public static ArgumentCaptor<OnSuccessListener> testOnSuccessListener = ArgumentCaptor.forClass(OnSuccessListener.class);
2731
public static ArgumentCaptor<OnFailureListener> testOnFailureListener = ArgumentCaptor.forClass(OnFailureListener.class);
32+
public static ArgumentCaptor<EventListener<DocumentSnapshot>> eventSnapshotListener = ArgumentCaptor.forClass(EventListener.class);
2833

2934
public static <T> void setupTask(Task<T> task) {
3035
when(task.addOnCompleteListener(testOnCompleteListener.capture())).thenReturn(task);
3136
when(task.addOnSuccessListener(testOnSuccessListener.capture())).thenReturn(task);
3237
when(task.addOnFailureListener(testOnFailureListener.capture())).thenReturn(task);
3338
}
39+
40+
41+
public static void setupOfflineTask(DocumentReference documentReference, ListenerRegistration registration) {
42+
when(documentReference.addSnapshotListener(eventSnapshotListener.capture())).thenReturn(registration);
43+
}
3444
}

0 commit comments

Comments
 (0)