Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame^] | 1 | package com.android.systemui.statusbar; |
| 2 | |
| 3 | import static junit.framework.Assert.assertFalse; |
| 4 | import static junit.framework.Assert.assertTrue; |
| 5 | |
| 6 | import static org.mockito.Mockito.verify; |
| 7 | import static org.mockito.Mockito.when; |
| 8 | |
| 9 | import android.app.Notification; |
| 10 | import android.content.Context; |
| 11 | import android.os.Handler; |
| 12 | import android.os.Looper; |
| 13 | import android.os.UserHandle; |
| 14 | import android.service.notification.NotificationListenerService; |
| 15 | import android.service.notification.StatusBarNotification; |
| 16 | import android.support.test.filters.SmallTest; |
| 17 | import android.testing.AndroidTestingRunner; |
| 18 | import android.testing.TestableLooper; |
| 19 | |
| 20 | import com.android.systemui.SysuiTestCase; |
| 21 | |
| 22 | import com.google.android.collect.Sets; |
| 23 | |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | import org.mockito.Mock; |
| 28 | import org.mockito.MockitoAnnotations; |
| 29 | |
| 30 | @SmallTest |
| 31 | @RunWith(AndroidTestingRunner.class) |
| 32 | @TestableLooper.RunWithLooper |
| 33 | public class NotificationRemoteInputManagerTest extends SysuiTestCase { |
| 34 | private static final String TEST_PACKAGE_NAME = "test"; |
| 35 | private static final int TEST_UID = 0; |
| 36 | |
| 37 | private Handler mHandler; |
| 38 | private TestableNotificationRemoteInputManager mRemoteInputManager; |
| 39 | private StatusBarNotification mSbn; |
| 40 | private NotificationData.Entry mEntry; |
| 41 | |
| 42 | @Mock private NotificationPresenter mPresenter; |
| 43 | @Mock private RemoteInputController.Delegate mDelegate; |
| 44 | @Mock private NotificationLockscreenUserManager mLockscreenUserManager; |
| 45 | @Mock private NotificationRemoteInputManager.Callback mCallback; |
| 46 | @Mock private RemoteInputController mController; |
| 47 | @Mock private NotificationListenerService.RankingMap mRanking; |
| 48 | @Mock private ExpandableNotificationRow mRow; |
| 49 | |
| 50 | @Before |
| 51 | public void setUp() { |
| 52 | MockitoAnnotations.initMocks(this); |
| 53 | mHandler = new Handler(Looper.getMainLooper()); |
| 54 | |
| 55 | when(mPresenter.getHandler()).thenReturn(mHandler); |
| 56 | when(mPresenter.getLatestRankingMap()).thenReturn(mRanking); |
| 57 | |
| 58 | mRemoteInputManager = new TestableNotificationRemoteInputManager(mLockscreenUserManager, |
| 59 | mContext); |
| 60 | mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, |
| 61 | 0, new Notification(), UserHandle.CURRENT, null, 0); |
| 62 | mEntry = new NotificationData.Entry(mSbn); |
| 63 | mEntry.row = mRow; |
| 64 | |
| 65 | mRemoteInputManager.setUpWithPresenterForTest(mPresenter, mCallback, mDelegate, |
| 66 | mController); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void testOnRemoveNotificationNotKept() { |
| 71 | assertFalse(mRemoteInputManager.onRemoveNotification(mEntry)); |
| 72 | assertTrue(mRemoteInputManager.getRemoteInputEntriesToRemoveOnCollapse().isEmpty()); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testOnRemoveNotificationKept() { |
| 77 | when(mController.isRemoteInputActive(mEntry)).thenReturn(true); |
| 78 | assertTrue(mRemoteInputManager.onRemoveNotification(mEntry)); |
| 79 | assertTrue(mRemoteInputManager.getRemoteInputEntriesToRemoveOnCollapse().equals( |
| 80 | Sets.newArraySet(mEntry))); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testPerformOnRemoveNotification() { |
| 85 | when(mController.isRemoteInputActive(mEntry)).thenReturn(true); |
| 86 | mRemoteInputManager.getKeysKeptForRemoteInput().add(mEntry.key); |
| 87 | mRemoteInputManager.onPerformRemoveNotification(mSbn, mEntry); |
| 88 | |
| 89 | verify(mController).removeRemoteInput(mEntry, null); |
| 90 | assertTrue(mRemoteInputManager.getKeysKeptForRemoteInput().isEmpty()); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testRemoveRemoteInputEntriesKeptUntilCollapsed() { |
| 95 | mRemoteInputManager.getRemoteInputEntriesToRemoveOnCollapse().add(mEntry); |
| 96 | mRemoteInputManager.removeRemoteInputEntriesKeptUntilCollapsed(); |
| 97 | |
| 98 | assertTrue(mRemoteInputManager.getRemoteInputEntriesToRemoveOnCollapse().isEmpty()); |
| 99 | verify(mController).removeRemoteInput(mEntry, null); |
| 100 | verify(mPresenter).removeNotification(mEntry.key, mRanking); |
| 101 | } |
| 102 | |
| 103 | private class TestableNotificationRemoteInputManager extends NotificationRemoteInputManager { |
| 104 | |
| 105 | public TestableNotificationRemoteInputManager( |
| 106 | NotificationLockscreenUserManager lockscreenUserManager, Context context) { |
| 107 | super(lockscreenUserManager, context); |
| 108 | } |
| 109 | |
| 110 | public void setUpWithPresenterForTest(NotificationPresenter presenter, |
| 111 | Callback callback, |
| 112 | RemoteInputController.Delegate delegate, |
| 113 | RemoteInputController controller) { |
| 114 | super.setUpWithPresenter(presenter, callback, delegate); |
| 115 | mRemoteInputController = controller; |
| 116 | } |
| 117 | } |
| 118 | } |