blob: d7ca5b410e56fcf6bc1cd0244109b18424dd0ad9 [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;
38import android.support.test.filters.SmallTest;
39import android.testing.AndroidTestingRunner;
40import android.testing.TestableLooper;
41
Jason Monk297c04e2018-08-23 17:16:59 -040042import com.android.systemui.Dependency;
Eliot Courtney09322282017-11-09 15:31:19 +090043import com.android.systemui.SysuiTestCase;
Rohan Shah20790b82018-07-02 17:21:04 -070044import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050045import com.android.systemui.statusbar.notification.collection.NotificationData;
Evan Lairddb601aa2018-09-18 17:47:46 -040046import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
Eliot Courtney09322282017-11-09 15:31:19 +090047import com.android.systemui.statusbar.policy.DeviceProvisionedController;
48
49import com.google.android.collect.Lists;
50
51import org.junit.Before;
52import org.junit.Test;
53import org.junit.runner.RunWith;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090054import org.mockito.Mock;
55import org.mockito.MockitoAnnotations;
Eliot Courtney09322282017-11-09 15:31:19 +090056
57@SmallTest
58@RunWith(AndroidTestingRunner.class)
59@TestableLooper.RunWithLooper
60public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090061 @Mock private NotificationPresenter mPresenter;
62 @Mock private UserManager mUserManager;
63
64 // Dependency mocks:
65 @Mock private NotificationEntryManager mEntryManager;
Evan Lairddb601aa2018-09-18 17:47:46 -040066 @Mock private NotificationData mNotificationData;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090067 @Mock private DeviceProvisionedController mDeviceProvisionedController;
Evan Lairddb601aa2018-09-18 17:47:46 -040068 @Mock private StatusBarKeyguardViewManager mKeyguardViewManager;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090069
Eliot Courtney09322282017-11-09 15:31:19 +090070 private int mCurrentUserId;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090071 private TestNotificationLockscreenUserManager mLockscreenUserManager;
Eliot Courtney09322282017-11-09 15:31:19 +090072
73 @Before
74 public void setUp() {
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090075 MockitoAnnotations.initMocks(this);
76 mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
77 mDependency.injectTestDependency(DeviceProvisionedController.class,
78 mDeviceProvisionedController);
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090079
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090080 mContext.addMockSystemService(UserManager.class, mUserManager);
81 mCurrentUserId = ActivityManager.getCurrentUser();
Eliot Courtney09322282017-11-09 15:31:19 +090082
83 when(mUserManager.getProfiles(mCurrentUserId)).thenReturn(Lists.newArrayList(
84 new UserInfo(mCurrentUserId, "", 0), new UserInfo(mCurrentUserId + 1, "", 0)));
Evan Lairddb601aa2018-09-18 17:47:46 -040085 when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
Jason Monk297c04e2018-08-23 17:16:59 -040086 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
87 Handler.createAsync(Looper.myLooper()));
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090088
89 mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext);
Jason Monk297c04e2018-08-23 17:16:59 -040090 mLockscreenUserManager.setUpWithPresenter(mPresenter);
Eliot Courtney09322282017-11-09 15:31:19 +090091 }
92
93 @Test
94 public void testLockScreenShowNotificationsChangeUpdatesNotifications() {
95 mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090096 verify(mEntryManager, times(1)).updateNotifications();
Eliot Courtney09322282017-11-09 15:31:19 +090097 }
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 Courtneya6d8cf22017-10-20 13:26:58 +0900135 verify(mEntryManager, times(1)).updateNotifications();
Eliot Courtney09322282017-11-09 15:31:19 +0900136 }
137
138 @Test
Eliot Courtney09322282017-11-09 15:31:19 +0900139 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 Monk297c04e2018-08-23 17:16:59 -0400154 private class TestNotificationLockscreenUserManager extends NotificationLockscreenUserManagerImpl {
Eliot Courtney09322282017-11-09 15:31:19 +0900155 public TestNotificationLockscreenUserManager(Context context) {
156 super(context);
157 }
158
Eliot Courtney09322282017-11-09 15:31:19 +0900159 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}