@Test public void shippingInfoWidget_whenValidationFails_errorTextRenders() { mCountryAutoCompleteTextView.updateUIForCountryEntered(Locale.US.getDisplayCountry()); mShippingInfoWidget.validateAllFields(); assertTrue(mAddressLine1TextInputLayout.isErrorEnabled()); assertTrue(mCityTextInputLayout.isErrorEnabled()); assertTrue(mNameTextInputLayout.isErrorEnabled()); assertTrue(mPostalCodeTextInputLayout.isErrorEnabled()); assertTrue(mStateTextInputLayout.isErrorEnabled()); mAddressLine1EditText.setText("123 Fake Address"); mNameEditText.setText("Fake Name"); mCityEditText.setText("Fake City"); mPostalEditText.setText("12345"); mStateEditText.setText("CA"); mShippingInfoWidget.validateAllFields(); assertFalse(mAddressLine1TextInputLayout.isErrorEnabled()); assertFalse(mCityTextInputLayout.isErrorEnabled()); assertFalse(mNameTextInputLayout.isErrorEnabled()); assertFalse(mPostalCodeTextInputLayout.isErrorEnabled()); assertFalse(mStateTextInputLayout.isErrorEnabled()); mPostalEditText.setText(""); mShippingInfoWidget.validateAllFields(); assertTrue(mPostalCodeTextInputLayout.isErrorEnabled()); mCountryAutoCompleteTextView.updateUIForCountryEntered(new Locale("", mNoPostalCodeCountry).getDisplayCountry()); mShippingInfoWidget.validateAllFields(); assertFalse(mStateTextInputLayout.isErrorEnabled()); }
public boolean validateAllFields() { 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.isUSZipCodeValid(mPostalCodeEditText.getText().toString().trim()); } else if (countrySelected.equals(Locale.CANADA.getCountry())) { postalCodeValid = CountryUtils.isUSZipCodeValid(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); boolean requiredNameEmpty = mNameEditText.getText().toString().isEmpty(); mNameEditText.setShouldShowError(requiredNameEmpty); boolean requiredStateEmpty = mStateEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(STATE_FIELD) && !mHiddenShippingInfoFields.contains(STATE_FIELD); mStateEditText.setShouldShowError(requiredStateEmpty); boolean requiredPhoneNumberEmpty = mPhoneNumberEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(PHONE_FIELD) && !mHiddenShippingInfoFields.contains(PHONE_FIELD); mPhoneNumberEditText.setShouldShowError(requiredPhoneNumberEmpty); return postalCodeValid && !requiredAddressLine1Empty && !requiredCityEmpty && !requiredStateEmpty && !requiredNameEmpty && !requiredPhoneNumberEmpty; }
ShippingInfoWidget extends LinearLayout { public boolean validateAllFields() { 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.isUSZipCodeValid(mPostalCodeEditText.getText().toString().trim()); } else if (countrySelected.equals(Locale.CANADA.getCountry())) { postalCodeValid = CountryUtils.isUSZipCodeValid(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); boolean requiredNameEmpty = mNameEditText.getText().toString().isEmpty(); mNameEditText.setShouldShowError(requiredNameEmpty); boolean requiredStateEmpty = mStateEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(STATE_FIELD) && !mHiddenShippingInfoFields.contains(STATE_FIELD); mStateEditText.setShouldShowError(requiredStateEmpty); boolean requiredPhoneNumberEmpty = mPhoneNumberEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(PHONE_FIELD) && !mHiddenShippingInfoFields.contains(PHONE_FIELD); mPhoneNumberEditText.setShouldShowError(requiredPhoneNumberEmpty); return postalCodeValid && !requiredAddressLine1Empty && !requiredCityEmpty && !requiredStateEmpty && !requiredNameEmpty && !requiredPhoneNumberEmpty; } }
ShippingInfoWidget extends LinearLayout { public boolean validateAllFields() { 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.isUSZipCodeValid(mPostalCodeEditText.getText().toString().trim()); } else if (countrySelected.equals(Locale.CANADA.getCountry())) { postalCodeValid = CountryUtils.isUSZipCodeValid(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); boolean requiredNameEmpty = mNameEditText.getText().toString().isEmpty(); mNameEditText.setShouldShowError(requiredNameEmpty); boolean requiredStateEmpty = mStateEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(STATE_FIELD) && !mHiddenShippingInfoFields.contains(STATE_FIELD); mStateEditText.setShouldShowError(requiredStateEmpty); boolean requiredPhoneNumberEmpty = mPhoneNumberEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(PHONE_FIELD) && !mHiddenShippingInfoFields.contains(PHONE_FIELD); mPhoneNumberEditText.setShouldShowError(requiredPhoneNumberEmpty); return postalCodeValid && !requiredAddressLine1Empty && !requiredCityEmpty && !requiredStateEmpty && !requiredNameEmpty && !requiredPhoneNumberEmpty; } ShippingInfoWidget(Context context);  ShippingInfoWidget(Context context, AttributeSet attrs);  ShippingInfoWidget(Context context, AttributeSet attrs, int defStyleAttr); }
ShippingInfoWidget extends LinearLayout { public boolean validateAllFields() { 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.isUSZipCodeValid(mPostalCodeEditText.getText().toString().trim()); } else if (countrySelected.equals(Locale.CANADA.getCountry())) { postalCodeValid = CountryUtils.isUSZipCodeValid(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); boolean requiredNameEmpty = mNameEditText.getText().toString().isEmpty(); mNameEditText.setShouldShowError(requiredNameEmpty); boolean requiredStateEmpty = mStateEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(STATE_FIELD) && !mHiddenShippingInfoFields.contains(STATE_FIELD); mStateEditText.setShouldShowError(requiredStateEmpty); boolean requiredPhoneNumberEmpty = mPhoneNumberEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(PHONE_FIELD) && !mHiddenShippingInfoFields.contains(PHONE_FIELD); mPhoneNumberEditText.setShouldShowError(requiredPhoneNumberEmpty); return postalCodeValid && !requiredAddressLine1Empty && !requiredCityEmpty && !requiredStateEmpty && !requiredNameEmpty && !requiredPhoneNumberEmpty; } ShippingInfoWidget(Context context);  ShippingInfoWidget(Context context, AttributeSet attrs);  ShippingInfoWidget(Context context, AttributeSet attrs, int defStyleAttr); void setOptionalFields(@Nullable List<String> optionalAddressFields); void setHiddenFields(@Nullable List<String> hiddenAddressFields); ShippingInformation getShippingInformation(); void populateShippingInfo(@Nullable ShippingInformation shippingInformation); boolean validateAllFields(); }
ShippingInfoWidget extends LinearLayout { public boolean validateAllFields() { 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.isUSZipCodeValid(mPostalCodeEditText.getText().toString().trim()); } else if (countrySelected.equals(Locale.CANADA.getCountry())) { postalCodeValid = CountryUtils.isUSZipCodeValid(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); boolean requiredNameEmpty = mNameEditText.getText().toString().isEmpty(); mNameEditText.setShouldShowError(requiredNameEmpty); boolean requiredStateEmpty = mStateEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(STATE_FIELD) && !mHiddenShippingInfoFields.contains(STATE_FIELD); mStateEditText.setShouldShowError(requiredStateEmpty); boolean requiredPhoneNumberEmpty = mPhoneNumberEditText.getText().toString().isEmpty() && !mOptionalShippingInfoFields.contains(PHONE_FIELD) && !mHiddenShippingInfoFields.contains(PHONE_FIELD); mPhoneNumberEditText.setShouldShowError(requiredPhoneNumberEmpty); return postalCodeValid && !requiredAddressLine1Empty && !requiredCityEmpty && !requiredStateEmpty && !requiredNameEmpty && !requiredPhoneNumberEmpty; } ShippingInfoWidget(Context context);  ShippingInfoWidget(Context context, AttributeSet attrs);  ShippingInfoWidget(Context context, AttributeSet attrs, int defStyleAttr); void setOptionalFields(@Nullable List<String> optionalAddressFields); void setHiddenFields(@Nullable List<String> hiddenAddressFields); ShippingInformation getShippingInformation(); void populateShippingInfo(@Nullable ShippingInformation shippingInformation); boolean validateAllFields(); static final String ADDRESS_LINE_ONE_FIELD; static final String ADDRESS_LINE_TWO_FIELD; static final String CITY_FIELD; static final String POSTAL_CODE_FIELD; static final String STATE_FIELD; static final String PHONE_FIELD; }