Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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; |
| 18 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 19 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 20 | import static android.content.Intent.ACTION_USER_SWITCHED; |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 21 | import static android.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 22 | |
Evan Laird | 25f0275 | 2019-08-14 19:25:06 -0400 | [diff] [blame] | 23 | import static com.android.systemui.statusbar.notification.stack.NotificationSectionsManager.BUCKET_SILENT; |
| 24 | |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 25 | import static junit.framework.Assert.assertFalse; |
| 26 | import static junit.framework.Assert.assertTrue; |
| 27 | |
Beverly | 85d4c19 | 2019-09-30 11:40:39 -0400 | [diff] [blame] | 28 | import static org.mockito.ArgumentMatchers.anyString; |
Beverly | 7c1ad5b | 2019-12-05 17:17:15 -0500 | [diff] [blame] | 29 | import static org.mockito.Mockito.mock; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 30 | import static org.mockito.Mockito.times; |
| 31 | import static org.mockito.Mockito.verify; |
| 32 | import static org.mockito.Mockito.when; |
| 33 | |
| 34 | import android.app.ActivityManager; |
Dave Mankoff | 9427d1f | 2019-11-19 21:52:51 -0500 | [diff] [blame] | 35 | import android.app.KeyguardManager; |
Beverly | 7c1ad5b | 2019-12-05 17:17:15 -0500 | [diff] [blame] | 36 | import android.app.Notification; |
Dave Mankoff | 9427d1f | 2019-11-19 21:52:51 -0500 | [diff] [blame] | 37 | import android.app.admin.DevicePolicyManager; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 38 | import android.content.BroadcastReceiver; |
| 39 | import android.content.Context; |
| 40 | import android.content.Intent; |
| 41 | import android.content.pm.UserInfo; |
| 42 | import android.database.ContentObserver; |
| 43 | import android.os.Handler; |
| 44 | import android.os.Looper; |
| 45 | import android.os.UserManager; |
| 46 | import android.provider.Settings; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 47 | import android.testing.AndroidTestingRunner; |
| 48 | import android.testing.TestableLooper; |
| 49 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 50 | import androidx.test.filters.SmallTest; |
| 51 | |
Dave Mankoff | 9427d1f | 2019-11-19 21:52:51 -0500 | [diff] [blame] | 52 | import com.android.internal.statusbar.IStatusBarService; |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 53 | import com.android.systemui.Dependency; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 54 | import com.android.systemui.SysuiTestCase; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 55 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Dave Mankoff | 9427d1f | 2019-11-19 21:52:51 -0500 | [diff] [blame] | 56 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 57 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | 8c1b763 | 2019-07-19 14:26:15 -0400 | [diff] [blame] | 58 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Beverly | 79c89ec | 2019-12-13 10:33:01 -0500 | [diff] [blame] | 59 | import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 60 | import com.android.systemui.statusbar.policy.DeviceProvisionedController; |
Lucas Dupin | d236ee3 | 2019-10-08 15:33:59 -0700 | [diff] [blame] | 61 | import com.android.systemui.statusbar.policy.KeyguardStateController; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 62 | |
| 63 | import com.google.android.collect.Lists; |
| 64 | |
| 65 | import org.junit.Before; |
| 66 | import org.junit.Test; |
| 67 | import org.junit.runner.RunWith; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 68 | import org.mockito.Mock; |
| 69 | import org.mockito.MockitoAnnotations; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 70 | |
| 71 | @SmallTest |
| 72 | @RunWith(AndroidTestingRunner.class) |
| 73 | @TestableLooper.RunWithLooper |
| 74 | public class NotificationLockscreenUserManagerTest extends SysuiTestCase { |
Dave Mankoff | 9427d1f | 2019-11-19 21:52:51 -0500 | [diff] [blame] | 75 | @Mock |
| 76 | private NotificationPresenter mPresenter; |
| 77 | @Mock |
| 78 | private UserManager mUserManager; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 79 | |
| 80 | // Dependency mocks: |
Dave Mankoff | 9427d1f | 2019-11-19 21:52:51 -0500 | [diff] [blame] | 81 | @Mock |
| 82 | private NotificationEntryManager mEntryManager; |
| 83 | @Mock |
| 84 | private DevicePolicyManager mDevicePolicyManager; |
| 85 | @Mock |
| 86 | private IStatusBarService mIStatusBarService; |
| 87 | @Mock |
| 88 | private KeyguardManager mKeyguardManager; |
| 89 | @Mock |
| 90 | private DeviceProvisionedController mDeviceProvisionedController; |
| 91 | @Mock |
| 92 | private StatusBarStateController mStatusBarStateController; |
| 93 | @Mock |
| 94 | private BroadcastDispatcher mBroadcastDispatcher; |
| 95 | @Mock |
| 96 | private KeyguardStateController mKeyguardStateController; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 97 | |
Lucas Dupin | 960b7e7f | 2020-01-24 15:47:28 -0800 | [diff] [blame] | 98 | private UserInfo mCurrentUser; |
| 99 | private UserInfo mSecondaryUser; |
| 100 | private UserInfo mWorkUser; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 101 | private TestNotificationLockscreenUserManager mLockscreenUserManager; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 102 | |
| 103 | @Before |
| 104 | public void setUp() { |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 105 | MockitoAnnotations.initMocks(this); |
| 106 | mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 107 | |
Lucas Dupin | 960b7e7f | 2020-01-24 15:47:28 -0800 | [diff] [blame] | 108 | int currentUserId = ActivityManager.getCurrentUser(); |
| 109 | mCurrentUser = new UserInfo(currentUserId, "", 0); |
| 110 | mSecondaryUser = new UserInfo(currentUserId + 1, "", 0); |
| 111 | mWorkUser = new UserInfo(currentUserId + 2, "" /* name */, null /* iconPath */, 0, |
| 112 | UserManager.USER_TYPE_PROFILE_MANAGED); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 113 | |
Lucas Dupin | 960b7e7f | 2020-01-24 15:47:28 -0800 | [diff] [blame] | 114 | when(mUserManager.getProfiles(currentUserId)).thenReturn(Lists.newArrayList( |
| 115 | mCurrentUser, mSecondaryUser, mWorkUser)); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 116 | mDependency.injectTestDependency(Dependency.MAIN_HANDLER, |
| 117 | Handler.createAsync(Looper.myLooper())); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 118 | |
| 119 | mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 120 | mLockscreenUserManager.setUpWithPresenter(mPresenter); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testLockScreenShowNotificationsChangeUpdatesNotifications() { |
| 125 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
Beverly | 85d4c19 | 2019-09-30 11:40:39 -0400 | [diff] [blame] | 126 | verify(mEntryManager, times(1)).updateNotifications(anyString()); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testLockScreenShowNotificationsFalse() { |
| 131 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 132 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0); |
| 133 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 134 | assertFalse(mLockscreenUserManager.shouldShowLockscreenNotifications()); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void testLockScreenShowNotificationsTrue() { |
| 139 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 140 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1); |
| 141 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 142 | assertTrue(mLockscreenUserManager.shouldShowLockscreenNotifications()); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void testLockScreenAllowPrivateNotificationsTrue() { |
| 147 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 148 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1); |
| 149 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
Lucas Dupin | 960b7e7f | 2020-01-24 15:47:28 -0800 | [diff] [blame] | 150 | assertTrue(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUser.id)); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void testLockScreenAllowPrivateNotificationsFalse() { |
Lucas Dupin | 960b7e7f | 2020-01-24 15:47:28 -0800 | [diff] [blame] | 155 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 156 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mCurrentUser.id); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 157 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
Lucas Dupin | 960b7e7f | 2020-01-24 15:47:28 -0800 | [diff] [blame] | 158 | assertFalse(mLockscreenUserManager.userAllowsNotificationsInPublic(mCurrentUser.id)); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void testLockScreenAllowsWorkPrivateNotificationsFalse() { |
| 163 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 164 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mWorkUser.id); |
| 165 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 166 | assertFalse(mLockscreenUserManager.allowsManagedPrivateNotificationsInPublic()); |
| 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testLockScreenAllowsWorkPrivateNotificationsTrue() { |
| 171 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 172 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mWorkUser.id); |
| 173 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 174 | assertTrue(mLockscreenUserManager.allowsManagedPrivateNotificationsInPublic()); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void testSettingsObserverUpdatesNotifications() { |
| 179 | when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true); |
| 180 | mLockscreenUserManager.getSettingsObserverForTest().onChange(false); |
Beverly | 85d4c19 | 2019-09-30 11:40:39 -0400 | [diff] [blame] | 181 | verify(mEntryManager, times(1)).updateNotifications(anyString()); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Test |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 185 | public void testActionUserSwitchedCallsOnUserSwitched() { |
| 186 | Intent intent = new Intent() |
| 187 | .setAction(ACTION_USER_SWITCHED) |
Lucas Dupin | 960b7e7f | 2020-01-24 15:47:28 -0800 | [diff] [blame] | 188 | .putExtra(Intent.EXTRA_USER_HANDLE, mSecondaryUser.id); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 189 | mLockscreenUserManager.getBaseBroadcastReceiverForTest().onReceive(mContext, intent); |
Lucas Dupin | 960b7e7f | 2020-01-24 15:47:28 -0800 | [diff] [blame] | 190 | verify(mPresenter, times(1)).onUserSwitched(mSecondaryUser.id); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void testIsLockscreenPublicMode() { |
Lucas Dupin | 960b7e7f | 2020-01-24 15:47:28 -0800 | [diff] [blame] | 195 | assertFalse(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUser.id)); |
| 196 | mLockscreenUserManager.setLockscreenPublicMode(true, mCurrentUser.id); |
| 197 | assertTrue(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUser.id)); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 198 | } |
| 199 | |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 200 | @Test |
| 201 | public void testShowSilentNotifications_settingSaysShow() { |
| 202 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 203 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1); |
| 204 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 205 | NOTIFICATION_NEW_INTERRUPTION_MODEL, 1); |
| 206 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 207 | Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1); |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 208 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 209 | NotificationEntry entry = new NotificationEntryBuilder() |
| 210 | .setImportance(IMPORTANCE_LOW) |
| 211 | .build(); |
Evan Laird | 25f0275 | 2019-08-14 19:25:06 -0400 | [diff] [blame] | 212 | entry.setBucket(BUCKET_SILENT); |
| 213 | |
| 214 | assertTrue(mLockscreenUserManager.shouldShowOnKeyguard(entry)); |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Test |
| 218 | public void testShowSilentNotifications_settingSaysHide() { |
| 219 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 220 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1); |
| 221 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 222 | NOTIFICATION_NEW_INTERRUPTION_MODEL, 1); |
| 223 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 224 | Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0); |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 225 | |
Beverly | 7c1ad5b | 2019-12-05 17:17:15 -0500 | [diff] [blame] | 226 | final Notification notification = mock(Notification.class); |
| 227 | when(notification.isForegroundService()).thenReturn(true); |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 228 | NotificationEntry entry = new NotificationEntryBuilder() |
| 229 | .setImportance(IMPORTANCE_LOW) |
Beverly | 7c1ad5b | 2019-12-05 17:17:15 -0500 | [diff] [blame] | 230 | .setNotification(notification) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 231 | .build(); |
Evan Laird | 25f0275 | 2019-08-14 19:25:06 -0400 | [diff] [blame] | 232 | entry.setBucket(BUCKET_SILENT); |
| 233 | assertFalse(mLockscreenUserManager.shouldShowOnKeyguard(entry)); |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | private class TestNotificationLockscreenUserManager |
| 237 | extends NotificationLockscreenUserManagerImpl { |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 238 | public TestNotificationLockscreenUserManager(Context context) { |
Dave Mankoff | 9427d1f | 2019-11-19 21:52:51 -0500 | [diff] [blame] | 239 | super(context, mBroadcastDispatcher, mDevicePolicyManager, mUserManager, |
| 240 | mIStatusBarService, NotificationLockscreenUserManagerTest.this.mKeyguardManager, |
| 241 | mStatusBarStateController, Handler.createAsync(Looper.myLooper()), |
| 242 | mDeviceProvisionedController, mKeyguardStateController); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 243 | } |
| 244 | |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 245 | public BroadcastReceiver getBaseBroadcastReceiverForTest() { |
| 246 | return mBaseBroadcastReceiver; |
| 247 | } |
| 248 | |
| 249 | public ContentObserver getLockscreenSettingsObserverForTest() { |
| 250 | return mLockscreenSettingsObserver; |
| 251 | } |
| 252 | |
| 253 | public ContentObserver getSettingsObserverForTest() { |
| 254 | return mSettingsObserver; |
| 255 | } |
| 256 | } |
| 257 | } |