Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.notification; |
| 18 | |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.anyInt; |
| 23 | import static org.mockito.ArgumentMatchers.anyString; |
| 24 | import static org.mockito.ArgumentMatchers.eq; |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.Manifest; |
| 29 | import android.app.Notification; |
| 30 | import android.content.pm.IPackageManager; |
| 31 | import android.content.pm.PackageManager; |
| 32 | import android.os.Bundle; |
| 33 | import android.service.notification.StatusBarNotification; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 34 | import android.testing.AndroidTestingRunner; |
Kevin Han | a7c21be | 2020-04-01 17:58:35 -0700 | [diff] [blame] | 35 | import android.testing.TestableLooper; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 36 | import android.testing.TestableLooper.RunWithLooper; |
| 37 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 38 | import androidx.test.annotation.UiThreadTest; |
| 39 | import androidx.test.filters.SmallTest; |
| 40 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 41 | import com.android.systemui.ForegroundServiceController; |
| 42 | import com.android.systemui.SysuiTestCase; |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 43 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Lucas Dupin | d236ee3 | 2019-10-08 15:33:59 -0700 | [diff] [blame] | 44 | import com.android.systemui.statusbar.NotificationLockscreenUserManager; |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 45 | import com.android.systemui.statusbar.notification.NotificationEntryManager.KeyguardEnvironment; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 46 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Beverly | 79c89ec | 2019-12-13 10:33:01 -0500 | [diff] [blame] | 47 | import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 48 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Kevin Han | 933dc7c | 2020-01-29 11:17:46 -0800 | [diff] [blame] | 49 | import com.android.systemui.statusbar.notification.row.NotificationTestHelper; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 50 | import com.android.systemui.statusbar.phone.NotificationGroupManager; |
| 51 | import com.android.systemui.statusbar.phone.ShadeController; |
| 52 | |
| 53 | import org.junit.Before; |
| 54 | import org.junit.Test; |
| 55 | import org.junit.runner.RunWith; |
| 56 | import org.mockito.Mock; |
| 57 | import org.mockito.MockitoAnnotations; |
| 58 | |
| 59 | @SmallTest |
| 60 | @RunWith(AndroidTestingRunner.class) |
| 61 | @RunWithLooper |
| 62 | public 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 Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 74 | KeyguardEnvironment mEnvironment; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 75 | 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 { |
Beverly | 1467c9e | 2020-02-18 13:31:29 -0500 | [diff] [blame] | 82 | allowTestableLooperAsMainThread(); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 83 | 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 Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 96 | new NotificationGroupManager(mock(StatusBarStateController.class))); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 97 | mDependency.injectMockDependency(ShadeController.class); |
Lucas Dupin | d236ee3 | 2019-10-08 15:33:59 -0700 | [diff] [blame] | 98 | mDependency.injectMockDependency(NotificationLockscreenUserManager.class); |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 99 | mDependency.injectTestDependency(KeyguardEnvironment.class, mEnvironment); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 100 | when(mEnvironment.isDeviceProvisioned()).thenReturn(true); |
| 101 | when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true); |
Kevin Han | a7c21be | 2020-04-01 17:58:35 -0700 | [diff] [blame] | 102 | NotificationTestHelper testHelper = new NotificationTestHelper( |
| 103 | mContext, |
| 104 | mDependency, |
| 105 | TestableLooper.get(this)); |
| 106 | mRow = testHelper.createRow(); |
Heemin Seog | e9f4e96 | 2019-12-05 11:55:27 -0800 | [diff] [blame] | 107 | mNotificationFilter = new NotificationFilter(mock(StatusBarStateController.class)); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 108 | } |
| 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 Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 149 | StatusBarNotification sbn = mRow.getEntry().getSbn(); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 150 | 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 Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 159 | StatusBarNotification sbn = mRow.getEntry().getSbn(); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 160 | 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 Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 187 | StatusBarNotification sbn = mRow.getEntry().getSbn(); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 188 | 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 Burns | 47c98f1 | 2019-09-06 17:12:07 -0400 | [diff] [blame] | 205 | NotificationEntry entry = new NotificationEntryBuilder() |
| 206 | .setSuspended(true) |
| 207 | .build(); |
| 208 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 209 | assertTrue(mNotificationFilter.shouldFilterOut(entry)); |
| 210 | |
| 211 | // not hidden |
Ned Burns | 47c98f1 | 2019-09-06 17:12:07 -0400 | [diff] [blame] | 212 | entry = new NotificationEntryBuilder() |
| 213 | .setSuspended(false) |
| 214 | .build(); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 215 | 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 | } |