/** * Determines whether or not the icon should show the card brand instead of the * CVC helper icon. * * @param brand the {@link CardBrand} in question, used for determining max length * @param cvcHasFocus {@code true} if the CVC entry field has focus, {@code false} otherwise * @param cvcText the current content of {@link #mCvcNumberEditText} * @return {@code true} if we should show the brand of the card, or {@code false} if we * should show the CVC helper icon instead */ @VisibleForTesting static boolean shouldIconShowBrand( @NonNull @Card.CardBrand String brand, boolean cvcHasFocus, @Nullable String cvcText) { if (!cvcHasFocus) { return true; } return ViewUtils.isCvcMaximalLength(brand, cvcText); }
private void flipToCvcIconIfNotFinished() { if (ViewUtils.isCvcMaximalLength(mCardBrand, mCvcEditText.getText().toString())) { return; } @DrawableRes int resourceId = Card.AMERICAN_EXPRESS.equals(mCardBrand) ? R.drawable.ic_cvc_amex : R.drawable.ic_cvc; updateDrawable(resourceId, true); }
@Test public void isCvcMaximalLength_whenTooManyDigits_returnsFalse() { assertFalse(ViewUtils.isCvcMaximalLength(Card.AMERICAN_EXPRESS, "12345")); assertFalse(ViewUtils.isCvcMaximalLength(Card.VISA, "1234")); assertFalse(ViewUtils.isCvcMaximalLength(Card.MASTERCARD, "123456")); assertFalse(ViewUtils.isCvcMaximalLength(Card.DINERS_CLUB, "1234567")); assertFalse(ViewUtils.isCvcMaximalLength(Card.DISCOVER, "12345678")); assertFalse(ViewUtils.isCvcMaximalLength(Card.JCB, "123456789012345")); }
@Test public void isCvcMaximalLength_whenNotEnoughDigits_returnsFalse() { assertFalse(ViewUtils.isCvcMaximalLength(Card.AMERICAN_EXPRESS, "")); assertFalse(ViewUtils.isCvcMaximalLength(Card.VISA, "1")); assertFalse(ViewUtils.isCvcMaximalLength(Card.MASTERCARD, "12")); assertFalse(ViewUtils.isCvcMaximalLength(Card.DINERS_CLUB, "")); assertFalse(ViewUtils.isCvcMaximalLength(Card.DISCOVER, "8")); assertFalse(ViewUtils.isCvcMaximalLength(Card.JCB, "1")); }
@Test public void isCvcMaximalLength_whenThreeDigitsAndNotAmEx_returnsTrue() { assertTrue(ViewUtils.isCvcMaximalLength(Card.VISA, "123")); assertTrue(ViewUtils.isCvcMaximalLength(Card.MASTERCARD, "345")); assertTrue(ViewUtils.isCvcMaximalLength(Card.JCB, "678")); assertTrue(ViewUtils.isCvcMaximalLength(Card.DINERS_CLUB, "910")); assertTrue(ViewUtils.isCvcMaximalLength(Card.DISCOVER, "234")); assertTrue(ViewUtils.isCvcMaximalLength(Card.UNKNOWN, "333")); }
@Override public void onTextChanged(String text) { if (ViewUtils.isCvcMaximalLength(mCardBrand, text)) { updateBrand(mCardBrand); if (mShouldShowPostalCode) { mPostalCodeEditText.requestFocus(); } if (mCardInputListener != null) { mCardInputListener.onCvcComplete(); } } else { flipToCvcIconIfNotFinished(); } mCvcEditText.setShouldShowError(false); } });
@Override public void onTextChanged(String text) { if (mCardInputListener != null && ViewUtils.isCvcMaximalLength (mCardNumberEditText.getCardBrand(), text)) { mCardInputListener.onCvcComplete(); } updateIconCvc(mCardNumberEditText.getCardBrand(), mCvcNumberEditText.hasFocus(), text); } });
@Test public void isCvcMaximalLength_whenWhitespaceAndNotEnoughDigits_returnsFalse() { assertFalse(ViewUtils.isCvcMaximalLength(Card.AMERICAN_EXPRESS, " ")); assertFalse(ViewUtils.isCvcMaximalLength(Card.VISA, " 1")); }
@Test public void isCvcMaximalLength_whenFourDigitsAndIsAmEx_returnsTrue() { assertTrue(ViewUtils.isCvcMaximalLength(Card.AMERICAN_EXPRESS, "1234")); }
@Test public void isCvcMaximalLength_whenThreeDigitsAndIsAmEx_returnsFalse() { assertFalse(ViewUtils.isCvcMaximalLength(Card.AMERICAN_EXPRESS, "123")); }
@Test public void isCvcMaximalLength_whenNull_returnsFalse() { assertFalse(ViewUtils.isCvcMaximalLength(Card.AMERICAN_EXPRESS, null)); }