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