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.accounts; 17 18 import android.content.Context; 19 import android.content.pm.UserInfo; 20 import android.os.UserManager; 21 import android.provider.Settings.Global; 22 import android.support.v7.preference.Preference; 23 import android.support.v7.preference.PreferenceScreen; 24 import com.android.settingslib.RestrictedSwitchPreference; 25 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.annotation.Config; 35 import org.robolectric.shadows.ShadowApplication; 36 37 import static com.google.common.truth.Truth.assertThat; 38 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 39 import static org.mockito.Matchers.any; 40 import static org.mockito.Matchers.anyInt; 41 import static org.mockito.Mockito.mock; 42 import static org.mockito.Mockito.verify; 43 import static org.mockito.Mockito.when; 44 45 @RunWith(SettingsRobolectricTestRunner.class) 46 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 47 public class AddUserWhenLockedPreferenceControllerTest { 48 49 @Mock(answer = RETURNS_DEEP_STUBS) 50 private PreferenceScreen mScreen; 51 @Mock(answer = RETURNS_DEEP_STUBS) 52 private UserInfo mUserInfo; 53 @Mock(answer = RETURNS_DEEP_STUBS) 54 private UserManager mUserManager; 55 56 private Context mContext; 57 private AddUserWhenLockedPreferenceController mController; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 ShadowApplication shadowContext = ShadowApplication.getInstance(); 63 shadowContext.setSystemService(Context.USER_SERVICE, mUserManager); 64 mContext = shadowContext.getApplicationContext(); 65 mController = new AddUserWhenLockedPreferenceController(mContext); 66 } 67 68 @Test displayPref_NotAdmin_shouldNotDisplay()69 public void displayPref_NotAdmin_shouldNotDisplay() { 70 when(mUserManager.getUserInfo(anyInt())).thenReturn(mUserInfo); 71 when(mUserInfo.isAdmin()).thenReturn(false); 72 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 73 when(mScreen.getPreferenceCount()).thenReturn(1); 74 when(mScreen.getPreference(0)).thenReturn(preference); 75 when(preference.getKey()).thenReturn(mController.getPreferenceKey()); 76 77 mController.displayPreference(mScreen); 78 79 verify(mScreen).removePreference(any(Preference.class)); 80 } 81 82 @Test updateState_preferenceSetCheckedWhenSettingIsOn()83 public void updateState_preferenceSetCheckedWhenSettingIsOn() { 84 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 85 Global.putInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 1); 86 87 mController.updateState(preference); 88 89 verify(preference).setChecked(true); 90 } 91 92 @Test updateState_preferenceSetUncheckedWhenSettingIsOff()93 public void updateState_preferenceSetUncheckedWhenSettingIsOff() { 94 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 95 Global.putInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 0); 96 97 mController.updateState(preference); 98 99 verify(preference).setChecked(false); 100 } 101 102 @Test onPreferenceChange_SettingIsOnWhenPreferenceChecked()103 public void onPreferenceChange_SettingIsOnWhenPreferenceChecked() { 104 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 105 preference.setChecked(true); 106 107 mController.onPreferenceChange(preference, Boolean.TRUE); 108 109 assertThat(Global.getInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 0)) 110 .isEqualTo(1); 111 } 112 113 @Test onPreferenceChange_SettingIsOffWhenPreferenceNotChecked()114 public void onPreferenceChange_SettingIsOffWhenPreferenceNotChecked() { 115 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 116 preference.setChecked(false); 117 118 mController.onPreferenceChange(preference, Boolean.FALSE); 119 120 assertThat(Global.getInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 0)) 121 .isEqualTo(0); 122 } 123 124 } 125