blob: 490b2eafe47178b2d75b75cc98e4fcb8ed10eed8 [file] [log] [blame]
Eliot Courtney09322282017-11-09 15:31:19 +09001/*
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
17package com.android.systemui.statusbar;
18
Eliot Courtney09322282017-11-09 15:31:19 +090019import static android.content.Intent.ACTION_USER_SWITCHED;
20
21import static junit.framework.Assert.assertFalse;
22import static junit.framework.Assert.assertTrue;
23
Eliot Courtney09322282017-11-09 15:31:19 +090024import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
26import static org.mockito.Mockito.when;
27
28import android.app.ActivityManager;
29import android.content.BroadcastReceiver;
30import android.content.Context;
31import android.content.Intent;
32import android.content.pm.UserInfo;
33import android.database.ContentObserver;
34import android.os.Handler;
35import android.os.Looper;
36import android.os.UserManager;
37import android.provider.Settings;
Eliot Courtney09322282017-11-09 15:31:19 +090038import android.testing.AndroidTestingRunner;
39import android.testing.TestableLooper;
40
Brett Chabot84151d92019-02-27 15:37:59 -080041import androidx.test.filters.SmallTest;
42
Jason Monk297c04e2018-08-23 17:16:59 -040043import com.android.systemui.Dependency;
Eliot Courtney09322282017-11-09 15:31:19 +090044import com.android.systemui.SysuiTestCase;
Rohan Shah20790b82018-07-02 17:21:04 -070045import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050046import com.android.systemui.statusbar.notification.collection.NotificationData;
Evan Lairddb601aa2018-09-18 17:47:46 -040047import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
Eliot Courtney09322282017-11-09 15:31:19 +090048import com.android.systemui.statusbar.policy.DeviceProvisionedController;
49
50import com.google.android.collect.Lists;
51
52import org.junit.Before;
53import org.junit.Test;
54import org.junit.runner.RunWith;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090055import org.mockito.Mock;
56import org.mockito.MockitoAnnotations;
Eliot Courtney09322282017-11-09 15:31:19 +090057
58@SmallTest
59@RunWith(AndroidTestingRunner.class)
60@TestableLooper.RunWithLooper
61public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090062 @Mock private NotificationPresenter mPresenter;
63 @Mock private UserManager mUserManager;
64
65 // Dependency mocks:
66 @Mock private NotificationEntryManager mEntryManager;
Evan Lairddb601aa2018-09-18 17:47:46 -040067 @Mock private NotificationData mNotificationData;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090068 @Mock private DeviceProvisionedController mDeviceProvisionedController;
Evan Lairddb601aa2018-09-18 17:47:46 -040069 @Mock private StatusBarKeyguardViewManager mKeyguardViewManager;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090070
Eliot Courtney09322282017-11-09 15:31:19 +090071 private int mCurrentUserId;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090072 private TestNotificationLockscreenUserManager mLockscreenUserManager;
Eliot Courtney09322282017-11-09 15:31:19 +090073
74 @Before
75 public void setUp() {
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090076 MockitoAnnotations.initMocks(this);
77 mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
78 mDependency.injectTestDependency(DeviceProvisionedController.class,
79 mDeviceProvisionedController);
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090080
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090081 mContext.addMockSystemService(UserManager.class, mUserManager);
82 mCurrentUserId = ActivityManager.getCurrentUser();
Eliot Courtney09322282017-11-09 15:31:19 +090083
84 when(mUserManager.getProfiles(mCurrentUserId)).thenReturn(Lists.newArrayList(
85 new UserInfo(mCurrentUserId, "", 0), new UserInfo(mCurrentUserId + 1, "", 0)));
Evan Lairddb601aa2018-09-18 17:47:46 -040086 when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
Jason Monk297c04e2018-08-23 17:16:59 -040087 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
88 Handler.createAsync(Looper.myLooper()));
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090089
90 mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext);
Jason Monk297c04e2018-08-23 17:16:59 -040091 mLockscreenUserManager.setUpWithPresenter(mPresenter);
Eliot Courtney09322282017-11-09 15:31:19 +090092 }
93
94 @Test
95 public void testLockScreenShowNotificationsChangeUpdatesNotifications() {
96 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090097 verify(mEntryManager, times(1)).updateNotifications();
Eliot Courtney09322282017-11-09 15:31:19 +090098 }
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 Courtneya6d8cf22017-10-20 13:26:58 +0900136 verify(mEntryManager, times(1)).updateNotifications();
Eliot Courtney09322282017-11-09 15:31:19 +0900137 }
138
139 @Test
Eliot Courtney09322282017-11-09 15:31:19 +0900140 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 Monk297c04e2018-08-23 17:16:59 -0400155 private class TestNotificationLockscreenUserManager extends NotificationLockscreenUserManagerImpl {
Eliot Courtney09322282017-11-09 15:31:19 +0900156 public TestNotificationLockscreenUserManager(Context context) {
157 super(context);
158 }
159
Eliot Courtney09322282017-11-09 15:31:19 +0900160 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}