Add test coverage for important public API's under Notification

Bug: 134702128
Test: Robolectric
Change-Id: I4d60c92d40a1fe6a5457106cfdd7fb9d9cab5751
diff --git a/src/com/android/car/notification/PreprocessingManager.java b/src/com/android/car/notification/PreprocessingManager.java
index 624960c..7ccc027 100644
--- a/src/com/android/car/notification/PreprocessingManager.java
+++ b/src/com/android/car/notification/PreprocessingManager.java
@@ -430,6 +430,11 @@
         return notifications;
     }
 
+    @VisibleForTesting
+    protected Map getOldNotifications() {
+        return mOldNotifications;
+    }
+
     public void setCarUxRestrictionManagerWrapper(CarUxRestrictionManagerWrapper manager) {
         try {
             if (manager == null || manager.getCurrentCarUxRestrictions() == null) {
diff --git a/tests/robotests/src/com/android/car/notification/NotificationClickHandlerFactoryTest.java b/tests/robotests/src/com/android/car/notification/NotificationClickHandlerFactoryTest.java
index 0f53dca..9b18367 100644
--- a/tests/robotests/src/com/android/car/notification/NotificationClickHandlerFactoryTest.java
+++ b/tests/robotests/src/com/android/car/notification/NotificationClickHandlerFactoryTest.java
@@ -34,6 +34,8 @@
 import android.content.Context;
 import android.content.Intent;
 import android.os.RemoteException;
+import android.os.UserHandle;
+import android.service.notification.NotificationStats;
 import android.service.notification.StatusBarNotification;
 import android.view.View;
 import android.widget.Button;
@@ -146,6 +148,32 @@
     }
 
     @Test
+    public void onClickClickHandler_shouldAutoCancel_callsBarServiceOnNotificationClear() {
+        mAlertEntry1.getStatusBarNotification().getNotification().flags =
+                mStatusBarNotification1.getNotification().flags | Notification.FLAG_AUTO_CANCEL;
+        UserHandle user = new UserHandle( /* handle= */ 0);
+        when(mStatusBarNotification1.getUser()).thenReturn(user);
+        mNotificationClickHandlerFactory.getClickHandler(mAlertEntry1).onClick(mView);
+        NotificationVisibility notificationVisibility = NotificationVisibility.obtain(
+                mAlertEntry1.getKey(), /* rank= */ -1, /* count= */ -1, /* visible= */ true);
+
+        try {
+            verify(mBarService).onNotificationClear(
+                    mAlertEntry1.getStatusBarNotification().getPackageName(),
+                    mAlertEntry1.getStatusBarNotification().getTag(),
+                    mAlertEntry1.getStatusBarNotification().getId(),
+                    mAlertEntry1.getStatusBarNotification().getUser().getIdentifier(),
+                    mAlertEntry1.getKey(),
+                    NotificationStats.DISMISSAL_SHADE,
+                    NotificationStats.DISMISS_SENTIMENT_NEUTRAL,
+                    notificationVisibility);
+        } catch (RemoteException ex) {
+            // ignore
+        }
+
+    }
+
+    @Test
     public void onClickActionClickHandler_isReplyAction_sendsPendingIntent() {
         ShadowCarAssistUtils.addMessageNotification(mStatusBarNotification1.getKey());
         PendingIntent pendingIntent = PendingIntent.getForegroundService(
diff --git a/tests/robotests/src/com/android/car/notification/PreprocessingManagerTest.java b/tests/robotests/src/com/android/car/notification/PreprocessingManagerTest.java
index 17ce9b2..8249353 100644
--- a/tests/robotests/src/com/android/car/notification/PreprocessingManagerTest.java
+++ b/tests/robotests/src/com/android/car/notification/PreprocessingManagerTest.java
@@ -423,6 +423,75 @@
         }
     }
 
+    @Test
+    public void onUpdateNotifications_notificationRemoved_removesNotification() {
+        mPreprocessingManager.init(mAlertEntriesMap, mRankingMap);
+
+        List<NotificationGroup> newList =
+                mPreprocessingManager.updateNotifications(
+                        /* showLessImportantNotifications= */ false,
+                        mImportantForeground,
+                        CarNotificationListener.NOTIFY_NOTIFICATION_REMOVED,
+                        mRankingMap);
+
+        assertThat(mPreprocessingManager.getOldNotifications().containsKey(
+                mImportantForeground.getKey())).isFalse();
+    }
+
+    @Test
+    public void onUpdateNotification_notificationPosted_isUpdate_putsNotification() {
+        mPreprocessingManager.init(mAlertEntriesMap, mRankingMap);
+        int beforeSize = mPreprocessingManager.getOldNotifications().size();
+        Notification newNotification = new Notification.Builder(mContext, CHANNEL_ID)
+                .setContentTitle("NEW_TITLE")
+                .setGroup(OVERRIDE_GROUP_KEY)
+                .setGroupSummary(false)
+                .build();
+        newNotification.category = Notification.CATEGORY_NAVIGATION;
+        when(mImportantForeground.getStatusBarNotification().getNotification())
+                .thenReturn(newNotification);
+        List<NotificationGroup> newList =
+                mPreprocessingManager.updateNotifications(
+                        /* showLessImportantNotifications= */ false,
+                        mImportantForeground,
+                        CarNotificationListener.NOTIFY_NOTIFICATION_POSTED,
+                        mRankingMap);
+
+        int afterSize = mPreprocessingManager.getOldNotifications().size();
+        AlertEntry updated = (AlertEntry) mPreprocessingManager.getOldNotifications().get(
+                mImportantForeground.getKey());
+        assertThat(updated).isNotNull();
+        assertThat(updated.getNotification().category).isEqualTo(Notification.CATEGORY_NAVIGATION);
+        assertThat(afterSize).isEqualTo(beforeSize);
+    }
+
+    @Test
+    public void onUpdateNotification_notificationPosted_isNotUpdate_addsNotification() {
+        mPreprocessingManager.init(mAlertEntriesMap, mRankingMap);
+        int beforeSize = mPreprocessingManager.getOldNotifications().size();
+        Notification additionalNotification =
+                generateNotification( /* isForegrond= */ true, /* isNavigation= */ false);
+        additionalNotification.category = Notification.CATEGORY_MESSAGE;
+        when(mAdditionalStatusBarNotification.getKey()).thenReturn("ADDITIONAL");
+        when(mAdditionalStatusBarNotification.getGroupKey()).thenReturn(GROUP_KEY_C);
+        when(mAdditionalStatusBarNotification.getNotification()).thenReturn(additionalNotification);
+        AlertEntry additionalAlertEntry = new AlertEntry(mAdditionalStatusBarNotification);
+
+        List<NotificationGroup> newList =
+                mPreprocessingManager.updateNotifications(
+                        /* showLessImportantNotifications= */ false,
+                        additionalAlertEntry,
+                        CarNotificationListener.NOTIFY_NOTIFICATION_POSTED,
+                        mRankingMap);
+
+        int afterSize = mPreprocessingManager.getOldNotifications().size();
+        AlertEntry posted = (AlertEntry) mPreprocessingManager.getOldNotifications().get(
+                additionalAlertEntry.getKey());
+        assertThat(posted).isNotNull();
+        assertThat(posted.getKey()).isEqualTo("ADDITIONAL");
+        assertThat(afterSize).isEqualTo(beforeSize + 1);
+    }
+
     /**
      * Wraps StatusBarNotifications with AlertEntries and generates AlertEntriesMap and
      * RankingsMap.