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();
@Before public void setup() { MockitoAnnotations.initMocks(this); Locale.setDefault(Locale.US); ActivityController<ShippingInfoTestActivity> activityController = Robolectric.buildActivity(ShippingInfoTestActivity.class).create().start(); List<String> countries = new ArrayList<>(CountryUtils.getCountryNameToCodeMap().keySet()); mCountryAdapter = new CountryAdapter(activityController.get(), countries); }
private void renderCountrySpecificLabels(String countrySelected) { if (countrySelected.equals(Locale.US.getCountry())) { renderUSForm(); } else if (countrySelected.equals(Locale.UK.getCountry())) { renderGreatBritainForm(); } else if (countrySelected.equals(Locale.CANADA.getCountry())) { renderCanadianForm(); } else { renderInternationalForm(); } if (CountryUtils.doesCountryUsePostalCode(countrySelected) && !mHiddenShippingInfoFields .contains(POSTAL_CODE_FIELD)) { mPostalCodeTextInputLayout.setVisibility(VISIBLE); } else { mPostalCodeTextInputLayout.setVisibility(GONE); } }
@Test public void canadianPostalCodeTest() { assertTrue(CountryUtils.isCanadianPostalCodeValid("K1A 0B1")); assertTrue(CountryUtils.isCanadianPostalCodeValid("B1Z 0B9")); assertFalse(CountryUtils.isCanadianPostalCodeValid("K1A 0D1")); assertFalse(CountryUtils.isCanadianPostalCodeValid("94107")); assertFalse(CountryUtils.isCanadianPostalCodeValid("94107-1234")); assertFalse(CountryUtils.isCanadianPostalCodeValid("W1A 0B1")); assertFalse(CountryUtils.isCanadianPostalCodeValid("123")); assertFalse(CountryUtils.isCanadianPostalCodeValid("")); }
@Test public void usZipCodeTest() { assertTrue(CountryUtils.isUSZipCodeValid("94107")); assertTrue(CountryUtils.isUSZipCodeValid("94107-1234")); assertFalse(CountryUtils.isUSZipCodeValid("941071234")); assertFalse(CountryUtils.isUSZipCodeValid("9410a1234")); assertFalse(CountryUtils.isUSZipCodeValid("94107-")); assertFalse(CountryUtils.isUSZipCodeValid("9410&")); assertFalse(CountryUtils.isUSZipCodeValid("K1A 0B1")); assertFalse(CountryUtils.isUSZipCodeValid("")); }
@Test public void ukPostalCodeTest() { assertTrue(CountryUtils.isUKPostcodeValid("L1 8JQ")); assertTrue(CountryUtils.isUKPostcodeValid("GU16 7HF")); assertTrue(CountryUtils.isUKPostcodeValid("PO16 7GZ")); assertFalse(CountryUtils.isUKPostcodeValid("94107")); assertFalse(CountryUtils.isUKPostcodeValid("94107-1234")); assertFalse(CountryUtils.isUKPostcodeValid("!1A 0B1")); assertFalse(CountryUtils.isUKPostcodeValid("Z1A 0B1")); assertFalse(CountryUtils.isUKPostcodeValid("123")); } }
private void initView() { inflate(getContext(), R.layout.country_autocomplete_textview, this); mCountryAutocomplete = findViewById(R.id.autocomplete_country_cat); mCountryNameToCode = CountryUtils.getCountryNameToCodeMap(); final ArrayAdapter countryAdapter = new CountryAdapter(getContext(), new ArrayList<>(mCountryNameToCode.keySet())); mCountryAutocomplete.setThreshold(0); mCountryAutocomplete.setAdapter(countryAdapter); mCountryAutocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { String countryEntered = mCountryAutocomplete.getText().toString(); updateUIForCountryEntered(countryEntered); } }); String defaultCountryEntered = (String) countryAdapter.getItem(0); updateUIForCountryEntered(defaultCountryEntered); mCountryAutocomplete.setText(defaultCountryEntered); mCountryAutocomplete.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean focused) { String countryEntered = mCountryAutocomplete.getText().toString(); if (focused) { mCountryAutocomplete.showDropDown(); } else { updateUIForCountryEntered(countryEntered); } } }); }
@Test public void postalCodeCountryTest() { assertTrue(CountryUtils.doesCountryUsePostalCode("US")); assertTrue(CountryUtils.doesCountryUsePostalCode("UK")); assertTrue(CountryUtils.doesCountryUsePostalCode("CA")); assertFalse(CountryUtils.doesCountryUsePostalCode("DM")); }