17
17
import static com .google .common .truth .Truth .assertThat ;
18
18
import static org .junit .Assert .assertNull ;
19
19
import static org .junit .Assert .fail ;
20
+ import static org .mockito .ArgumentMatchers .any ;
20
21
import static org .mockito .ArgumentMatchers .anyLong ;
21
22
import static org .mockito .ArgumentMatchers .anyString ;
22
- import static org .mockito .Mockito .any ;
23
23
import static org .mockito .Mockito .when ;
24
24
25
25
import androidx .annotation .NonNull ;
26
26
import androidx .test .core .app .ApplicationProvider ;
27
- import androidx .test .ext .junit .runners .AndroidJUnit4 ;
28
27
import com .google .android .gms .tasks .Tasks ;
29
28
import com .google .firebase .FirebaseApp ;
30
29
import com .google .firebase .FirebaseOptions ;
34
33
import com .google .firebase .segmentation .local .CustomInstallationIdCacheEntryValue ;
35
34
import com .google .firebase .segmentation .remote .SegmentationServiceClient ;
36
35
import java .util .concurrent .ExecutionException ;
36
+ import java .util .concurrent .ExecutorService ;
37
+ import java .util .concurrent .LinkedBlockingQueue ;
38
+ import java .util .concurrent .ThreadPoolExecutor ;
39
+ import java .util .concurrent .TimeUnit ;
37
40
import org .junit .After ;
38
41
import org .junit .Before ;
39
42
import org .junit .FixMethodOrder ;
42
45
import org .junit .runners .MethodSorters ;
43
46
import org .mockito .Mock ;
44
47
import org .mockito .MockitoAnnotations ;
48
+ import org .robolectric .RobolectricTestRunner ;
45
49
46
- /**
47
- * Instrumented test, which will execute on an Android device.
48
- *
49
- * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
50
- */
51
- @ RunWith (AndroidJUnit4 .class )
50
+ @ RunWith (RobolectricTestRunner .class )
52
51
@ FixMethodOrder (MethodSorters .NAME_ASCENDING )
53
- public class FirebaseSegmentationInstrumentedTest {
54
-
52
+ public class FirebaseSegmentationTest {
55
53
private static final String CUSTOM_INSTALLATION_ID = "123" ;
56
54
private static final String FIREBASE_INSTANCE_ID = "cAAAAAAAAAA" ;
57
55
58
56
private FirebaseApp firebaseApp ;
59
57
@ Mock private FirebaseInstanceId firebaseInstanceId ;
60
58
@ Mock private SegmentationServiceClient backendClientReturnsOk ;
61
59
@ Mock private SegmentationServiceClient backendClientReturnsError ;
60
+
62
61
private CustomInstallationIdCache actualCache ;
63
62
@ Mock private CustomInstallationIdCache cacheReturnsError ;
64
63
64
+ private ExecutorService taskExecutor ;
65
+
65
66
@ Before
66
67
public void setUp () {
67
68
MockitoAnnotations .initMocks (this );
@@ -105,6 +106,8 @@ public String getToken() {
105
106
}));
106
107
when (cacheReturnsError .insertOrUpdateCacheEntry (any ())).thenReturn (false );
107
108
when (cacheReturnsError .readCacheEntryValue ()).thenReturn (null );
109
+
110
+ taskExecutor = new ThreadPoolExecutor (0 , 1 , 30L , TimeUnit .SECONDS , new LinkedBlockingQueue <>());
108
111
}
109
112
110
113
@ After
@@ -119,7 +122,11 @@ public void testUpdateCustomInstallationId_CacheOk_BackendOk() throws Exception
119
122
firebaseApp , firebaseInstanceId , actualCache , backendClientReturnsOk );
120
123
121
124
// No exception, means success.
122
- assertNull (Tasks .await (firebaseSegmentation .setCustomInstallationId (CUSTOM_INSTALLATION_ID )));
125
+ TestOnCompleteListener <Void > onCompleteListener = new TestOnCompleteListener <>();
126
+ firebaseSegmentation
127
+ .setCustomInstallationId (CUSTOM_INSTALLATION_ID )
128
+ .addOnCompleteListener (taskExecutor , onCompleteListener );
129
+ assertNull (onCompleteListener .await ());
123
130
CustomInstallationIdCacheEntryValue entryValue = actualCache .readCacheEntryValue ();
124
131
assertThat (entryValue .getCustomInstallationId ()).isEqualTo (CUSTOM_INSTALLATION_ID );
125
132
assertThat (entryValue .getFirebaseInstanceId ()).isEqualTo (FIREBASE_INSTANCE_ID );
@@ -135,7 +142,11 @@ public void testUpdateCustomInstallationId_CacheOk_BackendError_Retryable()
135
142
136
143
// Expect exception
137
144
try {
138
- Tasks .await (firebaseSegmentation .setCustomInstallationId (CUSTOM_INSTALLATION_ID ));
145
+ TestOnCompleteListener <Void > onCompleteListener = new TestOnCompleteListener <>();
146
+ firebaseSegmentation
147
+ .setCustomInstallationId (CUSTOM_INSTALLATION_ID )
148
+ .addOnCompleteListener (taskExecutor , onCompleteListener );
149
+ onCompleteListener .await ();
139
150
fail ();
140
151
} catch (ExecutionException expected ) {
141
152
Throwable cause = expected .getCause ();
@@ -163,7 +174,11 @@ public void testUpdateCustomInstallationId_CacheOk_BackendError_NotRetryable()
163
174
164
175
// Expect exception
165
176
try {
166
- Tasks .await (firebaseSegmentation .setCustomInstallationId (CUSTOM_INSTALLATION_ID ));
177
+ TestOnCompleteListener <Void > onCompleteListener = new TestOnCompleteListener <>();
178
+ firebaseSegmentation
179
+ .setCustomInstallationId (CUSTOM_INSTALLATION_ID )
180
+ .addOnCompleteListener (taskExecutor , onCompleteListener );
181
+ onCompleteListener .await ();
167
182
fail ();
168
183
} catch (ExecutionException expected ) {
169
184
Throwable cause = expected .getCause ();
@@ -184,7 +199,11 @@ public void testUpdateCustomInstallationId_CacheError_BackendOk() throws Interru
184
199
185
200
// Expect exception
186
201
try {
187
- Tasks .await (firebaseSegmentation .setCustomInstallationId (CUSTOM_INSTALLATION_ID ));
202
+ TestOnCompleteListener <Void > onCompleteListener = new TestOnCompleteListener <>();
203
+ firebaseSegmentation
204
+ .setCustomInstallationId (CUSTOM_INSTALLATION_ID )
205
+ .addOnCompleteListener (taskExecutor , onCompleteListener );
206
+ onCompleteListener .await ();
188
207
fail ();
189
208
} catch (ExecutionException expected ) {
190
209
Throwable cause = expected .getCause ();
@@ -206,7 +225,11 @@ public void testClearCustomInstallationId_CacheOk_BackendOk() throws Exception {
206
225
firebaseApp , firebaseInstanceId , actualCache , backendClientReturnsOk );
207
226
208
227
// No exception, means success.
209
- assertNull (Tasks .await (firebaseSegmentation .setCustomInstallationId (null )));
228
+ TestOnCompleteListener <Void > onCompleteListener = new TestOnCompleteListener <>();
229
+ firebaseSegmentation
230
+ .setCustomInstallationId (null )
231
+ .addOnCompleteListener (taskExecutor , onCompleteListener );
232
+ assertNull (onCompleteListener .await ());
210
233
CustomInstallationIdCacheEntryValue entryValue = actualCache .readCacheEntryValue ();
211
234
assertNull (entryValue );
212
235
}
@@ -224,7 +247,11 @@ public void testClearCustomInstallationId_CacheOk_BackendError() throws Exceptio
224
247
225
248
// Expect exception
226
249
try {
227
- Tasks .await (firebaseSegmentation .setCustomInstallationId (null ));
250
+ TestOnCompleteListener <Void > onCompleteListener = new TestOnCompleteListener <>();
251
+ firebaseSegmentation
252
+ .setCustomInstallationId (null )
253
+ .addOnCompleteListener (taskExecutor , onCompleteListener );
254
+ onCompleteListener .await ();
228
255
fail ();
229
256
} catch (ExecutionException expected ) {
230
257
Throwable cause = expected .getCause ();
@@ -248,7 +275,11 @@ public void testClearCustomInstallationId_CacheError_BackendOk() throws Interrup
248
275
249
276
// Expect exception
250
277
try {
251
- Tasks .await (firebaseSegmentation .setCustomInstallationId (CUSTOM_INSTALLATION_ID ));
278
+ TestOnCompleteListener <Void > onCompleteListener = new TestOnCompleteListener <>();
279
+ firebaseSegmentation
280
+ .setCustomInstallationId (null )
281
+ .addOnCompleteListener (taskExecutor , onCompleteListener );
282
+ onCompleteListener .await ();
252
283
fail ();
253
284
} catch (ExecutionException expected ) {
254
285
Throwable cause = expected .getCause ();
0 commit comments