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