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.Mockito.mock; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.provider.Settings.Global; 27 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.testutils.shadow.ShadowUserManager; 31 import com.android.settingslib.RestrictedSwitchPreference; 32 33 import org.junit.After; 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.annotation.Config; 42 43 @RunWith(RobolectricTestRunner.class) 44 @Config(shadows = {ShadowUserManager.class}) 45 public class AddUserWhenLockedPreferenceControllerTest { 46 47 @Mock(answer = RETURNS_DEEP_STUBS) 48 private PreferenceScreen mScreen; 49 @Mock(answer = RETURNS_DEEP_STUBS) 50 51 private Context mContext; 52 private ShadowUserManager mUserManager; 53 private AddUserWhenLockedPreferenceController mController; 54 55 @Before setUp()56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 mContext = RuntimeEnvironment.application; 59 mUserManager = ShadowUserManager.getShadow(); 60 mController = new AddUserWhenLockedPreferenceController(mContext, "fake_key"); 61 mUserManager.setSupportsMultipleUsers(true); 62 } 63 64 @After tearDown()65 public void tearDown() { 66 ShadowUserManager.reset(); 67 } 68 69 @Test displayPref_NotAdmin_shouldNotDisplay()70 public void displayPref_NotAdmin_shouldNotDisplay() { 71 mUserManager.setIsAdminUser(false); 72 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 73 when(preference.getKey()).thenReturn(mController.getPreferenceKey()); 74 when(mScreen.findPreference(preference.getKey())).thenReturn(preference); 75 76 mController.displayPreference(mScreen); 77 78 verify(preference).setVisible(false); 79 } 80 81 @Test updateState_NotAdmin_shouldNotDisplayPreference()82 public void updateState_NotAdmin_shouldNotDisplayPreference() { 83 mUserManager.setIsAdminUser(false); 84 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 85 86 mController.updateState(preference); 87 88 verify(preference).setVisible(false); 89 } 90 91 @Test updateState_Admin_shouldDisplayPreference()92 public void updateState_Admin_shouldDisplayPreference() { 93 mUserManager.setIsAdminUser(true); 94 mUserManager.setUserSwitcherEnabled(true); 95 mUserManager.setSupportsMultipleUsers(true); 96 final AddUserWhenLockedPreferenceController controller = 97 new AddUserWhenLockedPreferenceController(mContext, "fake_key"); 98 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 99 100 controller.updateState(preference); 101 102 verify(preference).setVisible(true); 103 } 104 105 @Test updateState_preferenceSetCheckedWhenSettingIsOn()106 public void updateState_preferenceSetCheckedWhenSettingIsOn() { 107 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 108 Global.putInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 1); 109 110 mController.updateState(preference); 111 112 verify(preference).setChecked(true); 113 } 114 115 @Test updateState_preferenceSetUncheckedWhenSettingIsOff()116 public void updateState_preferenceSetUncheckedWhenSettingIsOff() { 117 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 118 Global.putInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 0); 119 120 mController.updateState(preference); 121 122 verify(preference).setChecked(false); 123 } 124 125 @Test onPreferenceChange_SettingIsOnWhenPreferenceChecked()126 public void onPreferenceChange_SettingIsOnWhenPreferenceChecked() { 127 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 128 preference.setChecked(true); 129 130 mController.onPreferenceChange(preference, Boolean.TRUE); 131 132 assertThat(Global.getInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 0)) 133 .isEqualTo(1); 134 } 135 136 @Test onPreferenceChange_SettingIsOffWhenPreferenceNotChecked()137 public void onPreferenceChange_SettingIsOffWhenPreferenceNotChecked() { 138 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 139 preference.setChecked(false); 140 141 mController.onPreferenceChange(preference, Boolean.FALSE); 142 143 assertThat(Global.getInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 0)) 144 .isEqualTo(0); 145 } 146 } 147