blob: b881c098a1a3196aa8d952dbf02b7023e2450bae [file] [log] [blame]
Eliot Courtneye77edea2017-11-15 14:25:21 +09001package com.android.systemui.statusbar;
2
3import static junit.framework.Assert.assertFalse;
4import static junit.framework.Assert.assertTrue;
5
6import static org.mockito.Mockito.verify;
7import static org.mockito.Mockito.when;
8
9import android.app.Notification;
10import android.content.Context;
11import android.os.Handler;
12import android.os.Looper;
13import android.os.UserHandle;
14import android.service.notification.NotificationListenerService;
15import android.service.notification.StatusBarNotification;
16import android.support.test.filters.SmallTest;
17import android.testing.AndroidTestingRunner;
18import android.testing.TestableLooper;
19
20import com.android.systemui.SysuiTestCase;
21
22import com.google.android.collect.Sets;
23
24import org.junit.Before;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.mockito.Mock;
28import org.mockito.MockitoAnnotations;
29
30@SmallTest
31@RunWith(AndroidTestingRunner.class)
32@TestableLooper.RunWithLooper
33public 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}