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; |
Steve Elliott | d38a70f | 2020-05-11 14:01:08 -0400 | [diff] [blame] | 48 | import com.android.systemui.statusbar.notification.people.PeopleNotificationIdentifier; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 49 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Kevin Han | 933dc7c | 2020-01-29 11:17:46 -0800 | [diff] [blame] | 50 | import com.android.systemui.statusbar.notification.row.NotificationTestHelper; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 51 | import com.android.systemui.statusbar.phone.NotificationGroupManager; |
| 52 | import com.android.systemui.statusbar.phone.ShadeController; |
| 53 | |
| 54 | import org.junit.Before; |
| 55 | import org.junit.Test; |
| 56 | import org.junit.runner.RunWith; |
| 57 | import org.mockito.Mock; |
| 58 | import org.mockito.MockitoAnnotations; |
| 59 | |
| 60 | @SmallTest |
| 61 | @RunWith(AndroidTestingRunner.class) |
| 62 | @RunWithLooper |
| 63 | public 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 Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 75 | KeyguardEnvironment mEnvironment; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 76 | 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 { |
Beverly | 1467c9e | 2020-02-18 13:31:29 -0500 | [diff] [blame] | 83 | allowTestableLooperAsMainThread(); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 84 | 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 Elliott | d38a70f | 2020-05-11 14:01:08 -0400 | [diff] [blame] | 97 | new NotificationGroupManager( |
| 98 | mock(StatusBarStateController.class), |
| 99 | () -> mock(PeopleNotificationIdentifier.class))); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 100 | mDependency.injectMockDependency(ShadeController.class); |
Lucas Dupin | d236ee3 | 2019-10-08 15:33:59 -0700 | [diff] [blame] | 101 | mDependency.injectMockDependency(NotificationLockscreenUserManager.class); |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 102 | mDependency.injectTestDependency(KeyguardEnvironment.class, mEnvironment); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 103 | when(mEnvironment.isDeviceProvisioned()).thenReturn(true); |
| 104 | when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true); |
Kevin Han | a7c21be | 2020-04-01 17:58:35 -0700 | [diff] [blame] | 105 | NotificationTestHelper testHelper = new NotificationTestHelper( |
| 106 | mContext, |
| 107 | mDependency, |
| 108 | TestableLooper.get(this)); |
| 109 | mRow = testHelper.createRow(); |
Heemin Seog | e9f4e96 | 2019-12-05 11:55:27 -0800 | [diff] [blame] | 110 | mNotificationFilter = new NotificationFilter(mock(StatusBarStateController.class)); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 111 | } |
| 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 Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 152 | StatusBarNotification sbn = mRow.getEntry().getSbn(); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 153 | 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 Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 162 | StatusBarNotification sbn = mRow.getEntry().getSbn(); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 163 | 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 Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 190 | StatusBarNotification sbn = mRow.getEntry().getSbn(); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 191 | 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 Burns | 47c98f1 | 2019-09-06 17:12:07 -0400 | [diff] [blame] | 208 | NotificationEntry entry = new NotificationEntryBuilder() |
| 209 | .setSuspended(true) |
| 210 | .build(); |
| 211 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 212 | assertTrue(mNotificationFilter.shouldFilterOut(entry)); |
| 213 | |
| 214 | // not hidden |
Ned Burns | 47c98f1 | 2019-09-06 17:12:07 -0400 | [diff] [blame] | 215 | entry = new NotificationEntryBuilder() |
| 216 | .setSuspended(false) |
| 217 | .build(); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 218 | 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 | } |