Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 1 | package com.android.systemui.statusbar; |
| 2 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 3 | import static junit.framework.Assert.assertEquals; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 4 | import static junit.framework.Assert.assertFalse; |
Gus Prevas | 21437b3 | 2018-12-05 10:36:13 -0500 | [diff] [blame] | 5 | import static junit.framework.Assert.assertTrue; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 6 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 7 | import static org.mockito.Mockito.mock; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 8 | import static org.mockito.Mockito.verify; |
| 9 | import static org.mockito.Mockito.when; |
| 10 | |
| 11 | import android.app.Notification; |
| 12 | import android.content.Context; |
| 13 | import android.os.Handler; |
| 14 | import android.os.Looper; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 15 | import android.os.SystemClock; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 16 | import android.os.UserHandle; |
| 17 | import android.service.notification.NotificationListenerService; |
| 18 | import android.service.notification.StatusBarNotification; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 19 | import android.testing.AndroidTestingRunner; |
| 20 | import android.testing.TestableLooper; |
| 21 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 22 | import androidx.test.filters.SmallTest; |
| 23 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 24 | import com.android.systemui.SysuiTestCase; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 25 | import com.android.systemui.statusbar.NotificationRemoteInputManager.RemoteInputActiveExtender; |
| 26 | import com.android.systemui.statusbar.NotificationRemoteInputManager.RemoteInputHistoryExtender; |
| 27 | import com.android.systemui.statusbar.NotificationRemoteInputManager.SmartReplyHistoryExtender; |
Gus Prevas | 21437b3 | 2018-12-05 10:36:13 -0500 | [diff] [blame] | 28 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 29 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Gus Prevas | 21437b3 | 2018-12-05 10:36:13 -0500 | [diff] [blame] | 30 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Gus Prevas | 772e532 | 2018-12-21 16:22:16 -0500 | [diff] [blame] | 31 | import com.android.systemui.statusbar.phone.ShadeController; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 32 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 33 | import com.google.android.collect.Sets; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.mockito.Mock; |
| 39 | import org.mockito.MockitoAnnotations; |
| 40 | |
Gus Prevas | 772e532 | 2018-12-21 16:22:16 -0500 | [diff] [blame] | 41 | import dagger.Lazy; |
| 42 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 43 | @SmallTest |
| 44 | @RunWith(AndroidTestingRunner.class) |
| 45 | @TestableLooper.RunWithLooper |
| 46 | public class NotificationRemoteInputManagerTest extends SysuiTestCase { |
| 47 | private static final String TEST_PACKAGE_NAME = "test"; |
| 48 | private static final int TEST_UID = 0; |
| 49 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 50 | @Mock private NotificationPresenter mPresenter; |
| 51 | @Mock private RemoteInputController.Delegate mDelegate; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 52 | @Mock private NotificationRemoteInputManager.Callback mCallback; |
| 53 | @Mock private RemoteInputController mController; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 54 | @Mock private SmartReplyController mSmartReplyController; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 55 | @Mock private NotificationListenerService.RankingMap mRanking; |
| 56 | @Mock private ExpandableNotificationRow mRow; |
| 57 | |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 58 | // Dependency mocks: |
| 59 | @Mock private NotificationEntryManager mEntryManager; |
| 60 | @Mock private NotificationLockscreenUserManager mLockscreenUserManager; |
| 61 | |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 62 | private TestableNotificationRemoteInputManager mRemoteInputManager; |
| 63 | private StatusBarNotification mSbn; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 64 | private NotificationEntry mEntry; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 65 | private RemoteInputHistoryExtender mRemoteInputHistoryExtender; |
| 66 | private SmartReplyHistoryExtender mSmartReplyHistoryExtender; |
| 67 | private RemoteInputActiveExtender mRemoteInputActiveExtender; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 68 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 69 | @Before |
| 70 | public void setUp() { |
| 71 | MockitoAnnotations.initMocks(this); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 72 | |
Gus Prevas | 772e532 | 2018-12-21 16:22:16 -0500 | [diff] [blame] | 73 | mRemoteInputManager = new TestableNotificationRemoteInputManager(mContext, |
| 74 | mLockscreenUserManager, mSmartReplyController, mEntryManager, |
| 75 | () -> mock(ShadeController.class), |
| 76 | Handler.createAsync(Looper.myLooper())); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 77 | mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, |
| 78 | 0, new Notification(), UserHandle.CURRENT, null, 0); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 79 | mEntry = new NotificationEntry(mSbn); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 80 | mEntry.setRow(mRow); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 81 | |
Gus Prevas | 21437b3 | 2018-12-05 10:36:13 -0500 | [diff] [blame] | 82 | mRemoteInputManager.setUpWithPresenterForTest(mCallback, |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 83 | mDelegate, mController); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 84 | for (NotificationLifetimeExtender extender : mRemoteInputManager.getLifetimeExtenders()) { |
| 85 | extender.setCallback( |
| 86 | mock(NotificationLifetimeExtender.NotificationSafeToRemoveCallback.class)); |
| 87 | } |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testPerformOnRemoveNotification() { |
| 92 | when(mController.isRemoteInputActive(mEntry)).thenReturn(true); |
Gus Prevas | 772e532 | 2018-12-21 16:22:16 -0500 | [diff] [blame] | 93 | mRemoteInputManager.onPerformRemoveNotification(mEntry, mSbn.getKey()); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 94 | |
| 95 | verify(mController).removeRemoteInput(mEntry, null); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 99 | public void testShouldExtendLifetime_remoteInputActive() { |
| 100 | when(mController.isRemoteInputActive(mEntry)).thenReturn(true); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 101 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 102 | assertTrue(mRemoteInputActiveExtender.shouldExtendLifetime(mEntry)); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 103 | } |
| 104 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 105 | @Test |
| 106 | public void testShouldExtendLifetime_isSpinning() { |
| 107 | NotificationRemoteInputManager.FORCE_REMOTE_INPUT_HISTORY = true; |
| 108 | when(mController.isSpinning(mEntry.key)).thenReturn(true); |
| 109 | |
| 110 | assertTrue(mRemoteInputHistoryExtender.shouldExtendLifetime(mEntry)); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testShouldExtendLifetime_recentRemoteInput() { |
| 115 | NotificationRemoteInputManager.FORCE_REMOTE_INPUT_HISTORY = true; |
| 116 | mEntry.lastRemoteInputSent = SystemClock.elapsedRealtime(); |
| 117 | |
| 118 | assertTrue(mRemoteInputHistoryExtender.shouldExtendLifetime(mEntry)); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void testShouldExtendLifetime_smartReplySending() { |
| 123 | NotificationRemoteInputManager.FORCE_REMOTE_INPUT_HISTORY = true; |
| 124 | when(mSmartReplyController.isSendingSmartReply(mEntry.key)).thenReturn(true); |
| 125 | |
| 126 | assertTrue(mSmartReplyHistoryExtender.shouldExtendLifetime(mEntry)); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testNotificationWithRemoteInputActiveIsRemovedOnCollapse() { |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 131 | mRemoteInputActiveExtender.setShouldManageLifetime(mEntry, true /* shouldManage */); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 132 | |
| 133 | assertEquals(mRemoteInputManager.getEntriesKeptForRemoteInputActive(), |
| 134 | Sets.newArraySet(mEntry)); |
| 135 | |
| 136 | mRemoteInputManager.onPanelCollapsed(); |
| 137 | |
| 138 | assertTrue(mRemoteInputManager.getEntriesKeptForRemoteInputActive().isEmpty()); |
| 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void testRebuildWithRemoteInput_noExistingInputNoSpinner() { |
| 143 | StatusBarNotification newSbn = |
| 144 | mRemoteInputManager.rebuildNotificationWithRemoteInput(mEntry, "A Reply", false); |
| 145 | CharSequence[] messages = newSbn.getNotification().extras |
| 146 | .getCharSequenceArray(Notification.EXTRA_REMOTE_INPUT_HISTORY); |
| 147 | assertEquals(1, messages.length); |
| 148 | assertEquals("A Reply", messages[0]); |
| 149 | assertFalse(newSbn.getNotification().extras |
| 150 | .getBoolean(Notification.EXTRA_SHOW_REMOTE_INPUT_SPINNER, false)); |
| 151 | assertTrue(newSbn.getNotification().extras |
| 152 | .getBoolean(Notification.EXTRA_HIDE_SMART_REPLIES, false)); |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testRebuildWithRemoteInput_noExistingInputWithSpinner() { |
| 157 | StatusBarNotification newSbn = |
| 158 | mRemoteInputManager.rebuildNotificationWithRemoteInput(mEntry, "A Reply", true); |
| 159 | CharSequence[] messages = newSbn.getNotification().extras |
| 160 | .getCharSequenceArray(Notification.EXTRA_REMOTE_INPUT_HISTORY); |
| 161 | assertEquals(1, messages.length); |
| 162 | assertEquals("A Reply", messages[0]); |
| 163 | assertTrue(newSbn.getNotification().extras |
| 164 | .getBoolean(Notification.EXTRA_SHOW_REMOTE_INPUT_SPINNER, false)); |
| 165 | assertTrue(newSbn.getNotification().extras |
| 166 | .getBoolean(Notification.EXTRA_HIDE_SMART_REPLIES, false)); |
| 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testRebuildWithRemoteInput_withExistingInput() { |
| 171 | // Setup a notification entry with 1 remote input. |
| 172 | StatusBarNotification newSbn = |
| 173 | mRemoteInputManager.rebuildNotificationWithRemoteInput(mEntry, "A Reply", false); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 174 | NotificationEntry entry = new NotificationEntry(newSbn); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 175 | |
| 176 | // Try rebuilding to add another reply. |
| 177 | newSbn = mRemoteInputManager.rebuildNotificationWithRemoteInput(entry, "Reply 2", true); |
| 178 | CharSequence[] messages = newSbn.getNotification().extras |
| 179 | .getCharSequenceArray(Notification.EXTRA_REMOTE_INPUT_HISTORY); |
| 180 | assertEquals(2, messages.length); |
| 181 | assertEquals("Reply 2", messages[0]); |
| 182 | assertEquals("A Reply", messages[1]); |
| 183 | } |
| 184 | |
| 185 | @Test |
| 186 | public void testRebuildNotificationForCanceledSmartReplies() { |
| 187 | // Try rebuilding to remove spinner and hide buttons. |
| 188 | StatusBarNotification newSbn = |
| 189 | mRemoteInputManager.rebuildNotificationForCanceledSmartReplies(mEntry); |
| 190 | assertFalse(newSbn.getNotification().extras |
| 191 | .getBoolean(Notification.EXTRA_SHOW_REMOTE_INPUT_SPINNER, false)); |
| 192 | assertTrue(newSbn.getNotification().extras |
| 193 | .getBoolean(Notification.EXTRA_HIDE_SMART_REPLIES, false)); |
| 194 | } |
| 195 | |
| 196 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 197 | private class TestableNotificationRemoteInputManager extends NotificationRemoteInputManager { |
| 198 | |
Gus Prevas | 772e532 | 2018-12-21 16:22:16 -0500 | [diff] [blame] | 199 | |
| 200 | TestableNotificationRemoteInputManager(Context context, |
| 201 | NotificationLockscreenUserManager lockscreenUserManager, |
| 202 | SmartReplyController smartReplyController, |
| 203 | NotificationEntryManager notificationEntryManager, |
| 204 | Lazy<ShadeController> shadeController, |
| 205 | Handler mainHandler) { |
| 206 | super(context, lockscreenUserManager, smartReplyController, notificationEntryManager, |
| 207 | shadeController, mainHandler); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 208 | } |
| 209 | |
Gus Prevas | 21437b3 | 2018-12-05 10:36:13 -0500 | [diff] [blame] | 210 | public void setUpWithPresenterForTest(Callback callback, |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 211 | RemoteInputController.Delegate delegate, |
| 212 | RemoteInputController controller) { |
Gus Prevas | 21437b3 | 2018-12-05 10:36:13 -0500 | [diff] [blame] | 213 | super.setUpWithCallback(callback, delegate); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 214 | mRemoteInputController = controller; |
| 215 | } |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 216 | |
| 217 | @Override |
| 218 | protected void addLifetimeExtenders() { |
| 219 | mRemoteInputActiveExtender = new RemoteInputActiveExtender(); |
| 220 | mRemoteInputHistoryExtender = new RemoteInputHistoryExtender(); |
| 221 | mSmartReplyHistoryExtender = new SmartReplyHistoryExtender(); |
| 222 | mLifetimeExtenders.add(mRemoteInputHistoryExtender); |
| 223 | mLifetimeExtenders.add(mSmartReplyHistoryExtender); |
| 224 | mLifetimeExtenders.add(mRemoteInputActiveExtender); |
| 225 | } |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 226 | } |
| 227 | } |