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; |
| 35 | import android.testing.TestableLooper; |
| 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; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.NotificationTestHelper; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 45 | import com.android.systemui.statusbar.notification.collection.NotificationData; |
| 46 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 47 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 48 | import com.android.systemui.statusbar.phone.NotificationGroupManager; |
| 49 | import com.android.systemui.statusbar.phone.ShadeController; |
| 50 | |
| 51 | import org.junit.Before; |
| 52 | import org.junit.Test; |
| 53 | import org.junit.runner.RunWith; |
| 54 | import org.mockito.Mock; |
| 55 | import org.mockito.MockitoAnnotations; |
| 56 | |
| 57 | @SmallTest |
| 58 | @RunWith(AndroidTestingRunner.class) |
| 59 | @RunWithLooper |
| 60 | public 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 Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 94 | new NotificationGroupManager(mock(StatusBarStateController.class))); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 95 | 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 Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 198 | NotificationEntry entry = new NotificationEntry(mMockStatusBarNotification); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 199 | entry.suspended = true; |
| 200 | assertTrue(mNotificationFilter.shouldFilterOut(entry)); |
| 201 | |
| 202 | // not hidden |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 203 | entry = new NotificationEntry(mMockStatusBarNotification); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 204 | 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 | } |