17
17
import static com .google .common .truth .Truth .assertWithMessage ;
18
18
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_APP_ID_1 ;
19
19
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_AUTH_TOKEN ;
20
+ import static com .google .firebase .installations .FisAndroidTestConstants .TEST_CREATION_TIMESTAMP_1 ;
20
21
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_FID_1 ;
21
22
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_PROJECT_ID ;
22
23
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_REFRESH_TOKEN ;
28
29
29
30
import androidx .test .core .app .ApplicationProvider ;
30
31
import androidx .test .runner .AndroidJUnit4 ;
32
+ import com .google .android .gms .common .util .Clock ;
31
33
import com .google .android .gms .tasks .Tasks ;
32
34
import com .google .firebase .FirebaseApp ;
33
35
import com .google .firebase .FirebaseOptions ;
37
39
import com .google .firebase .installations .remote .FirebaseInstallationServiceException ;
38
40
import com .google .firebase .installations .remote .InstallationResponse ;
39
41
import java .util .concurrent .ExecutionException ;
40
- import java .util .concurrent .Executor ;
42
+ import java .util .concurrent .ExecutorService ;
41
43
import java .util .concurrent .SynchronousQueue ;
42
44
import java .util .concurrent .ThreadPoolExecutor ;
43
45
import java .util .concurrent .TimeUnit ;
59
61
@ FixMethodOrder (MethodSorters .NAME_ASCENDING )
60
62
public class FirebaseInstallationsInstrumentedTest {
61
63
private FirebaseApp firebaseApp ;
62
- private Executor executor ;
64
+ private ExecutorService executor ;
63
65
private PersistedFid persistedFid ;
64
66
@ Mock private FirebaseInstallationServiceClient backendClientReturnsOk ;
65
67
@ Mock private FirebaseInstallationServiceClient backendClientReturnsError ;
66
68
@ Mock private PersistedFid persistedFidReturnsError ;
67
69
@ Mock private Utils mockUtils ;
70
+ @ Mock private Clock mockClock ;
68
71
69
72
@ Before
70
73
public void setUp () throws FirebaseInstallationServiceException {
71
74
MockitoAnnotations .initMocks (this );
72
75
FirebaseApp .clearInstancesForTest ();
73
- executor = new ThreadPoolExecutor (0 , 2 , 30L , TimeUnit .SECONDS , new SynchronousQueue <>());
76
+ executor = new ThreadPoolExecutor (0 , 2 , 10L , TimeUnit .SECONDS , new SynchronousQueue <>());
74
77
firebaseApp =
75
78
FirebaseApp .initializeApp (
76
79
ApplicationProvider .getApplicationContext (),
@@ -100,6 +103,7 @@ public void setUp() throws FirebaseInstallationServiceException {
100
103
when (persistedFidReturnsError .insertOrUpdatePersistedFidEntry (any ())).thenReturn (false );
101
104
when (persistedFidReturnsError .readPersistedFidEntryValue ()).thenReturn (null );
102
105
when (mockUtils .createRandomFid ()).thenReturn (TEST_FID_1 );
106
+ when (mockClock .currentTimeMillis ()).thenReturn (TEST_CREATION_TIMESTAMP_1 );
103
107
}
104
108
105
109
@ After
@@ -111,7 +115,7 @@ public void cleanUp() throws Exception {
111
115
public void testGetId_PersistedFidOk_BackendOk () throws Exception {
112
116
FirebaseInstallations firebaseInstallations =
113
117
new FirebaseInstallations (
114
- executor , firebaseApp , backendClientReturnsOk , persistedFid , mockUtils );
118
+ mockClock , executor , firebaseApp , backendClientReturnsOk , persistedFid , mockUtils );
115
119
116
120
// No exception, means success.
117
121
assertWithMessage ("getId Task fails." )
@@ -126,7 +130,7 @@ public void testGetId_PersistedFidOk_BackendOk() throws Exception {
126
130
.isEqualTo (PersistedFid .RegistrationStatus .PENDING );
127
131
128
132
// Waiting for Task that registers FID on the FIS Servers
129
- Thread . sleep (500 );
133
+ executor . awaitTermination (500 , TimeUnit . MILLISECONDS );
130
134
131
135
PersistedFidEntry updatedFidEntry = persistedFid .readPersistedFidEntryValue ();
132
136
assertWithMessage ("Persisted Fid doesn't match" )
@@ -141,7 +145,7 @@ public void testGetId_PersistedFidOk_BackendOk() throws Exception {
141
145
public void testGetId_multipleCalls_sameFIDReturned () throws Exception {
142
146
FirebaseInstallations firebaseInstallations =
143
147
new FirebaseInstallations (
144
- executor , firebaseApp , backendClientReturnsOk , persistedFid , mockUtils );
148
+ mockClock , executor , firebaseApp , backendClientReturnsOk , persistedFid , mockUtils );
145
149
146
150
// No exception, means success.
147
151
assertWithMessage ("getId Task fails." )
@@ -158,7 +162,7 @@ public void testGetId_multipleCalls_sameFIDReturned() throws Exception {
158
162
Tasks .await (firebaseInstallations .getId ());
159
163
160
164
// Waiting for Task that registers FID on the FIS Servers
161
- Thread . sleep (500 );
165
+ executor . awaitTermination (500 , TimeUnit . MILLISECONDS );
162
166
163
167
PersistedFidEntry updatedFidEntry = persistedFid .readPersistedFidEntryValue ();
164
168
assertWithMessage ("Persisted Fid doesn't match" )
@@ -173,7 +177,7 @@ public void testGetId_multipleCalls_sameFIDReturned() throws Exception {
173
177
public void testGetId_PersistedFidOk_BackendError () throws Exception {
174
178
FirebaseInstallations firebaseInstallations =
175
179
new FirebaseInstallations (
176
- executor , firebaseApp , backendClientReturnsError , persistedFid , mockUtils );
180
+ mockClock , executor , firebaseApp , backendClientReturnsError , persistedFid , mockUtils );
177
181
178
182
Tasks .await (firebaseInstallations .getId ());
179
183
@@ -186,7 +190,7 @@ public void testGetId_PersistedFidOk_BackendError() throws Exception {
186
190
.isEqualTo (PersistedFid .RegistrationStatus .PENDING );
187
191
188
192
// Waiting for Task that registers FID on the FIS Servers
189
- Thread . sleep (500 );
193
+ executor . awaitTermination (500 , TimeUnit . MILLISECONDS );
190
194
191
195
PersistedFidEntry updatedFidEntry = persistedFid .readPersistedFidEntryValue ();
192
196
assertWithMessage ("Persisted Fid doesn't match" )
@@ -201,7 +205,12 @@ public void testGetId_PersistedFidOk_BackendError() throws Exception {
201
205
public void testGetId_PersistedFidError_BackendOk () throws InterruptedException {
202
206
FirebaseInstallations firebaseInstallations =
203
207
new FirebaseInstallations (
204
- executor , firebaseApp , backendClientReturnsOk , persistedFidReturnsError , mockUtils );
208
+ mockClock ,
209
+ executor ,
210
+ firebaseApp ,
211
+ backendClientReturnsOk ,
212
+ persistedFidReturnsError ,
213
+ mockUtils );
205
214
206
215
// Expect exception
207
216
try {
0 commit comments