fix: 引入Settings的Module

This commit is contained in:
2024-12-10 14:57:24 +08:00
parent ad8fc8731d
commit df105485bd
6934 changed files with 896168 additions and 2 deletions

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security.screenlock;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import com.android.internal.widget.LockPatternUtils;
import com.android.settingslib.core.lifecycle.ObservablePreferenceFragment;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class AutoPinConfirmPreferenceControllerTest {
private static final Integer TEST_USER_ID = 1;
@Mock
private LockPatternUtils mLockPatternUtils;
@Mock
private ObservablePreferenceFragment mParentFragment;
private AutoPinConfirmPreferenceController mController;
private SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
Context context = ApplicationProvider.getApplicationContext();
mController =
new AutoPinConfirmPreferenceController(context, TEST_USER_ID, mLockPatternUtils,
mParentFragment);
mPreference = new SwitchPreference(context);
}
@Test
public void isAvailable_featureEnabledAndLockSetToNone_shouldReturnFalse() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_featureEnabledAndLockSetToPassword_shouldReturnFalse() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
when(mLockPatternUtils.getCredentialTypeForUser(TEST_USER_ID))
.thenReturn(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_featureEnabledAndLockSetToPIN_lengthLessThanSix_shouldReturnFalse() {
when(mLockPatternUtils.getCredentialTypeForUser(TEST_USER_ID))
.thenReturn(LockPatternUtils.CREDENTIAL_TYPE_PIN);
when(mLockPatternUtils.getPinLength(TEST_USER_ID)).thenReturn(5);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_featureEnabledAndLockSetToPIN_lengthMoreThanEqSix_shouldReturnTrue() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
when(mLockPatternUtils.getCredentialTypeForUser(TEST_USER_ID))
.thenReturn(LockPatternUtils.CREDENTIAL_TYPE_PIN);
when(mLockPatternUtils.getPinLength(TEST_USER_ID)).thenReturn(6);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateState_ChangingSettingState_shouldSetPreferenceToAppropriateCheckedState() {
// When auto_pin_confirm setting is disabled, switchPreference is unchecked
when(mLockPatternUtils.isAutoPinConfirmEnabled(TEST_USER_ID)).thenReturn(false);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
// When auto_pin_confirm setting is enabled, switchPreference is checked
when(mLockPatternUtils.isAutoPinConfirmEnabled(TEST_USER_ID)).thenReturn(true);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security.screenlock;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.display.TimeoutListPreference;
import com.android.settings.security.trustagent.TrustAgentManager;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.Collections;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowDevicePolicyManager.class)
public class LockAfterTimeoutPreferenceControllerTest {
private static final int TEST_USER_ID = 0;
@Mock
private LockPatternUtils mLockPatternUtils;
@Mock
private TrustAgentManager mTrustAgentManager;
@Mock
private TimeoutListPreference mPreference;
private Context mContext;
private LockAfterTimeoutPreferenceController mController;
private FakeFeatureFactory mFeatureFactory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mFeatureFactory = FakeFeatureFactory.setupForTest();
when(mFeatureFactory.securityFeatureProvider.getTrustAgentManager())
.thenReturn(mTrustAgentManager);
mController = new LockAfterTimeoutPreferenceController(
mContext, TEST_USER_ID, mLockPatternUtils);
}
@Test
public void isAvailable_lockSetToPattern_shouldReturnTrue() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID))
.thenReturn(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_lockSetToPin_shouldReturnTrue() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID))
.thenReturn(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_lockSetToPassword_shouldReturnTrue() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID))
.thenReturn(DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_lockSetToNone_shouldReturnFalse() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void testUpdateStateWithAdminTimeouts() {
final int userId = UserHandle.myUserId();
final long adminTimeout = 10000;
final int displayTimeout = 5000;
final UserManager um = mock(UserManager.class);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(um);
when(um.getProfiles(userId)).thenReturn(Collections.emptyList());
// Fake list of timeout values.
when(mPreference.getEntries()).thenReturn(new CharSequence[] {"10"});
when(mPreference.getEntryValues()).thenReturn(new CharSequence[] {"10000"});
Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, displayTimeout);
ShadowDevicePolicyManager.getShadow().setMaximumTimeToLock(userId, adminTimeout);
mController.updateState(mPreference);
verify(mPreference).removeUnusableTimeouts(adminTimeout - displayTimeout, null);
}
}

View File

@@ -0,0 +1,145 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security.screenlock;
import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC;
import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.UserManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
@RunWith(RobolectricTestRunner.class)
public class LockScreenPreferenceControllerTest {
private static final int FAKE_PROFILE_USER_ID = 1234;
@Mock
private LockPatternUtils mLockPatternUtils;
@Mock
private UserManager mUm;
@Mock
private PreferenceScreen mScreen;
private Lifecycle mLifecycle;
private LifecycleOwner mLifecycleOwner;
private FakeFeatureFactory mFeatureFactory;
private Context mContext;
private LockScreenPreferenceController mController;
private Preference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
ShadowApplication.getInstance().setSystemService(Context.USER_SERVICE, mUm);
mFeatureFactory = FakeFeatureFactory.setupForTest();
when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
.thenReturn(mLockPatternUtils);
when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {FAKE_PROFILE_USER_ID});
mPreference = new Preference(mContext);
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mController = new LockScreenPreferenceController(mContext, "Test_key");
}
@Test
public void getAvailabilityStatus_notSecure_lockscreenDisabled_AVAILABLE() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_notSecure_lockscreenEnabled_AVAILABLE() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_secure_hasLockScreen_AVAILABLE() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
when(mLockPatternUtils.getKeyguardStoredPasswordQuality(anyInt()))
.thenReturn(PASSWORD_QUALITY_ALPHABETIC);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_secure_noLockScreen_AVAILABLE() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
when(mLockPatternUtils.getKeyguardStoredPasswordQuality(anyInt()))
.thenReturn(PASSWORD_QUALITY_UNSPECIFIED);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void onResume_available_shouldShow() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
when(mLockPatternUtils.getKeyguardStoredPasswordQuality(anyInt()))
.thenReturn(PASSWORD_QUALITY_ALPHABETIC);
mController.displayPreference(mScreen);
mLifecycle.handleLifecycleEvent(ON_RESUME);
assertThat(mPreference.isVisible()).isTrue();
}
@Test
public void onResume_unavailable_shouldShow() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
when(mLockPatternUtils.getKeyguardStoredPasswordQuality(anyInt()))
.thenReturn(PASSWORD_QUALITY_UNSPECIFIED);
mController.displayPreference(mScreen);
mLifecycle.handleLifecycleEvent(ON_RESUME);
assertThat(mPreference.isVisible()).isTrue();
}
}

View File

