[Auto Pin Confirm]: Changes to allow PIN auto confirm feature to be controllable by the user
- Add SwitchPreference to allow user to control the pin auto confirm feature - Add Checkbox option during the PIN setup in Security app - Disable the opt-in checkbox during SUW entry point for PIN setup - Update SwitchPreference availability appropriately according to current PIN length - Update the pin_auto_confirm setting appropriately according to state of switchPreference or checkbox state (in PIN setup) - Update the error-message when PIN Too short to let user know six digit is recommended Bug: 262926000 Bug: 262936383 Bug: 262934702 Bug: 262935305 Test: Manual Test Test: atest SettingsRoboTests Change-Id: Ib9e09bd5ce44652158e77f80e8be19c4dd50f3bf
This commit is contained in:
@@ -27,8 +27,10 @@ import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
|
||||
import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX;
|
||||
import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
|
||||
import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
|
||||
import static android.provider.DeviceConfig.NAMESPACE_AUTO_PIN_CONFIRMATION;
|
||||
import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
|
||||
|
||||
import static com.android.internal.widget.LockPatternUtils.FLAG_ENABLE_AUTO_PIN_CONFIRMATION;
|
||||
import static com.android.internal.widget.LockPatternUtils.PASSWORD_TYPE_KEY;
|
||||
import static com.android.settings.password.ChooseLockGeneric.CONFIRM_CREDENTIALS;
|
||||
|
||||
@@ -43,15 +45,21 @@ import android.app.admin.PasswordMetrics;
|
||||
import android.app.admin.PasswordPolicy;
|
||||
import android.content.Intent;
|
||||
import android.os.UserHandle;
|
||||
import android.provider.DeviceConfig;
|
||||
import android.view.View;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.android.internal.widget.LockscreenCredential;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.password.ChooseLockPassword.ChooseLockPasswordFragment;
|
||||
import com.android.settings.password.ChooseLockPassword.IntentBuilder;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||
import com.android.settings.testutils.shadow.ShadowDeviceConfig;
|
||||
import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
|
||||
import com.android.settings.testutils.shadow.ShadowLockPatternUtils;
|
||||
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||
import com.android.settings.widget.ScrollToParentEditText;
|
||||
|
||||
import com.google.android.setupdesign.GlifLayout;
|
||||
|
||||
@@ -71,9 +79,9 @@ import org.robolectric.shadows.ShadowDrawable;
|
||||
ShadowLockPatternUtils.class,
|
||||
ShadowUtils.class,
|
||||
ShadowDevicePolicyManager.class,
|
||||
ShadowDeviceConfig.class,
|
||||
})
|
||||
public class ChooseLockPasswordTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
SettingsShadowResources.overrideResource(
|
||||
@@ -387,7 +395,9 @@ public class ChooseLockPasswordTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void processAndValidatePasswordRequirements_defaultPinMinimumLength() {
|
||||
public void processAndValidatePasswordRequirements_autoPinDisabled_defaultPinMinimumLength() {
|
||||
DeviceConfig.setProperty(NAMESPACE_AUTO_PIN_CONFIRMATION, FLAG_ENABLE_AUTO_PIN_CONFIRMATION,
|
||||
/* value= */ "false", /* makeDefault= */ false);
|
||||
PasswordPolicy policy = new PasswordPolicy();
|
||||
policy.quality = PASSWORD_QUALITY_UNSPECIFIED;
|
||||
|
||||
@@ -399,6 +409,22 @@ public class ChooseLockPasswordTest {
|
||||
"PIN must be at least 4 digits");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void processAndValidatePasswordRequirements_autoPinEnabled_defaultPinMinimumLength() {
|
||||
DeviceConfig.setProperty(NAMESPACE_AUTO_PIN_CONFIRMATION, FLAG_ENABLE_AUTO_PIN_CONFIRMATION,
|
||||
/* value= */ "true", /* makeDefault= */ false);
|
||||
PasswordPolicy policy = new PasswordPolicy();
|
||||
policy.quality = PASSWORD_QUALITY_UNSPECIFIED;
|
||||
|
||||
assertPasswordValidationResult(
|
||||
/* minMetrics */ policy.getMinMetrics(),
|
||||
/* minComplexity= */ PASSWORD_COMPLEXITY_NONE,
|
||||
/* passwordType= */ PASSWORD_QUALITY_NUMERIC,
|
||||
/* userEnteredPassword= */ LockscreenCredential.createPassword("11"),
|
||||
"PIN must be at least 4 digits"
|
||||
+ ", but a 6-digit PIN is recommended for added security");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void processAndValidatePasswordRequirements_maximumLength() {
|
||||
PasswordPolicy policy = new PasswordPolicy();
|
||||
@@ -424,6 +450,123 @@ public class ChooseLockPasswordTest {
|
||||
"PIN must be at least 8 digits");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoPinConfirmOption_featureEnabledAndUntouchedByUser_changeStateAsPerRules() {
|
||||
DeviceConfig.setProperty(NAMESPACE_AUTO_PIN_CONFIRMATION, FLAG_ENABLE_AUTO_PIN_CONFIRMATION,
|
||||
/* value= */ "true", /* makeDefault= */ false);
|
||||
ChooseLockPassword passwordActivity = setupActivityWithPinTypeAndDefaultPolicy();
|
||||
|
||||
ChooseLockPasswordFragment fragment = getChooseLockPasswordFragment(passwordActivity);
|
||||
ScrollToParentEditText passwordEntry = passwordActivity.findViewById(R.id.password_entry);
|
||||
CheckBox pinAutoConfirmOption = passwordActivity
|
||||
.findViewById(R.id.auto_pin_confirm_enabler);
|
||||
TextView securityMessage =
|
||||
passwordActivity.findViewById(R.id.auto_pin_confirm_security_message);
|
||||
|
||||
passwordEntry.setText("1234");
|
||||
fragment.updateUi();
|
||||
assertThat(pinAutoConfirmOption.getVisibility()).isEqualTo(View.GONE);
|
||||
assertThat(securityMessage.getVisibility()).isEqualTo(View.GONE);
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isFalse();
|
||||
|
||||
passwordEntry.setText("123456");
|
||||
fragment.updateUi();
|
||||
assertThat(pinAutoConfirmOption.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(securityMessage.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isTrue();
|
||||
|
||||
passwordEntry.setText("12345678");
|
||||
fragment.updateUi();
|
||||
assertThat(pinAutoConfirmOption.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(securityMessage.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isFalse();
|
||||
|
||||
passwordEntry.setText("123456");
|
||||
fragment.updateUi();
|
||||
assertThat(pinAutoConfirmOption.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(securityMessage.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoPinConfirmOption_featureEnabledAndModifiedByUser_shouldChangeStateAsPerRules() {
|
||||
DeviceConfig.setProperty(NAMESPACE_AUTO_PIN_CONFIRMATION, FLAG_ENABLE_AUTO_PIN_CONFIRMATION,
|
||||
/* value= */ "true", /* makeDefault= */ false);
|
||||
ChooseLockPassword passwordActivity = setupActivityWithPinTypeAndDefaultPolicy();
|
||||
|
||||
ChooseLockPasswordFragment fragment = getChooseLockPasswordFragment(passwordActivity);
|
||||
ScrollToParentEditText passwordEntry = passwordActivity.findViewById(R.id.password_entry);
|
||||
CheckBox pinAutoConfirmOption = passwordActivity
|
||||
.findViewById(R.id.auto_pin_confirm_enabler);
|
||||
TextView securityMessage =
|
||||
passwordActivity.findViewById(R.id.auto_pin_confirm_security_message);
|
||||
|
||||
passwordEntry.setText("123456");
|
||||
fragment.updateUi();
|
||||
assertThat(pinAutoConfirmOption.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(securityMessage.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isTrue();
|
||||
|
||||
pinAutoConfirmOption.performClick();
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isFalse();
|
||||
|
||||
passwordEntry.setText("12345678");
|
||||
fragment.updateUi();
|
||||
assertThat(pinAutoConfirmOption.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(securityMessage.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isFalse();
|
||||
|
||||
passwordEntry.setText("123456");
|
||||
fragment.updateUi();
|
||||
assertThat(pinAutoConfirmOption.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(securityMessage.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoPinConfirmOption_featureDisabled_shouldRemainInvisibleAndUnchecked() {
|
||||
DeviceConfig.setProperty(NAMESPACE_AUTO_PIN_CONFIRMATION, FLAG_ENABLE_AUTO_PIN_CONFIRMATION,
|
||||
/* value= */ "false", /* makeDefault= */ false);
|
||||
ChooseLockPassword passwordActivity = setupActivityWithPinTypeAndDefaultPolicy();
|
||||
|
||||
ChooseLockPasswordFragment fragment = getChooseLockPasswordFragment(passwordActivity);
|
||||
ScrollToParentEditText passwordEntry = passwordActivity.findViewById(R.id.password_entry);
|
||||
CheckBox pinAutoConfirmOption = passwordActivity
|
||||
.findViewById(R.id.auto_pin_confirm_enabler);
|
||||
TextView securityMessage =
|
||||
passwordActivity.findViewById(R.id.auto_pin_confirm_security_message);
|
||||
|
||||
passwordEntry.setText("1234");
|
||||
fragment.updateUi();
|
||||
assertThat(pinAutoConfirmOption.getVisibility()).isEqualTo(View.GONE);
|
||||
assertThat(securityMessage.getVisibility()).isEqualTo(View.GONE);
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isFalse();
|
||||
|
||||
passwordEntry.setText("123456");
|
||||
fragment.updateUi();
|
||||
assertThat(pinAutoConfirmOption.getVisibility()).isEqualTo(View.GONE);
|
||||
assertThat(securityMessage.getVisibility()).isEqualTo(View.GONE);
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isFalse();
|
||||
|
||||
passwordEntry.setText("12345678");
|
||||
fragment.updateUi();
|
||||
assertThat(pinAutoConfirmOption.getVisibility()).isEqualTo(View.GONE);
|
||||
assertThat(securityMessage.getVisibility()).isEqualTo(View.GONE);
|
||||
assertThat(pinAutoConfirmOption.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
private ChooseLockPassword setupActivityWithPinTypeAndDefaultPolicy() {
|
||||
PasswordPolicy policy = new PasswordPolicy();
|
||||
policy.quality = PASSWORD_QUALITY_UNSPECIFIED;
|
||||
|
||||
return buildChooseLockPasswordActivity(
|
||||
new IntentBuilder(application)
|
||||
.setUserId(UserHandle.myUserId())
|
||||
.setPasswordType(PASSWORD_QUALITY_NUMERIC)
|
||||
.setPasswordRequirement(PASSWORD_COMPLEXITY_NONE, policy.getMinMetrics())
|
||||
.build());
|
||||
}
|
||||
|
||||
private ChooseLockPassword buildChooseLockPasswordActivity(Intent intent) {
|
||||
return Robolectric.buildActivity(ChooseLockPassword.class, intent).setup().get();
|
||||
}
|
||||
|
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* 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.verify;
|
||||
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 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;
|
||||
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);
|
||||
mPreference = new SwitchPreference(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_featureEnabledAndLockSetToNone_shouldReturnFalse() {
|
||||
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
|
||||
when(mLockPatternUtils.isAutoPinConfirmFeatureAvailable()).thenReturn(true);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_featureEnabledAndLockSetToPassword_shouldReturnFalse() {
|
||||
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
|
||||
when(mLockPatternUtils.isAutoPinConfirmFeatureAvailable()).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.isAutoPinConfirmFeatureAvailable()).thenReturn(true);
|
||||
when(mLockPatternUtils.getCredentialTypeForUser(TEST_USER_ID))
|
||||
.thenReturn(LockPatternUtils.CREDENTIAL_TYPE_PIN);
|
||||
when(mLockPatternUtils.getPinLength(TEST_USER_ID)).thenReturn(5L);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_featureEnabledAndLockSetToPIN_lengthMoreThanEqSix_shouldReturnTrue() {
|
||||
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
|
||||
when(mLockPatternUtils.isAutoPinConfirmFeatureAvailable()).thenReturn(true);
|
||||
when(mLockPatternUtils.getCredentialTypeForUser(TEST_USER_ID))
|
||||
.thenReturn(LockPatternUtils.CREDENTIAL_TYPE_PIN);
|
||||
when(mLockPatternUtils.getPinLength(TEST_USER_ID)).thenReturn(6L);
|
||||
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_featureDisabledAndLockSetToPIN_shouldReturnFalse() {
|
||||
when(mLockPatternUtils.isAutoPinConfirmFeatureAvailable()).thenReturn(false);
|
||||
when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
|
||||
when(mLockPatternUtils.getCredentialTypeForUser(TEST_USER_ID))
|
||||
.thenReturn(LockPatternUtils.CREDENTIAL_TYPE_PIN);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_ChangingSettingState_shouldSetPreferenceToAppropriateCheckedState() {
|
||||
when(mLockPatternUtils.isAutoPinConfirmFeatureAvailable()).thenReturn(true);
|
||||
// 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();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPreferenceChange_shouldUpdatePinAutoConfirmSetting() {
|
||||
when(mLockPatternUtils.isAutoPinConfirmFeatureAvailable()).thenReturn(true);
|
||||
mController.onPreferenceChange(mPreference, /* newValue= */ true);
|
||||
verify(mLockPatternUtils).setAutoPinConfirm(true, TEST_USER_ID);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user