blob: 2a013560bcd3aff48f158b0cf7d30898e5acf4a2 [file] [log] [blame]
Eliot Courtney21bc05f2017-10-19 17:03:34 +09001/*
2 * Copyright (C) 2017 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
17package com.android.systemui.statusbar;
18
Eliot Courtney21bc05f2017-10-19 17:03:34 +090019import static org.mockito.ArgumentMatchers.any;
Julia Reynolds12ad7ca2019-01-28 09:29:16 -050020import static org.mockito.Mockito.mock;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090021import static org.mockito.Mockito.verify;
22import static org.mockito.Mockito.when;
23
24import android.app.Notification;
Julia Reynolds12ad7ca2019-01-28 09:29:16 -050025import android.app.NotificationManager;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090026import android.os.Handler;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090027import android.os.UserHandle;
28import android.service.notification.NotificationListenerService;
29import android.service.notification.StatusBarNotification;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090030import android.testing.AndroidTestingRunner;
31import android.testing.TestableLooper;
32
Brett Chabot84151d92019-02-27 15:37:59 -080033import androidx.test.filters.SmallTest;
34
Jason Monk297c04e2018-08-23 17:16:59 -040035import com.android.systemui.Dependency;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090036import com.android.systemui.SysuiTestCase;
Rohan Shah20790b82018-07-02 17:21:04 -070037import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050038import com.android.systemui.statusbar.notification.collection.NotificationData;
39import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090040
41import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090044import org.mockito.Mock;
45import org.mockito.MockitoAnnotations;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090046
Eliot Courtney21bc05f2017-10-19 17:03:34 +090047@SmallTest
48@RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050049@TestableLooper.RunWithLooper
Eliot Courtney21bc05f2017-10-19 17:03:34 +090050public class NotificationListenerTest extends SysuiTestCase {
51 private static final String TEST_PACKAGE_NAME = "test";
52 private static final int TEST_UID = 0;
53
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090054 @Mock private NotificationPresenter mPresenter;
55 @Mock private NotificationListenerService.RankingMap mRanking;
56 @Mock private NotificationData mNotificationData;
57
58 // Dependency mocks:
59 @Mock private NotificationEntryManager mEntryManager;
60 @Mock private NotificationRemoteInputManager mRemoteInputManager;
Julia Reynolds12ad7ca2019-01-28 09:29:16 -050061 @Mock private NotificationManager mNotificationManager;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090062
Eliot Courtney21bc05f2017-10-19 17:03:34 +090063 private NotificationListener mListener;
64 private StatusBarNotification mSbn;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090065
66 @Before
67 public void setUp() {
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090068 MockitoAnnotations.initMocks(this);
69 mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
70 mDependency.injectTestDependency(NotificationRemoteInputManager.class,
71 mRemoteInputManager);
Jason Monk297c04e2018-08-23 17:16:59 -040072 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
73 new Handler(TestableLooper.get(this).getLooper()));
Julia Reynolds12ad7ca2019-01-28 09:29:16 -050074 mContext.addMockSystemService(NotificationManager.class, mNotificationManager);
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090075
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090076 when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
Eliot Courtney21bc05f2017-10-19 17:03:34 +090077
Eliot Courtney6c313d32017-12-14 19:57:51 +090078 mListener = new NotificationListener(mContext);
Eliot Courtney21bc05f2017-10-19 17:03:34 +090079 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0,
80 new Notification(), UserHandle.CURRENT, null, 0);
Eliot Courtney21bc05f2017-10-19 17:03:34 +090081 }
82
83 @Test
84 public void testNotificationAddCallsAddNotification() {
85 mListener.onNotificationPosted(mSbn, mRanking);
Jason Monk6dceace2018-05-15 20:24:07 -040086 TestableLooper.get(this).processAllMessages();
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090087 verify(mEntryManager).addNotification(mSbn, mRanking);
Eliot Courtney21bc05f2017-10-19 17:03:34 +090088 }
89
90 @Test
Eliot Courtney21bc05f2017-10-19 17:03:34 +090091 public void testNotificationUpdateCallsUpdateNotification() {
Ned Burnsf81c4c42019-01-07 14:10:43 -050092 when(mNotificationData.get(mSbn.getKey())).thenReturn(new NotificationEntry(mSbn));
Eliot Courtney21bc05f2017-10-19 17:03:34 +090093 mListener.onNotificationPosted(mSbn, mRanking);
Jason Monk6dceace2018-05-15 20:24:07 -040094 TestableLooper.get(this).processAllMessages();
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090095 verify(mEntryManager).updateNotification(mSbn, mRanking);
Eliot Courtney21bc05f2017-10-19 17:03:34 +090096 }
97
98 @Test
99 public void testNotificationRemovalCallsRemoveNotification() {
100 mListener.onNotificationRemoved(mSbn, mRanking);
Jason Monk6dceace2018-05-15 20:24:07 -0400101 TestableLooper.get(this).processAllMessages();
Eliot Courtneya6d8cf22017-10-20 13:26:58 +0900102 verify(mEntryManager).removeNotification(mSbn.getKey(), mRanking);
Eliot Courtney21bc05f2017-10-19 17:03:34 +0900103 }
104
105 @Test
106 public void testRankingUpdateCallsNotificationRankingUpdate() {
107 mListener.onNotificationRankingUpdate(mRanking);
Jason Monk6dceace2018-05-15 20:24:07 -0400108 TestableLooper.get(this).processAllMessages();
Eliot Courtney21bc05f2017-10-19 17:03:34 +0900109 // RankingMap may be modified by plugins.
Eliot Courtneya6d8cf22017-10-20 13:26:58 +0900110 verify(mEntryManager).updateNotificationRanking(any());
Eliot Courtney21bc05f2017-10-19 17:03:34 +0900111 }
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500112
113 @Test
114 public void testOnConnectReadStatusBarSetting() {
115 NotificationListener.NotificationSettingsListener settingsListener =
116 mock(NotificationListener.NotificationSettingsListener.class);
117 mListener.addNotificationSettingsListener(settingsListener);
118
119 when(mNotificationManager.shouldHideSilentStatusBarIcons()).thenReturn(true);
120
121 mListener.onListenerConnected();
122
123 verify(settingsListener).onStatusBarIconsBehaviorChanged(true);
124 }
125
126 @Test
127 public void testOnStatusBarIconsBehaviorChanged() {
128 NotificationListener.NotificationSettingsListener settingsListener =
129 mock(NotificationListener.NotificationSettingsListener.class);
130 mListener.addNotificationSettingsListener(settingsListener);
131
132 mListener.onStatusBarIconsBehaviorChanged(true);
133
134 verify(settingsListener).onStatusBarIconsBehaviorChanged(true);
135 }
Eliot Courtney21bc05f2017-10-19 17:03:34 +0900136}