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 | |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 98 | private int mCurrentUserId; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 99 | private TestNotificationLockscreenUserManager mLockscreenUserManager; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 100 | |
| 101 | @Before |
| 102 | public void setUp() { |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 103 | MockitoAnnotations.initMocks(this); |
| 104 | mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 105 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 106 | mCurrentUserId = ActivityManager.getCurrentUser(); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 107 | |
| 108 | when(mUserManager.getProfiles(mCurrentUserId)).thenReturn(Lists.newArrayList( |
| 109 | new UserInfo(mCurrentUserId, "", 0), new UserInfo(mCurrentUserId + 1, "", 0))); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 110 | mDependency.injectTestDependency(Dependency.MAIN_HANDLER, |
| 111 | Handler.createAsync(Looper.myLooper())); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 112 | |
| 113 | mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 114 | mLockscreenUserManager.setUpWithPresenter(mPresenter); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void testLockScreenShowNotificationsChangeUpdatesNotifications() { |
| 119 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
Beverly | 85d4c19 | 2019-09-30 11:40:39 -0400 | [diff] [blame] | 120 | verify(mEntryManager, times(1)).updateNotifications(anyString()); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testLockScreenShowNotificationsFalse() { |
| 125 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 126 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0); |
| 127 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 128 | assertFalse(mLockscreenUserManager.shouldShowLockscreenNotifications()); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testLockScreenShowNotificationsTrue() { |
| 133 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 134 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1); |
| 135 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 136 | assertTrue(mLockscreenUserManager.shouldShowLockscreenNotifications()); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void testLockScreenAllowPrivateNotificationsTrue() { |
| 141 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 142 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1); |
| 143 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 144 | assertTrue(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUserId)); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testLockScreenAllowPrivateNotificationsFalse() { |
| 149 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 150 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0); |
| 151 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 152 | assertFalse(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUserId)); |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testSettingsObserverUpdatesNotifications() { |
| 157 | when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true); |
| 158 | mLockscreenUserManager.getSettingsObserverForTest().onChange(false); |
Beverly | 85d4c19 | 2019-09-30 11:40:39 -0400 | [diff] [blame] | 159 | verify(mEntryManager, times(1)).updateNotifications(anyString()); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Test |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 163 | public void testActionUserSwitchedCallsOnUserSwitched() { |
| 164 | Intent intent = new Intent() |
| 165 | .setAction(ACTION_USER_SWITCHED) |
| 166 | .putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId + 1); |
| 167 | mLockscreenUserManager.getBaseBroadcastReceiverForTest().onReceive(mContext, intent); |
| 168 | verify(mPresenter, times(1)).onUserSwitched(mCurrentUserId + 1); |
| 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void testIsLockscreenPublicMode() { |
| 173 | assertFalse(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUserId)); |
| 174 | mLockscreenUserManager.setLockscreenPublicMode(true, mCurrentUserId); |
| 175 | assertTrue(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUserId)); |
| 176 | } |
| 177 | |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 178 | @Test |
| 179 | public void testShowSilentNotifications_settingSaysShow() { |
| 180 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 181 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1); |
| 182 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 183 | NOTIFICATION_NEW_INTERRUPTION_MODEL, 1); |
| 184 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 185 | Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1); |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 186 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 187 | NotificationEntry entry = new NotificationEntryBuilder() |
| 188 | .setImportance(IMPORTANCE_LOW) |
| 189 | .build(); |
Evan Laird | 25f0275 | 2019-08-14 19:25:06 -0400 | [diff] [blame] | 190 | entry.setBucket(BUCKET_SILENT); |
| 191 | |
| 192 | assertTrue(mLockscreenUserManager.shouldShowOnKeyguard(entry)); |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Test |
| 196 | public void testShowSilentNotifications_settingSaysHide() { |
| 197 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 198 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1); |
| 199 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 200 | NOTIFICATION_NEW_INTERRUPTION_MODEL, 1); |
| 201 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 202 | Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0); |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 203 | |
Beverly | 7c1ad5b | 2019-12-05 17:17:15 -0500 | [diff] [blame] | 204 | final Notification notification = mock(Notification.class); |
| 205 | when(notification.isForegroundService()).thenReturn(true); |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 206 | NotificationEntry entry = new NotificationEntryBuilder() |
| 207 | .setImportance(IMPORTANCE_LOW) |
Beverly | 7c1ad5b | 2019-12-05 17:17:15 -0500 | [diff] [blame] | 208 | .setNotification(notification) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 209 | .build(); |
Evan Laird | 25f0275 | 2019-08-14 19:25:06 -0400 | [diff] [blame] | 210 | entry.setBucket(BUCKET_SILENT); |
| 211 | assertFalse(mLockscreenUserManager.shouldShowOnKeyguard(entry)); |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | private class TestNotificationLockscreenUserManager |
| 215 | extends NotificationLockscreenUserManagerImpl { |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 216 | public TestNotificationLockscreenUserManager(Context context) { |
Dave Mankoff | 9427d1f | 2019-11-19 21:52:51 -0500 | [diff] [blame] | 217 | super(context, mBroadcastDispatcher, mDevicePolicyManager, mUserManager, |
| 218 | mIStatusBarService, NotificationLockscreenUserManagerTest.this.mKeyguardManager, |
| 219 | mStatusBarStateController, Handler.createAsync(Looper.myLooper()), |
| 220 | mDeviceProvisionedController, mKeyguardStateController); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 221 | } |
| 222 | |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 223 | public BroadcastReceiver getBaseBroadcastReceiverForTest() { |
| 224 | return mBaseBroadcastReceiver; |
| 225 | } |
| 226 | |
| 227 | public ContentObserver getLockscreenSettingsObserverForTest() { |
| 228 | return mLockscreenSettingsObserver; |
| 229 | } |
| 230 | |
| 231 | public ContentObserver getSettingsObserverForTest() { |
| 232 | return mSettingsObserver; |
| 233 | } |
| 234 | } |
| 235 | } |