From d87bc490e81c7390cecdf556f6ebe331f7f8563f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ros=C3=A1rio=20P=2E=20Fernandes?= Date: Sat, 31 Jul 2021 17:08:38 +0200 Subject: [PATCH] test(firestore): create FirestorePagingSourceTest --- .../paging/FirestorePagingSourceTest.java | 124 ++++++++++++++++++ .../firebase/ui/firestore/paging/PageKey.java | 13 ++ 2 files changed, 137 insertions(+) create mode 100644 firestore/src/androidTest/java/com/firebase/ui/firestore/paging/FirestorePagingSourceTest.java diff --git a/firestore/src/androidTest/java/com/firebase/ui/firestore/paging/FirestorePagingSourceTest.java b/firestore/src/androidTest/java/com/firebase/ui/firestore/paging/FirestorePagingSourceTest.java new file mode 100644 index 000000000..3cec97075 --- /dev/null +++ b/firestore/src/androidTest/java/com/firebase/ui/firestore/paging/FirestorePagingSourceTest.java @@ -0,0 +1,124 @@ +package com.firebase.ui.firestore.paging; + +import com.google.android.gms.tasks.Tasks; +import com.google.firebase.firestore.DocumentSnapshot; +import com.google.firebase.firestore.Query; +import com.google.firebase.firestore.QuerySnapshot; +import com.google.firebase.firestore.Source; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.List; + +import androidx.paging.PagingSource; +import androidx.paging.PagingSource.LoadParams.Append; +import androidx.paging.PagingSource.LoadParams.Refresh; +import androidx.paging.PagingSource.LoadResult.Page; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@RunWith(AndroidJUnit4.class) +public class FirestorePagingSourceTest { + + @Mock + Query mMockQuery; + + ArrayList mMockSnapshots = new ArrayList<>(); + Exception mMockException = new Exception("Could not load Data"); + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + initMockQuery(); + + for (int i = 0; i < 2; i++) { + mMockSnapshots.add(mock(DocumentSnapshot.class)); + } + } + + @Test + public void testLoadInitial_success() { + FirestorePagingSource pagingSource = new FirestorePagingSource(mMockQuery, Source.DEFAULT); + mockQuerySuccess(mMockSnapshots); + Page expected = new Page<>(mMockSnapshots, null, new PageKey(null, null)); + + Refresh refreshRequest = new Refresh<>(null, 2, false); + PagingSource.LoadResult actual = + pagingSource.loadSingle(refreshRequest).blockingGet(); + + assertTrue(actual instanceof Page); + assertEquals(expected, actual); + } + + @Test + public void testLoadInitial_failure() { + FirestorePagingSource pagingSource = new FirestorePagingSource(mMockQuery, Source.DEFAULT); + mockQueryFailure(mMockException); + PagingSource.LoadResult.Error expected = + new PagingSource.LoadResult.Error<>(mMockException); + + Refresh refreshRequest = new Refresh<>(null, 2, false); + PagingSource.LoadResult actual = + pagingSource.loadSingle(refreshRequest).blockingGet(); + + assertEquals(expected, actual); + } + + @Test + public void testLoadAfter_success() { + FirestorePagingSource pagingSource = new FirestorePagingSource(mMockQuery, Source.DEFAULT); + mockQuerySuccess(mMockSnapshots); + PageKey pageKey = new PageKey(null, null); + Page expected = new Page<>(mMockSnapshots, null, pageKey); + + Append appendRequest = new Append<>(pageKey, 2, false); + PagingSource.LoadResult actual = + pagingSource.loadSingle(appendRequest).blockingGet(); + + assertTrue(actual instanceof Page); + assertEquals(expected, actual); + } + + @Test + public void testLoadAfter_failure() { + FirestorePagingSource pagingSource = new FirestorePagingSource(mMockQuery, Source.DEFAULT); + mockQueryFailure(mMockException); + PageKey pageKey = new PageKey(null, null); + PagingSource.LoadResult.Error expected = + new PagingSource.LoadResult.Error<>(mMockException); + + Append appendRequest = new Append<>(pageKey, 2, false); + PagingSource.LoadResult actual = + pagingSource.loadSingle(appendRequest).blockingGet(); + + assertEquals(expected, actual); + } + + private void initMockQuery() { + when(mMockQuery.startAfter(any(DocumentSnapshot.class))).thenReturn(mMockQuery); + when(mMockQuery.endBefore(any(DocumentSnapshot.class))).thenReturn(mMockQuery); + when(mMockQuery.limit(anyLong())).thenReturn(mMockQuery); + } + + private void mockQuerySuccess(List snapshots) { + QuerySnapshot mockSnapshot = mock(QuerySnapshot.class); + when(mockSnapshot.getDocuments()).thenReturn(snapshots); + + when(mMockQuery.get(Source.DEFAULT)).thenReturn(Tasks.forResult(mockSnapshot)); + } + + private void mockQueryFailure(Exception exception) { + when(mMockQuery.get(Source.DEFAULT)).thenReturn(Tasks.forException(exception)); + } +} diff --git a/firestore/src/main/java/com/firebase/ui/firestore/paging/PageKey.java b/firestore/src/main/java/com/firebase/ui/firestore/paging/PageKey.java index 94bd79ad0..d6ca06580 100644 --- a/firestore/src/main/java/com/firebase/ui/firestore/paging/PageKey.java +++ b/firestore/src/main/java/com/firebase/ui/firestore/paging/PageKey.java @@ -38,6 +38,19 @@ public Query getPageQuery(@NonNull Query baseQuery, int size) { return pageQuery; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PageKey key = (PageKey) o; + if (mStartAfter == null && key.mStartAfter == null) + return true; + if (mEndBefore == null && key.mEndBefore == null) + return true; + return mStartAfter.getId() == key.mStartAfter.getId() && + mEndBefore.getId() == key.mEndBefore.getId(); + } + @Override @NonNull public String toString() {