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