1
1
package com .firebase .ui .firestore ;
2
2
3
- import android .util .Log ;
4
-
5
- import com .firebase .ui .firestore .paging .FirestorePagingSource ;
3
+ import com .firebase .ui .firestore .paging .FirestoreDataSource ;
6
4
import com .firebase .ui .firestore .paging .LoadingState ;
7
5
import com .firebase .ui .firestore .paging .PageKey ;
8
6
import com .google .android .gms .tasks .Tasks ;
26
24
import androidx .annotation .Nullable ;
27
25
import androidx .arch .core .executor .testing .InstantTaskExecutorRule ;
28
26
import androidx .lifecycle .Observer ;
29
- import androidx .paging .PagingSource ;
27
+ import androidx .paging .PageKeyedDataSource ;
30
28
import androidx .test .ext .junit .runners .AndroidJUnit4 ;
31
29
32
30
import static org .junit .Assert .assertEquals ;
36
34
import static org .mockito .Mockito .when ;
37
35
38
36
@ RunWith (AndroidJUnit4 .class )
39
- public class FirestorePagingSourceTest {
37
+ public class FirestoreDataSourceTest {
40
38
41
- private FirestorePagingSource mPagingSource ;
39
+ private FirestoreDataSource mDataSource ;
42
40
43
41
/**
44
42
* Needed to run tasks on the main thread so observeForever() doesn't throw.
@@ -47,43 +45,46 @@ public class FirestorePagingSourceTest {
47
45
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule ();
48
46
49
47
@ Mock Query mMockQuery ;
48
+ @ Mock PageKeyedDataSource .LoadInitialCallback <PageKey , DocumentSnapshot > mInitialCallback ;
49
+ @ Mock PageKeyedDataSource .LoadCallback <PageKey , DocumentSnapshot > mAfterCallback ;
50
50
51
51
@ Before
52
52
public void setUp () {
53
53
MockitoAnnotations .initMocks (this );
54
54
initMockQuery ();
55
55
56
- // Create a testing paging source
57
- mPagingSource = new FirestorePagingSource (mMockQuery , Source .DEFAULT );
56
+ // Create a testing data source
57
+ mDataSource = new FirestoreDataSource (mMockQuery , Source .DEFAULT );
58
58
}
59
59
60
60
@ Test
61
61
public void testLoadInitial_success () throws Exception {
62
62
mockQuerySuccess (new ArrayList <DocumentSnapshot >());
63
63
64
64
TestObserver <LoadingState > observer = new TestObserver <>(2 );
65
- mPagingSource .getLoadingState ().observeForever (observer );
65
+ mDataSource .getLoadingState ().observeForever (observer );
66
66
67
67
// Kick off an initial load of 20 items
68
- PagingSource .LoadParams .Refresh <PageKey > params = new PagingSource .LoadParams .Refresh <>(null , 20 , false );
69
- mPagingSource .loadSingle (params ).blockingSubscribe ();
68
+ PageKeyedDataSource .LoadInitialParams <PageKey > params =
69
+ new PageKeyedDataSource .LoadInitialParams <>(20 , false );
70
+ mDataSource .loadInitial (params , mInitialCallback );
70
71
71
- // Should go from LOADING_INITIAL --> LOADED --> LOADING_FINISHED
72
+ // Should go from LOADING_INITIAL --> LOADED
72
73
observer .await ();
73
- observer .assertResults (Arrays .asList (LoadingState .LOADING_INITIAL , LoadingState .LOADED ,
74
- LoadingState .FINISHED ));
74
+ observer .assertResults (Arrays .asList (LoadingState .LOADING_INITIAL , LoadingState .LOADED ));
75
75
}
76
76
77
77
@ Test
78
78
public void testLoadInitial_failure () throws Exception {
79
79
mockQueryFailure ("Could not get initial documents." );
80
80
81
81
TestObserver <LoadingState > observer = new TestObserver <>(2 );
82
- mPagingSource .getLoadingState ().observeForever (observer );
82
+ mDataSource .getLoadingState ().observeForever (observer );
83
83
84
84
// Kick off an initial load of 20 items
85
- PagingSource .LoadParams .Refresh <PageKey > params = new PagingSource .LoadParams .Refresh <>(null , 20 , false );
86
- mPagingSource .loadSingle (params ).blockingSubscribe ();
85
+ PageKeyedDataSource .LoadInitialParams <PageKey > params =
86
+ new PageKeyedDataSource .LoadInitialParams <>(20 , false );
87
+ mDataSource .loadInitial (params , mInitialCallback );
87
88
88
89
// Should go from LOADING_INITIAL --> ERROR
89
90
observer .await ();
@@ -95,39 +96,71 @@ public void testLoadAfter_success() throws Exception {
95
96
mockQuerySuccess (new ArrayList <DocumentSnapshot >());
96
97
97
98
TestObserver <LoadingState > observer = new TestObserver <>(2 );
98
- mPagingSource .getLoadingState ().observeForever (observer );
99
+ mDataSource .getLoadingState ().observeForever (observer );
99
100
100
101
// Kick off an initial load of 20 items
101
102
PageKey pageKey = new PageKey (null , null );
102
- PagingSource .LoadParams .Append <PageKey > params = new PagingSource .LoadParams .Append <>(pageKey , 20 , false );
103
- mPagingSource .loadSingle (params ).blockingSubscribe ();
103
+ PageKeyedDataSource .LoadParams <PageKey > params =
104
+ new PageKeyedDataSource .LoadParams <>(pageKey , 20 );
105
+ mDataSource .loadAfter (params , mAfterCallback );
104
106
105
- // Should go from LOADING_MORE --> LOADED --> LOADING_FINISHED
107
+ // Should go from LOADING_MORE --> LOADED
106
108
observer .await ();
107
- observer .assertResults (Arrays .asList (LoadingState .LOADING_MORE , LoadingState .LOADED ,
108
- LoadingState .FINISHED ));
109
+ observer .assertResults (Arrays .asList (LoadingState .LOADING_MORE , LoadingState .LOADED ));
109
110
}
110
111
111
112
@ Test
112
113
public void testLoadAfter_failure () throws Exception {
113
114
mockQueryFailure ("Could not load more documents." );
114
115
115
116
TestObserver <LoadingState > observer = new TestObserver <>(2 );
116
- mPagingSource .getLoadingState ().observeForever (observer );
117
+ mDataSource .getLoadingState ().observeForever (observer );
117
118
118
119
// Kick off an initial load of 20 items
119
120
PageKey pageKey = new PageKey (null , null );
120
- PagingSource .LoadParams .Append <PageKey > params = new PagingSource .LoadParams .Append <>(pageKey , 20 , false );
121
- mPagingSource .loadSingle (params ).blockingSubscribe ();
121
+ PageKeyedDataSource .LoadParams <PageKey > params =
122
+ new PageKeyedDataSource .LoadParams <>(pageKey , 20 );
123
+ mDataSource .loadAfter (params , mAfterCallback );
122
124
123
125
// Should go from LOADING_MORE --> ERROR
124
126
observer .await ();
125
127
observer .assertResults (Arrays .asList (LoadingState .LOADING_MORE , LoadingState .ERROR ));
126
128
}
127
129
130
+ @ Test
131
+ public void testLoadAfter_retry () throws Exception {
132
+ mockQueryFailure ("Could not load more documents." );
133
+
134
+ TestObserver <LoadingState > observer1 = new TestObserver <>(2 );
135
+ mDataSource .getLoadingState ().observeForever (observer1 );
136
+
137
+ // Kick off an initial load of 20 items
138
+ PageKey pageKey = new PageKey (null , null );
139
+ PageKeyedDataSource .LoadParams <PageKey > params =
140
+ new PageKeyedDataSource .LoadParams <>(pageKey , 20 );
141
+ mDataSource .loadAfter (params , mAfterCallback );
142
+
143
+ // Should go from LOADING_MORE --> ERROR
144
+ observer1 .await ();
145
+ observer1 .assertResults (Arrays .asList (LoadingState .LOADING_MORE , LoadingState .ERROR ));
146
+
147
+ // Create a new observer
148
+ TestObserver <LoadingState > observer2 = new TestObserver <>(3 );
149
+ mDataSource .getLoadingState ().observeForever (observer2 );
150
+
151
+ // Retry the load
152
+ mockQuerySuccess (new ArrayList <DocumentSnapshot >());
153
+ mDataSource .retry ();
154
+
155
+ // Should go from ERROR --> LOADING_MORE --> SUCCESS
156
+ observer2 .await ();
157
+ observer2 .assertResults (
158
+ Arrays .asList (LoadingState .ERROR , LoadingState .LOADING_MORE , LoadingState .LOADED ));
159
+ }
160
+
128
161
private void initMockQuery () {
129
- when (mMockQuery .startAfter (any (DocumentSnapshot . class ))).thenReturn (mMockQuery );
130
- when (mMockQuery .endBefore (any (DocumentSnapshot . class ))).thenReturn (mMockQuery );
162
+ when (mMockQuery .startAfter (any ())).thenReturn (mMockQuery );
163
+ when (mMockQuery .endBefore (any ())).thenReturn (mMockQuery );
131
164
when (mMockQuery .limit (anyLong ())).thenReturn (mMockQuery );
132
165
}
133
166
0 commit comments