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