blob: c6d57e6df028369d751438abf4c1974618e0ce95 [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
Evan Laird181de622019-10-24 09:53:02 -040019import static android.app.NotificationManager.IMPORTANCE_LOW;
Eliot Courtney09322282017-11-09 15:31:19 +090020import static android.content.Intent.ACTION_USER_SWITCHED;
Julia Reynolds5b655c32019-04-24 14:40:17 -040021import static android.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL;
Eliot Courtney09322282017-11-09 15:31:19 +090022
Evan Laird25f02752019-08-14 19:25:06 -040023import static com.android.systemui.statusbar.notification.stack.NotificationSectionsManager.BUCKET_SILENT;
24
Eliot Courtney09322282017-11-09 15:31:19 +090025import static junit.framework.Assert.assertFalse;
26import static junit.framework.Assert.assertTrue;
27
Beverly85d4c192019-09-30 11:40:39 -040028import static org.mockito.ArgumentMatchers.anyString;
Beverly7c1ad5b2019-12-05 17:17:15 -050029import static org.mockito.Mockito.mock;
Eliot Courtney09322282017-11-09 15:31:19 +090030import static org.mockito.Mockito.times;
31import static org.mockito.Mockito.verify;
32import static org.mockito.Mockito.when;
33
34import android.app.ActivityManager;
Dave Mankoff9427d1f2019-11-19 21:52:51 -050035import android.app.KeyguardManager;
Beverly7c1ad5b2019-12-05 17:17:15 -050036import android.app.Notification;
Dave Mankoff9427d1f2019-11-19 21:52:51 -050037import android.app.admin.DevicePolicyManager;
Eliot Courtney09322282017-11-09 15:31:19 +090038import android.content.BroadcastReceiver;
39import android.content.Context;
40import android.content.Intent;
41import android.content.pm.UserInfo;
42import android.database.ContentObserver;
43import android.os.Handler;
44import android.os.Looper;
45import android.os.UserManager;
46import android.provider.Settings;
Eliot Courtney09322282017-11-09 15:31:19 +090047import android.testing.AndroidTestingRunner;
48import android.testing.TestableLooper;
49
Brett Chabot84151d92019-02-27 15:37:59 -080050import androidx.test.filters.SmallTest;
51
Dave Mankoff9427d1f2019-11-19 21:52:51 -050052import com.android.internal.statusbar.IStatusBarService;
Jason Monk297c04e2018-08-23 17:16:59 -040053import com.android.systemui.Dependency;
Eliot Courtney09322282017-11-09 15:31:19 +090054import com.android.systemui.SysuiTestCase;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000055import com.android.systemui.broadcast.BroadcastDispatcher;
Dave Mankoff9427d1f2019-11-19 21:52:51 -050056import com.android.systemui.plugins.statusbar.StatusBarStateController;
Rohan Shah20790b82018-07-02 17:21:04 -070057import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burns8c1b7632019-07-19 14:26:15 -040058import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverly79c89ec2019-12-13 10:33:01 -050059import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
Eliot Courtney09322282017-11-09 15:31:19 +090060import com.android.systemui.statusbar.policy.DeviceProvisionedController;
Lucas Dupind236ee32019-10-08 15:33:59 -070061import com.android.systemui.statusbar.policy.KeyguardStateController;
Eliot Courtney09322282017-11-09 15:31:19 +090062
63import com.google.android.collect.Lists;
64
65import org.junit.Before;
66import org.junit.Test;
67import org.junit.runner.RunWith;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090068import org.mockito.Mock;
69import org.mockito.MockitoAnnotations;
Eliot Courtney09322282017-11-09 15:31:19 +090070
71@SmallTest
72@RunWith(AndroidTestingRunner.class)
73@TestableLooper.RunWithLooper
74public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
Dave Mankoff9427d1f2019-11-19 21:52:51 -050075 @Mock
76 private NotificationPresenter mPresenter;
77 @Mock
78 private UserManager mUserManager;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090079
80 // Dependency mocks:
Dave Mankoff9427d1f2019-11-19 21:52:51 -050081 @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 Courtney8f56b0e2017-12-14 18:54:28 +090097
Lucas Dupin960b7e7f2020-01-24 15:47:28 -080098 private UserInfo mCurrentUser;
99 private UserInfo mSecondaryUser;
100 private UserInfo mWorkUser;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +0900101 private TestNotificationLockscreenUserManager mLockscreenUserManager;
Eliot Courtney09322282017-11-09 15:31:19 +0900102
103 @Before
104 public void setUp() {
Eliot Courtney8f56b0e2017-12-14 18:54:28 +0900105 MockitoAnnotations.initMocks(this);
106 mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
Eliot Courtneya6d8cf22017-10-20 13:26:58 +0900107
Lucas Dupin960b7e7f2020-01-24 15:47:28 -0800108 int currentUserId = ActivityManager.getCurrentUser();
109 mCurrentUser = new UserInfo(currentUserId, "", 0);
110 mSecondaryUser = new UserInfo(currentUserId + 1, "", 0);
111 mWorkUser = new UserInfo(currentUserId + 2, "" /* name */, null /* iconPath */, 0,
112 UserManager.USER_TYPE_PROFILE_MANAGED);
Eliot Courtney09322282017-11-09 15:31:19 +0900113
Lucas Dupin960b7e7f2020-01-24 15:47:28 -0800114 when(mUserManager.getProfiles(currentUserId)).thenReturn(Lists.newArrayList(
115 mCurrentUser, mSecondaryUser, mWorkUser));
Jason Monk297c04e2018-08-23 17:16:59 -0400116 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
117 Handler.createAsync(Looper.myLooper()));
Eliot Courtneya6d8cf22017-10-20 13:26:58 +0900118
119 mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext);
Jason Monk297c04e2018-08-23 17:16:59 -0400120 mLockscreenUserManager.setUpWithPresenter(mPresenter);
Eliot Courtney09322282017-11-09 15:31:19 +0900121 }
122
123 @Test
124 public void testLockScreenShowNotificationsChangeUpdatesNotifications() {
125 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
Beverly85d4c192019-09-30 11:40:39 -0400126 verify(mEntryManager, times(1)).updateNotifications(anyString());
Eliot Courtney09322282017-11-09 15:31:19 +0900127 }
128
129 @Test
130 public void testLockScreenShowNotificationsFalse() {
131 Settings.Secure.putInt(mContext.getContentResolver(),
132 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0);
133 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
134 assertFalse(mLockscreenUserManager.shouldShowLockscreenNotifications());
135 }
136
137 @Test
138 public void testLockScreenShowNotificationsTrue() {
139 Settings.Secure.putInt(mContext.getContentResolver(),
140 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1);
141 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
142 assertTrue(mLockscreenUserManager.shouldShowLockscreenNotifications());
143 }
144
145 @Test
146 public void testLockScreenAllowPrivateNotificationsTrue() {
147 Settings.Secure.putInt(mContext.getContentResolver(),
148 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1);
149 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
Lucas Dupin960b7e7f2020-01-24 15:47:28 -0800150 assertTrue(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUser.id));
Eliot Courtney09322282017-11-09 15:31:19 +0900151 }
152
153 @Test
154 public void testLockScreenAllowPrivateNotificationsFalse() {
Lucas Dupin960b7e7f2020-01-24 15:47:28 -0800155 Settings.Secure.putIntForUser(mContext.getContentResolver(),
156 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mCurrentUser.id);
Eliot Courtney09322282017-11-09 15:31:19 +0900157 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
Lucas Dupin960b7e7f2020-01-24 15:47:28 -0800158 assertFalse(mLockscreenUserManager.userAllowsNotificationsInPublic(mCurrentUser.id));
159 }
160
161 @Test
162 public void testLockScreenAllowsWorkPrivateNotificationsFalse() {
163 Settings.Secure.putIntForUser(mContext.getContentResolver(),
164 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mWorkUser.id);
165 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
166 assertFalse(mLockscreenUserManager.allowsManagedPrivateNotificationsInPublic());
167 }
168
169 @Test
170 public void testLockScreenAllowsWorkPrivateNotificationsTrue() {
171 Settings.Secure.putIntForUser(mContext.getContentResolver(),
172 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mWorkUser.id);
173 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
174 assertTrue(mLockscreenUserManager.allowsManagedPrivateNotificationsInPublic());
Eliot Courtney09322282017-11-09 15:31:19 +0900175 }
176
177 @Test
178 public void testSettingsObserverUpdatesNotifications() {
179 when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true);
180 mLockscreenUserManager.getSettingsObserverForTest().onChange(false);
Beverly85d4c192019-09-30 11:40:39 -0400181 verify(mEntryManager, times(1)).updateNotifications(anyString());
Eliot Courtney09322282017-11-09 15:31:19 +0900182 }
183
184 @Test
Eliot Courtney09322282017-11-09 15:31:19 +0900185 public void testActionUserSwitchedCallsOnUserSwitched() {
186 Intent intent = new Intent()
187 .setAction(ACTION_USER_SWITCHED)
Lucas Dupin960b7e7f2020-01-24 15:47:28 -0800188 .putExtra(Intent.EXTRA_USER_HANDLE, mSecondaryUser.id);
Eliot Courtney09322282017-11-09 15:31:19 +0900189 mLockscreenUserManager.getBaseBroadcastReceiverForTest().onReceive(mContext, intent);
Lucas Dupin960b7e7f2020-01-24 15:47:28 -0800190 verify(mPresenter, times(1)).onUserSwitched(mSecondaryUser.id);
Eliot Courtney09322282017-11-09 15:31:19 +0900191 }
192
193 @Test
194 public void testIsLockscreenPublicMode() {
Lucas Dupin960b7e7f2020-01-24 15:47:28 -0800195 assertFalse(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUser.id));
196 mLockscreenUserManager.setLockscreenPublicMode(true, mCurrentUser.id);
197 assertTrue(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUser.id));
Eliot Courtney09322282017-11-09 15:31:19 +0900198 }
199
Julia Reynolds5b655c32019-04-24 14:40:17 -0400200 @Test
201 public void testShowSilentNotifications_settingSaysShow() {
202 Settings.Secure.putInt(mContext.getContentResolver(),
203 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1);
204 Settings.Secure.putInt(mContext.getContentResolver(),
205 NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
206 Settings.Secure.putInt(mContext.getContentResolver(),
207 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
Julia Reynolds5b655c32019-04-24 14:40:17 -0400208
Evan Laird181de622019-10-24 09:53:02 -0400209 NotificationEntry entry = new NotificationEntryBuilder()
210 .setImportance(IMPORTANCE_LOW)
211 .build();
Evan Laird25f02752019-08-14 19:25:06 -0400212 entry.setBucket(BUCKET_SILENT);
213
214 assertTrue(mLockscreenUserManager.shouldShowOnKeyguard(entry));
Julia Reynolds5b655c32019-04-24 14:40:17 -0400215 }
216
217 @Test
218 public void testShowSilentNotifications_settingSaysHide() {
219 Settings.Secure.putInt(mContext.getContentResolver(),
220 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1);
221 Settings.Secure.putInt(mContext.getContentResolver(),
222 NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
223 Settings.Secure.putInt(mContext.getContentResolver(),
224 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
Julia Reynolds5b655c32019-04-24 14:40:17 -0400225
Beverly7c1ad5b2019-12-05 17:17:15 -0500226 final Notification notification = mock(Notification.class);
227 when(notification.isForegroundService()).thenReturn(true);
Evan Laird181de622019-10-24 09:53:02 -0400228 NotificationEntry entry = new NotificationEntryBuilder()
229 .setImportance(IMPORTANCE_LOW)
Beverly7c1ad5b2019-12-05 17:17:15 -0500230 .setNotification(notification)
Evan Laird181de622019-10-24 09:53:02 -0400231 .build();
Evan Laird25f02752019-08-14 19:25:06 -0400232 entry.setBucket(BUCKET_SILENT);
233 assertFalse(mLockscreenUserManager.shouldShowOnKeyguard(entry));
Julia Reynolds5b655c32019-04-24 14:40:17 -0400234 }
235
236 private class TestNotificationLockscreenUserManager
237 extends NotificationLockscreenUserManagerImpl {
Eliot Courtney09322282017-11-09 15:31:19 +0900238 public TestNotificationLockscreenUserManager(Context context) {
Dave Mankoff9427d1f2019-11-19 21:52:51 -0500239 super(context, mBroadcastDispatcher, mDevicePolicyManager, mUserManager,
240 mIStatusBarService, NotificationLockscreenUserManagerTest.this.mKeyguardManager,
241 mStatusBarStateController, Handler.createAsync(Looper.myLooper()),
242 mDeviceProvisionedController, mKeyguardStateController);
Eliot Courtney09322282017-11-09 15:31:19 +0900243 }
244
Eliot Courtney09322282017-11-09 15:31:19 +0900245 public BroadcastReceiver getBaseBroadcastReceiverForTest() {
246 return mBaseBroadcastReceiver;
247 }
248
249 public ContentObserver getLockscreenSettingsObserverForTest() {
250 return mLockscreenSettingsObserver;
251 }
252
253 public ContentObserver getSettingsObserverForTest() {
254 return mSettingsObserver;
255 }
256 }
257}