blob: 3a6accea2b5402f170f98e84d8d02e3ff63b6a61 [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
Eliot Courtney09322282017-11-09 15:31:19 +090098 private int mCurrentUserId;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090099 private TestNotificationLockscreenUserManager mLockscreenUserManager;
Eliot Courtney09322282017-11-09 15:31:19 +0900100
101 @Before
102 public void setUp() {
Eliot Courtney8f56b0e2017-12-14 18:54:28 +0900103 MockitoAnnotations.initMocks(this);
104 mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
Eliot Courtneya6d8cf22017-10-20 13:26:58 +0900105
Eliot Courtneya6d8cf22017-10-20 13:26:58 +0900106 mCurrentUserId = ActivityManager.getCurrentUser();
Eliot Courtney09322282017-11-09 15:31:19 +0900107
108 when(mUserManager.getProfiles(mCurrentUserId)).thenReturn(Lists.newArrayList(
109 new UserInfo(mCurrentUserId, "", 0), new UserInfo(mCurrentUserId + 1, "", 0)));
Jason Monk297c04e2018-08-23 17:16:59 -0400110 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
111 Handler.createAsync(Looper.myLooper()));
Eliot Courtneya6d8cf22017-10-20 13:26:58 +0900112
113 mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext);
Jason Monk297c04e2018-08-23 17:16:59 -0400114 mLockscreenUserManager.setUpWithPresenter(mPresenter);
Eliot Courtney09322282017-11-09 15:31:19 +0900115 }
116
117 @Test
118 public void testLockScreenShowNotificationsChangeUpdatesNotifications() {
119 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
Beverly85d4c192019-09-30 11:40:39 -0400120 verify(mEntryManager, times(1)).updateNotifications(anyString());
Eliot Courtney09322282017-11-09 15:31:19 +0900121 }
122
123 @Test
124 public void testLockScreenShowNotificationsFalse() {
125 Settings.Secure.putInt(mContext.getContentResolver(),
126 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0);
127 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
128 assertFalse(mLockscreenUserManager.shouldShowLockscreenNotifications());
129 }
130
131 @Test
132 public void testLockScreenShowNotificationsTrue() {
133 Settings.Secure.putInt(mContext.getContentResolver(),
134 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1);
135 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
136 assertTrue(mLockscreenUserManager.shouldShowLockscreenNotifications());
137 }
138
139 @Test
140 public void testLockScreenAllowPrivateNotificationsTrue() {
141 Settings.Secure.putInt(mContext.getContentResolver(),
142 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1);
143 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
144 assertTrue(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUserId));
145 }
146
147 @Test
148 public void testLockScreenAllowPrivateNotificationsFalse() {
149 Settings.Secure.putInt(mContext.getContentResolver(),
150 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0);
151 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
152 assertFalse(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUserId));
153 }
154
155 @Test
156 public void testSettingsObserverUpdatesNotifications() {
157 when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true);
158 mLockscreenUserManager.getSettingsObserverForTest().onChange(false);
Beverly85d4c192019-09-30 11:40:39 -0400159 verify(mEntryManager, times(1)).updateNotifications(anyString());
Eliot Courtney09322282017-11-09 15:31:19 +0900160 }
161
162 @Test
Eliot Courtney09322282017-11-09 15:31:19 +0900163 public void testActionUserSwitchedCallsOnUserSwitched() {
164 Intent intent = new Intent()
165 .setAction(ACTION_USER_SWITCHED)
166 .putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId + 1);
167 mLockscreenUserManager.getBaseBroadcastReceiverForTest().onReceive(mContext, intent);
168 verify(mPresenter, times(1)).onUserSwitched(mCurrentUserId + 1);
169 }
170
171 @Test
172 public void testIsLockscreenPublicMode() {
173 assertFalse(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUserId));
174 mLockscreenUserManager.setLockscreenPublicMode(true, mCurrentUserId);
175 assertTrue(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUserId));
176 }
177
Julia Reynolds5b655c32019-04-24 14:40:17 -0400178 @Test
179 public void testShowSilentNotifications_settingSaysShow() {
180 Settings.Secure.putInt(mContext.getContentResolver(),
181 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1);
182 Settings.Secure.putInt(mContext.getContentResolver(),
183 NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
184 Settings.Secure.putInt(mContext.getContentResolver(),
185 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
Julia Reynolds5b655c32019-04-24 14:40:17 -0400186
Evan Laird181de622019-10-24 09:53:02 -0400187 NotificationEntry entry = new NotificationEntryBuilder()
188 .setImportance(IMPORTANCE_LOW)
189 .build();
Evan Laird25f02752019-08-14 19:25:06 -0400190 entry.setBucket(BUCKET_SILENT);
191
192 assertTrue(mLockscreenUserManager.shouldShowOnKeyguard(entry));
Julia Reynolds5b655c32019-04-24 14:40:17 -0400193 }
194
195 @Test
196 public void testShowSilentNotifications_settingSaysHide() {
197 Settings.Secure.putInt(mContext.getContentResolver(),
198 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1);
199 Settings.Secure.putInt(mContext.getContentResolver(),
200 NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
201 Settings.Secure.putInt(mContext.getContentResolver(),
202 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
Julia Reynolds5b655c32019-04-24 14:40:17 -0400203
Beverly7c1ad5b2019-12-05 17:17:15 -0500204 final Notification notification = mock(Notification.class);
205 when(notification.isForegroundService()).thenReturn(true);
Evan Laird181de622019-10-24 09:53:02 -0400206 NotificationEntry entry = new NotificationEntryBuilder()
207 .setImportance(IMPORTANCE_LOW)
Beverly7c1ad5b2019-12-05 17:17:15 -0500208 .setNotification(notification)
Evan Laird181de622019-10-24 09:53:02 -0400209 .build();
Evan Laird25f02752019-08-14 19:25:06 -0400210 entry.setBucket(BUCKET_SILENT);
211 assertFalse(mLockscreenUserManager.shouldShowOnKeyguard(entry));
Julia Reynolds5b655c32019-04-24 14:40:17 -0400212 }
213
214 private class TestNotificationLockscreenUserManager
215 extends NotificationLockscreenUserManagerImpl {
Eliot Courtney09322282017-11-09 15:31:19 +0900216 public TestNotificationLockscreenUserManager(Context context) {
Dave Mankoff9427d1f2019-11-19 21:52:51 -0500217 super(context, mBroadcastDispatcher, mDevicePolicyManager, mUserManager,
218 mIStatusBarService, NotificationLockscreenUserManagerTest.this.mKeyguardManager,
219 mStatusBarStateController, Handler.createAsync(Looper.myLooper()),
220 mDeviceProvisionedController, mKeyguardStateController);
Eliot Courtney09322282017-11-09 15:31:19 +0900221 }
222
Eliot Courtney09322282017-11-09 15:31:19 +0900223 public BroadcastReceiver getBaseBroadcastReceiverForTest() {
224 return mBaseBroadcastReceiver;
225 }
226
227 public ContentObserver getLockscreenSettingsObserverForTest() {
228 return mLockscreenSettingsObserver;
229 }
230
231 public ContentObserver getSettingsObserverForTest() {
232 return mSettingsObserver;
233 }
234 }
235}