26
26
import static org .mockito .ArgumentMatchers .anyString ;
27
27
import static org .mockito .ArgumentMatchers .argThat ;
28
28
import static org .mockito .ArgumentMatchers .eq ;
29
+ import static org .mockito .Mockito .doAnswer ;
29
30
import static org .mockito .Mockito .doReturn ;
30
31
import static org .mockito .Mockito .mock ;
31
32
import static org .mockito .Mockito .never ;
32
33
import static org .mockito .Mockito .spy ;
33
34
import static org .mockito .Mockito .times ;
34
35
import static org .mockito .Mockito .verify ;
36
+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
35
37
import static org .robolectric .Shadows .shadowOf ;
36
38
37
39
import android .app .Activity ;
53
55
import android .os .Process ;
54
56
import androidx .test .core .app .ActivityScenario ;
55
57
import androidx .test .core .app .ApplicationProvider ;
58
+ import com .google .android .gms .cloudmessaging .CloudMessage ;
59
+ import com .google .android .gms .cloudmessaging .Rpc ;
56
60
import com .google .common .collect .ImmutableSet ;
57
61
import com .google .common .collect .Sets ;
58
62
import com .google .firebase .FirebaseApp ;
74
78
import org .junit .Before ;
75
79
import org .junit .Test ;
76
80
import org .junit .runner .RunWith ;
81
+ import org .mockito .ArgumentCaptor ;
77
82
import org .mockito .ArgumentMatcher ;
78
83
import org .mockito .MockitoAnnotations ;
79
84
import org .robolectric .Robolectric ;
@@ -211,6 +216,43 @@ public void testMessage_nullIntentExtras() throws Exception {
211
216
assertThat (shadowOf (context ).getNextStartedService ()).isNull ();
212
217
}
213
218
219
+ @ Test
220
+ public void messageHandled () throws Exception {
221
+ RemoteMessageBuilder builder =
222
+ new RemoteMessageBuilder ()
223
+ .setFrom (DEFAULT_FROM )
224
+ .setTo (DEFAULT_TO )
225
+ .addData ("key1" , "value1" )
226
+ .setMessageId ("message_id_456" )
227
+ .setSentTime (123456789 );
228
+ Intent intent = builder .buildIntent ();
229
+ Rpc mockRpc = mock (Rpc .class );
230
+ service .setRpcForTesting (mockRpc );
231
+ CountDownLatch latch = new CountDownLatch (1 );
232
+ doAnswer (
233
+ invocation -> {
234
+ latch .await ();
235
+ return null ;
236
+ })
237
+ .when (service )
238
+ .onMessageReceived (any (RemoteMessage .class ));
239
+
240
+ shadowOf (context ).clearStartedServices ();
241
+ sendBroadcastToReceiver (intent );
242
+ processInternalStartService (context );
243
+
244
+ // Shouldn't call messageHandled while onMessageReceived is still running.
245
+ verifyNoMoreInteractions (mockRpc );
246
+ // Finish onMessageReceived, messageHandled should now be called.
247
+ latch .countDown ();
248
+ flushTasks ();
249
+ ArgumentCaptor <CloudMessage > messageCaptor = ArgumentCaptor .forClass (CloudMessage .class );
250
+ verify (mockRpc ).messageHandled (messageCaptor .capture ());
251
+ CloudMessage message = messageCaptor .getValue ();
252
+ assertThat (message ).isNotNull ();
253
+ assertThat (message .getIntent ()).isEqualTo (intent );
254
+ }
255
+
214
256
@ Test
215
257
public void testDuplicateMessageDropped () throws Exception {
216
258
String messageId = "a.message.id" ;
@@ -308,6 +350,7 @@ public void testOnNewToken() throws Exception {
308
350
309
351
ServiceStarter .getInstance ().startMessagingService (context , intent );
310
352
processInternalStartService (context );
353
+ flushTasks ();
311
354
312
355
verify (service ).onNewToken ("token123" );
313
356
}
@@ -685,6 +728,7 @@ public void startServiceViaReceiver(Intent intent) throws InterruptedException {
685
728
shadowOf (context ).clearStartedServices ();
686
729
sendBroadcastToReceiver (intent );
687
730
processInternalStartService (context );
731
+ flushTasks ();
688
732
}
689
733
690
734
/**
@@ -700,7 +744,6 @@ public void processInternalStartService(Application application) throws Interrup
700
744
assertEquals (application .getPackageName (), serviceIntent .getPackage ());
701
745
702
746
service .onStartCommand (serviceIntent , 0 /* flags */ , 1 /* startId */ );
703
- flushTasks ();
704
747
}
705
748
706
749
// Flush the Service background tasks
0 commit comments