Commit 71bf3494 authored by Anthony Vallee-Dubois's avatar Anthony Vallee-Dubois Committed by Commit Bot

[Web Payments] Normalize addresses before selecting region in editor

Bug: 746502
Change-Id: I883596553eb46ccb5b927cd5eb0d98a2beb93607
Reviewed-on: https://chromium-review.googlesource.com/576455Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: default avatarMathieu Perreault <mathp@chromium.org>
Commit-Queue: Mathieu Perreault <mathp@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488497}
parent 4a3ab2ba
...@@ -813,7 +813,8 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestCreditCardEditorTest, ...@@ -813,7 +813,8 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestCreditCardEditorTest,
autofill::ADDRESS_HOME_STREET_ADDRESS); autofill::ADDRESS_HOME_STREET_ADDRESS);
SetEditorTextfieldValue(base::ASCIIToUTF16("BobCity"), SetEditorTextfieldValue(base::ASCIIToUTF16("BobCity"),
autofill::ADDRESS_HOME_CITY); autofill::ADDRESS_HOME_CITY);
SetComboboxValue(base::UTF8ToUTF16("CA"), autofill::ADDRESS_HOME_STATE); SetComboboxValue(base::UTF8ToUTF16("California"),
autofill::ADDRESS_HOME_STATE);
SetEditorTextfieldValue(base::ASCIIToUTF16("BobZip"), SetEditorTextfieldValue(base::ASCIIToUTF16("BobZip"),
autofill::ADDRESS_HOME_ZIP); autofill::ADDRESS_HOME_ZIP);
SetEditorTextfieldValue(base::ASCIIToUTF16("5755555555"), SetEditorTextfieldValue(base::ASCIIToUTF16("5755555555"),
......
...@@ -41,6 +41,33 @@ namespace { ...@@ -41,6 +41,33 @@ namespace {
// http://www.cplusplus.com/reference/string/string/npos // http://www.cplusplus.com/reference/string/string/npos
const size_t kInvalidCountryIndex = static_cast<size_t>(-1); const size_t kInvalidCountryIndex = static_cast<size_t>(-1);
// Used to normalize a profile in place and synchronously from an
// AddressNormalizer that already loaded the normalization rules.
class SynchronousAddressNormalizerDelegate
: public AddressNormalizer::Delegate {
public:
// Doesn't take ownership of |profile_to_normalize| but does modify it.
SynchronousAddressNormalizerDelegate(
autofill::AutofillProfile* profile_to_normalize)
: normalized(false), profile_to_normalize_(profile_to_normalize) {}
~SynchronousAddressNormalizerDelegate() override { DCHECK(normalized); }
private:
void OnAddressNormalized(
const autofill::AutofillProfile& normalized_profile) override {
*profile_to_normalize_ = normalized_profile;
normalized = true;
}
void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override {
normalized = false;
}
bool normalized;
autofill::AutofillProfile* profile_to_normalize_;
};
} // namespace } // namespace
ShippingAddressEditorViewController::ShippingAddressEditorViewController( ShippingAddressEditorViewController::ShippingAddressEditorViewController(
...@@ -89,11 +116,11 @@ base::string16 ShippingAddressEditorViewController::GetInitialValueForType( ...@@ -89,11 +116,11 @@ base::string16 ShippingAddressEditorViewController::GetInitialValueForType(
autofill::l10n::CaseInsensitiveCompare compare; autofill::l10n::CaseInsensitiveCompare compare;
for (const auto& region : region_model_->GetRegions()) { for (const auto& region : region_model_->GetRegions()) {
base::string16 region_code = base::UTF8ToUTF16(region.first); if (compare.StringsEqual(initial_region,
if (compare.StringsEqual(initial_region, region_code) || base::UTF8ToUTF16(region.first)) ||
compare.StringsEqual(initial_region, compare.StringsEqual(initial_region,
base::UTF8ToUTF16(region.second))) { base::UTF8ToUTF16(region.second))) {
return region_code; return base::UTF8ToUTF16(region.second);
} }
} }
...@@ -372,6 +399,17 @@ void ShippingAddressEditorViewController::UpdateEditorFields() { ...@@ -372,6 +399,17 @@ void ShippingAddressEditorViewController::UpdateEditorFields() {
void ShippingAddressEditorViewController::OnDataChanged(bool synchronous) { void ShippingAddressEditorViewController::OnDataChanged(bool synchronous) {
SaveFieldsToProfile(&temporary_profile_, /*ignore_errors*/ true); SaveFieldsToProfile(&temporary_profile_, /*ignore_errors*/ true);
// This function is called after rules are successfully loaded. Because of
// this, normalization is guaranteed to be synchronous. If they're not loaded,
// something went wrong with the network call and normalization can't happen
// (there's no data to go in the region combobox anyways).
std::string country_code = countries_[chosen_country_index_].first;
if (state()->GetAddressNormalizer()->AreRulesLoadedForRegion(country_code)) {
SynchronousAddressNormalizerDelegate delegate(&temporary_profile_);
state()->GetAddressNormalizer()->StartAddressNormalization(
temporary_profile_, country_code, 1, &delegate);
}
UpdateEditorFields(); UpdateEditorFields();
if (synchronous) { if (synchronous) {
UpdateEditorView(); UpdateEditorView();
......
...@@ -297,8 +297,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, AsyncData) { ...@@ -297,8 +297,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, AsyncData) {
SetCommonFields(); SetCommonFields();
SetComboboxValue(base::UTF8ToUTF16("United States"), SetComboboxValue(base::UTF8ToUTF16("United States"),
autofill::ADDRESS_HOME_COUNTRY); autofill::ADDRESS_HOME_COUNTRY);
SetComboboxValue(base::UTF8ToUTF16(kAnyStateCode), SetComboboxValue(base::UTF8ToUTF16(kAnyState), autofill::ADDRESS_HOME_STATE);
autofill::ADDRESS_HOME_STATE);
std::string country_code(GetSelectedCountryCode()); std::string country_code(GetSelectedCountryCode());
...@@ -320,7 +319,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, AsyncData) { ...@@ -320,7 +319,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, AsyncData) {
DCHECK(profile); DCHECK(profile);
EXPECT_EQ(base::ASCIIToUTF16(country_code), EXPECT_EQ(base::ASCIIToUTF16(country_code),
profile->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); profile->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
EXPECT_EQ(base::ASCIIToUTF16(kAnyStateCode), EXPECT_EQ(base::ASCIIToUTF16(kAnyState),
profile->GetRawInfo(autofill::ADDRESS_HOME_STATE)); profile->GetRawInfo(autofill::ADDRESS_HOME_STATE));
ExpectExistingRequiredFields(/*unset_types=*/nullptr, ExpectExistingRequiredFields(/*unset_types=*/nullptr,
/*accept_empty_phone_number=*/false); /*accept_empty_phone_number=*/false);
...@@ -376,12 +375,12 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, ...@@ -376,12 +375,12 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
if (use_regions1) { if (use_regions1) {
ASSERT_EQ(2, region_model->GetItemCount()); ASSERT_EQ(2, region_model->GetItemCount());
EXPECT_EQ(base::ASCIIToUTF16("---"), region_model->GetItemAt(0)); EXPECT_EQ(base::ASCIIToUTF16("---"), region_model->GetItemAt(0));
EXPECT_EQ(base::ASCIIToUTF16("1a"), region_model->GetItemAt(1)); EXPECT_EQ(base::ASCIIToUTF16("region1a"), region_model->GetItemAt(1));
} else { } else {
ASSERT_EQ(3, region_model->GetItemCount()); ASSERT_EQ(3, region_model->GetItemCount());
EXPECT_EQ(base::ASCIIToUTF16("---"), region_model->GetItemAt(0)); EXPECT_EQ(base::ASCIIToUTF16("---"), region_model->GetItemAt(0));
EXPECT_EQ(base::ASCIIToUTF16("2a"), region_model->GetItemAt(1)); EXPECT_EQ(base::ASCIIToUTF16("region2a"), region_model->GetItemAt(1));
EXPECT_EQ(base::ASCIIToUTF16("2b"), region_model->GetItemAt(2)); EXPECT_EQ(base::ASCIIToUTF16("region2b"), region_model->GetItemAt(2));
} }
use_regions1 = !use_regions1; use_regions1 = !use_regions1;
} }
...@@ -653,7 +652,8 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, ...@@ -653,7 +652,8 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
OpenShippingAddressEditorScreen(); OpenShippingAddressEditorScreen();
SetCommonFields(); SetCommonFields();
SetComboboxValue(base::UTF8ToUTF16("CA"), autofill::ADDRESS_HOME_STATE); SetComboboxValue(base::UTF8ToUTF16("California"),
autofill::ADDRESS_HOME_STATE);
// Set an Australian phone number in international format. // Set an Australian phone number in international format.
SetEditorTextfieldValue(base::UTF8ToUTF16("+61 2 9374 4000"), SetEditorTextfieldValue(base::UTF8ToUTF16("+61 2 9374 4000"),
...@@ -993,7 +993,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, ...@@ -993,7 +993,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
EXPECT_FALSE(GetComboboxValue(autofill::ADDRESS_HOME_COUNTRY).empty()); EXPECT_FALSE(GetComboboxValue(autofill::ADDRESS_HOME_COUNTRY).empty());
// Expect that the state was selected. // Expect that the state was selected.
EXPECT_EQ(base::ASCIIToUTF16("CA"), EXPECT_EQ(base::ASCIIToUTF16("California"),
GetComboboxValue(autofill::ADDRESS_HOME_STATE)); GetComboboxValue(autofill::ADDRESS_HOME_STATE));
// Expect that the save button is enabled, since the profile is now valid. // Expect that the save button is enabled, since the profile is now valid.
...@@ -1045,7 +1045,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, ...@@ -1045,7 +1045,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
EXPECT_FALSE(GetComboboxValue(autofill::ADDRESS_HOME_COUNTRY).empty()); EXPECT_FALSE(GetComboboxValue(autofill::ADDRESS_HOME_COUNTRY).empty());
// Expect that the state was selected. // Expect that the state was selected.
EXPECT_EQ(base::ASCIIToUTF16("CA"), EXPECT_EQ(base::ASCIIToUTF16("California"),
GetComboboxValue(autofill::ADDRESS_HOME_STATE)); GetComboboxValue(autofill::ADDRESS_HOME_STATE));
// Expect that the save button is enabled, since the profile is now valid. // Expect that the save button is enabled, since the profile is now valid.
...@@ -1081,7 +1081,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, ...@@ -1081,7 +1081,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
DialogViewID::SHIPPING_ADDRESS_SHEET_LIST_VIEW); DialogViewID::SHIPPING_ADDRESS_SHEET_LIST_VIEW);
// Expect that the state was selected. // Expect that the state was selected.
EXPECT_EQ(base::ASCIIToUTF16("CA"), EXPECT_EQ(base::ASCIIToUTF16("California"),
GetComboboxValue(autofill::ADDRESS_HOME_STATE)); GetComboboxValue(autofill::ADDRESS_HOME_STATE));
} }
...@@ -1112,7 +1112,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, ...@@ -1112,7 +1112,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
DialogViewID::SHIPPING_ADDRESS_SHEET_LIST_VIEW); DialogViewID::SHIPPING_ADDRESS_SHEET_LIST_VIEW);
// Expect that the state was selected. // Expect that the state was selected.
EXPECT_EQ(base::ASCIIToUTF16("CA"), EXPECT_EQ(base::ASCIIToUTF16("California"),
GetComboboxValue(autofill::ADDRESS_HOME_STATE)); GetComboboxValue(autofill::ADDRESS_HOME_STATE));
} }
......
...@@ -53,8 +53,8 @@ base::string16 RegionComboboxModel::GetItemAt(int index) { ...@@ -53,8 +53,8 @@ base::string16 RegionComboboxModel::GetItemAt(int index) {
if (static_cast<size_t>(index) >= regions_.size()) if (static_cast<size_t>(index) >= regions_.size())
return l10n_util::GetStringUTF16(IDS_AUTOFILL_LOADING_REGIONS); return l10n_util::GetStringUTF16(IDS_AUTOFILL_LOADING_REGIONS);
if (!regions_[index].first.empty()) if (!regions_[index].second.empty())
return base::UTF8ToUTF16(regions_[index].first); return base::UTF8ToUTF16(regions_[index].second);
// The separator item. Implemented for platforms that don't yet support // The separator item. Implemented for platforms that don't yet support
// IsItemSeparatorAt(). // IsItemSeparatorAt().
......
...@@ -20,7 +20,9 @@ namespace autofill { ...@@ -20,7 +20,9 @@ namespace autofill {
// Strings used in more than one place and must be the same everywhere. // Strings used in more than one place and must be the same everywhere.
const char kQuebecCode[] = "QC"; const char kQuebecCode[] = "QC";
const char kQuebecName[] = "Quebec";
const char kOntarioCode[] = "ON"; const char kOntarioCode[] = "ON";
const char kOntarioName[] = "Ontario";
// Make sure the two regions returned by the source are properly set in the // Make sure the two regions returned by the source are properly set in the
// model. // model.
...@@ -30,15 +32,15 @@ TEST(RegionComboboxModelTest, QuebecOntarioRegions) { ...@@ -30,15 +32,15 @@ TEST(RegionComboboxModelTest, QuebecOntarioRegions) {
model.LoadRegionData("", &test_region_data_loader, 0); model.LoadRegionData("", &test_region_data_loader, 0);
std::vector<std::pair<std::string, std::string>> regions; std::vector<std::pair<std::string, std::string>> regions;
regions.push_back(std::make_pair(kQuebecCode, "Quebec")); regions.push_back(std::make_pair(kQuebecCode, kQuebecName));
regions.push_back(std::make_pair(kOntarioCode, "Ontario")); regions.push_back(std::make_pair(kOntarioCode, kOntarioName));
test_region_data_loader.SendAsynchronousData(regions); test_region_data_loader.SendAsynchronousData(regions);
EXPECT_EQ(3, model.GetItemCount()); EXPECT_EQ(3, model.GetItemCount());
EXPECT_EQ(base::ASCIIToUTF16("---"), model.GetItemAt(0)); EXPECT_EQ(base::ASCIIToUTF16("---"), model.GetItemAt(0));
EXPECT_EQ(base::ASCIIToUTF16(kQuebecCode), model.GetItemAt(1)); EXPECT_EQ(base::ASCIIToUTF16(kQuebecName), model.GetItemAt(1));
EXPECT_EQ(base::ASCIIToUTF16(kOntarioCode), model.GetItemAt(2)); EXPECT_EQ(base::ASCIIToUTF16(kOntarioName), model.GetItemAt(2));
EXPECT_FALSE(model.failed_to_load_data()); EXPECT_FALSE(model.failed_to_load_data());
} }
......
...@@ -254,6 +254,10 @@ bool PaymentRequestState::IsPaymentAppInvoked() const { ...@@ -254,6 +254,10 @@ bool PaymentRequestState::IsPaymentAppInvoked() const {
return !!response_helper_; return !!response_helper_;
} }
AddressNormalizer* PaymentRequestState::GetAddressNormalizer() {
return payment_request_delegate_->GetAddressNormalizer();
}
void PaymentRequestState::PopulateProfileCache() { void PaymentRequestState::PopulateProfileCache() {
std::vector<autofill::AutofillProfile*> profiles = std::vector<autofill::AutofillProfile*> profiles =
personal_data_manager_->GetProfilesToSuggest(); personal_data_manager_->GetProfilesToSuggest();
......
...@@ -185,6 +185,8 @@ class PaymentRequestState : public PaymentResponseHelper::Delegate, ...@@ -185,6 +185,8 @@ class PaymentRequestState : public PaymentResponseHelper::Delegate,
// generation has begun. False otherwise. // generation has begun. False otherwise.
bool IsPaymentAppInvoked() const; bool IsPaymentAppInvoked() const;
AddressNormalizer* GetAddressNormalizer();
private: private:
// Fetches the Autofill Profiles for this user from the PersonalDataManager, // Fetches the Autofill Profiles for this user from the PersonalDataManager,
// and stores copies of them, owned by this PaymentRequestState, in // and stores copies of them, owned by this PaymentRequestState, in
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment