blob: 277ac244cec533548423df4a432bfd558600fb15 [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;
Gus Prevasec9e1f02018-12-18 15:28:12 -050048import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Kevin Han933dc7c2020-01-29 11:17:46 -080049import com.android.systemui.statusbar.notification.row.NotificationTestHelper;
Gus Prevasec9e1f02018-12-18 15:28:12 -050050import com.android.systemui.statusbar.phone.NotificationGroupManager;
51import com.android.systemui.statusbar.phone.ShadeController;
52
53import org.junit.Before;
54import org.junit.Test;
55import org.junit.runner.RunWith;
56import org.mockito.Mock;
57import org.mockito.MockitoAnnotations;
58
59@SmallTest
60@RunWith(AndroidTestingRunner.class)
61@RunWithLooper
62public class NotificationFilterTest extends SysuiTestCase {
63
64 private static final int UID_NORMAL = 123;
65 private static final int UID_ALLOW_DURING_SETUP = 456;
66 private static final String TEST_HIDDEN_NOTIFICATION_KEY = "testHiddenNotificationKey";
67
68 private final StatusBarNotification mMockStatusBarNotification =
69 mock(StatusBarNotification.class);
70
71 @Mock
72 ForegroundServiceController mFsc;
73 @Mock
Evan Laird181de622019-10-24 09:53:02 -040074 KeyguardEnvironment mEnvironment;
Gus Prevasec9e1f02018-12-18 15:28:12 -050075 private final IPackageManager mMockPackageManager = mock(IPackageManager.class);
76
77 private NotificationFilter mNotificationFilter;
78 private ExpandableNotificationRow mRow;
79
80 @Before
81 public void setUp() throws Exception {
Beverly1467c9e2020-02-18 13:31:29 -050082 allowTestableLooperAsMainThread();
Gus Prevasec9e1f02018-12-18 15:28:12 -050083 MockitoAnnotations.initMocks(this);
84 when(mMockStatusBarNotification.getUid()).thenReturn(UID_NORMAL);
85
86 when(mMockPackageManager.checkUidPermission(
87 eq(Manifest.permission.NOTIFICATION_DURING_SETUP),
88 eq(UID_NORMAL)))
89 .thenReturn(PackageManager.PERMISSION_DENIED);
90 when(mMockPackageManager.checkUidPermission(
91 eq(Manifest.permission.NOTIFICATION_DURING_SETUP),
92 eq(UID_ALLOW_DURING_SETUP)))
93 .thenReturn(PackageManager.PERMISSION_GRANTED);
94 mDependency.injectTestDependency(ForegroundServiceController.class, mFsc);
95 mDependency.injectTestDependency(NotificationGroupManager.class,
Selim Cinekc3fec682019-06-06 18:11:07 -070096 new NotificationGroupManager(mock(StatusBarStateController.class)));
Gus Prevasec9e1f02018-12-18 15:28:12 -050097 mDependency.injectMockDependency(ShadeController.class);
Lucas Dupind236ee32019-10-08 15:33:59 -070098 mDependency.injectMockDependency(NotificationLockscreenUserManager.class);
Evan Laird181de622019-10-24 09:53:02 -040099 mDependency.injectTestDependency(KeyguardEnvironment.class, mEnvironment);
Gus Prevasec9e1f02018-12-18 15:28:12 -0500100 when(mEnvironment.isDeviceProvisioned()).thenReturn(true);
101 when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true);
Kevin Hana7c21be2020-04-01 17:58:35 -0700102 NotificationTestHelper testHelper = new NotificationTestHelper(
103 mContext,
104 mDependency,
105 TestableLooper.get(this));
106 mRow = testHelper.createRow();
Heemin Seoge9f4e962019-12-05 11:55:27 -0800107 mNotificationFilter = new NotificationFilter(mock(StatusBarStateController.class));
Gus Prevasec9e1f02018-12-18 15:28:12 -0500108 }
109
110 @Test
111 @UiThreadTest
112 public void testShowNotificationEvenIfUnprovisioned_FalseIfNoExtra() {
113 initStatusBarNotification(false);
114 when(mMockStatusBarNotification.getUid()).thenReturn(UID_ALLOW_DURING_SETUP);
115
116 assertFalse(
117 NotificationFilter.showNotificationEvenIfUnprovisioned(
118 mMockPackageManager,
119 mMockStatusBarNotification));
120 }
121
122 @Test
123 @UiThreadTest
124 public void testShowNotificationEvenIfUnprovisioned_FalseIfNoPermission() {
125 initStatusBarNotification(true);
126
127 assertFalse(
128 NotificationFilter.showNotificationEvenIfUnprovisioned(
129 mMockPackageManager,
130 mMockStatusBarNotification));
131 }
132
133 @Test
134 @UiThreadTest
135 public void testShowNotificationEvenIfUnprovisioned_TrueIfHasPermissionAndExtra() {
136 initStatusBarNotification(true);
137 when(mMockStatusBarNotification.getUid()).thenReturn(UID_ALLOW_DURING_SETUP);
138
139 assertTrue(
140 NotificationFilter.showNotificationEvenIfUnprovisioned(
141 mMockPackageManager,
142 mMockStatusBarNotification));
143 }
144
145 @Test
146 public void testSuppressSystemAlertNotification() {
147 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(false);
148 when(mFsc.isSystemAlertNotification(any())).thenReturn(true);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400149 StatusBarNotification sbn = mRow.getEntry().getSbn();
Gus Prevasec9e1f02018-12-18 15:28:12 -0500150 Bundle bundle = new Bundle();
151 bundle.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[]{"something"});
152 sbn.getNotification().extras = bundle;
153
154 assertTrue(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
155 }
156
157 @Test
158 public void testDoNotSuppressSystemAlertNotification() {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400159 StatusBarNotification sbn = mRow.getEntry().getSbn();
Gus Prevasec9e1f02018-12-18 15:28:12 -0500160 Bundle bundle = new Bundle();
161 bundle.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[]{"something"});
162 sbn.getNotification().extras = bundle;
163
164 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(true);
165 when(mFsc.isSystemAlertNotification(any())).thenReturn(true);
166
167 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
168
169 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(true);
170 when(mFsc.isSystemAlertNotification(any())).thenReturn(false);
171
172 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
173
174 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(false);
175 when(mFsc.isSystemAlertNotification(any())).thenReturn(false);
176
177 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
178 }
179
180 @Test
181 public void testDoNotSuppressMalformedSystemAlertNotification() {
182 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(true);
183
184 // missing extra
185 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
186
Ned Burns00b4b2d2019-10-17 22:09:27 -0400187 StatusBarNotification sbn = mRow.getEntry().getSbn();
Gus Prevasec9e1f02018-12-18 15:28:12 -0500188 Bundle bundle = new Bundle();
189 bundle.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[]{});
190 sbn.getNotification().extras = bundle;
191
192 // extra missing values
193 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
194 }
195
196 @Test
197 public void testShouldFilterHiddenNotifications() {
198 initStatusBarNotification(false);
199 // setup
200 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(false);
201 when(mFsc.isSystemAlertNotification(any())).thenReturn(false);
202
203 // test should filter out hidden notifications:
204 // hidden
Ned Burns47c98f12019-09-06 17:12:07 -0400205 NotificationEntry entry = new NotificationEntryBuilder()
206 .setSuspended(true)
207 .build();
208
Gus Prevasec9e1f02018-12-18 15:28:12 -0500209 assertTrue(mNotificationFilter.shouldFilterOut(entry));
210
211 // not hidden
Ned Burns47c98f12019-09-06 17:12:07 -0400212 entry = new NotificationEntryBuilder()
213 .setSuspended(false)
214 .build();
Gus Prevasec9e1f02018-12-18 15:28:12 -0500215 assertFalse(mNotificationFilter.shouldFilterOut(entry));
216 }
217
218 private void initStatusBarNotification(boolean allowDuringSetup) {
219 Bundle bundle = new Bundle();
220 bundle.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, allowDuringSetup);
221 Notification notification = new Notification.Builder(mContext, "test")
222 .addExtras(bundle)
223 .build();
224 when(mMockStatusBarNotification.getNotification()).thenReturn(notification);
225 }
226}