1 /* 2 * Copyright (C) 2018 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.security; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.ArgumentMatchers.anyInt; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.provider.Settings; 26 27 import androidx.preference.SwitchPreference; 28 29 import com.android.internal.widget.LockPatternUtils; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 import org.robolectric.util.ReflectionHelpers; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class LockdownButtonPreferenceControllerTest { 42 43 @Mock 44 private LockPatternUtils mLockPatternUtils; 45 private SwitchPreference mPreference; 46 47 private Context mContext; 48 private LockdownButtonPreferenceController mController; 49 50 @Before setUp()51 public void setUp() { 52 MockitoAnnotations.initMocks(this); 53 mContext = RuntimeEnvironment.application; 54 mPreference = new SwitchPreference(mContext); 55 56 mController = spy(new LockdownButtonPreferenceController(mContext, "TestKey")); 57 ReflectionHelpers.setField(mController, "mLockPatternUtils", mLockPatternUtils); 58 } 59 60 @Test isAvailable_lockSet_shouldReturnTrue()61 public void isAvailable_lockSet_shouldReturnTrue() { 62 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 63 assertThat(mController.isAvailable()).isTrue(); 64 } 65 66 @Test isAvailable_lockUnset_shouldReturnFalse()67 public void isAvailable_lockUnset_shouldReturnFalse() { 68 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false); 69 assertThat(mController.isAvailable()).isFalse(); 70 } 71 72 @Test onPreferenceChange_settingIsUpdated()73 public void onPreferenceChange_settingIsUpdated() { 74 boolean state = Settings.Secure.getInt(mContext.getContentResolver(), 75 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0) != 0; 76 assertThat(mController.onPreferenceChange(mPreference, !state)).isTrue(); 77 boolean newState = Settings.Secure.getInt(mContext.getContentResolver(), 78 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0) != 0; 79 assertThat(newState).isEqualTo(!state); 80 } 81 82 @Test onSettingChange_preferenceIsUpdated()83 public void onSettingChange_preferenceIsUpdated() { 84 boolean state = Settings.Secure.getInt(mContext.getContentResolver(), 85 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0) != 0; 86 mController.updateState(mPreference); 87 assertThat(mPreference.isChecked()).isEqualTo(state); 88 Settings.Secure.putInt(mContext.getContentResolver(), 89 Settings.Secure.LOCKDOWN_IN_POWER_MENU, state ? 0 : 1); 90 91 mController.updateState(mPreference); 92 assertThat(mPreference.isChecked()).isEqualTo(!state); 93 } 94 } 95