diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-04 22:54:54 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-04 22:54:54 +0000 |
commit | 0623cfe447b581a9062a7c570536eef075d0ba30 (patch) | |
tree | 2770a30b487f08886ad603c561788e7d82f40431 /third_party | |
parent | 3d7a36587a8f78ec4cdb75067d8f00c9b6834fcc (diff) | |
download | chromium_src-0623cfe447b581a9062a7c570536eef075d0ba30.zip chromium_src-0623cfe447b581a9062a7c570536eef075d0ba30.tar.gz chromium_src-0623cfe447b581a9062a7c570536eef075d0ba30.tar.bz2 |
rAc: Handle case where validation rules are not yet downloaded
Also, mark required fields regardless of whether full validation is possible.
BUG=340365
R=dbeam@chromium.org
Review URL: https://codereview.chromium.org/137223005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@248799 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party')
3 files changed, 102 insertions, 19 deletions
diff --git a/third_party/libaddressinput/chromium/cpp/res/messages.grdp b/third_party/libaddressinput/chromium/cpp/res/messages.grdp index a09c6d9..ce7b442 100644 --- a/third_party/libaddressinput/chromium/cpp/res/messages.grdp +++ b/third_party/libaddressinput/chromium/cpp/res/messages.grdp @@ -116,7 +116,7 @@ https://code.google.com/p/libaddressinput/source/browse/trunk/java/res/values/ad <message name="IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD" desc="Message to be shown when a required field is empty"> - Required + Field is required </message> <message name="IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY" diff --git a/third_party/libaddressinput/chromium/cpp/src/address_validator.cc b/third_party/libaddressinput/chromium/cpp/src/address_validator.cc index b08dc83..0657d30 100644 --- a/third_party/libaddressinput/chromium/cpp/src/address_validator.cc +++ b/third_party/libaddressinput/chromium/cpp/src/address_validator.cc @@ -32,6 +32,7 @@ #include "country_rules_aggregator.h" #include "grit/libaddressinput_strings.h" +#include "region_data_constants.h" #include "retriever.h" #include "rule.h" #include "ruleset.h" @@ -111,7 +112,17 @@ class AddressValidatorImpl : public AddressValidator { AddressProblems* problems) const { std::map<std::string, const Ruleset*>::const_iterator ruleset_it = rules_.find(address.country_code); + + // We can still validate the required fields even if the full ruleset isn't + // ready. if (ruleset_it == rules_.end()) { + Rule rule; + rule.CopyFrom(Rule::GetDefault()); + if (rule.ParseSerializedRule( + RegionDataConstants::GetRegionData(address.country_code))) { + EnforceRequiredFields(rule, address, filter, problems); + } + return loading_rules_.find(address.country_code) != loading_rules_.end() ? RULES_NOT_READY : RULES_UNAVAILABLE; @@ -121,24 +132,7 @@ class AddressValidatorImpl : public AddressValidator { assert(ruleset != NULL); const Rule& country_rule = ruleset->GetLanguageCodeRule(address.language_code); - - // Validate required fields. - for (std::vector<AddressField>::const_iterator - field_it = country_rule.GetRequired().begin(); - field_it != country_rule.GetRequired().end(); - ++field_it) { - bool field_empty = *field_it != STREET_ADDRESS - ? address.GetFieldValue(*field_it).empty() - : IsEmptyStreetAddress(address.address_lines); - if (field_empty && - FilterAllows( - filter, *field_it, AddressProblem::MISSING_REQUIRED_FIELD)) { - problems->push_back(AddressProblem( - *field_it, - AddressProblem::MISSING_REQUIRED_FIELD, - IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD)); - } - } + EnforceRequiredFields(country_rule, address, filter, problems); // Validate general postal code format. A country-level rule specifies the // regular expression for the whole postal code. @@ -218,6 +212,29 @@ class AddressValidatorImpl : public AddressValidator { } } + // Adds problems for just the required fields portion of |country_rule|. + void EnforceRequiredFields(const Rule& country_rule, + const AddressData& address, + const AddressProblemFilter& filter, + AddressProblems* problems) const { + for (std::vector<AddressField>::const_iterator + field_it = country_rule.GetRequired().begin(); + field_it != country_rule.GetRequired().end(); + ++field_it) { + bool field_empty = *field_it != STREET_ADDRESS + ? address.GetFieldValue(*field_it).empty() + : IsEmptyStreetAddress(address.address_lines); + if (field_empty && + FilterAllows( + filter, *field_it, AddressProblem::MISSING_REQUIRED_FIELD)) { + problems->push_back(AddressProblem( + *field_it, + AddressProblem::MISSING_REQUIRED_FIELD, + IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD)); + } + } + } + // Loads the ruleset for a country code. CountryRulesAggregator aggregator_; diff --git a/third_party/libaddressinput/chromium/cpp/test/address_validator_test.cc b/third_party/libaddressinput/chromium/cpp/test/address_validator_test.cc index 258b488..519517e 100644 --- a/third_party/libaddressinput/chromium/cpp/test/address_validator_test.cc +++ b/third_party/libaddressinput/chromium/cpp/test/address_validator_test.cc @@ -60,5 +60,71 @@ TEST_F(AddressValidatorTest, EmptyAddressNoFatalFailure) { validator_->ValidateAddress(address, AddressProblemFilter(), &problems)); } +TEST_F(AddressValidatorTest, BasicValidation) { + // US rules should always be available, even though this load call fails. + validator_->LoadRules("US"); + AddressData address; + address.country_code = "US"; + address.language_code = "en"; + address.administrative_area = "TX"; + address.locality = "Paris"; + address.postal_code = "75461"; + address.address_lines.push_back("123 Main St"); + AddressProblems problems; + EXPECT_EQ( + AddressValidator::SUCCESS, + validator_->ValidateAddress(address, AddressProblemFilter(), &problems)); + + // TODO(estade): this should be EXPECT_TRUE(problems.empty()). Postal code + // validation is broken at the moment. + EXPECT_EQ(1U, problems.size()); +} + +TEST_F(AddressValidatorTest, BasicValidationFailure) { + // US rules should always be available, even though this load call fails. + validator_->LoadRules("US"); + AddressData address; + address.country_code = "US"; + address.language_code = "en"; + address.administrative_area = "XT"; + address.locality = "Paris"; + address.postal_code = "75461"; + address.address_lines.push_back("123 Main St"); + AddressProblems problems; + EXPECT_EQ( + AddressValidator::SUCCESS, + validator_->ValidateAddress(address, AddressProblemFilter(), &problems)); + + ASSERT_EQ(1U, problems.size()); + EXPECT_EQ(AddressProblem::UNKNOWN_VALUE, problems[0].type); + EXPECT_EQ(ADMIN_AREA, problems[0].field); +} + +TEST_F(AddressValidatorTest, ValidationFailureWithNoRulesPresent) { + // The fake downloader/fake storage will fail to get these rules. + validator_->LoadRules("CA"); + AddressData address; + address.country_code = "CA"; + address.language_code = "en"; + address.administrative_area = "Never never land"; + address.locality = "grassy knoll"; + address.postal_code = "1234"; + address.address_lines.push_back("123 Main St"); + AddressProblems problems; + EXPECT_EQ( + AddressValidator::RULES_UNAVAILABLE, + validator_->ValidateAddress(address, AddressProblemFilter(), &problems)); + EXPECT_TRUE(problems.empty()); + + // Field requirements are still enforced even if the rules aren't downloaded. + address.administrative_area = ""; + problems.clear(); + EXPECT_EQ( + AddressValidator::RULES_UNAVAILABLE, + validator_->ValidateAddress(address, AddressProblemFilter(), &problems)); + ASSERT_EQ(1U, problems.size()); + EXPECT_EQ(AddressProblem::MISSING_REQUIRED_FIELD, problems[0].type); +} + } // namespace addressinput } // namespace i18n |