@Override public void onDeleteEmpty() { String fieldText = backUpTarget.getText().toString(); if (fieldText.length() > 1) { backUpTarget.setText( fieldText.substring(0, fieldText.length() - 1)); } backUpTarget.requestFocus(); backUpTarget.setSelection(backUpTarget.length()); } }
private void initView() { mHandler = new Handler(); listenForTextChanges(); listenForDeleteEmpty(); determineDefaultErrorColor(); mCachedColorStateList = getTextColors(); }
/** * Override of {@link View#isEnabled()} that returns {@code true} only * if all three sub-controls are enabled. * * @return {@code true} if the card number field, expiry field, and cvc field are enabled, * {@code false} otherwise */ @Override public boolean isEnabled() { return mCardNumberEditText.isEnabled() && mExpiryDateEditText.isEnabled() && mCvcNumberEditText.isEnabled(); }
@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); } });
private boolean isCvcLengthValid() { String cvcValue = mCvcNumberEditText.getText().toString().trim(); int cvcLength = cvcValue.length(); if (mIsAmEx && cvcLength == Card.CVC_LENGTH_AMERICAN_EXPRESS) { return true; } if (cvcLength == Card.CVC_LENGTH_COMMON) { return true; } return false; }
mCvcNumberEditText.setErrorColor(mErrorColorInt); new BackUpFieldDeleteListener(mCardNumberEditText)); mCvcNumberEditText.setDeleteEmptyListener( new BackUpFieldDeleteListener(mExpiryDateEditText)); mCvcNumberEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { mCvcNumberEditText.setAfterTextChangedListener( new StripeEditText.AfterTextChangedListener() { @Override
@Test public void setErrorColor_whenInError_overridesDefault() { // By default, the text color in this test activity is a light theme @ColorInt int blueError = 0x0000ff; mEditText.setErrorColor(blueError); mEditText.setShouldShowError(true); int currentColorInt = mEditText.getTextColors().getDefaultColor(); assertEquals(blueError, currentColorInt); } }
@Before public void setup() { MockitoAnnotations.initMocks(this); mActivityController = Robolectric.buildActivity(CardInputTestActivity.class) .create().start(); // Note that the CVC EditText is a StripeEditText mEditText = mActivityController.get().getCvcEditText(); mEditText.setText(""); mEditText.setDeleteEmptyListener(mDeleteEmptyListener); mEditText.setAfterTextChangedListener(mAfterTextChangedListener); }
@Test public void onDeleteFromCvcDate_whenEmpty_shiftsFocusToExpiryAndDeletesDigit() { // This test will be invalid if run between 2080 and 2112. Please update the code. assertTrue(Calendar.getInstance().get(Calendar.YEAR) < 2080); mCardInputWidget.setCardInputListener(mCardInputListener); mCardNumberEditText.setText(VALID_VISA_WITH_SPACES); verify(mCardInputListener).onCardComplete(); verify(mCardInputListener).onFocusChange(FOCUS_EXPIRY); mExpiryEditText.append("12"); mExpiryEditText.append("79"); verify(mCardInputListener).onExpirationComplete(); verify(mCardInputListener).onFocusChange(FOCUS_CVC); assertTrue(mCvcEditText.hasFocus()); // Clearing already-verified data. reset(mCardInputListener); ViewTestUtils.sendDeleteKeyEvent(mCvcEditText); verify(mCardInputListener).onFocusChange(FOCUS_EXPIRY); assertEquals(R.id.et_cvc_number, mOnGlobalFocusChangeListener.getOldFocusId()); assertEquals(R.id.et_expiry_date, mOnGlobalFocusChangeListener.getNewFocusId()); String expectedResult = "12/7"; assertEquals(expectedResult, mExpiryEditText.getText().toString()); assertEquals(expectedResult.length(), mExpiryEditText.getSelectionStart()); }
boolean postalCodeValid = true; String countrySelected = mCountryAutoCompleteTextView.getSelectedCountryCode(); if (mPostalCodeEditText.getText().toString().isEmpty() && (mOptionalShippingInfoFields .contains(POSTAL_CODE_FIELD) || mHiddenShippingInfoFields.contains (POSTAL_CODE_FIELD))) { postalCodeValid = true; } else if (countrySelected.equals(Locale.US.getCountry())) { postalCodeValid = CountryUtils.isUSZipCodeValid(mPostalCodeEditText.getText() .toString().trim()); } else if (countrySelected.equals(Locale.UK.getCountry())) { postalCodeValid = CountryUtils.isUKPostcodeValid(mPostalCodeEditText.getText() .toString().trim()); } else if (countrySelected.equals(Locale.CANADA.getCountry())) { postalCodeValid = CountryUtils.isCanadianPostalCodeValid(mPostalCodeEditText.getText() .toString().trim()); } else if (CountryUtils.doesCountryUsePostalCode(countrySelected)) { postalCodeValid = !mPostalCodeEditText.getText().toString().isEmpty(); mPostalCodeEditText.setShouldShowError(!postalCodeValid); boolean requiredAddressLine1Empty = mAddressEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(ADDRESS_LINE_ONE_FIELD) && !mHiddenShippingInfoFields.contains(ADDRESS_LINE_ONE_FIELD); mAddressEditText.setShouldShowError(requiredAddressLine1Empty); boolean requiredCityEmpty = mCityEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(CITY_FIELD) && !mHiddenShippingInfoFields .contains(CITY_FIELD); mCityEditText.setShouldShowError(requiredCityEmpty);
@Test public void onDeleteFromCvcDate_whenNotEmpty_doesNotShiftFocusOrDeleteEntry() { // This test will be invalid if run between 2080 and 2112. Please update the code. assertTrue(Calendar.getInstance().get(Calendar.YEAR) < 2080); mCardNumberEditText.setText(VALID_AMEX_WITH_SPACES); mExpiryEditText.append("12"); mExpiryEditText.append("79"); assertTrue(mCvcEditText.hasFocus()); mCvcEditText.append("123"); ViewTestUtils.sendDeleteKeyEvent(mCvcEditText); assertTrue(mCvcEditText.hasFocus()); assertEquals("12/79", mExpiryEditText.getText().toString()); }
/** * Clear all entered data and hide all error messages. */ public void clear() { mCardNumberEditText.setText(""); mExpiryDateEditText.setText(""); mCvcEditText.setText(""); mPostalCodeEditText.setText(""); mCardNumberEditText.setShouldShowError(false); mExpiryDateEditText.setShouldShowError(false); mCvcEditText.setShouldShowError(false); mPostalCodeEditText.setShouldShowError(false); updateBrand(Card.UNKNOWN); }
/** * Clear all text fields in the CardInputWidget. */ public void clear() { if (mCardNumberEditText.hasFocus() || mExpiryDateEditText.hasFocus() || mCvcNumberEditText.hasFocus() || this.hasFocus()) { mCardNumberEditText.requestFocus(); } mCvcNumberEditText.setText(""); mExpiryDateEditText.setText(""); mCardNumberEditText.setText(""); }
@Test public void deleteWhenEmpty_fromPostalCode_shiftsToCvc() { mCardMultilineWidget.setCardInputListener(mFullCardListener); mFullGroup.cardNumberEditText.setText(VALID_DINERS_CLUB_WITH_SPACES); mFullGroup.expiryDateEditText.append("12"); mFullGroup.expiryDateEditText.append("50"); mFullGroup.cvcEditText.append("123"); reset(mFullCardListener); ViewTestUtils.sendDeleteKeyEvent(mFullGroup.postalCodeEditText); verify(mFullCardListener, times(1)).onFocusChange(FOCUS_CVC); assertEquals("12", mFullGroup.cvcEditText.getText().toString()); }
@Test public void onDeleteFromExpiryDate_whenNotEmpty_doesNotShiftFocusOrDeleteDigit() { mCardNumberEditText.setText(VALID_AMEX_WITH_SPACES); assertTrue(mExpiryEditText.hasFocus()); mExpiryEditText.append("1"); ViewTestUtils.sendDeleteKeyEvent(mExpiryEditText); assertTrue(mExpiryEditText.hasFocus()); assertEquals(VALID_AMEX_WITH_SPACES, mCardNumberEditText.getText().toString()); }
@Override public void onExpiryDateComplete() { mCvcNumberEditText.requestFocus(); if (mCardInputListener != null) { mCardInputListener.onExpirationComplete(); } } });
@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 deleteText_whenSelectionAtBeginningButLengthNonZero_doesNotCallListener() { mEditText.append("12"); verify(mAfterTextChangedListener).onTextChanged("12"); mEditText.setSelection(0); ViewTestUtils.sendDeleteKeyEvent(mEditText); verifyZeroInteractions(mDeleteEmptyListener); verifyNoMoreInteractions(mAfterTextChangedListener); }
@Override public void onTextChanged(String text) { if (isPostalCodeMaximalLength(true, text) && mCardInputListener != null) { mCardInputListener.onPostalCodeComplete(); } mPostalCodeEditText.setShouldShowError(false); } });
private void determineDefaultErrorColor() { mCachedColorStateList = getTextColors(); int color = mCachedColorStateList.getDefaultColor(); if (isColorDark(color)) { // Note: if the _text_ color is dark, then this is a // light theme, and vice-versa. mDefaultErrorColorResId = R.color.error_text_light_theme; } else { mDefaultErrorColorResId = R.color.error_text_dark_theme; } }