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