Merge "Fix FingerprintEnrollIntroFragment buttons"

This commit is contained in:
TreeHugger Robot
2022-11-23 14:31:09 +00:00
committed by Android (Google) Code Review
6 changed files with 150 additions and 140 deletions

View File

@@ -44,6 +44,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import com.android.settings.R; import com.android.settings.R;
@@ -120,15 +121,6 @@ public class FingerprintEnrollIntroFragment extends Fragment {
footerTitle2.setText( footerTitle2.setText(
R.string.security_settings_fingerprint_enroll_introduction_footer_title_2); R.string.security_settings_fingerprint_enroll_introduction_footer_title_2);
return mView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final Context context = view.getContext();
final TextView footerLink = mView.findViewById(R.id.footer_learn_more); final TextView footerLink = mView.findViewById(R.id.footer_learn_more);
footerLink.setMovementMethod(LinkMovementMethod.getInstance()); footerLink.setMovementMethod(LinkMovementMethod.getInstance());
final String footerLinkStr = getContext().getString( final String footerLinkStr = getContext().getString(
@@ -139,18 +131,28 @@ public class FingerprintEnrollIntroFragment extends Fragment {
// footer buttons // footer buttons
mPrimaryFooterButton = new FooterButton.Builder(context) mPrimaryFooterButton = new FooterButton.Builder(context)
.setText(R.string.security_settings_fingerprint_enroll_introduction_agree) .setText(R.string.security_settings_fingerprint_enroll_introduction_agree)
.setListener(mViewModel::onNextButtonClick)
.setButtonType(FooterButton.ButtonType.OPT_IN) .setButtonType(FooterButton.ButtonType.OPT_IN)
.setTheme(R.style.SudGlifButton_Primary) .setTheme(R.style.SudGlifButton_Primary)
.build(); .build();
mSecondaryFooterButton = new FooterButton.Builder(context) mSecondaryFooterButton = new FooterButton.Builder(context)
.setListener(mViewModel::onSkipOrCancelButtonClick)
.setButtonType(FooterButton.ButtonType.NEXT) .setButtonType(FooterButton.ButtonType.NEXT)
.setTheme(R.style.SudGlifButton_Primary) .setTheme(R.style.SudGlifButton_Primary)
.build(); .build();
getFooterBarMixin().setPrimaryButton(mPrimaryFooterButton); getFooterBarMixin().setPrimaryButton(mPrimaryFooterButton);
getFooterBarMixin().setSecondaryButton(mSecondaryFooterButton, true /* usePrimaryStyle */); getFooterBarMixin().setSecondaryButton(mSecondaryFooterButton, true /* usePrimaryStyle */);
return mView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final Context context = view.getContext();
mPrimaryFooterButton.setOnClickListener(mViewModel::onNextButtonClick);
mSecondaryFooterButton.setOnClickListener(mViewModel::onSkipOrCancelButtonClick);
if (mViewModel.canAssumeUdfps()) { if (mViewModel.canAssumeUdfps()) {
mFooterMessage6.setVisibility(View.VISIBLE); mFooterMessage6.setVisibility(View.VISIBLE);
mIconShield.setVisibility(View.VISIBLE); mIconShield.setVisibility(View.VISIBLE);
@@ -158,7 +160,7 @@ public class FingerprintEnrollIntroFragment extends Fragment {
mFooterMessage6.setVisibility(View.GONE); mFooterMessage6.setVisibility(View.GONE);
mIconShield.setVisibility(View.GONE); mIconShield.setVisibility(View.GONE);
} }
mSecondaryFooterButton.setText(getContext(), mSecondaryFooterButton.setText(context,
mViewModel.getEnrollmentRequest().isAfterSuwOrSuwSuggestedAction() mViewModel.getEnrollmentRequest().isAfterSuwOrSuwSuggestedAction()
? R.string.security_settings_fingerprint_enroll_introduction_cancel ? R.string.security_settings_fingerprint_enroll_introduction_cancel
: R.string.security_settings_fingerprint_enroll_introduction_no_thanks); : R.string.security_settings_fingerprint_enroll_introduction_no_thanks);
@@ -174,18 +176,31 @@ public class FingerprintEnrollIntroFragment extends Fragment {
setHeaderText(getActivity(), setHeaderText(getActivity(),
R.string.security_settings_fingerprint_enroll_introduction_title); R.string.security_settings_fingerprint_enroll_introduction_title);
} }
observePageStatusLiveDataIfNeed();
}
mViewModel.getPageStatusLiveData().observe(this, this::updateFooterButtons); private void observePageStatusLiveDataIfNeed() {
final LiveData<FingerprintEnrollIntroStatus> statusLiveData =
mViewModel.getPageStatusLiveData();
final FingerprintEnrollIntroStatus status = statusLiveData.getValue();
if (status != null && status.hasScrollToBottom()) {
// Do not requireScrollWithButton() again when "I agree" or "Done" button is visible,
// because if we requireScrollWithButton() again, it will become "More" after scroll-up.
return;
}
final RequireScrollMixin requireScrollMixin = getLayout() final RequireScrollMixin requireScrollMixin = getLayout()
.getMixin(RequireScrollMixin.class); .getMixin(RequireScrollMixin.class);
requireScrollMixin.requireScrollWithButton(getActivity(), mPrimaryFooterButton, requireScrollMixin.requireScrollWithButton(getActivity(), mPrimaryFooterButton,
getMoreButtonTextRes(), mViewModel::onNextButtonClick); getMoreButtonTextRes(), mViewModel::onNextButtonClick);
requireScrollMixin.setOnRequireScrollStateChangedListener(scrollNeeded -> {
if (!scrollNeeded) { // Always set true to setHasScrolledToBottom() before registering listener through
mViewModel.setHasScrolledToBottom(); // setOnRequireScrollStateChangedListener(), because listener will not be called if first
} // scrollNeeded is true
}); mViewModel.setHasScrolledToBottom(true);
requireScrollMixin.setOnRequireScrollStateChangedListener(
scrollNeeded -> mViewModel.setHasScrolledToBottom(!scrollNeeded));
statusLiveData.observe(this, this::updateFooterButtons);
} }
@Override @Override

View File

@@ -95,14 +95,9 @@ public class FingerprintEnrollmentActivity extends FragmentActivity {
mViewModel = viewModelProvider.get(FingerprintEnrollmentViewModel.class); mViewModel = viewModelProvider.get(FingerprintEnrollmentViewModel.class);
mViewModel.setRequest(new EnrollmentRequest(getIntent(), getApplicationContext())); mViewModel.setRequest(new EnrollmentRequest(getIntent(), getApplicationContext()));
mViewModel.setSavedInstanceState(savedInstanceState); mViewModel.setSavedInstanceState(savedInstanceState);
getLifecycle().addObserver(mViewModel);
mAutoCredentialViewModel = viewModelProvider.get(AutoCredentialViewModel.class); mAutoCredentialViewModel = viewModelProvider.get(AutoCredentialViewModel.class);
mAutoCredentialViewModel.setCredentialModel(savedInstanceState, getIntent()); mAutoCredentialViewModel.setCredentialModel(savedInstanceState, getIntent());
mAutoCredentialViewModel.getGenerateChallengeFailLiveData().observe(this,
this::onGenerateChallengeFail);
mViewModel.getSetResultLiveData().observe(this, this::onSetActivityResult);
checkCredential(); checkCredential();
// Theme // Theme
@@ -113,16 +108,10 @@ public class FingerprintEnrollmentActivity extends FragmentActivity {
// fragment // fragment
setContentView(R.layout.biometric_enrollment_container); setContentView(R.layout.biometric_enrollment_container);
final FingerprintEnrollIntroViewModel fingerprintEnrollIntroViewModel = final FingerprintEnrollIntroViewModel introViewModel =
viewModelProvider.get(FingerprintEnrollIntroViewModel.class); viewModelProvider.get(FingerprintEnrollIntroViewModel.class);
fingerprintEnrollIntroViewModel.setEnrollmentRequest(mViewModel.getRequest()); introViewModel.setEnrollmentRequest(mViewModel.getRequest());
fingerprintEnrollIntroViewModel.setUserId(mAutoCredentialViewModel.getUserId()); introViewModel.setUserId(mAutoCredentialViewModel.getUserId());
// Clear ActionLiveData in FragmentViewModel to prevent getting previous action when
// recreate
fingerprintEnrollIntroViewModel.clearActionLiveData();
fingerprintEnrollIntroViewModel.getActionLiveData().observe(
this, this::observeIntroAction);
if (savedInstanceState == null) { if (savedInstanceState == null) {
final String tag = "FingerprintEnrollIntroFragment"; final String tag = "FingerprintEnrollIntroFragment";
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
@@ -131,9 +120,21 @@ public class FingerprintEnrollmentActivity extends FragmentActivity {
tag) tag)
.commit(); .commit();
} }
// observe LiveData
getLifecycle().addObserver(mViewModel);
mViewModel.getSetResultLiveData().observe(this, this::onSetActivityResult);
mAutoCredentialViewModel.getGenerateChallengeFailedLiveData().observe(this,
this::onGenerateChallengeFailed);
// Clear ActionLiveData in FragmentViewModel to prevent getting previous action during
// recreate, like press 'I agree' then press 'back' in FingerprintEnrollFindSensor activity.
introViewModel.clearActionLiveData();
introViewModel.getActionLiveData().observe(this, this::observeIntroAction);
} }
private void onGenerateChallengeFail(@NonNull Boolean isFail) { private void onGenerateChallengeFailed(@NonNull Boolean ignoredBoolean) {
onSetActivityResult(new ActivityResult(RESULT_CANCELED, null)); onSetActivityResult(new ActivityResult(RESULT_CANCELED, null));
} }
@@ -148,7 +149,7 @@ public class FingerprintEnrollmentActivity extends FragmentActivity {
private void checkCredential() { private void checkCredential() {
switch (mAutoCredentialViewModel.checkCredential()) { switch (mAutoCredentialViewModel.checkCredential()) {
case CREDENTIAL_FAIL_NEED_TO_CHOOSE_LOCK: { case CREDENTIAL_FAIL_NEED_TO_CHOOSE_LOCK: {
final Intent intent = mAutoCredentialViewModel.getChooseLockIntent(this, final Intent intent = mAutoCredentialViewModel.createChooseLockIntent(this,
mViewModel.getRequest().isSuw(), mViewModel.getRequest().getSuwExtras()); mViewModel.getRequest().isSuw(), mViewModel.getRequest().getSuwExtras());
if (!mViewModel.isWaitingActivityResult().compareAndSet(false, true)) { if (!mViewModel.isWaitingActivityResult().compareAndSet(false, true)) {
Log.w(TAG, "chooseLock, fail to set isWaiting flag to true"); Log.w(TAG, "chooseLock, fail to set isWaiting flag to true");
@@ -157,7 +158,7 @@ public class FingerprintEnrollmentActivity extends FragmentActivity {
return; return;
} }
case CREDENTIAL_FAIL_NEED_TO_CONFIRM_LOCK: { case CREDENTIAL_FAIL_NEED_TO_CONFIRM_LOCK: {
final boolean launched = mAutoCredentialViewModel.getConfirmLockLauncher( final boolean launched = mAutoCredentialViewModel.createConfirmLockLauncher(
this, this,
LAUNCH_CONFIRM_LOCK_ACTIVITY, LAUNCH_CONFIRM_LOCK_ACTIVITY,
getString(R.string.security_settings_fingerprint_preference_title) getString(R.string.security_settings_fingerprint_preference_title)
@@ -215,7 +216,7 @@ public class FingerprintEnrollmentActivity extends FragmentActivity {
final Intent intent = new Intent(this, isSuw final Intent intent = new Intent(this, isSuw
? SetupFingerprintEnrollFindSensor.class ? SetupFingerprintEnrollFindSensor.class
: FingerprintEnrollFindSensor.class); : FingerprintEnrollFindSensor.class);
intent.putExtras(mAutoCredentialViewModel.getCredentialIntentExtra()); intent.putExtras(mAutoCredentialViewModel.createCredentialIntentExtra());
intent.putExtras(mViewModel.getNextActivityBaseIntentExtras()); intent.putExtras(mViewModel.getNextActivityBaseIntentExtras());
mNextActivityLauncher.launch(intent); mNextActivityLauncher.launch(intent);
} }

View File

@@ -68,7 +68,7 @@ public class AutoCredentialViewModel extends AndroidViewModel {
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
/** /**
* Valid credential, activity doesn't need to do anything. * Valid credential, activity does nothing.
*/ */
public static final int CREDENTIAL_VALID = 0; public static final int CREDENTIAL_VALID = 0;
@@ -170,7 +170,7 @@ public class AutoCredentialViewModel extends AndroidViewModel {
@NonNull private final LockPatternUtils mLockPatternUtils; @NonNull private final LockPatternUtils mLockPatternUtils;
@NonNull private final ChallengeGenerator mChallengeGenerator; @NonNull private final ChallengeGenerator mChallengeGenerator;
private CredentialModel mCredentialModel = null; private CredentialModel mCredentialModel = null;
@NonNull private final MutableLiveData<Boolean> mGenerateChallengeFailLiveData = @NonNull private final MutableLiveData<Boolean> mGenerateChallengeFailedLiveData =
new MutableLiveData<>(); new MutableLiveData<>();
public AutoCredentialViewModel( public AutoCredentialViewModel(
@@ -186,11 +186,13 @@ public class AutoCredentialViewModel extends AndroidViewModel {
* Set CredentialModel, the source is coming from savedInstanceState or activity intent * Set CredentialModel, the source is coming from savedInstanceState or activity intent
*/ */
public void setCredentialModel(@Nullable Bundle savedInstanceState, @NonNull Intent intent) { public void setCredentialModel(@Nullable Bundle savedInstanceState, @NonNull Intent intent) {
mCredentialModel = new CredentialModel( final Bundle bundle;
savedInstanceState != null if (savedInstanceState != null) {
? savedInstanceState.getBundle(KEY_CREDENTIAL_MODEL) bundle = savedInstanceState.getBundle(KEY_CREDENTIAL_MODEL);
: intent.getExtras(), } else {
SystemClock.elapsedRealtimeClock()); bundle = intent.getExtras();
}
mCredentialModel = new CredentialModel(bundle, SystemClock.elapsedRealtimeClock());
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "setCredentialModel " + mCredentialModel + ", savedInstanceState exist:" Log.d(TAG, "setCredentialModel " + mCredentialModel + ", savedInstanceState exist:"
@@ -206,8 +208,8 @@ public class AutoCredentialViewModel extends AndroidViewModel {
} }
@NonNull @NonNull
public LiveData<Boolean> getGenerateChallengeFailLiveData() { public LiveData<Boolean> getGenerateChallengeFailedLiveData() {
return mGenerateChallengeFailLiveData; return mGenerateChallengeFailedLiveData;
} }
/** /**
@@ -238,7 +240,7 @@ public class AutoCredentialViewModel extends AndroidViewModel {
mCredentialModel.setToken(newToken); mCredentialModel.setToken(newToken);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
Log.e(TAG, "generateChallenge, IllegalStateException", e); Log.e(TAG, "generateChallenge, IllegalStateException", e);
mGenerateChallengeFailLiveData.postValue(true); mGenerateChallengeFailedLiveData.postValue(true);
return; return;
} }
@@ -252,7 +254,7 @@ public class AutoCredentialViewModel extends AndroidViewModel {
// Check credential again // Check credential again
if (!isValidCredential()) { if (!isValidCredential()) {
Log.w(TAG, "generateChallenge, invalid Credential"); Log.w(TAG, "generateChallenge, invalid Credential");
mGenerateChallengeFailLiveData.postValue(true); mGenerateChallengeFailedLiveData.postValue(true);
} }
}); });
mChallengeGenerator.generateChallenge(getUserId()); mChallengeGenerator.generateChallenge(getUserId());
@@ -312,7 +314,7 @@ public class AutoCredentialViewModel extends AndroidViewModel {
* Get Credential intent extra which will be used to launch next activity. * Get Credential intent extra which will be used to launch next activity.
*/ */
@NonNull @NonNull
public Bundle getCredentialIntentExtra() { public Bundle createCredentialIntentExtra() {
final Bundle retBundle = new Bundle(); final Bundle retBundle = new Bundle();
final long gkPwHandle = mCredentialModel.getGkPwHandle(); final long gkPwHandle = mCredentialModel.getGkPwHandle();
if (CredentialModel.isValidGkPwHandle(gkPwHandle)) { if (CredentialModel.isValidGkPwHandle(gkPwHandle)) {
@@ -332,10 +334,10 @@ public class AutoCredentialViewModel extends AndroidViewModel {
} }
/** /**
* Get Intent for choosing lock * Create Intent for choosing lock
*/ */
@NonNull @NonNull
public Intent getChooseLockIntent(@NonNull Context context, boolean isSuw, public Intent createChooseLockIntent(@NonNull Context context, boolean isSuw,
@NonNull Bundle suwExtras) { @NonNull Bundle suwExtras) {
final Intent intent = BiometricUtils.getChooseLockIntent(context, isSuw, final Intent intent = BiometricUtils.getChooseLockIntent(context, isSuw,
suwExtras); suwExtras);
@@ -352,10 +354,10 @@ public class AutoCredentialViewModel extends AndroidViewModel {
} }
/** /**
* Get ConfirmLockLauncher * Create ConfirmLockLauncher
*/ */
@NonNull @NonNull
public ChooseLockSettingsHelper getConfirmLockLauncher(@NonNull Activity activity, public ChooseLockSettingsHelper createConfirmLockLauncher(@NonNull Activity activity,
int requestCode, @NonNull String title) { int requestCode, @NonNull String title) {
final ChooseLockSettingsHelper.Builder builder = final ChooseLockSettingsHelper.Builder builder =
new ChooseLockSettingsHelper.Builder(activity); new ChooseLockSettingsHelper.Builder(activity);

View File

@@ -145,7 +145,7 @@ public class FingerprintEnrollIntroViewModel extends AndroidViewModel
} }
/** /**
* Clear user's action live data (like clicking Agree, Skip, or Done) * Clear user's action live data
*/ */
public void clearActionLiveData() { public void clearActionLiveData() {
mActionLiveData.setValue(null); mActionLiveData.setValue(null);
@@ -168,8 +168,8 @@ public class FingerprintEnrollIntroViewModel extends AndroidViewModel
/** /**
* Update onboarding intro page has scrolled to bottom * Update onboarding intro page has scrolled to bottom
*/ */
public void setHasScrolledToBottom() { public void setHasScrolledToBottom(boolean value) {
mHasScrolledToBottomLiveData.postValue(true); mHasScrolledToBottomLiveData.postValue(value);
} }
/** /**

View File

@@ -26,6 +26,8 @@ import static com.android.settings.biometrics.BiometricEnrollBase.EXTRA_KEY_SENS
import static com.android.settings.biometrics2.ui.model.CredentialModel.INVALID_CHALLENGE; import static com.android.settings.biometrics2.ui.model.CredentialModel.INVALID_CHALLENGE;
import static com.android.settings.biometrics2.ui.model.CredentialModel.INVALID_GK_PW_HANDLE; import static com.android.settings.biometrics2.ui.model.CredentialModel.INVALID_GK_PW_HANDLE;
import static com.android.settings.biometrics2.ui.model.CredentialModel.INVALID_SENSOR_ID; import static com.android.settings.biometrics2.ui.model.CredentialModel.INVALID_SENSOR_ID;
import static com.android.settings.biometrics2.ui.model.CredentialModel.isValidGkPwHandle;
import static com.android.settings.biometrics2.ui.model.CredentialModel.isValidToken;
import static com.android.settings.biometrics2.ui.model.CredentialModelTest.newCredentialModelIntentExtras; import static com.android.settings.biometrics2.ui.model.CredentialModelTest.newCredentialModelIntentExtras;
import static com.android.settings.biometrics2.ui.model.CredentialModelTest.newGkPwHandleCredentialIntentExtras; import static com.android.settings.biometrics2.ui.model.CredentialModelTest.newGkPwHandleCredentialIntentExtras;
import static com.android.settings.biometrics2.ui.model.CredentialModelTest.newInvalidChallengeCredentialIntentExtras; import static com.android.settings.biometrics2.ui.model.CredentialModelTest.newInvalidChallengeCredentialIntentExtras;
@@ -58,9 +60,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.internal.widget.LockPatternUtils; import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.VerifyCredentialResponse; import com.android.internal.widget.VerifyCredentialResponse;
import com.android.settings.biometrics2.ui.model.CredentialModel;
import com.android.settings.password.ChooseLockPattern; import com.android.settings.password.ChooseLockPattern;
import com.android.settings.password.ChooseLockSettingsHelper;
import com.android.settings.testutils.InstantTaskExecutorRule; import com.android.settings.testutils.InstantTaskExecutorRule;
import org.junit.Before; import org.junit.Before;
@@ -79,12 +79,12 @@ public class AutoCredentialViewModelTest {
@Mock private LockPatternUtils mLockPatternUtils; @Mock private LockPatternUtils mLockPatternUtils;
private TestChallengeGenerator mChallengeGenerator = null; private TestChallengeGenerator mChallengeGenerator = null;
private AutoCredentialViewModel mAutoCredentialViewModel; private AutoCredentialViewModel mViewModel;
@Before @Before
public void setUp() { public void setUp() {
mChallengeGenerator = new TestChallengeGenerator(); mChallengeGenerator = new TestChallengeGenerator();
mAutoCredentialViewModel = new AutoCredentialViewModel( mViewModel = new AutoCredentialViewModel(
ApplicationProvider.getApplicationContext(), ApplicationProvider.getApplicationContext(),
mLockPatternUtils, mLockPatternUtils,
mChallengeGenerator); mChallengeGenerator);
@@ -99,22 +99,21 @@ public class AutoCredentialViewModelTest {
} }
@Test @Test
public void testGetCredentialIntentExtra_sameResultFromSavedInstanceOrIntent() { public void testSetCredentialModel_sameResultFromSavedInstanceOrIntent() {
final Bundle extras = newCredentialModelIntentExtras(12, 33, 1, new byte[] { 2, 3 }, 3L); final Bundle extras = newCredentialModelIntentExtras(12, 33, 1, new byte[] { 2, 3 }, 3L);
AutoCredentialViewModel autoCredentialViewModel2 = new AutoCredentialViewModel( AutoCredentialViewModel viewModel2 = new AutoCredentialViewModel(
ApplicationProvider.getApplicationContext(), ApplicationProvider.getApplicationContext(),
mLockPatternUtils, mLockPatternUtils,
mChallengeGenerator); mChallengeGenerator);
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null, new Intent().putExtras(extras));
new Intent().putExtras(extras));
final Bundle savedInstance = new Bundle(); final Bundle savedInstance = new Bundle();
savedInstance.putBundle(KEY_CREDENTIAL_MODEL, extras); savedInstance.putBundle(KEY_CREDENTIAL_MODEL, extras);
autoCredentialViewModel2.setCredentialModel(savedInstance, new Intent()); viewModel2.setCredentialModel(savedInstance, new Intent());
final Bundle bundle1 = mAutoCredentialViewModel.getCredentialIntentExtra(); final Bundle bundle1 = mViewModel.createCredentialIntentExtra();
final Bundle bundle2 = autoCredentialViewModel2.getCredentialIntentExtra(); final Bundle bundle2 = viewModel2.createCredentialIntentExtra();
assertThat(bundle1.getLong(EXTRA_KEY_GK_PW_HANDLE)) assertThat(bundle1.getLong(EXTRA_KEY_GK_PW_HANDLE))
.isEqualTo(bundle2.getLong(EXTRA_KEY_GK_PW_HANDLE)); .isEqualTo(bundle2.getLong(EXTRA_KEY_GK_PW_HANDLE));
assertThat(bundle1.getLong(Intent.EXTRA_USER_ID)) assertThat(bundle1.getLong(Intent.EXTRA_USER_ID))
@@ -134,23 +133,22 @@ public class AutoCredentialViewModelTest {
} }
@Test @Test
public void testGetCredentialIntentExtra_sameResultFromSavedInstanceOrIntent_invalidValues() { public void testSetCredentialModel_sameResultFromSavedInstanceOrIntent_invalidValues() {
final Bundle extras = newCredentialModelIntentExtras(UserHandle.USER_NULL, final Bundle extras = newCredentialModelIntentExtras(UserHandle.USER_NULL,
INVALID_CHALLENGE, INVALID_SENSOR_ID, null, INVALID_GK_PW_HANDLE); INVALID_CHALLENGE, INVALID_SENSOR_ID, null, INVALID_GK_PW_HANDLE);
AutoCredentialViewModel autoCredentialViewModel2 = new AutoCredentialViewModel( AutoCredentialViewModel viewModel2 = new AutoCredentialViewModel(
ApplicationProvider.getApplicationContext(), ApplicationProvider.getApplicationContext(),
mLockPatternUtils, mLockPatternUtils,
mChallengeGenerator); mChallengeGenerator);
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null, new Intent().putExtras(extras));
new Intent().putExtras(extras));
final Bundle savedInstance = new Bundle(); final Bundle savedInstance = new Bundle();
savedInstance.putBundle(KEY_CREDENTIAL_MODEL, extras); savedInstance.putBundle(KEY_CREDENTIAL_MODEL, extras);
autoCredentialViewModel2.setCredentialModel(savedInstance, new Intent()); viewModel2.setCredentialModel(savedInstance, new Intent());
final Bundle bundle1 = mAutoCredentialViewModel.getCredentialIntentExtra(); final Bundle bundle1 = mViewModel.createCredentialIntentExtra();
final Bundle bundle2 = autoCredentialViewModel2.getCredentialIntentExtra(); final Bundle bundle2 = viewModel2.createCredentialIntentExtra();
assertThat(bundle1.containsKey(EXTRA_KEY_GK_PW_HANDLE)).isFalse(); assertThat(bundle1.containsKey(EXTRA_KEY_GK_PW_HANDLE)).isFalse();
assertThat(bundle2.containsKey(EXTRA_KEY_GK_PW_HANDLE)).isFalse(); assertThat(bundle2.containsKey(EXTRA_KEY_GK_PW_HANDLE)).isFalse();
assertThat(bundle1.containsKey(EXTRA_KEY_CHALLENGE_TOKEN)).isFalse(); assertThat(bundle1.containsKey(EXTRA_KEY_CHALLENGE_TOKEN)).isFalse();
@@ -164,83 +162,83 @@ public class AutoCredentialViewModelTest {
@Test @Test
public void testCheckCredential_validCredentialCase() { public void testCheckCredential_validCredentialCase() {
final int userId = 99; final int userId = 99;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newValidTokenCredentialIntentExtras(userId))); new Intent().putExtras(newValidTokenCredentialIntentExtras(userId)));
when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn( when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn(
PASSWORD_QUALITY_SOMETHING); PASSWORD_QUALITY_SOMETHING);
// Run credential check // Run credential check
@CredentialAction final int action = mAutoCredentialViewModel.checkCredential(); @CredentialAction final int action = mViewModel.checkCredential();
assertThat(action).isEqualTo(CREDENTIAL_VALID); assertThat(action).isEqualTo(CREDENTIAL_VALID);
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
} }
@Test @Test
public void testCheckCredential_needToChooseLock() { public void testCheckCredential_needToChooseLock() {
final int userId = 100; final int userId = 100;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId))); new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId)));
when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn( when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn(
PASSWORD_QUALITY_UNSPECIFIED); PASSWORD_QUALITY_UNSPECIFIED);
// Run credential check // Run credential check
@CredentialAction final int action = mAutoCredentialViewModel.checkCredential(); @CredentialAction final int action = mViewModel.checkCredential();
assertThat(action).isEqualTo(CREDENTIAL_FAIL_NEED_TO_CHOOSE_LOCK); assertThat(action).isEqualTo(CREDENTIAL_FAIL_NEED_TO_CHOOSE_LOCK);
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
} }
@Test @Test
public void testCheckCredential_needToConfirmLockFoSomething() { public void testCheckCredential_needToConfirmLockFoSomething() {
final int userId = 101; final int userId = 101;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId))); new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId)));
when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn( when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn(
PASSWORD_QUALITY_SOMETHING); PASSWORD_QUALITY_SOMETHING);
// Run credential check // Run credential check
@CredentialAction final int action = mAutoCredentialViewModel.checkCredential(); @CredentialAction final int action = mViewModel.checkCredential();
assertThat(action).isEqualTo(CREDENTIAL_FAIL_NEED_TO_CONFIRM_LOCK); assertThat(action).isEqualTo(CREDENTIAL_FAIL_NEED_TO_CONFIRM_LOCK);
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
} }
@Test @Test
public void testCheckCredential_needToConfirmLockForNumeric() { public void testCheckCredential_needToConfirmLockForNumeric() {
final int userId = 102; final int userId = 102;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId))); new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId)));
when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn( when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn(
PASSWORD_QUALITY_NUMERIC); PASSWORD_QUALITY_NUMERIC);
// Run credential check // Run credential check
@CredentialAction final int action = mAutoCredentialViewModel.checkCredential(); @CredentialAction final int action = mViewModel.checkCredential();
assertThat(action).isEqualTo(CREDENTIAL_FAIL_NEED_TO_CONFIRM_LOCK); assertThat(action).isEqualTo(CREDENTIAL_FAIL_NEED_TO_CONFIRM_LOCK);
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
} }
@Test @Test
public void testCheckCredential_needToConfirmLockForAlphabetic() { public void testCheckCredential_needToConfirmLockForAlphabetic() {
final int userId = 103; final int userId = 103;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId))); new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId)));
when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn( when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn(
PASSWORD_QUALITY_ALPHABETIC); PASSWORD_QUALITY_ALPHABETIC);
// Run credential check // Run credential check
@CredentialAction final int action = mAutoCredentialViewModel.checkCredential(); @CredentialAction final int action = mViewModel.checkCredential();
assertThat(action).isEqualTo(CREDENTIAL_FAIL_NEED_TO_CONFIRM_LOCK); assertThat(action).isEqualTo(CREDENTIAL_FAIL_NEED_TO_CONFIRM_LOCK);
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
} }
@Test @Test
public void testCheckCredential_generateChallenge() { public void testCheckCredential_generateChallenge() {
final int userId = 104; final int userId = 104;
final long gkPwHandle = 1111L; final long gkPwHandle = 1111L;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newGkPwHandleCredentialIntentExtras(userId, gkPwHandle))); new Intent().putExtras(newGkPwHandleCredentialIntentExtras(userId, gkPwHandle)));
when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn( when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn(
PASSWORD_QUALITY_SOMETHING); PASSWORD_QUALITY_SOMETHING);
@@ -252,17 +250,15 @@ public class AutoCredentialViewModelTest {
.thenReturn(newGoodCredential(gkPwHandle, new byte[] { 1 })); .thenReturn(newGoodCredential(gkPwHandle, new byte[] { 1 }));
// Run credential check // Run credential check
@CredentialAction final int action = mAutoCredentialViewModel.checkCredential(); @CredentialAction final int action = mViewModel.checkCredential();
assertThat(action).isEqualTo(CREDENTIAL_IS_GENERATING_CHALLENGE); assertThat(action).isEqualTo(CREDENTIAL_IS_GENERATING_CHALLENGE);
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
final Bundle extras = mAutoCredentialViewModel.getCredentialIntentExtra(); final Bundle extras = mViewModel.createCredentialIntentExtra();
assertThat(extras.getInt(EXTRA_KEY_SENSOR_ID)).isEqualTo(newSensorId); assertThat(extras.getInt(EXTRA_KEY_SENSOR_ID)).isEqualTo(newSensorId);
assertThat(extras.getLong(EXTRA_KEY_CHALLENGE)).isEqualTo(newChallenge); assertThat(extras.getLong(EXTRA_KEY_CHALLENGE)).isEqualTo(newChallenge);
assertThat(CredentialModel.isValidToken(extras.getByteArray(EXTRA_KEY_CHALLENGE_TOKEN))) assertThat(isValidToken(extras.getByteArray(EXTRA_KEY_CHALLENGE_TOKEN))).isTrue();
.isTrue(); assertThat(isValidGkPwHandle(extras.getLong(EXTRA_KEY_GK_PW_HANDLE))).isFalse();
assertThat(CredentialModel.isValidGkPwHandle(extras.getLong(EXTRA_KEY_GK_PW_HANDLE)))
.isFalse();
assertThat(mChallengeGenerator.mCallbackRunCount).isEqualTo(1); assertThat(mChallengeGenerator.mCallbackRunCount).isEqualTo(1);
} }
@@ -270,7 +266,7 @@ public class AutoCredentialViewModelTest {
public void testCheckCredential_generateChallengeFail() { public void testCheckCredential_generateChallengeFail() {
final int userId = 104; final int userId = 104;
final long gkPwHandle = 1111L; final long gkPwHandle = 1111L;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newGkPwHandleCredentialIntentExtras(userId, gkPwHandle))); new Intent().putExtras(newGkPwHandleCredentialIntentExtras(userId, gkPwHandle)));
when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn( when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn(
PASSWORD_QUALITY_SOMETHING); PASSWORD_QUALITY_SOMETHING);
@@ -282,21 +278,21 @@ public class AutoCredentialViewModelTest {
.thenReturn(newBadCredential(0)); .thenReturn(newBadCredential(0));
// Run credential check // Run credential check
@CredentialAction final int action = mAutoCredentialViewModel.checkCredential(); @CredentialAction final int action = mViewModel.checkCredential();
assertThat(action).isEqualTo(CREDENTIAL_IS_GENERATING_CHALLENGE); assertThat(action).isEqualTo(CREDENTIAL_IS_GENERATING_CHALLENGE);
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isTrue(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isTrue();
assertThat(mChallengeGenerator.mCallbackRunCount).isEqualTo(1); assertThat(mChallengeGenerator.mCallbackRunCount).isEqualTo(1);
} }
@Test @Test
public void testGetUserId_fromIntent() { public void testGetUserId_fromIntent() {
final int userId = 106; final int userId = 106;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId))); new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId)));
// Get userId // Get userId
assertThat(mAutoCredentialViewModel.getUserId()).isEqualTo(userId); assertThat(mViewModel.getUserId()).isEqualTo(userId);
} }
@Test @Test
@@ -305,79 +301,79 @@ public class AutoCredentialViewModelTest {
final Bundle savedInstance = new Bundle(); final Bundle savedInstance = new Bundle();
savedInstance.putBundle(KEY_CREDENTIAL_MODEL, savedInstance.putBundle(KEY_CREDENTIAL_MODEL,
newInvalidChallengeCredentialIntentExtras(userId)); newInvalidChallengeCredentialIntentExtras(userId));
mAutoCredentialViewModel.setCredentialModel(savedInstance, new Intent()); mViewModel.setCredentialModel(savedInstance, new Intent());
// Get userId // Get userId
assertThat(mAutoCredentialViewModel.getUserId()).isEqualTo(userId); assertThat(mViewModel.getUserId()).isEqualTo(userId);
} }
@Test @Test
public void testCheckNewCredentialFromActivityResult_invalidChooseLock() { public void testCheckNewCredentialFromActivityResult_invalidChooseLock() {
final int userId = 107; final int userId = 107;
final long gkPwHandle = 3333L; final long gkPwHandle = 3333L;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newGkPwHandleCredentialIntentExtras(userId, gkPwHandle))); new Intent().putExtras(newGkPwHandleCredentialIntentExtras(userId, gkPwHandle)));
final Intent intent = new Intent(); final Intent intent = new Intent();
intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_GK_PW_HANDLE, gkPwHandle); intent.putExtra(EXTRA_KEY_GK_PW_HANDLE, gkPwHandle);
// run checkNewCredentialFromActivityResult() // run checkNewCredentialFromActivityResult()
final boolean ret = mAutoCredentialViewModel.checkNewCredentialFromActivityResult(true, final boolean ret = mViewModel.checkNewCredentialFromActivityResult(true,
new ActivityResult(ChooseLockPattern.RESULT_FINISHED + 1, intent)); new ActivityResult(ChooseLockPattern.RESULT_FINISHED + 1, intent));
assertThat(ret).isFalse(); assertThat(ret).isFalse();
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
} }
@Test @Test
public void testCheckNewCredentialFromActivityResult_invalidConfirmLock() { public void testCheckNewCredentialFromActivityResult_invalidConfirmLock() {
final int userId = 107; final int userId = 107;
final long gkPwHandle = 3333L; final long gkPwHandle = 3333L;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newGkPwHandleCredentialIntentExtras(userId, gkPwHandle))); new Intent().putExtras(newGkPwHandleCredentialIntentExtras(userId, gkPwHandle)));
final Intent intent = new Intent(); final Intent intent = new Intent();
intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_GK_PW_HANDLE, gkPwHandle); intent.putExtra(EXTRA_KEY_GK_PW_HANDLE, gkPwHandle);
// run checkNewCredentialFromActivityResult() // run checkNewCredentialFromActivityResult()
final boolean ret = mAutoCredentialViewModel.checkNewCredentialFromActivityResult(false, final boolean ret = mViewModel.checkNewCredentialFromActivityResult(false,
new ActivityResult(Activity.RESULT_OK + 1, intent)); new ActivityResult(Activity.RESULT_OK + 1, intent));
assertThat(ret).isFalse(); assertThat(ret).isFalse();
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
} }
@Test @Test
public void testCheckNewCredentialFromActivityResult_nullDataChooseLock() { public void testCheckNewCredentialFromActivityResult_nullDataChooseLock() {
final int userId = 108; final int userId = 108;
final long gkPwHandle = 4444L; final long gkPwHandle = 4444L;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newGkPwHandleCredentialIntentExtras(userId, gkPwHandle))); new Intent().putExtras(newGkPwHandleCredentialIntentExtras(userId, gkPwHandle)));
// run checkNewCredentialFromActivityResult() // run checkNewCredentialFromActivityResult()
final boolean ret = mAutoCredentialViewModel.checkNewCredentialFromActivityResult(true, final boolean ret = mViewModel.checkNewCredentialFromActivityResult(true,
new ActivityResult(ChooseLockPattern.RESULT_FINISHED, null)); new ActivityResult(ChooseLockPattern.RESULT_FINISHED, null));
assertThat(ret).isFalse(); assertThat(ret).isFalse();
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
} }
@Test @Test
public void testCheckNewCredentialFromActivityResult_nullDataConfirmLock() { public void testCheckNewCredentialFromActivityResult_nullDataConfirmLock() {
final int userId = 109; final int userId = 109;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId))); new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId)));
// run checkNewCredentialFromActivityResult() // run checkNewCredentialFromActivityResult()
final boolean ret = mAutoCredentialViewModel.checkNewCredentialFromActivityResult(false, final boolean ret = mViewModel.checkNewCredentialFromActivityResult(false,
new ActivityResult(Activity.RESULT_OK, null)); new ActivityResult(Activity.RESULT_OK, null));
assertThat(ret).isFalse(); assertThat(ret).isFalse();
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
} }
@Test @Test
public void testCheckNewCredentialFromActivityResult_validChooseLock() { public void testCheckNewCredentialFromActivityResult_validChooseLock() {
final int userId = 108; final int userId = 108;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId))); new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId)));
when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn( when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn(
PASSWORD_QUALITY_SOMETHING); PASSWORD_QUALITY_SOMETHING);
@@ -390,20 +386,17 @@ public class AutoCredentialViewModelTest {
.thenReturn(newGoodCredential(gkPwHandle, new byte[] { 1 })); .thenReturn(newGoodCredential(gkPwHandle, new byte[] { 1 }));
// Run checkNewCredentialFromActivityResult() // Run checkNewCredentialFromActivityResult()
final Intent intent = new Intent().putExtra(ChooseLockSettingsHelper.EXTRA_KEY_GK_PW_HANDLE, final Intent intent = new Intent().putExtra(EXTRA_KEY_GK_PW_HANDLE, gkPwHandle);
gkPwHandle); final boolean ret = mViewModel.checkNewCredentialFromActivityResult(true,
final boolean ret = mAutoCredentialViewModel.checkNewCredentialFromActivityResult(true,
new ActivityResult(ChooseLockPattern.RESULT_FINISHED, intent)); new ActivityResult(ChooseLockPattern.RESULT_FINISHED, intent));
assertThat(ret).isTrue(); assertThat(ret).isTrue();
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
final Bundle extras = mAutoCredentialViewModel.getCredentialIntentExtra(); final Bundle extras = mViewModel.createCredentialIntentExtra();
assertThat(extras.getInt(EXTRA_KEY_SENSOR_ID)).isEqualTo(newSensorId); assertThat(extras.getInt(EXTRA_KEY_SENSOR_ID)).isEqualTo(newSensorId);
assertThat(extras.getLong(EXTRA_KEY_CHALLENGE)).isEqualTo(newChallenge); assertThat(extras.getLong(EXTRA_KEY_CHALLENGE)).isEqualTo(newChallenge);
assertThat(CredentialModel.isValidToken(extras.getByteArray(EXTRA_KEY_CHALLENGE_TOKEN))) assertThat(isValidToken(extras.getByteArray(EXTRA_KEY_CHALLENGE_TOKEN))).isTrue();
.isTrue(); assertThat(isValidGkPwHandle(extras.getLong(EXTRA_KEY_GK_PW_HANDLE))).isFalse();
assertThat(CredentialModel.isValidGkPwHandle(extras.getLong(EXTRA_KEY_GK_PW_HANDLE)))
.isFalse();
assertThat(mChallengeGenerator.mCallbackRunCount).isEqualTo(1); assertThat(mChallengeGenerator.mCallbackRunCount).isEqualTo(1);
} }
@@ -411,7 +404,7 @@ public class AutoCredentialViewModelTest {
@Test @Test
public void testCheckNewCredentialFromActivityResult_validConfirmLock() { public void testCheckNewCredentialFromActivityResult_validConfirmLock() {
final int userId = 109; final int userId = 109;
mAutoCredentialViewModel.setCredentialModel(null, mViewModel.setCredentialModel(null,
new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId))); new Intent().putExtras(newInvalidChallengeCredentialIntentExtras(userId)));
when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn( when(mLockPatternUtils.getActivePasswordQuality(userId)).thenReturn(
PASSWORD_QUALITY_SOMETHING); PASSWORD_QUALITY_SOMETHING);
@@ -424,20 +417,17 @@ public class AutoCredentialViewModelTest {
.thenReturn(newGoodCredential(gkPwHandle, new byte[] { 1 })); .thenReturn(newGoodCredential(gkPwHandle, new byte[] { 1 }));
// Run checkNewCredentialFromActivityResult() // Run checkNewCredentialFromActivityResult()
final Intent intent = new Intent().putExtra(ChooseLockSettingsHelper.EXTRA_KEY_GK_PW_HANDLE, final Intent intent = new Intent().putExtra(EXTRA_KEY_GK_PW_HANDLE, gkPwHandle);
gkPwHandle); final boolean ret = mViewModel.checkNewCredentialFromActivityResult(false,
final boolean ret = mAutoCredentialViewModel.checkNewCredentialFromActivityResult(false,
new ActivityResult(Activity.RESULT_OK, intent)); new ActivityResult(Activity.RESULT_OK, intent));
assertThat(ret).isTrue(); assertThat(ret).isTrue();
assertThat(mAutoCredentialViewModel.getGenerateChallengeFailLiveData().getValue()).isNull(); assertThat(mViewModel.getGenerateChallengeFailedLiveData().getValue()).isNull();
final Bundle extras = mAutoCredentialViewModel.getCredentialIntentExtra(); final Bundle extras = mViewModel.createCredentialIntentExtra();
assertThat(extras.getInt(EXTRA_KEY_SENSOR_ID)).isEqualTo(newSensorId); assertThat(extras.getInt(EXTRA_KEY_SENSOR_ID)).isEqualTo(newSensorId);
assertThat(extras.getLong(EXTRA_KEY_CHALLENGE)).isEqualTo(newChallenge); assertThat(extras.getLong(EXTRA_KEY_CHALLENGE)).isEqualTo(newChallenge);
assertThat(CredentialModel.isValidToken(extras.getByteArray(EXTRA_KEY_CHALLENGE_TOKEN))) assertThat(isValidToken(extras.getByteArray(EXTRA_KEY_CHALLENGE_TOKEN))).isTrue();
.isTrue(); assertThat(isValidGkPwHandle(extras.getLong(EXTRA_KEY_GK_PW_HANDLE))).isFalse();
assertThat(CredentialModel.isValidGkPwHandle(extras.getLong(EXTRA_KEY_GK_PW_HANDLE)))
.isFalse();
assertThat(mChallengeGenerator.mCallbackRunCount).isEqualTo(1); assertThat(mChallengeGenerator.mCallbackRunCount).isEqualTo(1);
} }

View File

@@ -215,11 +215,13 @@ public class FingerprintEnrollIntroViewModelTest {
@Test @Test
public void testSetHasScrolledToBottom() { public void testSetHasScrolledToBottom() {
mViewModel.setHasScrolledToBottom(); mViewModel.setHasScrolledToBottom(true);
FingerprintEnrollIntroStatus status = mViewModel.getPageStatusLiveData().getValue(); FingerprintEnrollIntroStatus status = mViewModel.getPageStatusLiveData().getValue();
assertThat(status.hasScrollToBottom()).isEqualTo(true); assertThat(status.hasScrollToBottom()).isEqualTo(true);
mViewModel.setHasScrolledToBottom(false);
status = mViewModel.getPageStatusLiveData().getValue();
assertThat(status.hasScrollToBottom()).isEqualTo(false);
} }
@Test @Test