@Test public void shippingInfoWidget_whenErrorOccurs_errorsRenderInternationalized() { mCountryAutoCompleteTextView.updateUIForCountryEntered(Locale.US.getDisplayCountry()); mShippingInfoWidget.validateAllFields(); assertEquals(mStateTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_state_required)); assertEquals(mPostalCodeTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_zip_invalid)); mCountryAutoCompleteTextView.updateUIForCountryEntered(Locale.UK.getDisplayCountry()); mShippingInfoWidget.validateAllFields(); assertEquals(mStateTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_county_required)); assertEquals(mPostalCodeTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_postcode_invalid)); mCountryAutoCompleteTextView.updateUIForCountryEntered( Locale.CANADA.getDisplayCountry()); mShippingInfoWidget.validateAllFields(); assertEquals(mStateTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_province_required)); assertEquals(mPostalCodeTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_postal_code_invalid)); mCountryAutoCompleteTextView.updateUIForCountryEntered(new Locale("", mNoPostalCodeCountry).getDisplayCountry()); mShippingInfoWidget.validateAllFields(); assertEquals(mStateTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_region_generic_required)); }
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; }