Skip to content

Commit 530196b

Browse files
committed
Add test for notification_open duplicate logging based on message ID.
1 parent 33f2413 commit 530196b

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

firebase-messaging/src/test/java/com/google/firebase/messaging/FirebaseMessagingServiceRoboTest.java

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import com.google.android.gms.cloudmessaging.CloudMessage;
5959
import com.google.android.gms.cloudmessaging.Rpc;
6060
import com.google.common.collect.ImmutableSet;
61-
import com.google.common.collect.Sets;
6261
import com.google.firebase.FirebaseApp;
6362
import com.google.firebase.FirebaseOptions;
6463
import com.google.firebase.iid.FirebaseInstanceIdReceiver;
@@ -106,6 +105,8 @@ public class FirebaseMessagingServiceRoboTest {
106105
// blank activity
107106
public static class MyTestActivity extends Activity {}
108107

108+
public static class MySecondTestActivity extends Activity {}
109+
109110
private static final AnalyticsValidator analyticsValidator =
110111
FakeConnectorComponent.getAnalyticsValidator();
111112

@@ -502,6 +503,43 @@ public void testNotification_clickAnalytics_recreateActivity() throws Exception
502503
}
503504
}
504505

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+
505543
/** Test that a notification logs the correct event on dismiss. */
506544
@Test
507545
public void testNotification_dismissAnalytics() throws Exception {
@@ -768,43 +806,4 @@ private static Set<ServiceConnection> getBoundServiceConnections() {
768806
.getBoundServiceConnections();
769807
return ImmutableSet.copyOf(connList.toArray(new ServiceConnection[0]));
770808
}
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-
}
810809
}

0 commit comments

Comments
 (0)