blob: 85a0fbd74550cb2034095c3c7b217904bd318d23 [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;
Julia Reynolds5b655c32019-04-24 14:40:17 -040020import static android.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL;
Eliot Courtney09322282017-11-09 15:31:19 +090021
Evan Laird25f02752019-08-14 19:25:06 -040022import static com.android.systemui.statusbar.notification.stack.NotificationSectionsManager.BUCKET_SILENT;
23
Eliot Courtney09322282017-11-09 15:31:19 +090024import static junit.framework.Assert.assertFalse;
25import static junit.framework.Assert.assertTrue;
26
Julia Reynolds5b655c32019-04-24 14:40:17 -040027import static org.mockito.ArgumentMatchers.any;
Beverly85d4c192019-09-30 11:40:39 -040028import static org.mockito.ArgumentMatchers.anyString;
Eliot Courtney09322282017-11-09 15:31:19 +090029import static org.mockito.Mockito.times;
30import static org.mockito.Mockito.verify;
31import static org.mockito.Mockito.when;
32
33import android.app.ActivityManager;
34import android.content.BroadcastReceiver;
35import android.content.Context;
36import android.content.Intent;
37import android.content.pm.UserInfo;
38import android.database.ContentObserver;
39import android.os.Handler;
40import android.os.Looper;
41import android.os.UserManager;
42import android.provider.Settings;
Eliot Courtney09322282017-11-09 15:31:19 +090043import android.testing.AndroidTestingRunner;
44import android.testing.TestableLooper;
45
Brett Chabot84151d92019-02-27 15:37:59 -080046import androidx.test.filters.SmallTest;
47
Jason Monk297c04e2018-08-23 17:16:59 -040048import com.android.systemui.Dependency;
Eliot Courtney09322282017-11-09 15:31:19 +090049import com.android.systemui.SysuiTestCase;
Rohan Shah20790b82018-07-02 17:21:04 -070050import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050051import com.android.systemui.statusbar.notification.collection.NotificationData;
Ned Burns8c1b7632019-07-19 14:26:15 -040052import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Evan Lairddb601aa2018-09-18 17:47:46 -040053import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
Eliot Courtney09322282017-11-09 15:31:19 +090054import com.android.systemui.statusbar.policy.DeviceProvisionedController;
Lucas Dupind236ee32019-10-08 15:33:59 -070055import com.android.systemui.statusbar.policy.KeyguardStateController;
Eliot Courtney09322282017-11-09 15:31:19 +090056
57import com.google.android.collect.Lists;
58
59import org.junit.Before;
60import org.junit.Test;
61import org.junit.runner.RunWith;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090062import org.mockito.Mock;
63import org.mockito.MockitoAnnotations;
Eliot Courtney09322282017-11-09 15:31:19 +090064
65@SmallTest
66@RunWith(AndroidTestingRunner.class)
67@TestableLooper.RunWithLooper
68public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090069 @Mock private NotificationPresenter mPresenter;
70 @Mock private UserManager mUserManager;
71
72 // Dependency mocks:
73 @Mock private NotificationEntryManager mEntryManager;
Evan Lairddb601aa2018-09-18 17:47:46 -040074 @Mock private NotificationData mNotificationData;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090075 @Mock private DeviceProvisionedController mDeviceProvisionedController;
Evan Lairddb601aa2018-09-18 17:47:46 -040076 @Mock private StatusBarKeyguardViewManager mKeyguardViewManager;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090077
Eliot Courtney09322282017-11-09 15:31:19 +090078 private int mCurrentUserId;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090079 private TestNotificationLockscreenUserManager mLockscreenUserManager;
Eliot Courtney09322282017-11-09 15:31:19 +090080
81 @Before
82 public void setUp() {
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090083 MockitoAnnotations.initMocks(this);
84 mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
85 mDependency.injectTestDependency(DeviceProvisionedController.class,
86 mDeviceProvisionedController);
Lucas Dupind236ee32019-10-08 15:33:59 -070087 mDependency.injectMockDependency(KeyguardStateController.class);
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090088
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090089 mContext.addMockSystemService(UserManager.class, mUserManager);
90 mCurrentUserId = ActivityManager.getCurrentUser();
Eliot Courtney09322282017-11-09 15:31:19 +090091
92 when(mUserManager.getProfiles(mCurrentUserId)).thenReturn(Lists.newArrayList(
93 new UserInfo(mCurrentUserId, "", 0), new UserInfo(mCurrentUserId + 1, "", 0)));
Evan Lairddb601aa2018-09-18 17:47:46 -040094 when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
Jason Monk297c04e2018-08-23 17:16:59 -040095 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
96 Handler.createAsync(Looper.myLooper()));
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090097
98 mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext);
Jason Monk297c04e2018-08-23 17:16:59 -040099 mLockscreenUserManager.setUpWithPresenter(mPresenter);
Eliot Courtney09322282017-11-09 15:31:19 +0900100 }
101
102 @Test
103 public void testLockScreenShowNotificationsChangeUpdatesNotifications() {
104 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
Beverly85d4c192019-09-30 11:40:39 -0400105 verify(mEntryManager, times(1)).updateNotifications(anyString());
Eliot Courtney09322282017-11-09 15:31:19 +0900106 }
107
108 @Test
109 public void testLockScreenShowNotificationsFalse() {
110 Settings.Secure.putInt(mContext.getContentResolver(),
111 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0);
112 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
113 assertFalse(mLockscreenUserManager.shouldShowLockscreenNotifications());
114 }
115
116 @Test
117 public void testLockScreenShowNotificationsTrue() {
118 Settings.Secure.putInt(mContext.getContentResolver(),
119 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1);
120 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
121 assertTrue(mLockscreenUserManager.shouldShowLockscreenNotifications());
122 }
123
124 @Test
125 public void testLockScreenAllowPrivateNotificationsTrue() {
126 Settings.Secure.putInt(mContext.getContentResolver(),
127 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1);
128 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
129 assertTrue(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUserId));
130 }
131
132 @Test
133 public void testLockScreenAllowPrivateNotificationsFalse() {
134 Settings.Secure.putInt(mContext.getContentResolver(),
135 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0);
136 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
137 assertFalse(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUserId));
138 }
139
140 @Test
141 public void testSettingsObserverUpdatesNotifications() {
142 when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true);
143 mLockscreenUserManager.getSettingsObserverForTest().onChange(false);
Beverly85d4c192019-09-30 11:40:39 -0400144 verify(mEntryManager, times(1)).updateNotifications(anyString());
Eliot Courtney09322282017-11-09 15:31:19 +0900145 }
146
147 @Test
Eliot Courtney09322282017-11-09 15:31:19 +0900148 public void testActionUserSwitchedCallsOnUserSwitched() {
149 Intent intent = new Intent()
150 .setAction(ACTION_USER_SWITCHED)
151 .putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId + 1);
152 mLockscreenUserManager.getBaseBroadcastReceiverForTest().onReceive(mContext, intent);
153 verify(mPresenter, times(1)).onUserSwitched(mCurrentUserId + 1);
154 }
155
156 @Test
157 public void testIsLockscreenPublicMode() {
158 assertFalse(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUserId));
159 mLockscreenUserManager.setLockscreenPublicMode(true, mCurrentUserId);
160 assertTrue(mLockscreenUserManager.isLockscreenPublicMode(mCurrentUserId));
161 }
162
Julia Reynolds5b655c32019-04-24 14:40:17 -0400163 @Test
164 public void testShowSilentNotifications_settingSaysShow() {
165 Settings.Secure.putInt(mContext.getContentResolver(),
166 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1);
167 Settings.Secure.putInt(mContext.getContentResolver(),
168 NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
169 Settings.Secure.putInt(mContext.getContentResolver(),
170 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
171 when(mNotificationData.isHighPriority(any())).thenReturn(false);
172
Evan Laird25f02752019-08-14 19:25:06 -0400173 NotificationEntry entry = new NotificationEntryBuilder().build();
174 entry.setBucket(BUCKET_SILENT);
175
176 assertTrue(mLockscreenUserManager.shouldShowOnKeyguard(entry));
Julia Reynolds5b655c32019-04-24 14:40:17 -0400177 }
178
179 @Test
180 public void testShowSilentNotifications_settingSaysHide() {
181 Settings.Secure.putInt(mContext.getContentResolver(),
182 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1);
183 Settings.Secure.putInt(mContext.getContentResolver(),
184 NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
185 Settings.Secure.putInt(mContext.getContentResolver(),
186 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
187 when(mNotificationData.isHighPriority(any())).thenReturn(false);
188
Evan Laird25f02752019-08-14 19:25:06 -0400189 NotificationEntry entry = new NotificationEntryBuilder().build();
190 entry.setBucket(BUCKET_SILENT);
191 assertFalse(mLockscreenUserManager.shouldShowOnKeyguard(entry));
Julia Reynolds5b655c32019-04-24 14:40:17 -0400192 }
193
194 private class TestNotificationLockscreenUserManager
195 extends NotificationLockscreenUserManagerImpl {
Eliot Courtney09322282017-11-09 15:31:19 +0900196 public TestNotificationLockscreenUserManager(Context context) {
197 super(context);
198 }
199
Eliot Courtney09322282017-11-09 15:31:19 +0900200 public BroadcastReceiver getBaseBroadcastReceiverForTest() {
201 return mBaseBroadcastReceiver;
202 }
203
204 public ContentObserver getLockscreenSettingsObserverForTest() {
205 return mLockscreenSettingsObserver;
206 }
207
208 public ContentObserver getSettingsObserverForTest() {
209 return mSettingsObserver;
210 }
211 }
212}