/** * Gets a {@link Card} object from the user input, if all fields are valid. If not, returns * {@code null}. * * @return a valid {@link Card} object based on user input, or {@code null} if any field is * invalid */ @Nullable public Card getCard() { if (validateAllFields()) { String cardNumber = mCardNumberEditText.getCardNumber(); int[] cardDate = mExpiryDateEditText.getValidDateFields(); String cvcValue = mCvcEditText.getText().toString(); Card card = new Card(cardNumber, cardDate[0], cardDate[1], cvcValue); if (mShouldShowPostalCode) { card.setAddressZip(mPostalCodeEditText.getText().toString()); } return card.addLoggingToken(CARD_MULTILINE_TOKEN); } return null; }
@Test public void clear_whenFieldsInErrorState_clearsFieldsAndHidesErrors() { // The input date here will be invalid after 2050. Please update the test. assertTrue(Calendar.getInstance().get(Calendar.YEAR) < 2050); String badVisa = VALID_VISA_WITH_SPACES.substring(VALID_VISA_WITH_SPACES.length() - 1); badVisa += 3; // Makes this 4242 4242 4242 4243 mFullGroup.cardNumberEditText.setText(badVisa); mFullGroup.expiryDateEditText.append("01"); mFullGroup.expiryDateEditText.append("11"); mFullGroup.cvcEditText.append("12"); mFullGroup.postalCodeEditText.append("1234"); mCardMultilineWidget.validateAllFields(); assertTrue(mFullGroup.cardNumberEditText.getShouldShowError()); assertTrue(mFullGroup.expiryDateEditText.getShouldShowError()); assertTrue(mFullGroup.cvcEditText.getShouldShowError()); assertTrue(mFullGroup.postalCodeEditText.getShouldShowError()); mCardMultilineWidget.clear(); assertFalse(mFullGroup.cardNumberEditText.getShouldShowError()); assertFalse(mFullGroup.expiryDateEditText.getShouldShowError()); assertFalse(mFullGroup.cvcEditText.getShouldShowError()); assertFalse(mFullGroup.postalCodeEditText.getShouldShowError()); }