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