1 /*
2  * Copyright (C) 2016 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 package com.android.settings.users;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.provider.Settings.Global;
28 
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.internal.widget.LockPatternUtils;
32 import com.android.settings.core.BasePreferenceController;
33 import com.android.settings.testutils.shadow.ShadowUserManager;
34 import com.android.settingslib.RestrictedSwitchPreference;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.annotation.Config;
45 import org.robolectric.util.ReflectionHelpers;
46 
47 @RunWith(RobolectricTestRunner.class)
48 @Config(shadows = {ShadowUserManager.class})
49 public class AddUserWhenLockedPreferenceControllerTest {
50 
51     @Mock(answer = RETURNS_DEEP_STUBS)
52     private PreferenceScreen mScreen;
53     @Mock(answer = RETURNS_DEEP_STUBS)
54 
55     private Context mContext;
56     private ShadowUserManager mUserManager;
57     private AddUserWhenLockedPreferenceController mController;
58     private LockPatternUtils mLockPatternUtils;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         mContext = RuntimeEnvironment.application;
64         mLockPatternUtils = mock(LockPatternUtils.class);
65         mUserManager = ShadowUserManager.getShadow();
66         mController = new AddUserWhenLockedPreferenceController(mContext, "fake_key");
67         ReflectionHelpers.setField(mController, "mLockPatternUtils", mLockPatternUtils);
68         mUserManager.setSupportsMultipleUsers(true);
69     }
70 
71     @After
tearDown()72     public void tearDown() {
73         ShadowUserManager.reset();
74     }
75 
76     @Test
displayPref_NotAdmin_shouldNotDisplay()77     public void displayPref_NotAdmin_shouldNotDisplay() {
78         mUserManager.setIsAdminUser(false);
79         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
80         final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class);
81         when(preference.getKey()).thenReturn(mController.getPreferenceKey());
82         when(mScreen.findPreference(preference.getKey())).thenReturn(preference);
83 
84         mController.displayPreference(mScreen);
85 
86         verify(preference).setVisible(false);
87     }
88 
89     @Test
updateState_NotAdmin_shouldNotDisplayPreference()90     public void updateState_NotAdmin_shouldNotDisplayPreference() {
91         mUserManager.setIsAdminUser(false);
92         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
93         final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class);
94 
95         mController.updateState(preference);
96 
97         verify(preference).setVisible(false);
98     }
99 
100     @Test
updateState_Admin_shouldDisplayPreference()101     public void updateState_Admin_shouldDisplayPreference() {
102         mUserManager.setIsAdminUser(true);
103         mUserManager.setUserSwitcherEnabled(true);
104         mUserManager.setSupportsMultipleUsers(true);
105         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
106         final AddUserWhenLockedPreferenceController controller =
107                 new AddUserWhenLockedPreferenceController(mContext, "fake_key");
108         ReflectionHelpers.setField(controller, "mLockPatternUtils", mLockPatternUtils);
109         final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class);
110 
111         controller.updateState(preference);
112 
113         verify(preference).setVisible(true);
114     }
115 
116     @Test
updateState_preferenceSetCheckedWhenSettingIsOn()117     public void updateState_preferenceSetCheckedWhenSettingIsOn() {
118         final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class);
119         Global.putInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 1);
120 
121         mController.updateState(preference);
122 
123         verify(preference).setChecked(true);
124     }
125 
126     @Test
updateState_preferenceSetUncheckedWhenSettingIsOff()127     public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
128         final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class);
129         Global.putInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 0);
130 
131         mController.updateState(preference);
132 
133         verify(preference).setChecked(false);
134     }
135 
136     @Test
onPreferenceChange_SettingIsOnWhenPreferenceChecked()137     public void onPreferenceChange_SettingIsOnWhenPreferenceChecked() {
138         final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class);
139         preference.setChecked(true);
140 
141         mController.onPreferenceChange(preference, Boolean.TRUE);
142 
143         assertThat(Global.getInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 0))
144                 .isEqualTo(1);
145     }
146 
147     @Test
onPreferenceChange_SettingIsOffWhenPreferenceNotChecked()148     public void onPreferenceChange_SettingIsOffWhenPreferenceNotChecked() {
149         final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class);
150         preference.setChecked(false);
151 
152         mController.onPreferenceChange(preference, Boolean.FALSE);
153 
154         assertThat(Global.getInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 0))
155                 .isEqualTo(0);
156     }
157 
158     @Test
updateState_insecureLockScreen_shouldNotDisplayPreference()159     public void updateState_insecureLockScreen_shouldNotDisplayPreference() {
160         mUserManager.setIsAdminUser(true);
161         mUserManager.setUserSwitcherEnabled(true);
162         mUserManager.setSupportsMultipleUsers(true);
163         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
164         final AddUserWhenLockedPreferenceController controller =
165                 new AddUserWhenLockedPreferenceController(mContext, "fake_key");
166         ReflectionHelpers.setField(controller, "mLockPatternUtils", mLockPatternUtils);
167         final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class);
168 
169         controller.updateState(preference);
170 
171         verify(preference).setVisible(false);
172         assertThat(controller.getAvailabilityStatus()).isEqualTo(
173                 BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
174     }
175 
176     @Test
updateState_secureLockScreen_shouldDisplayPreference()177     public void updateState_secureLockScreen_shouldDisplayPreference() {
178         mUserManager.setIsAdminUser(true);
179         mUserManager.setUserSwitcherEnabled(true);
180         mUserManager.setSupportsMultipleUsers(true);
181         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
182         final AddUserWhenLockedPreferenceController controller =
183                 new AddUserWhenLockedPreferenceController(mContext, "fake_key");
184         ReflectionHelpers.setField(controller, "mLockPatternUtils", mLockPatternUtils);
185         final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class);
186 
187         controller.updateState(preference);
188 
189         verify(preference).setVisible(true);
190         assertThat(controller.getAvailabilityStatus()).isEqualTo(
191                 BasePreferenceController.AVAILABLE);
192     }
193 }
194