blob: 6f7751b2e2a5d9b86a8132de4b2c15832d6c144f [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;
35import android.testing.TestableLooper;
36import 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;
Gus Prevasec9e1f02018-12-18 15:28:12 -050044import com.android.systemui.statusbar.NotificationTestHelper;
Ned Burnsf81c4c42019-01-07 14:10:43 -050045import com.android.systemui.statusbar.notification.collection.NotificationData;
46import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Gus Prevasec9e1f02018-12-18 15:28:12 -050047import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
48import com.android.systemui.statusbar.phone.NotificationGroupManager;
49import com.android.systemui.statusbar.phone.ShadeController;
50
51import org.junit.Before;
52import org.junit.Test;
53import org.junit.runner.RunWith;
54import org.mockito.Mock;
55import org.mockito.MockitoAnnotations;
56
57@SmallTest
58@RunWith(AndroidTestingRunner.class)
59@RunWithLooper
60public class NotificationFilterTest extends SysuiTestCase {
61
62 private static final int UID_NORMAL = 123;
63 private static final int UID_ALLOW_DURING_SETUP = 456;
64 private static final String TEST_HIDDEN_NOTIFICATION_KEY = "testHiddenNotificationKey";
65
66 private final StatusBarNotification mMockStatusBarNotification =
67 mock(StatusBarNotification.class);
68
69 @Mock
70 ForegroundServiceController mFsc;
71 @Mock
72 NotificationData.KeyguardEnvironment mEnvironment;
73 private final IPackageManager mMockPackageManager = mock(IPackageManager.class);
74
75 private NotificationFilter mNotificationFilter;
76 private ExpandableNotificationRow mRow;
77
78 @Before
79 public void setUp() throws Exception {
80 com.android.systemui.util.Assert.sMainLooper = TestableLooper.get(this).getLooper();
81 MockitoAnnotations.initMocks(this);
82 when(mMockStatusBarNotification.getUid()).thenReturn(UID_NORMAL);
83
84 when(mMockPackageManager.checkUidPermission(
85 eq(Manifest.permission.NOTIFICATION_DURING_SETUP),
86 eq(UID_NORMAL)))
87 .thenReturn(PackageManager.PERMISSION_DENIED);
88 when(mMockPackageManager.checkUidPermission(
89 eq(Manifest.permission.NOTIFICATION_DURING_SETUP),
90 eq(UID_ALLOW_DURING_SETUP)))
91 .thenReturn(PackageManager.PERMISSION_GRANTED);
92 mDependency.injectTestDependency(ForegroundServiceController.class, mFsc);
93 mDependency.injectTestDependency(NotificationGroupManager.class,
Selim Cinekc3fec682019-06-06 18:11:07 -070094 new NotificationGroupManager(mock(StatusBarStateController.class)));
Gus Prevasec9e1f02018-12-18 15:28:12 -050095 mDependency.injectMockDependency(ShadeController.class);
96 mDependency.injectTestDependency(NotificationData.KeyguardEnvironment.class, mEnvironment);
97 when(mEnvironment.isDeviceProvisioned()).thenReturn(true);
98 when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true);
99 mRow = new NotificationTestHelper(getContext()).createRow();
100 mNotificationFilter = new NotificationFilter();
101 }
102
103 @Test
104 @UiThreadTest
105 public void testShowNotificationEvenIfUnprovisioned_FalseIfNoExtra() {
106 initStatusBarNotification(false);
107 when(mMockStatusBarNotification.getUid()).thenReturn(UID_ALLOW_DURING_SETUP);
108
109 assertFalse(
110 NotificationFilter.showNotificationEvenIfUnprovisioned(
111 mMockPackageManager,
112 mMockStatusBarNotification));
113 }
114
115 @Test
116 @UiThreadTest
117 public void testShowNotificationEvenIfUnprovisioned_FalseIfNoPermission() {
118 initStatusBarNotification(true);
119
120 assertFalse(
121 NotificationFilter.showNotificationEvenIfUnprovisioned(
122 mMockPackageManager,
123 mMockStatusBarNotification));
124 }
125
126 @Test
127 @UiThreadTest
128 public void testShowNotificationEvenIfUnprovisioned_TrueIfHasPermissionAndExtra() {
129 initStatusBarNotification(true);
130 when(mMockStatusBarNotification.getUid()).thenReturn(UID_ALLOW_DURING_SETUP);
131
132 assertTrue(
133 NotificationFilter.showNotificationEvenIfUnprovisioned(
134 mMockPackageManager,
135 mMockStatusBarNotification));
136 }
137
138 @Test
139 public void testSuppressSystemAlertNotification() {
140 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(false);
141 when(mFsc.isSystemAlertNotification(any())).thenReturn(true);
142 StatusBarNotification sbn = mRow.getEntry().notification;
143 Bundle bundle = new Bundle();
144 bundle.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[]{"something"});
145 sbn.getNotification().extras = bundle;
146
147 assertTrue(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
148 }
149
150 @Test
151 public void testDoNotSuppressSystemAlertNotification() {
152 StatusBarNotification sbn = mRow.getEntry().notification;
153 Bundle bundle = new Bundle();
154 bundle.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[]{"something"});
155 sbn.getNotification().extras = bundle;
156
157 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(true);
158 when(mFsc.isSystemAlertNotification(any())).thenReturn(true);
159
160 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
161
162 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(true);
163 when(mFsc.isSystemAlertNotification(any())).thenReturn(false);
164
165 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
166
167 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(false);
168 when(mFsc.isSystemAlertNotification(any())).thenReturn(false);
169
170 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
171 }
172
173 @Test
174 public void testDoNotSuppressMalformedSystemAlertNotification() {
175 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(true);
176
177 // missing extra
178 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
179
180 StatusBarNotification sbn = mRow.getEntry().notification;
181 Bundle bundle = new Bundle();
182 bundle.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[]{});
183 sbn.getNotification().extras = bundle;
184
185 // extra missing values
186 assertFalse(mNotificationFilter.shouldFilterOut(mRow.getEntry()));
187 }
188
189 @Test
190 public void testShouldFilterHiddenNotifications() {
191 initStatusBarNotification(false);
192 // setup
193 when(mFsc.isSystemAlertWarningNeeded(anyInt(), anyString())).thenReturn(false);
194 when(mFsc.isSystemAlertNotification(any())).thenReturn(false);
195
196 // test should filter out hidden notifications:
197 // hidden
Ned Burnsf81c4c42019-01-07 14:10:43 -0500198 NotificationEntry entry = new NotificationEntry(mMockStatusBarNotification);
Gus Prevasec9e1f02018-12-18 15:28:12 -0500199 entry.suspended = true;
200 assertTrue(mNotificationFilter.shouldFilterOut(entry));
201
202 // not hidden
Ned Burnsf81c4c42019-01-07 14:10:43 -0500203 entry = new NotificationEntry(mMockStatusBarNotification);
Gus Prevasec9e1f02018-12-18 15:28:12 -0500204 entry.suspended = false;
205 assertFalse(mNotificationFilter.shouldFilterOut(entry));
206 }
207
208 private void initStatusBarNotification(boolean allowDuringSetup) {
209 Bundle bundle = new Bundle();
210 bundle.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, allowDuringSetup);
211 Notification notification = new Notification.Builder(mContext, "test")
212 .addExtras(bundle)
213 .build();
214 when(mMockStatusBarNotification.getNotification()).thenReturn(notification);
215 }
216}