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 | |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 19 | import static android.content.Intent.ACTION_USER_SWITCHED; |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 20 | import static android.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 21 | |
Evan Laird | 25f0275 | 2019-08-14 19:25:06 -0400 | [diff] [blame] | 22 | import static com.android.systemui.statusbar.notification.stack.NotificationSectionsManager.BUCKET_SILENT; |
| 23 | |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 24 | import static junit.framework.Assert.assertFalse; |
| 25 | import static junit.framework.Assert.assertTrue; |
| 26 | |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 27 | import static org.mockito.ArgumentMatchers.any; |
Beverly | 85d4c19 | 2019-09-30 11:40:39 -0400 | [diff] [blame] | 28 | import static org.mockito.ArgumentMatchers.anyString; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 29 | import static org.mockito.Mockito.times; |
| 30 | import static org.mockito.Mockito.verify; |
| 31 | import static org.mockito.Mockito.when; |
| 32 | |
| 33 | import android.app.ActivityManager; |
| 34 | import android.content.BroadcastReceiver; |
| 35 | import android.content.Context; |
| 36 | import android.content.Intent; |
| 37 | import android.content.pm.UserInfo; |
| 38 | import android.database.ContentObserver; |
| 39 | import android.os.Handler; |
| 40 | import android.os.Looper; |
| 41 | import android.os.UserManager; |
| 42 | import android.provider.Settings; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 43 | import android.testing.AndroidTestingRunner; |
| 44 | import android.testing.TestableLooper; |
| 45 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 46 | import androidx.test.filters.SmallTest; |
| 47 | |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 48 | import com.android.systemui.Dependency; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 49 | import com.android.systemui.SysuiTestCase; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 50 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 51 | import com.android.systemui.statusbar.notification.collection.NotificationData; |
Ned Burns | 8c1b763 | 2019-07-19 14:26:15 -0400 | [diff] [blame] | 52 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Evan Laird | db601aa | 2018-09-18 17:47:46 -0400 | [diff] [blame] | 53 | import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 54 | import com.android.systemui.statusbar.policy.DeviceProvisionedController; |
Lucas Dupin | d236ee3 | 2019-10-08 15:33:59 -0700 | [diff] [blame] | 55 | import com.android.systemui.statusbar.policy.KeyguardStateController; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 56 | |
| 57 | import com.google.android.collect.Lists; |
| 58 | |
| 59 | import org.junit.Before; |
| 60 | import org.junit.Test; |
| 61 | import org.junit.runner.RunWith; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 62 | import org.mockito.Mock; |
| 63 | import org.mockito.MockitoAnnotations; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 64 | |
| 65 | @SmallTest |
| 66 | @RunWith(AndroidTestingRunner.class) |
| 67 | @TestableLooper.RunWithLooper |
| 68 | public class NotificationLockscreenUserManagerTest extends SysuiTestCase { |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 69 | @Mock private NotificationPresenter mPresenter; |
| 70 | @Mock private UserManager mUserManager; |
| 71 | |
| 72 | // Dependency mocks: |
| 73 | @Mock private NotificationEntryManager mEntryManager; |
Evan Laird | db601aa | 2018-09-18 17:47:46 -0400 | [diff] [blame] | 74 | @Mock private NotificationData mNotificationData; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 75 | @Mock private DeviceProvisionedController mDeviceProvisionedController; |
Evan Laird | db601aa | 2018-09-18 17:47:46 -0400 | [diff] [blame] | 76 | @Mock private StatusBarKeyguardViewManager mKeyguardViewManager; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 77 | |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 78 | private int mCurrentUserId; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 79 | private TestNotificationLockscreenUserManager mLockscreenUserManager; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 80 | |
| 81 | @Before |
| 82 | public void setUp() { |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 83 | MockitoAnnotations.initMocks(this); |
| 84 | mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager); |
| 85 | mDependency.injectTestDependency(DeviceProvisionedController.class, |
| 86 | mDeviceProvisionedController); |
Lucas Dupin | d236ee3 | 2019-10-08 15:33:59 -0700 | [diff] [blame] | 87 | mDependency.injectMockDependency(KeyguardStateController.class); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 88 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 89 | mContext.addMockSystemService(UserManager.class, mUserManager); |
| 90 | mCurrentUserId = ActivityManager.getCurrentUser(); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 91 | |
| 92 | when(mUserManager.getProfiles(mCurrentUserId)).thenReturn(Lists.newArrayList( |
| 93 | new UserInfo(mCurrentUserId, "", 0), new UserInfo(mCurrentUserId + 1, "", 0))); |
Evan Laird | db601aa | 2018-09-18 17:47:46 -0400 | [diff] [blame] | 94 | when(mEntryManager.getNotificationData()).thenReturn(mNotificationData); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 95 | mDependency.injectTestDependency(Dependency.MAIN_HANDLER, |
| 96 | Handler.createAsync(Looper.myLooper())); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 97 | |
| 98 | mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 99 | mLockscreenUserManager.setUpWithPresenter(mPresenter); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testLockScreenShowNotificationsChangeUpdatesNotifications() { |
| 104 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
Beverly | 85d4c19 | 2019-09-30 11:40:39 -0400 | [diff] [blame] | 105 | verify(mEntryManager, times(1)).updateNotifications(anyString()); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testLockScreenShowNotificationsFalse() { |
| 110 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 111 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0); |
| 112 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 113 | assertFalse(mLockscreenUserManager.shouldShowLockscreenNotifications()); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testLockScreenShowNotificationsTrue() { |
| 118 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 119 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1); |
| 120 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 121 | assertTrue(mLockscreenUserManager.shouldShowLockscreenNotifications()); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testLockScreenAllowPrivateNotificationsTrue() { |
| 126 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 127 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1); |
| 128 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 129 | assertTrue(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUserId)); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void testLockScreenAllowPrivateNotificationsFalse() { |
| 134 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 135 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0); |
| 136 | mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); |
| 137 | assertFalse(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUserId)); |
| 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testSettingsObserverUpdatesNotifications() { |
| 142 | when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true); |
| 143 | mLockscreenUserManager.getSettingsObserverForTest().onChange(false); |
Beverly | 85d4c19 | 2019-09-30 11:40:39 -0400 | [diff] [blame] | 144 | verify(mEntryManager, times(1)).updateNotifications(anyString()); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Test |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 148 | public void testActionUserSwitchedCallsOnUserSwitched() { |
| 149 | Intent intent = new Intent() |
| 150 | .setAction(ACTION_USER_SWITCHED) |
| 151 | .putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId + 1); |
| 152 | mLockscreenUserManager.getBaseBroadcastReceiverForTest().onReceive(mContext, intent); |
| 153 | verify(mPresenter, times(1)).onUserSwitched(mCurrentUserId + 1); |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void testIsLockscreenPublicMode() { |
| 158 | assertFalse(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUserId)); |
| 159 | mLockscreenUserManager.setLockscreenPublicMode(true, mCurrentUserId); |
| 160 | assertTrue(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUserId)); |
| 161 | } |
| 162 | |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 163 | @Test |
| 164 | public void testShowSilentNotifications_settingSaysShow() { |
| 165 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 166 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1); |
| 167 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 168 | NOTIFICATION_NEW_INTERRUPTION_MODEL, 1); |
| 169 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 170 | Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1); |
| 171 | when(mNotificationData.isHighPriority(any())).thenReturn(false); |
| 172 | |
Evan Laird | 25f0275 | 2019-08-14 19:25:06 -0400 | [diff] [blame] | 173 | NotificationEntry entry = new NotificationEntryBuilder().build(); |
| 174 | entry.setBucket(BUCKET_SILENT); |
| 175 | |
| 176 | assertTrue(mLockscreenUserManager.shouldShowOnKeyguard(entry)); |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void testShowSilentNotifications_settingSaysHide() { |
| 181 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 182 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1); |
| 183 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 184 | NOTIFICATION_NEW_INTERRUPTION_MODEL, 1); |
| 185 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 186 | Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0); |
| 187 | when(mNotificationData.isHighPriority(any())).thenReturn(false); |
| 188 | |
Evan Laird | 25f0275 | 2019-08-14 19:25:06 -0400 | [diff] [blame] | 189 | NotificationEntry entry = new NotificationEntryBuilder().build(); |
| 190 | entry.setBucket(BUCKET_SILENT); |
| 191 | assertFalse(mLockscreenUserManager.shouldShowOnKeyguard(entry)); |
Julia Reynolds | 5b655c3 | 2019-04-24 14:40:17 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | private class TestNotificationLockscreenUserManager |
| 195 | extends NotificationLockscreenUserManagerImpl { |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 196 | public TestNotificationLockscreenUserManager(Context context) { |
| 197 | super(context); |
| 198 | } |
| 199 | |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 200 | public BroadcastReceiver getBaseBroadcastReceiverForTest() { |
| 201 | return mBaseBroadcastReceiver; |
| 202 | } |
| 203 | |
| 204 | public ContentObserver getLockscreenSettingsObserverForTest() { |
| 205 | return mLockscreenSettingsObserver; |
| 206 | } |
| 207 | |
| 208 | public ContentObserver getSettingsObserverForTest() { |
| 209 | return mSettingsObserver; |
| 210 | } |
| 211 | } |
| 212 | } |