@@ -0,0 +1,98 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security.screenlock;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import androidx.preference.SwitchPreference;
import com.android.internal.widget.LockPatternUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class PatternVisiblePreferenceControllerTest {
private static final int TEST_USER_ID = 0;
@Mock
private LockPatternUtils mLockPatternUtils;
private Context mContext;
private PatternVisiblePreferenceController mController;
private SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController =
new PatternVisiblePreferenceController(mContext, TEST_USER_ID, mLockPatternUtils);
mPreference = new SwitchPreference(mContext);
}
@Test
public void isAvailable_lockSetToPattern_shouldReturnTrue() {
when(mLockPatternUtils.getCredentialTypeForUser(TEST_USER_ID))
.thenReturn(LockPatternUtils.CREDENTIAL_TYPE_PATTERN);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_lockSetToPin_shouldReturnFalse() {
when(mLockPatternUtils.getCredentialTypeForUser(TEST_USER_ID)).thenReturn(
LockPatternUtils.CREDENTIAL_TYPE_PIN);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_lockSetToNone_shouldReturnFalse() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void updateState_shouldSetPref() {
when(mLockPatternUtils.isVisiblePatternEnabled(TEST_USER_ID)).thenReturn(true);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
when(mLockPatternUtils.isVisiblePatternEnabled(TEST_USER_ID)).thenReturn(false);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void onPreferenceChange_shouldUpdateLockPatternUtils() {
mController.onPreferenceChange(mPreference, true /* newValue */);
verify(mLockPatternUtils).setVisiblePatternEnabled(true, TEST_USER_ID);
}
}

View File

@@ -0,0 +1,101 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security.screenlock;
import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PASSWORD_OR_PIN;
import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PATTERN;
import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PIN;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import androidx.preference.SwitchPreference;
import com.android.internal.widget.LockPatternUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class PinPrivacyPreferenceControllerTest {
private static final int TEST_USER_ID = 0;
@Mock
private LockPatternUtils mLockPatternUtils;
private Context mContext;
private PinPrivacyPreferenceController mController;
private SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController =
new PinPrivacyPreferenceController(mContext, TEST_USER_ID, mLockPatternUtils);
mPreference = new SwitchPreference(mContext);
}
@Test
public void isAvailable_lockSetToPin_shouldReturnTrue() {
when(mLockPatternUtils.getCredentialTypeForUser(TEST_USER_ID)).thenReturn(
CREDENTIAL_TYPE_PIN);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_lockSetToOther_shouldReturnFalse() {
when(mLockPatternUtils.getCredentialTypeForUser(TEST_USER_ID)).thenReturn(
CREDENTIAL_TYPE_PATTERN);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void updateState_shouldSetPref() {
when(mLockPatternUtils.isPinEnhancedPrivacyEnabled(TEST_USER_ID)).thenReturn(true);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void updateState_shouldSetPref_false() {
when(mLockPatternUtils.isPinEnhancedPrivacyEnabled(TEST_USER_ID)).thenReturn(false);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void onPreferenceChange_shouldUpdateLockPatternUtils() {
mController.onPreferenceChange(mPreference, true);
verify(mLockPatternUtils).setPinEnhancedPrivacyEnabled(true, TEST_USER_ID);
}
@Test
public void getPreferenceKey_returnsConst() {
assertThat(mController.getPreferenceKey().equals("enhancedPinPrivacy")).isTrue();
}
}

View File

@@ -0,0 +1,131 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security.screenlock;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import androidx.preference.SwitchPreference;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.R;
import com.android.settings.security.trustagent.TrustAgentManager;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class PowerButtonInstantLockPreferenceControllerTest {
private static final int TEST_USER_ID = 0;
@Mock
private LockPatternUtils mLockPatternUtils;
@Mock
private TrustAgentManager mTrustAgentManager;
private Context mContext;
private PowerButtonInstantLockPreferenceController mController;
private SwitchPreference mPreference;
private FakeFeatureFactory mFeatureFactory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mFeatureFactory = FakeFeatureFactory.setupForTest();
when(mFeatureFactory.securityFeatureProvider.getTrustAgentManager())
.thenReturn(mTrustAgentManager);
mPreference = new SwitchPreference(mContext);
mController = new PowerButtonInstantLockPreferenceController(
mContext, TEST_USER_ID, mLockPatternUtils);
}
@Test
public void isAvailable_lockSetToPattern_shouldReturnTrue() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID))
.thenReturn(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_lockSetToPin_shouldReturnTrue() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID))
.thenReturn(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_lockSetToPassword_shouldReturnTrue() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID))
.thenReturn(DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_lockSetToNone_shouldReturnFalse() {
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void updateState_shouldSetPref() {
final String fakeTrustAgent = "trust_agent";
when(mTrustAgentManager.getActiveTrustAgentLabel(mContext, mLockPatternUtils))
.thenReturn(fakeTrustAgent);
when(mLockPatternUtils.getPowerButtonInstantlyLocks(TEST_USER_ID)).thenReturn(true);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
assertThat(mPreference.getSummary()).isEqualTo(mContext.getString(
R.string.lockpattern_settings_power_button_instantly_locks_summary,
fakeTrustAgent));
when(mTrustAgentManager.getActiveTrustAgentLabel(mContext, mLockPatternUtils))
.thenReturn(null);
when(mLockPatternUtils.getPowerButtonInstantlyLocks(TEST_USER_ID)).thenReturn(false);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
assertThat(mPreference.getSummary()).isEqualTo(mContext.getString(
com.android.settingslib.R.string.summary_empty));
}
@Test
public void onPreferenceChange_shouldUpdateLockPatternUtils() {
mController.onPreferenceChange(mPreference, true /* newValue */);
verify(mLockPatternUtils).setPowerButtonInstantlyLocks(true, TEST_USER_ID);
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security.screenlock;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.security.OwnerInfoPreferenceController;
import com.android.settingslib.core.AbstractPreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RunWith(RobolectricTestRunner.class)
public class ScreenLockSettingsTest {
private ScreenLockSettings mSettings;
@Before
public void setUp() {
mSettings = new ScreenLockSettings();
}
@Test
public void verifyConstants() {
assertThat(mSettings.getMetricsCategory())
.isEqualTo(MetricsProto.MetricsEvent.SCREEN_LOCK_SETTINGS);
assertThat(mSettings.getPreferenceScreenResId()).isEqualTo(R.xml.screen_lock_settings);
}
@Test
public void onOwnerInfoUpdated_shouldUpdateOwnerInfoController() {
final Map<Class, List<AbstractPreferenceController>> preferenceControllers =
ReflectionHelpers.getField(mSettings, "mPreferenceControllers");
final OwnerInfoPreferenceController controller = mock(OwnerInfoPreferenceController.class);
List<AbstractPreferenceController> controllerList = new ArrayList<>();
controllerList.add(controller);
preferenceControllers.put(OwnerInfoPreferenceController.class, controllerList);
mSettings.onOwnerInfoUpdated();
verify(controller).updateSummary();
}
}