blob: 595ba89ca3b6fc05d9cc7889cf8ec717c1611500 [file] [log] [blame]
Gus Prevasec9e1f02018-12-18 15:28:12 -05001/*
2 * Copyright (C) 2018 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.notification;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.ArgumentMatchers.anyInt;
23import static org.mockito.ArgumentMatchers.anyString;
24import static org.mockito.ArgumentMatchers.eq;
25import static org.mockito.Mockito.mock;
26import static org.mockito.Mockito.when;
27
28import android.Manifest;
29import android.app.Notification;
30import android.content.pm.IPackageManager;
31import android.content.pm.PackageManager;
32import android.os.Bundle;
33import android.service.notification.StatusBarNotification;
Gus Prevasec9e1f02018-12-18 15:28:12 -050034import android.testing.AndroidTestingRunner;
Kevin Hana7c21be2020-04-01 17:58:35 -070035import android.testing.TestableLooper;
Gus Prevasec9e1f02018-12-18 15:28:12 -050036import android.testing.TestableLooper.RunWithLooper;
37
Brett Chabot84151d92019-02-27 15:37:59 -080038import androidx.test.annotation.UiThreadTest;
39import androidx.test.filters.SmallTest;
40
Gus Prevasec9e1f02018-12-18 15:28:12 -050041import com.android.systemui.ForegroundServiceController;
42import com.android.systemui.SysuiTestCase;
Selim Cinekc3fec682019-06-06 18:11:07 -070043import com.android.systemui.plugins.statusbar.StatusBarStateController;
Lucas Dupind236ee32019-10-08 15:33:59 -070044import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Evan Laird181de622019-10-24 09:53:02 -040045import com.android.systemui.statusbar.notification.NotificationEntryManager.KeyguardEnvironment;
Ned Burnsf81c4c42019-01-07 14:10:43 -050046import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverly79c89ec2019-12-13 10:33:01 -050047import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
Steve Elliottd38a70f2020-05-11 14:01:08 -040048import com.android.systemui.statusbar.notification.people.PeopleNotificationIdentifier;
Gus Prevasec9e1f02018-12-18 15:28:12 -050049import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Kevin Han933dc7c2020-01-29 11:17:46 -080050import com.android.systemui.statusbar.notification.row.NotificationTestHelper;
Gus Prevasec9e1f02018-12-18 15:28:12 -050051import com.android.systemui.statusbar.phone.NotificationGroupManager;
52import com.android.systemui.statusbar.phone.ShadeController;
53
54import org.junit.Before;
55import org.junit.Test;
56import org.junit.runner.RunWith;
57import org.mockito.Mock;
58import org.mockito.MockitoAnnotations;
59
60@SmallTest
61@RunWith(AndroidTestingRunner.class)
62@RunWithLooper
63public class NotificationFilterTest extends SysuiTestCase {
64
65 private static final int UID_NORMAL = 123;
66 private static final int UID_ALLOW_DURING_SETUP = 456;
67 private static final String TEST_HIDDEN_NOTIFICATION_KEY = "testHiddenNotificationKey";
68
69 private final StatusBarNotification mMockStatusBarNotification =
70 mock(StatusBarNotification.class);
71
72 @Mock
73 ForegroundServiceController mFsc;
74 @Mock
Evan Laird181de622019-10-24 09:53:02 -040075 KeyguardEnvironment mEnvironment;
Gus Prevasec9e1f02018-12-18 15:28:12 -050076 private final IPackageManager mMockPackageManager = mock(IPackageManager.class);
77
78 private NotificationFilter mNotificationFilter;
79 private ExpandableNotificationRow mRow;
80
81 @Before
82 public void setUp() throws Exception {
Beverly1467c9e2020-02-18 13:31:29 -050083 allowTestableLooperAsMainThread();
Gus Prevasec9e1f02018-12-18 15:28:12 -050084 MockitoAnnotations.initMocks(this);
85 when(mMockStatusBarNotification.getUid()).thenReturn(UID_NORMAL);
86
87 when(mMockPackageManager.checkUidPermission(
88 eq(Manifest.permission.NOTIFICATION_DURING_SETUP),
89 eq(UID_NORMAL)))
90 .thenReturn(PackageManager.PERMISSION_DENIED);
91 when(mMockPackageManager.checkUidPermission(
92 eq(Manifest.permission.NOTIFICATION_DURING_SETUP),
93 eq(UID_ALLOW_DURING_SETUP)))
94 .thenReturn(PackageManager.PERMISSION_GRANTED);
95 mDependency.injectTestDependency(ForegroundServiceController.class, mFsc);
96 mDependency.injectTestDependency(NotificationGroupManager.class,
Steve Elliottd38a70f2020-05-11 14:01:08 -040097 new NotificationGroupManager(
98 mock(StatusBarStateController.class),
99 () -> mock(PeopleNotificationIdentifier.class)));
Gus Prevasec9e1f02018-12-18 15:28:12 -0500100 mDependency.injectMockDependency(ShadeController.class);
Lucas Dupind236ee32019-10-08 15:33:59 -0700101 mDependency.injectMockDependency(NotificationLockscreenUserManager.class);
Evan Laird181de622019-10-24 09:53:02 -0400102 mDependency.injectTestDependency(KeyguardEnvironment.class, mEnvironment);
Gus Prevasec9e1f02018-12-18 15:28:12 -0500103 when(mEnvironment.isDeviceProvisioned()).thenReturn(true);
104 when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true);
Kevin Hana7c21be2020-04-01 17:58:35 -0700105 NotificationTestHelper testHelper = new NotificationTestHelper(
106 mContext,
107 mDependency,
108 TestableLooper.get(this));
109 mRow = testHelper.createRow();
Heemin Seoge9f4e962019-12-05 11:55:27 -0800110 mNotificationFilter = new NotificationFilter(mock(StatusBarStateController.class));
Gus Prevasec9e1f02018-12-18 15:28:12 -0500111 }
112
113 @Test
114 @UiThreadTest
115 public void testShowNotificationEvenIfUnprovisioned_FalseIfNoExtra() {
116 initStatusBarNotification(false);
117 when(mMockStatusBarNotification.getUid()).thenReturn(UID_ALLOW_DURING_SETUP);
118
119 assertFalse(
120 NotificationFilter.showNotificationEvenIfUnprovisioned(
121 mMockPackageManager,
122 mMockStatusBarNotification));
123 }
124
125 @Test
126 @UiThreadTest
127 public void testShowNotificationEvenIfUnprovisioned_FalseIfNoPermission() {
128 initStatusBarNotification(true);
129
130 assertFalse(
131 NotificationFilter.showNotificationEvenIfUnprovisioned(
132 mMockPackageManager,
133 mMockStatusBarNotification));
134 }
135
136 @Test
137 @UiThreadTest
138 public void testShowNotificationEvenIfUnprovisioned_TrueIfHasPermissionAndExtra() {
139 initStatusBarNotification(true);
140 when(mMockStatusBarNotification.getUid()).thenReturn(UID_ALLOW_DURING_SETUP);
141
142 assertTrue(
143 NotificationFilter.showNotificationEvenIfUnprovisioned(
144 mMockPackageManager,
145 mMockStatusBarNotification));
146 }
147
148 @Test
149 public void testSuppressSystemAlertNotification() {
150 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(false);
151 when(mFsc.isSystemAlertNotification(any())).thenReturn(true);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400152 StatusBarNotification sbn = mRow.getEntry().getSbn();
Gus Prevasec9e1f02018-12-18 15:28:12 -0500153 Bundle bundle = new Bundle();
154 bundle.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[]{"something"});
155 sbn.getNotification().extras = bundle;
156
157 assertTrue(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
158 }
159
160 @Test
161 public void testDoNotSuppressSystemAlertNotification() {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400162 StatusBarNotification sbn = mRow.getEntry().getSbn();
Gus Prevasec9e1f02018-12-18 15:28:12 -0500163 Bundle bundle = new Bundle();
164 bundle.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[]{"something"});
165 sbn.getNotification().extras = bundle;
166
167 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(true);
168 when(mFsc.isSystemAlertNotification(any())).thenReturn(true);
169
170 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
171
172 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(true);
173 when(mFsc.isSystemAlertNotification(any())).thenReturn(false);
174
175 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
176
177 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(false);
178 when(mFsc.isSystemAlertNotification(any())).thenReturn(false);
179
180 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
181 }
182
183 @Test
184 public void testDoNotSuppressMalformedSystemAlertNotification() {
185 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(true);
186
187 // missing extra
188 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
189
Ned Burns00b4b2d2019-10-17 22:09:27 -0400190 StatusBarNotification sbn = mRow.getEntry().getSbn();
Gus Prevasec9e1f02018-12-18 15:28:12 -0500191 Bundle bundle = new Bundle();
192 bundle.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[]{});
193 sbn.getNotification().extras = bundle;
194
195 // extra missing values
196 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
197 }
198
199 @Test
200 public void testShouldFilterHiddenNotifications() {
201 initStatusBarNotification(false);
202 // setup
203 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(false);
204 when(mFsc.isSystemAlertNotification(any())).thenReturn(false);
205
206 // test should filter out hidden notifications:
207 // hidden
Ned Burns47c98f12019-09-06 17:12:07 -0400208 NotificationEntry entry = new NotificationEntryBuilder()
209 .setSuspended(true)
210 .build();
211
Gus Prevasec9e1f02018-12-18 15:28:12 -0500212 assertTrue(mNotificationFilter.shouldFilterOut(entry));
213
214 // not hidden
Ned Burns47c98f12019-09-06 17:12:07 -0400215 entry = new NotificationEntryBuilder()
216 .setSuspended(false)
217 .build();
Gus Prevasec9e1f02018-12-18 15:28:12 -0500218 assertFalse(mNotificationFilter.shouldFilterOut(entry));
219 }
220
221 private void initStatusBarNotification(boolean allowDuringSetup) {
222 Bundle bundle = new Bundle();
223 bundle.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, allowDuringSetup);
224 Notification notification = new Notification.Builder(mContext, "test")
225 .addExtras(bundle)
226 .build();
227 when(mMockStatusBarNotification.getNotification()).thenReturn(notification);
228 }
229}