When a Bubble notification is clicked, show the bubble

After checking keyguard and collapsing the shade, ask
BubbleController to show the bubble associated with the
notification, instad of simply sending the contentIntent.

Bug: 123710619
Test: atest StatusBarNotificationActivityStarterTest
Change-Id: I7b43061447de0daa314deec5abad634fd73e9831
diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java
index e84c648..3aa9f73 100644
--- a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java
+++ b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java
@@ -293,6 +293,17 @@
     }
 
     /**
+     * Request the stack expand if needed, then select the specified Bubble as current.
+     *
+     * @param notificationKey the notification key for the bubble to be selected
+     */
+    public void expandStackAndSelectBubble(String notificationKey) {
+        if (mStackView != null && mBubbleData.getBubble(notificationKey) != null) {
+            mStackView.setExpandedBubble(notificationKey);
+        }
+    }
+
+    /**
      * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
      */
     void dismissStack(@DismissReason int reason) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationClicker.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationClicker.java
index b788f53..fd2f720 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationClicker.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationClicker.java
@@ -78,8 +78,7 @@
         row.setJustClicked(true);
         DejankUtils.postAfterTraversal(() -> row.setJustClicked(false));
 
-        // If it was a bubble we should close it
-        if (row.getEntry().isBubble()) {
+        if (!row.getEntry().isBubble()) {
             mBubbleController.collapseStack();
         }
 
@@ -95,7 +94,8 @@
      */
     public void register(ExpandableNotificationRow row, StatusBarNotification sbn) {
         Notification notification = sbn.getNotification();
-        if (notification.contentIntent != null || notification.fullScreenIntent != null) {
+        if (notification.contentIntent != null || notification.fullScreenIntent != null
+                || row.getEntry().isBubble()) {
             row.setOnClickListener(this);
         } else {
             row.setOnClickListener(null);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/NotificationRowBinderImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/NotificationRowBinderImpl.java
index b91cdaf..d3e5af8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/NotificationRowBinderImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/NotificationRowBinderImpl.java
@@ -220,8 +220,6 @@
             StatusBarNotification sbn,
             ExpandableNotificationRow row) {
         row.setIsLowPriority(entry.ambient);
-        // bind the click event to the content area
-        checkNotNull(mNotificationClicker).register(row, sbn);
 
         // Extract target SDK version.
         try {
@@ -257,6 +255,9 @@
         row.setNeedsRedaction(
                 Dependency.get(NotificationLockscreenUserManager.class).needsRedaction(entry));
         row.inflateViews();
+
+        // bind the click event to the content area
+        checkNotNull(mNotificationClicker).register(row, sbn);
     }
 
     private void logNotificationExpansion(String key, boolean userAction, boolean expanded) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
index db91d01..7e06232 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
@@ -25,6 +25,7 @@
 import static android.app.StatusBarManager.windowStateToString;
 import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY;
 
+import static com.android.systemui.Dependency.BG_HANDLER;
 import static com.android.systemui.Dependency.MAIN_HANDLER;
 import static com.android.systemui.keyguard.WakefulnessLifecycle.WAKEFULNESS_ASLEEP;
 import static com.android.systemui.keyguard.WakefulnessLifecycle.WAKEFULNESS_AWAKE;
@@ -1076,7 +1077,7 @@
                 mLockscreenUserManager, shadeController, mKeyguardMonitor,
                 mNotificationInterruptionStateProvider, mMetricsLogger,
                 new LockPatternUtils(mContext), Dependency.get(MAIN_HANDLER),
-                mActivityIntentHelper);
+                Dependency.get(BG_HANDLER), mActivityIntentHelper, mBubbleController);
 
         mGutsManager.setNotificationActivityStarter(mNotificationActivityStarter);
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java
index 215f5c4..e4af15c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java
@@ -48,6 +48,7 @@
 import com.android.systemui.EventLogTags;
 import com.android.systemui.UiOffloadThread;
 import com.android.systemui.assist.AssistManager;
+import com.android.systemui.bubbles.BubbleController;
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.plugins.statusbar.StatusBarStateController;
 import com.android.systemui.statusbar.CommandQueue;
@@ -98,7 +99,9 @@
     private final CommandQueue mCommandQueue;
     private final IDreamManager mDreamManager;
     private final Handler mMainThreadHandler;
+    private final Handler mBackgroundHandler;
     private final ActivityIntentHelper mActivityIntentHelper;
+    private final BubbleController mBubbleController;
 
     private boolean mIsCollapsingToShowActivityOverLockscreen;
 
@@ -125,7 +128,9 @@
             MetricsLogger metricsLogger,
             LockPatternUtils lockPatternUtils,
             Handler mainThreadHandler,
-            ActivityIntentHelper activityIntentHelper) {
+            Handler backgroundHandler,
+            ActivityIntentHelper activityIntentHelper,
+            BubbleController bubbleController) {
         mContext = context;
         mNotificationPanel = panel;
         mPresenter = presenter;
@@ -147,6 +152,7 @@
         mAssistManager = assistManager;
         mGroupManager = groupManager;
         mLockPatternUtils = lockPatternUtils;
+        mBackgroundHandler = backgroundHandler;
         mEntryManager.addNotificationEntryListener(new NotificationEntryListener() {
             @Override
             public void onPendingEntryAdded(NotificationEntry entry) {
@@ -156,6 +162,7 @@
         mStatusBarRemoteInputCallback = remoteInputCallback;
         mMainThreadHandler = mainThreadHandler;
         mActivityIntentHelper = activityIntentHelper;
+        mBubbleController = bubbleController;
     }
 
     /**
@@ -178,14 +185,24 @@
         final PendingIntent intent = notification.contentIntent != null
                 ? notification.contentIntent
                 : notification.fullScreenIntent;
+        final boolean isBubble = row.getEntry().isBubble();
+
+        // This code path is now executed for notification without a contentIntent.
+        // The only valid case is Bubble notifications. Guard against other cases
+        // entering here.
+        if (intent == null && !isBubble) {
+            Log.e(TAG, "onNotificationClicked called for non-clickable notification!");
+            return;
+        }
+
         final String notificationKey = sbn.getKey();
 
-        boolean isActivityIntent = intent.isActivity();
+        boolean isActivityIntent = intent != null && intent.isActivity() && !isBubble;
         final boolean afterKeyguardGone = isActivityIntent
                 && mActivityIntentHelper.wouldLaunchResolverActivity(intent.getIntent(),
                 mLockscreenUserManager.getCurrentUserId());
         final boolean wasOccluded = mShadeController.isOccluded();
-        boolean showOverLockscreen = mKeyguardMonitor.isShowing()
+        boolean showOverLockscreen = mKeyguardMonitor.isShowing() && intent != null
                 && mActivityIntentHelper.wouldShowOverLockscreen(intent.getIntent(),
                 mLockscreenUserManager.getCurrentUserId());
         ActivityStarter.OnDismissAction postKeyguardAction =
@@ -244,9 +261,8 @@
             mShadeController.addAfterKeyguardGoneRunnable(runnable);
             mShadeController.collapsePanel();
         } else {
-            new Thread(runnable).start();
+            mBackgroundHandler.postAtFrontOfQueue(runnable);
         }
-
         return !mNotificationPanel.isFullyCollapsed();
     }
 
@@ -287,6 +303,7 @@
         }
         Intent fillInIntent = null;
         NotificationEntry entry = row.getEntry();
+        final boolean isBubble = entry.isBubble();
         CharSequence remoteInputText = null;
         if (!TextUtils.isEmpty(entry.remoteInputText)) {
             remoteInputText = entry.remoteInputText;
@@ -295,8 +312,12 @@
             fillInIntent = new Intent().putExtra(Notification.EXTRA_REMOTE_INPUT_DRAFT,
                     remoteInputText.toString());
         }
-        startNotificationIntent(intent, fillInIntent, row, wasOccluded, isActivityIntent);
-        if (isActivityIntent) {
+        if (isBubble) {
+            expandBubbleStackOnMainThread(notificationKey);
+        } else {
+            startNotificationIntent(intent, fillInIntent, row, wasOccluded, isActivityIntent);
+        }
+        if (isActivityIntent || isBubble) {
             mAssistManager.hideAssist();
         }
         if (shouldCollapse()) {
@@ -316,18 +337,29 @@
         } catch (RemoteException ex) {
             // system process is dead if we're here.
         }
-        if (parentToCancelFinal != null) {
-            removeNotification(parentToCancelFinal);
-        }
-        if (shouldAutoCancel(sbn)
-                || mRemoteInputManager.isNotificationKeptForRemoteInputHistory(
-                notificationKey)) {
-            // Automatically remove all notifications that we may have kept around longer
-            removeNotification(sbn);
+        if (!isBubble) {
+            if (parentToCancelFinal != null) {
+                removeNotification(parentToCancelFinal);
+            }
+            if (shouldAutoCancel(sbn)
+                    || mRemoteInputManager.isNotificationKeptForRemoteInputHistory(
+                    notificationKey)) {
+                // Automatically remove all notifications that we may have kept around longer
+                removeNotification(sbn);
+            }
         }
         mIsCollapsingToShowActivityOverLockscreen = false;
     }
 
+    private void expandBubbleStackOnMainThread(String notificationKey) {
+        if (Looper.getMainLooper().isCurrentThread()) {
+            mBubbleController.expandStackAndSelectBubble(notificationKey);
+        } else {
+            mMainThreadHandler.post(
+                    () -> mBubbleController.expandStackAndSelectBubble(notificationKey));
+        }
+    }
+
     private void startNotificationIntent(PendingIntent intent, Intent fillInIntent,
             ExpandableNotificationRow row, boolean wasOccluded, boolean isActivityIntent) {
         RemoteAnimationAdapter adapter = mActivityLaunchAnimator.getLaunchAnimation(row,
diff --git a/packages/SystemUI/tests/src/com/android/systemui/bubbles/BubbleControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/bubbles/BubbleControllerTest.java
index 14bc71b..9fa85d3 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/bubbles/BubbleControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/bubbles/BubbleControllerTest.java
@@ -438,6 +438,22 @@
     }
 
     @Test
+    public void testExpandStackAndSelectBubble_removedFirst() {
+        final String key = mRow.getEntry().key;
+
+        mEntryListener.onPendingEntryAdded(mRow.getEntry());
+        mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
+
+        assertTrue(mRow.getEntry().isBubble());
+
+        // Simulate notification cancellation.
+        mEntryListener.onEntryRemoved(mRow.getEntry(), null /* notificationVisibility (unused) */,
+                false /* removedbyUser */);
+
+        mBubbleController.expandStackAndSelectBubble(key);
+    }
+
+    @Test
     public void testMarkNewNotificationAsBubble() {
         mEntryListener.onPendingEntryAdded(mRow.getEntry());
         assertTrue(mRow.getEntry().isBubble());
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationTestHelper.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationTestHelper.java
index 5ea4636..7e089a6 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationTestHelper.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationTestHelper.java
@@ -151,6 +151,16 @@
 
     /**
      * Returns an {@link ExpandableNotificationRow} that should be shown as a bubble.
+     */
+    public ExpandableNotificationRow createBubble()
+            throws Exception {
+        Notification n = createNotification(false /* isGroupSummary */,
+                null /* groupKey */, makeBubbleMetadata(null));
+        return generateRow(n, PKG, UID, USER_HANDLE, 0 /* extraInflationFlags */, IMPORTANCE_HIGH);
+    }
+
+    /**
+     * Returns an {@link ExpandableNotificationRow} that should be shown as a bubble.
      *
      * @param deleteIntent the intent to assign to {@link BubbleMetadata#deleteIntent}
      */
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarterTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarterTest.java
index 20af1ac..41e82cb 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarterTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarterTest.java
@@ -24,7 +24,10 @@
 import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
 
 import android.app.KeyguardManager;
@@ -49,6 +52,7 @@
 import com.android.systemui.ActivityIntentHelper;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.assist.AssistManager;
+import com.android.systemui.bubbles.BubbleController;
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.plugins.statusbar.StatusBarStateController;
 import com.android.systemui.statusbar.CommandQueue;
@@ -101,22 +105,23 @@
     private KeyguardMonitor mKeyguardMonitor;
     @Mock
     private Handler mHandler;
+    @Mock
+    private BubbleController mBubbleController;
 
     @Mock
     private ActivityIntentHelper mActivityIntentHelper;
     @Mock
     private PendingIntent mContentIntent;
     @Mock
+    private Intent mContentIntentInner;
+    @Mock
     private NotificationData mNotificationData;
-    @Mock
-    private NotificationEntry mNotificationEntry;
-    @Mock
-    private NotificationEntry mBubbleEntry;
 
     private NotificationActivityStarter mNotificationActivityStarter;
 
     private NotificationTestHelper mNotificationTestHelper;
-    ExpandableNotificationRow mNotificationRow;
+    private ExpandableNotificationRow mNotificationRow;
+    private ExpandableNotificationRow mBubbleNotificationRow;
 
     private final Answer<Void> mCallOnDismiss = answerVoid(
             (ActivityStarter.OnDismissAction dismissAction, Runnable cancel,
@@ -129,14 +134,32 @@
         when(mRemoteInputManager.getController()).thenReturn(mRemoteInputController);
         when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
 
-        mActiveNotifications = new ArrayList<>();
-        mActiveNotifications.add(mNotificationEntry);
-        mActiveNotifications.add(mBubbleEntry);
-        when(mNotificationData.getActiveNotifications()).thenReturn(mActiveNotifications);
-        when(mNotificationEntry.getRow()).thenReturn(mNotificationRow);
+        when(mContentIntent.isActivity()).thenReturn(true);
+        when(mContentIntent.getCreatorUserHandle()).thenReturn(UserHandle.of(1));
+        when(mContentIntent.getIntent()).thenReturn(mContentIntentInner);
 
         mNotificationTestHelper = new NotificationTestHelper(mContext);
+
+        // Create standard notification with contentIntent
         mNotificationRow = mNotificationTestHelper.createRow();
+        StatusBarNotification sbn = mNotificationRow.getStatusBarNotification();
+        sbn.getNotification().contentIntent = mContentIntent;
+        sbn.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
+
+        // Create bubble notification row with contentIntent
+        mBubbleNotificationRow = mNotificationTestHelper.createBubble();
+        StatusBarNotification bubbleSbn = mBubbleNotificationRow.getStatusBarNotification();
+        bubbleSbn.getNotification().contentIntent = mContentIntent;
+        bubbleSbn.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
+        // Do what BubbleController's NotificationEntryListener#onPendingEntryAdded does:
+        mBubbleNotificationRow.getEntry().setIsBubble(true);
+        mBubbleNotificationRow.getEntry().setShowInShadeWhenBubble(true);
+
+        mActiveNotifications = new ArrayList<>();
+        mActiveNotifications.add(mNotificationRow.getEntry());
+        mActiveNotifications.add(mBubbleNotificationRow.getEntry());
+        when(mNotificationData.getActiveNotifications()).thenReturn(mActiveNotifications);
+        when(mStatusBarStateController.getState()).thenReturn(StatusBarState.SHADE);
 
         mNotificationActivityStarter = new StatusBarNotificationActivityStarter(getContext(),
                 mock(CommandQueue.class), mAssistManager, mock(NotificationPanelView.class),
@@ -147,16 +170,8 @@
                 mock(StatusBarRemoteInputCallback.class), mock(NotificationGroupManager.class),
                 mock(NotificationLockscreenUserManager.class), mShadeController, mKeyguardMonitor,
                 mock(NotificationInterruptionStateProvider.class), mock(MetricsLogger.class),
-                mock(LockPatternUtils.class), mHandler, mActivityIntentHelper);
-
-
-        when(mContentIntent.isActivity()).thenReturn(true);
-        when(mContentIntent.getCreatorUserHandle()).thenReturn(UserHandle.of(1));
-
-        // SBNActivityStarter expects contentIntent or fullScreenIntent to be set
-        mNotificationRow.getEntry().notification.getNotification().contentIntent = mContentIntent;
-
-        when(mStatusBarStateController.getState()).thenReturn(StatusBarState.SHADE);
+                mock(LockPatternUtils.class), mHandler, mHandler, mActivityIntentHelper,
+                mBubbleController);
 
         // set up dismissKeyguardThenExecute to synchronously invoke the OnDismissAction arg
         doAnswer(mCallOnDismiss).when(mActivityStarter).dismissKeyguardThenExecute(
@@ -173,33 +188,26 @@
         // set up Handler to synchronously invoke the Runnable arg
         doAnswer(answerVoid(Runnable::run))
                 .when(mHandler).post(any(Runnable.class));
+
+        doAnswer(answerVoid(Runnable::run))
+                .when(mHandler).postAtFrontOfQueue(any(Runnable.class));
     }
 
     @Test
-    public void testOnNotificationClicked_whileKeyguardVisible()
+    public void testOnNotificationClicked_keyGuardShowing()
             throws PendingIntent.CanceledException, RemoteException {
         // Given
+        StatusBarNotification sbn = mNotificationRow.getStatusBarNotification();
+        sbn.getNotification().contentIntent = mContentIntent;
+        sbn.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
+
         when(mKeyguardMonitor.isShowing()).thenReturn(true);
         when(mShadeController.isOccluded()).thenReturn(true);
-        when(mContentIntent.isActivity()).thenReturn(true);
-        when(mActivityIntentHelper.wouldShowOverLockscreen(any(Intent.class), anyInt()))
-                .thenReturn(false);
-        when(mActivityIntentHelper.wouldLaunchResolverActivity(any(Intent.class), anyInt()))
-                .thenReturn(false);
-
-        StatusBarNotification statusBarNotification = mNotificationRow.getEntry().notification;
-        statusBarNotification.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
 
         // When
-        mNotificationActivityStarter.onNotificationClicked(statusBarNotification,
-                mNotificationRow);
+        mNotificationActivityStarter.onNotificationClicked(sbn, mNotificationRow);
 
         // Then
-        verify(mActivityStarter).dismissKeyguardThenExecute(
-                any(ActivityStarter.OnDismissAction.class),
-                any() /* cancel */,
-                anyBoolean() /* afterKeyguardGone */);
-
         verify(mShadeController, atLeastOnce()).collapsePanel();
 
         verify(mContentIntent).sendAndReturnResult(
@@ -214,9 +222,100 @@
         verify(mAssistManager).hideAssist();
 
         verify(mStatusBarService).onNotificationClick(
-                eq(mNotificationRow.getEntry().key), any(NotificationVisibility.class));
+                eq(sbn.getKey()), any(NotificationVisibility.class));
 
         // Notification is removed due to FLAG_AUTO_CANCEL
-        verify(mEntryManager).performRemoveNotification(eq(statusBarNotification));
+        verify(mEntryManager).performRemoveNotification(eq(sbn));
+    }
+
+    @Test
+    public void testOnNotificationClicked_bubble_noContentIntent_noKeyGuard()
+            throws RemoteException {
+        StatusBarNotification sbn = mBubbleNotificationRow.getStatusBarNotification();
+
+        // Given
+        sbn.getNotification().contentIntent = null;
+
+        // When
+        mNotificationActivityStarter.onNotificationClicked(sbn, mBubbleNotificationRow);
+
+        // Then
+        verify(mBubbleController).expandStackAndSelectBubble(eq(sbn.getKey()));
+
+        // This is called regardless, and simply short circuits when there is nothing to do.
+        verify(mShadeController, atLeastOnce()).collapsePanel();
+
+        verify(mAssistManager).hideAssist();
+
+        verify(mStatusBarService).onNotificationClick(
+                eq(sbn.getKey()), any(NotificationVisibility.class));
+
+        // The content intent should NOT be sent on click.
+        verifyZeroInteractions(mContentIntent);
+
+        // Notification should not be cancelled.
+        verify(mEntryManager, never()).performRemoveNotification(eq(sbn));
+    }
+
+    @Test
+    public void testOnNotificationClicked_bubble_noContentIntent_keyGuardShowing()
+            throws RemoteException {
+        StatusBarNotification sbn = mBubbleNotificationRow.getStatusBarNotification();
+
+        // Given
+        sbn.getNotification().contentIntent = null;
+        when(mKeyguardMonitor.isShowing()).thenReturn(true);
+        when(mShadeController.isOccluded()).thenReturn(true);
+
+        // When
+        mNotificationActivityStarter.onNotificationClicked(sbn, mBubbleNotificationRow);
+
+        // Then
+        verify(mBubbleController).expandStackAndSelectBubble(eq(sbn.getKey()));
+
+        verify(mShadeController, atLeastOnce()).collapsePanel();
+
+        verify(mAssistManager).hideAssist();
+
+        verify(mStatusBarService).onNotificationClick(
+                eq(sbn.getKey()), any(NotificationVisibility.class));
+
+        // The content intent should NOT be sent on click.
+        verifyZeroInteractions(mContentIntent);
+
+        // Notification should not be cancelled.
+        verify(mEntryManager, never()).performRemoveNotification(eq(sbn));
+    }
+
+    @Test
+    public void testOnNotificationClicked_bubble_withContentIntent_keyGuardShowing()
+            throws RemoteException {
+        StatusBarNotification sbn = mBubbleNotificationRow.getStatusBarNotification();
+
+        // Given
+        sbn.getNotification().contentIntent = mContentIntent;
+        when(mKeyguardMonitor.isShowing()).thenReturn(true);
+        when(mShadeController.isOccluded()).thenReturn(true);
+
+        // When
+        mNotificationActivityStarter.onNotificationClicked(sbn, mBubbleNotificationRow);
+
+        // Then
+        verify(mBubbleController).expandStackAndSelectBubble(eq(sbn.getKey()));
+
+        verify(mShadeController, atLeastOnce()).collapsePanel();
+
+        verify(mAssistManager).hideAssist();
+
+        verify(mStatusBarService).onNotificationClick(
+                eq(sbn.getKey()), any(NotificationVisibility.class));
+
+        // The content intent should NOT be sent on click.
+        verify(mContentIntent).getIntent();
+        verify(mContentIntent).isActivity();
+        verifyNoMoreInteractions(mContentIntent);
+
+        // Notification should not be cancelled.
+        verify(mEntryManager, never()).performRemoveNotification(eq(sbn));
     }
 }