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