|
58 | 58 | import com.google.android.gms.cloudmessaging.CloudMessage;
|
59 | 59 | import com.google.android.gms.cloudmessaging.Rpc;
|
60 | 60 | import com.google.common.collect.ImmutableSet;
|
61 |
| -import com.google.common.collect.Sets; |
62 | 61 | import com.google.firebase.FirebaseApp;
|
63 | 62 | import com.google.firebase.FirebaseOptions;
|
64 | 63 | import com.google.firebase.iid.FirebaseInstanceIdReceiver;
|
@@ -106,6 +105,8 @@ public class FirebaseMessagingServiceRoboTest {
|
106 | 105 | // blank activity
|
107 | 106 | public static class MyTestActivity extends Activity {}
|
108 | 107 |
|
| 108 | + public static class MySecondTestActivity extends Activity {} |
| 109 | + |
109 | 110 | private static final AnalyticsValidator analyticsValidator =
|
110 | 111 | FakeConnectorComponent.getAnalyticsValidator();
|
111 | 112 |
|
@@ -502,6 +503,43 @@ public void testNotification_clickAnalytics_recreateActivity() throws Exception
|
502 | 503 | }
|
503 | 504 | }
|
504 | 505 |
|
| 506 | + /** Test that a notification does not re-log events when the same notification is found. */ |
| 507 | + @Test |
| 508 | + public void notification_clickAnalytics_duplicateMessageId() throws Exception { |
| 509 | + FirebaseMessaging.getInstance(firebaseApp); // register activity lifecycle friends |
| 510 | + simulateNotificationMessageWithAnalytics(); |
| 511 | + |
| 512 | + PendingIntent clickPendingIntent = getSingleShownNotification().contentIntent; |
| 513 | + try (ActivityScenario<MyTestActivity> scenario = |
| 514 | + dispatchActivityIntentToActivity(clickPendingIntent)) { |
| 515 | + Intent secondActivityIntent = new Intent(); |
| 516 | + secondActivityIntent.putExtras(shadowOf(clickPendingIntent).getSavedIntent()); |
| 517 | + // Start another Activity with the same notification and check it doesn't log open again. |
| 518 | + try (ActivityScenario<MySecondTestActivity> secondScenario = |
| 519 | + ActivityScenario.launch(secondActivityIntent)) { |
| 520 | + List<AnalyticsValidator.LoggedEvent> events = analyticsValidator.getLoggedEvents(); |
| 521 | + assertThat(events).hasSize(2); |
| 522 | + AnalyticsValidator.LoggedEvent receiveEvent = events.get(0); |
| 523 | + assertThat(receiveEvent.getOrigin()).isEqualTo(Analytics.ORIGIN_FCM); |
| 524 | + assertThat(receiveEvent.getName()).isEqualTo(Analytics.EVENT_NOTIFICATION_RECEIVE); |
| 525 | + assertThat(receiveEvent.getParams()) |
| 526 | + .string(Analytics.PARAM_MESSAGE_ID) |
| 527 | + .isEqualTo("composer_key"); |
| 528 | + assertThat(receiveEvent.getParams()) |
| 529 | + .string(Analytics.PARAM_MESSAGE_NAME) |
| 530 | + .isEqualTo("composer_label"); |
| 531 | + assertThat(receiveEvent.getParams()) |
| 532 | + .integer(Analytics.PARAM_MESSAGE_TIME) |
| 533 | + .isEqualTo(1234567890); |
| 534 | + assertThat(receiveEvent.getParams()).doesNotContainKey(Analytics.PARAM_TOPIC); |
| 535 | + |
| 536 | + AnalyticsValidator.LoggedEvent openEvent = events.get(1); |
| 537 | + assertThat(openEvent.getOrigin()).isEqualTo(Analytics.ORIGIN_FCM); |
| 538 | + assertThat(openEvent.getName()).isEqualTo(Analytics.EVENT_NOTIFICATION_OPEN); |
| 539 | + } |
| 540 | + } |
| 541 | + } |
| 542 | + |
505 | 543 | /** Test that a notification logs the correct event on dismiss. */
|
506 | 544 | @Test
|
507 | 545 | public void testNotification_dismissAnalytics() throws Exception {
|
@@ -768,43 +806,4 @@ private static Set<ServiceConnection> getBoundServiceConnections() {
|
768 | 806 | .getBoundServiceConnections();
|
769 | 807 | return ImmutableSet.copyOf(connList.toArray(new ServiceConnection[0]));
|
770 | 808 | }
|
771 |
| - |
772 |
| - /** |
773 |
| - * Calls handleIntent on {@link #service} in a background thread and connects any outgoing |
774 |
| - * bindService calls made in the meantime. |
775 |
| - * |
776 |
| - * <p>Returns a CountDownLatch that's finished when the call to handleIntent finishes. |
777 |
| - */ |
778 |
| - private CountDownLatch handleIntent(Intent intent, int time, TimeUnit unit) throws Exception { |
779 |
| - long timeoutAtMillis = System.currentTimeMillis() + unit.toMillis(time); |
780 |
| - |
781 |
| - // Connections that were active before we started |
782 |
| - Set<ServiceConnection> previousConnections = getBoundServiceConnections(); |
783 |
| - |
784 |
| - // Run the thing that will cause a bindService call in the background |
785 |
| - CountDownLatch finishedLatch = new CountDownLatch(1); |
786 |
| - executorService.execute( |
787 |
| - () -> { |
788 |
| - service.handleIntent(intent); |
789 |
| - finishedLatch.countDown(); |
790 |
| - }); |
791 |
| - |
792 |
| - // wait for the call to be made (service connection to appear) |
793 |
| - while (Sets.difference(getBoundServiceConnections(), previousConnections).isEmpty()) { |
794 |
| - assertWithMessage("timed out waiting for binder connection") |
795 |
| - .that(System.currentTimeMillis()) |
796 |
| - .isLessThan(timeoutAtMillis); |
797 |
| - |
798 |
| - TimeUnit.MILLISECONDS.sleep(50); |
799 |
| - } |
800 |
| - |
801 |
| - CountDownLatch flushLatch = new CountDownLatch(1); |
802 |
| - new Handler(Looper.getMainLooper()).post(flushLatch::countDown); |
803 |
| - shadowOf(Looper.getMainLooper()).idle(); |
804 |
| - |
805 |
| - long millisRemaining = Math.max(0, timeoutAtMillis - System.currentTimeMillis()); |
806 |
| - assertThat(flushLatch.await(millisRemaining, TimeUnit.MILLISECONDS)).isTrue(); |
807 |
| - |
808 |
| - return finishedLatch; |
809 |
| - } |
810 | 809 | }
|
0 commit comments