Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.bubbles; |
| 18 | |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 19 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
| 20 | |
Mady Mellor | fc02cc3 | 2019-04-01 14:47:55 -0700 | [diff] [blame] | 21 | import static com.google.common.truth.Truth.assertThat; |
| 22 | |
Mady Mellor | edd4ee1 | 2019-01-18 10:45:11 -0800 | [diff] [blame] | 23 | import static org.junit.Assert.assertEquals; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertTrue; |
Mady Mellor | fc02cc3 | 2019-04-01 14:47:55 -0700 | [diff] [blame] | 26 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 27 | import static org.mockito.Mockito.atLeastOnce; |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 28 | import static org.mockito.Mockito.never; |
| 29 | import static org.mockito.Mockito.times; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 30 | import static org.mockito.Mockito.verify; |
| 31 | import static org.mockito.Mockito.when; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 32 | |
| 33 | import android.app.IActivityManager; |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 34 | import android.app.Notification; |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 35 | import android.app.PendingIntent; |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 36 | import android.content.BroadcastReceiver; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 37 | import android.content.Context; |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 38 | import android.content.Intent; |
| 39 | import android.content.IntentFilter; |
| 40 | import android.graphics.drawable.Icon; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 41 | import android.testing.AndroidTestingRunner; |
| 42 | import android.testing.TestableLooper; |
| 43 | import android.view.WindowManager; |
| 44 | import android.widget.FrameLayout; |
| 45 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 46 | import androidx.test.filters.SmallTest; |
| 47 | |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 48 | import com.android.systemui.R; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 49 | import com.android.systemui.SysuiTestCase; |
| 50 | import com.android.systemui.statusbar.NotificationTestHelper; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 51 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
| 52 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 53 | import com.android.systemui.statusbar.notification.collection.NotificationData; |
Mady Mellor | 9bad224 | 2019-01-28 11:21:51 -0800 | [diff] [blame] | 54 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 55 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 56 | import com.android.systemui.statusbar.phone.DozeParameters; |
| 57 | import com.android.systemui.statusbar.phone.StatusBarWindowController; |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 58 | import com.android.systemui.statusbar.policy.ConfigurationController; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 59 | |
| 60 | import org.junit.Before; |
| 61 | import org.junit.Test; |
| 62 | import org.junit.runner.RunWith; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 63 | import org.mockito.ArgumentCaptor; |
| 64 | import org.mockito.Captor; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 65 | import org.mockito.Mock; |
| 66 | import org.mockito.MockitoAnnotations; |
| 67 | |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 68 | import java.util.concurrent.CountDownLatch; |
| 69 | import java.util.concurrent.TimeUnit; |
| 70 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 71 | @SmallTest |
| 72 | @RunWith(AndroidTestingRunner.class) |
| 73 | @TestableLooper.RunWithLooper(setAsMainLooper = true) |
| 74 | public class BubbleControllerTest extends SysuiTestCase { |
| 75 | |
Mady Mellor | 3ed4620 | 2019-03-26 20:22:35 -0700 | [diff] [blame] | 76 | // Some APIs rely on the app being foreground, check is via pkg name |
| 77 | private static final String FOREGROUND_TEST_PKG_NAME = "com.android.systemui.tests"; |
| 78 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 79 | @Mock |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 80 | private NotificationEntryManager mNotificationEntryManager; |
| 81 | @Mock |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 82 | private WindowManager mWindowManager; |
| 83 | @Mock |
| 84 | private IActivityManager mActivityManager; |
| 85 | @Mock |
| 86 | private DozeParameters mDozeParameters; |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 87 | @Mock |
| 88 | private ConfigurationController mConfigurationController; |
| 89 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 90 | private FrameLayout mStatusBarView; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 91 | @Captor |
| 92 | private ArgumentCaptor<NotificationEntryListener> mEntryListenerCaptor; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 93 | |
| 94 | private TestableBubbleController mBubbleController; |
| 95 | private StatusBarWindowController mStatusBarWindowController; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 96 | private NotificationEntryListener mEntryListener; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 97 | |
| 98 | private NotificationTestHelper mNotificationTestHelper; |
| 99 | private ExpandableNotificationRow mRow; |
| 100 | private ExpandableNotificationRow mRow2; |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 101 | private ExpandableNotificationRow mAutoExpandRow; |
Mady Mellor | 3ed4620 | 2019-03-26 20:22:35 -0700 | [diff] [blame] | 102 | private ExpandableNotificationRow mSuppressNotifRow; |
Mady Mellor | fc02cc3 | 2019-04-01 14:47:55 -0700 | [diff] [blame] | 103 | private ExpandableNotificationRow mNonBubbleNotifRow; |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 104 | |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 105 | @Mock |
| 106 | private NotificationData mNotificationData; |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 107 | @Mock |
| 108 | private BubbleController.BubbleStateChangeListener mBubbleStateChangeListener; |
| 109 | @Mock |
| 110 | private BubbleController.BubbleExpandListener mBubbleExpandListener; |
Mark Renouf | 6b2331c | 2019-03-21 13:40:08 -0400 | [diff] [blame] | 111 | @Mock |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 112 | private PendingIntent mDeleteIntent; |
| 113 | |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 114 | private BubbleData mBubbleData; |
| 115 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 116 | @Before |
| 117 | public void setUp() throws Exception { |
| 118 | MockitoAnnotations.initMocks(this); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 119 | mStatusBarView = new FrameLayout(mContext); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 120 | mDependency.injectTestDependency(NotificationEntryManager.class, mNotificationEntryManager); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 121 | |
| 122 | // Bubbles get added to status bar window view |
| 123 | mStatusBarWindowController = new StatusBarWindowController(mContext, mWindowManager, |
| 124 | mActivityManager, mDozeParameters); |
| 125 | mStatusBarWindowController.add(mStatusBarView, 120 /* height */); |
| 126 | |
| 127 | // Need notifications for bubbles |
| 128 | mNotificationTestHelper = new NotificationTestHelper(mContext); |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 129 | mRow = mNotificationTestHelper.createBubble(mDeleteIntent); |
| 130 | mRow2 = mNotificationTestHelper.createBubble(mDeleteIntent); |
Mady Mellor | fc02cc3 | 2019-04-01 14:47:55 -0700 | [diff] [blame] | 131 | mNonBubbleNotifRow = mNotificationTestHelper.createRow(); |
Mady Mellor | 3ed4620 | 2019-03-26 20:22:35 -0700 | [diff] [blame] | 132 | |
| 133 | // Some bubbles want to auto expand |
| 134 | Notification.BubbleMetadata autoExpandMetadata = |
| 135 | getBuilder().setAutoExpandBubble(true).build(); |
| 136 | mAutoExpandRow = mNotificationTestHelper.createBubble(autoExpandMetadata, |
| 137 | FOREGROUND_TEST_PKG_NAME); |
| 138 | |
| 139 | // Some bubbles want to suppress notifs |
| 140 | Notification.BubbleMetadata suppressNotifMetadata = |
| 141 | getBuilder().setSuppressInitialNotification(true).build(); |
| 142 | mSuppressNotifRow = mNotificationTestHelper.createBubble(suppressNotifMetadata, |
| 143 | FOREGROUND_TEST_PKG_NAME); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 144 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 145 | // Return non-null notification data from the NEM |
| 146 | when(mNotificationEntryManager.getNotificationData()).thenReturn(mNotificationData); |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 147 | when(mNotificationData.getChannel(mRow.getEntry().key)).thenReturn(mRow.getEntry().channel); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 148 | |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 149 | mBubbleData = new BubbleData(); |
| 150 | mBubbleController = new TestableBubbleController(mContext, mStatusBarWindowController, |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 151 | mBubbleData, mConfigurationController); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 152 | mBubbleController.setBubbleStateChangeListener(mBubbleStateChangeListener); |
| 153 | mBubbleController.setExpandListener(mBubbleExpandListener); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 154 | |
| 155 | // Get a reference to the BubbleController's entry listener |
| 156 | verify(mNotificationEntryManager, atLeastOnce()) |
| 157 | .addNotificationEntryListener(mEntryListenerCaptor.capture()); |
| 158 | mEntryListener = mEntryListenerCaptor.getValue(); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Test |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 162 | public void testAddBubble() { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 163 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 164 | assertTrue(mBubbleController.hasBubbles()); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 165 | |
| 166 | verify(mBubbleStateChangeListener).onHasBubblesChanged(true); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testHasBubbles() { |
| 171 | assertFalse(mBubbleController.hasBubbles()); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 172 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 173 | assertTrue(mBubbleController.hasBubbles()); |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testRemoveBubble() { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 178 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 179 | assertTrue(mBubbleController.hasBubbles()); |
| 180 | |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 181 | verify(mBubbleStateChangeListener).onHasBubblesChanged(true); |
| 182 | |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 183 | mBubbleController.removeBubble(mRow.getEntry().key, BubbleController.DISMISS_USER_GESTURE); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 184 | assertFalse(mStatusBarWindowController.getBubblesShowing()); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 185 | assertTrue(mRow.getEntry().isBubbleDismissed()); |
| 186 | verify(mNotificationEntryManager).updateNotifications(); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 187 | verify(mBubbleStateChangeListener).onHasBubblesChanged(false); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void testDismissStack() { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 192 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
| 193 | mBubbleController.updateBubble(mRow2.getEntry(), true /* updatePosition */); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 194 | assertTrue(mBubbleController.hasBubbles()); |
| 195 | |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 196 | mBubbleController.dismissStack(BubbleController.DISMISS_USER_GESTURE); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 197 | assertFalse(mStatusBarWindowController.getBubblesShowing()); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 198 | verify(mNotificationEntryManager).updateNotifications(); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 199 | assertTrue(mRow.getEntry().isBubbleDismissed()); |
| 200 | assertTrue(mRow2.getEntry().isBubbleDismissed()); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Test |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 204 | public void testExpandCollapseStack() { |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 205 | assertFalse(mBubbleController.isStackExpanded()); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 206 | |
| 207 | // Mark it as a bubble and add it explicitly |
| 208 | mEntryListener.onPendingEntryAdded(mRow.getEntry()); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 209 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 210 | |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 211 | // We should have bubbles & their notifs should show in the shade |
| 212 | assertTrue(mBubbleController.hasBubbles()); |
| 213 | assertTrue(mRow.getEntry().showInShadeWhenBubble()); |
Issei Suzuki | ac9fcb7 | 2019-02-04 17:45:57 +0100 | [diff] [blame] | 214 | assertFalse(mStatusBarWindowController.getBubbleExpanded()); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 215 | |
| 216 | // Expand the stack |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 217 | BubbleStackView stackView = mBubbleController.getStackView(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 218 | stackView.expandStack(); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 219 | assertTrue(mBubbleController.isStackExpanded()); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 220 | verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key); |
Issei Suzuki | ac9fcb7 | 2019-02-04 17:45:57 +0100 | [diff] [blame] | 221 | assertTrue(mStatusBarWindowController.getBubbleExpanded()); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 222 | |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 223 | // Make sure it's no longer in the shade |
| 224 | assertFalse(mRow.getEntry().showInShadeWhenBubble()); |
| 225 | |
| 226 | // Collapse |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 227 | stackView.collapseStack(); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 228 | verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow.getEntry().key); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 229 | assertFalse(mBubbleController.isStackExpanded()); |
Issei Suzuki | ac9fcb7 | 2019-02-04 17:45:57 +0100 | [diff] [blame] | 230 | assertFalse(mStatusBarWindowController.getBubbleExpanded()); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Test |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 234 | public void testCollapseAfterChangingExpandedBubble() { |
| 235 | // Mark it as a bubble and add it explicitly |
| 236 | mEntryListener.onPendingEntryAdded(mRow.getEntry()); |
| 237 | mEntryListener.onPendingEntryAdded(mRow2.getEntry()); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 238 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
| 239 | mBubbleController.updateBubble(mRow2.getEntry(), true /* updatePosition */); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 240 | |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 241 | // We should have bubbles & their notifs should show in the shade |
| 242 | assertTrue(mBubbleController.hasBubbles()); |
| 243 | assertTrue(mRow.getEntry().showInShadeWhenBubble()); |
| 244 | assertTrue(mRow2.getEntry().showInShadeWhenBubble()); |
| 245 | |
| 246 | // Expand |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 247 | BubbleStackView stackView = mBubbleController.getStackView(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 248 | stackView.expandStack(); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 249 | assertTrue(mBubbleController.isStackExpanded()); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 250 | verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow2.getEntry().key); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 251 | |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 252 | // Last added is the one that is expanded |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 253 | assertEquals(mRow2.getEntry(), stackView.getExpandedBubbleView().getEntry()); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 254 | assertFalse(mRow2.getEntry().showInShadeWhenBubble()); |
| 255 | |
| 256 | // Switch which bubble is expanded |
Mady Mellor | edd4ee1 | 2019-01-18 10:45:11 -0800 | [diff] [blame] | 257 | stackView.setExpandedBubble(mRow.getEntry()); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 258 | assertEquals(mRow.getEntry(), stackView.getExpandedBubbleView().getEntry()); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 259 | assertFalse(mRow.getEntry().showInShadeWhenBubble()); |
Mady Mellor | edd4ee1 | 2019-01-18 10:45:11 -0800 | [diff] [blame] | 260 | |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 261 | // collapse for previous bubble |
| 262 | verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow2.getEntry().key); |
| 263 | // expand for selected bubble |
| 264 | verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key); |
Mady Mellor | edd4ee1 | 2019-01-18 10:45:11 -0800 | [diff] [blame] | 265 | |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 266 | // Collapse |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 267 | mBubbleController.collapseStack(); |
| 268 | assertFalse(mBubbleController.isStackExpanded()); |
| 269 | } |
| 270 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 271 | @Test |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 272 | public void testExpansionRemovesShowInShade() { |
| 273 | // Mark it as a bubble and add it explicitly |
| 274 | mEntryListener.onPendingEntryAdded(mRow.getEntry()); |
| 275 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
| 276 | |
| 277 | // We should have bubbles & their notifs should show in the shade |
| 278 | assertTrue(mBubbleController.hasBubbles()); |
| 279 | assertTrue(mRow.getEntry().showInShadeWhenBubble()); |
| 280 | |
| 281 | // Expand |
| 282 | BubbleStackView stackView = mBubbleController.getStackView(); |
| 283 | stackView.expandStack(); |
| 284 | assertTrue(mBubbleController.isStackExpanded()); |
| 285 | verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key); |
| 286 | |
| 287 | // No longer show shade in notif after expansion |
| 288 | assertFalse(mRow.getEntry().showInShadeWhenBubble()); |
| 289 | } |
| 290 | |
| 291 | @Test |
| 292 | public void testRemoveLastExpandedCollapses() { |
| 293 | // Mark it as a bubble and add it explicitly |
| 294 | mEntryListener.onPendingEntryAdded(mRow.getEntry()); |
| 295 | mEntryListener.onPendingEntryAdded(mRow2.getEntry()); |
| 296 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
| 297 | mBubbleController.updateBubble(mRow2.getEntry(), true /* updatePosition */); |
| 298 | verify(mBubbleStateChangeListener).onHasBubblesChanged(true); |
| 299 | |
| 300 | // Expand |
| 301 | BubbleStackView stackView = mBubbleController.getStackView(); |
| 302 | stackView.expandStack(); |
| 303 | |
| 304 | assertTrue(mBubbleController.isStackExpanded()); |
| 305 | verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow2.getEntry().key); |
| 306 | |
| 307 | // Last added is the one that is expanded |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 308 | assertEquals(mRow2.getEntry(), stackView.getExpandedBubbleView().getEntry()); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 309 | assertFalse(mRow2.getEntry().showInShadeWhenBubble()); |
| 310 | |
| 311 | // Dismiss currently expanded |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 312 | mBubbleController.removeBubble(stackView.getExpandedBubbleView().getKey(), |
| 313 | BubbleController.DISMISS_USER_GESTURE); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 314 | verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow2.getEntry().key); |
| 315 | |
| 316 | // Make sure next bubble is selected |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 317 | assertEquals(mRow.getEntry(), stackView.getExpandedBubbleView().getEntry()); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 318 | verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key); |
| 319 | |
| 320 | // Dismiss that one |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 321 | mBubbleController.removeBubble(stackView.getExpandedBubbleView().getKey(), |
| 322 | BubbleController.DISMISS_USER_GESTURE); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 323 | |
| 324 | // Make sure state changes and collapse happens |
| 325 | verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow.getEntry().key); |
| 326 | verify(mBubbleStateChangeListener).onHasBubblesChanged(false); |
| 327 | assertFalse(mBubbleController.hasBubbles()); |
| 328 | } |
| 329 | |
| 330 | @Test |
Mady Mellor | 3ed4620 | 2019-03-26 20:22:35 -0700 | [diff] [blame] | 331 | public void testAutoExpand_FailsNotForeground() { |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 332 | assertFalse(mBubbleController.isStackExpanded()); |
| 333 | |
| 334 | // Add the auto expand bubble |
| 335 | mEntryListener.onPendingEntryAdded(mAutoExpandRow.getEntry()); |
| 336 | mBubbleController.updateBubble(mAutoExpandRow.getEntry(), true /* updatePosition */); |
| 337 | |
| 338 | // Expansion shouldn't change |
| 339 | verify(mBubbleExpandListener, never()).onBubbleExpandChanged(false /* expanded */, |
| 340 | mAutoExpandRow.getEntry().key); |
| 341 | assertFalse(mBubbleController.isStackExpanded()); |
| 342 | |
| 343 | // # of bubbles should change |
| 344 | verify(mBubbleStateChangeListener).onHasBubblesChanged(true /* hasBubbles */); |
| 345 | } |
| 346 | |
| 347 | @Test |
Mady Mellor | 3ed4620 | 2019-03-26 20:22:35 -0700 | [diff] [blame] | 348 | public void testAutoExpand_SucceedsForeground() { |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 349 | final CountDownLatch latch = new CountDownLatch(1); |
| 350 | BroadcastReceiver receiver = new BroadcastReceiver() { |
| 351 | @Override |
| 352 | public void onReceive(Context context, Intent intent) { |
| 353 | latch.countDown(); |
| 354 | } |
| 355 | }; |
| 356 | IntentFilter filter = new IntentFilter(BubblesTestActivity.BUBBLE_ACTIVITY_OPENED); |
| 357 | mContext.registerReceiver(receiver, filter); |
| 358 | |
| 359 | assertFalse(mBubbleController.isStackExpanded()); |
| 360 | |
| 361 | // Make ourselves foreground |
| 362 | Intent i = new Intent(mContext, BubblesTestActivity.class); |
| 363 | i.setFlags(FLAG_ACTIVITY_NEW_TASK); |
| 364 | mContext.startActivity(i); |
| 365 | |
| 366 | try { |
| 367 | latch.await(100, TimeUnit.MILLISECONDS); |
| 368 | } catch (InterruptedException e) { |
| 369 | e.printStackTrace(); |
| 370 | } |
| 371 | |
| 372 | // Add the auto expand bubble |
| 373 | mEntryListener.onPendingEntryAdded(mAutoExpandRow.getEntry()); |
| 374 | mBubbleController.updateBubble(mAutoExpandRow.getEntry(), true /* updatePosition */); |
| 375 | |
| 376 | // Expansion should change |
| 377 | verify(mBubbleExpandListener).onBubbleExpandChanged(true /* expanded */, |
| 378 | mAutoExpandRow.getEntry().key); |
| 379 | assertTrue(mBubbleController.isStackExpanded()); |
| 380 | |
| 381 | // # of bubbles should change |
| 382 | verify(mBubbleStateChangeListener).onHasBubblesChanged(true /* hasBubbles */); |
| 383 | mContext.unregisterReceiver(receiver); |
| 384 | } |
| 385 | |
Mady Mellor | 3ed4620 | 2019-03-26 20:22:35 -0700 | [diff] [blame] | 386 | @Test |
| 387 | public void testSuppressNotif_FailsNotForeground() { |
| 388 | // Add the suppress notif bubble |
| 389 | mEntryListener.onPendingEntryAdded(mSuppressNotifRow.getEntry()); |
| 390 | mBubbleController.updateBubble(mSuppressNotifRow.getEntry(), true /* updatePosition */); |
| 391 | |
Mady Mellor | fc02cc3 | 2019-04-01 14:47:55 -0700 | [diff] [blame] | 392 | // Should show in shade because we weren't forground |
Mady Mellor | 3ed4620 | 2019-03-26 20:22:35 -0700 | [diff] [blame] | 393 | assertTrue(mSuppressNotifRow.getEntry().showInShadeWhenBubble()); |
| 394 | |
| 395 | // # of bubbles should change |
| 396 | verify(mBubbleStateChangeListener).onHasBubblesChanged(true /* hasBubbles */); |
| 397 | } |
| 398 | |
| 399 | @Test |
| 400 | public void testSuppressNotif_SucceedsForeground() { |
| 401 | final CountDownLatch latch = new CountDownLatch(1); |
| 402 | BroadcastReceiver receiver = new BroadcastReceiver() { |
| 403 | @Override |
| 404 | public void onReceive(Context context, Intent intent) { |
| 405 | latch.countDown(); |
| 406 | } |
| 407 | }; |
| 408 | IntentFilter filter = new IntentFilter(BubblesTestActivity.BUBBLE_ACTIVITY_OPENED); |
| 409 | mContext.registerReceiver(receiver, filter); |
| 410 | |
| 411 | assertFalse(mBubbleController.isStackExpanded()); |
| 412 | |
| 413 | // Make ourselves foreground |
| 414 | Intent i = new Intent(mContext, BubblesTestActivity.class); |
| 415 | i.setFlags(FLAG_ACTIVITY_NEW_TASK); |
| 416 | mContext.startActivity(i); |
| 417 | |
| 418 | try { |
| 419 | latch.await(100, TimeUnit.MILLISECONDS); |
| 420 | } catch (InterruptedException e) { |
| 421 | e.printStackTrace(); |
| 422 | } |
| 423 | |
| 424 | // Add the suppress notif bubble |
| 425 | mEntryListener.onPendingEntryAdded(mSuppressNotifRow.getEntry()); |
| 426 | mBubbleController.updateBubble(mSuppressNotifRow.getEntry(), true /* updatePosition */); |
| 427 | |
Mady Mellor | fc02cc3 | 2019-04-01 14:47:55 -0700 | [diff] [blame] | 428 | // Should NOT show in shade because we were foreground |
Mady Mellor | 3ed4620 | 2019-03-26 20:22:35 -0700 | [diff] [blame] | 429 | assertFalse(mSuppressNotifRow.getEntry().showInShadeWhenBubble()); |
| 430 | |
| 431 | // # of bubbles should change |
| 432 | verify(mBubbleStateChangeListener).onHasBubblesChanged(true /* hasBubbles */); |
| 433 | mContext.unregisterReceiver(receiver); |
| 434 | } |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 435 | |
| 436 | @Test |
Mark Renouf | fec45da | 2019-03-13 13:24:27 -0400 | [diff] [blame] | 437 | public void testExpandStackAndSelectBubble_removedFirst() { |
| 438 | final String key = mRow.getEntry().key; |
| 439 | |
| 440 | mEntryListener.onPendingEntryAdded(mRow.getEntry()); |
| 441 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
| 442 | |
Mark Renouf | fec45da | 2019-03-13 13:24:27 -0400 | [diff] [blame] | 443 | // Simulate notification cancellation. |
| 444 | mEntryListener.onEntryRemoved(mRow.getEntry(), null /* notificationVisibility (unused) */, |
| 445 | false /* removedbyUser */); |
| 446 | |
| 447 | mBubbleController.expandStackAndSelectBubble(key); |
| 448 | } |
| 449 | |
| 450 | @Test |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 451 | public void testMarkNewNotificationAsShowInShade() { |
| 452 | mEntryListener.onPendingEntryAdded(mRow.getEntry()); |
| 453 | assertTrue(mRow.getEntry().showInShadeWhenBubble()); |
| 454 | } |
| 455 | |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 456 | @Test |
Mady Mellor | fc02cc3 | 2019-04-01 14:47:55 -0700 | [diff] [blame] | 457 | public void testAddNotif_notBubble() { |
| 458 | mEntryListener.onPendingEntryAdded(mNonBubbleNotifRow.getEntry()); |
| 459 | mEntryListener.onPreEntryUpdated(mNonBubbleNotifRow.getEntry()); |
| 460 | |
| 461 | verify(mBubbleStateChangeListener, never()).onHasBubblesChanged(anyBoolean()); |
| 462 | assertThat(mBubbleController.hasBubbles()).isFalse(); |
| 463 | } |
| 464 | |
| 465 | @Test |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 466 | public void testDeleteIntent_removeBubble_aged() throws PendingIntent.CanceledException { |
| 467 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
| 468 | mBubbleController.removeBubble(mRow.getEntry().key, BubbleController.DISMISS_AGED); |
| 469 | verify(mDeleteIntent, never()).send(); |
| 470 | } |
| 471 | |
| 472 | @Test |
| 473 | public void testDeleteIntent_removeBubble_user() throws PendingIntent.CanceledException { |
| 474 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
| 475 | mBubbleController.removeBubble(mRow.getEntry().key, BubbleController.DISMISS_USER_GESTURE); |
| 476 | verify(mDeleteIntent, times(1)).send(); |
| 477 | } |
| 478 | |
| 479 | @Test |
| 480 | public void testDeleteIntent_dismissStack() throws PendingIntent.CanceledException { |
| 481 | mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */); |
| 482 | mBubbleController.updateBubble(mRow2.getEntry(), true /* updatePosition */); |
| 483 | mBubbleController.dismissStack(BubbleController.DISMISS_USER_GESTURE); |
| 484 | verify(mDeleteIntent, times(2)).send(); |
| 485 | } |
| 486 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 487 | static class TestableBubbleController extends BubbleController { |
Issei Suzuki | c038754 | 2019-03-08 17:31:14 +0100 | [diff] [blame] | 488 | // Let's assume surfaces can be synchronized immediately. |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 489 | TestableBubbleController(Context context, |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 490 | StatusBarWindowController statusBarWindowController, BubbleData data, |
| 491 | ConfigurationController configurationController) { |
| 492 | super(context, statusBarWindowController, data, Runnable::run, configurationController); |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 493 | } |
Mady Mellor | 9bad224 | 2019-01-28 11:21:51 -0800 | [diff] [blame] | 494 | |
| 495 | @Override |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 496 | public boolean shouldAutoBubbleForFlags(Context c, NotificationEntry entry) { |
Mady Mellor | 9bad224 | 2019-01-28 11:21:51 -0800 | [diff] [blame] | 497 | return entry.notification.getNotification().getBubbleMetadata() != null; |
| 498 | } |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 499 | } |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 500 | |
| 501 | /** |
| 502 | * @return basic {@link android.app.Notification.BubbleMetadata.Builder} |
| 503 | */ |
| 504 | private Notification.BubbleMetadata.Builder getBuilder() { |
| 505 | Intent target = new Intent(mContext, BubblesTestActivity.class); |
| 506 | PendingIntent bubbleIntent = PendingIntent.getActivity(mContext, 0, target, 0); |
| 507 | return new Notification.BubbleMetadata.Builder() |
| 508 | .setIntent(bubbleIntent) |
| 509 | .setIcon(Icon.createWithResource(mContext, R.drawable.android)); |
| 510 | } |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 511 | } |