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,
autofill::ADDRESS_HOME_STREET_ADDRESS);
SetEditorTextfieldValue(base::ASCIIToUTF16("BobCity"),
autofill::ADDRESS_HOME_CITY);
SetComboboxValue(base::UTF8ToUTF16("CA"), autofill::ADDRESS_HOME_STATE);
SetComboboxValue(base::UTF8ToUTF16("California"),
autofill::ADDRESS_HOME_STATE);
SetEditorTextfieldValue(base::ASCIIToUTF16("BobZip"),
autofill::ADDRESS_HOME_ZIP);
SetEditorTextfieldValue(base::ASCIIToUTF16("5755555555"),
......
......@@ -41,6 +41,33 @@ namespace {
// http://www.cplusplus.com/reference/string/string/npos
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
ShippingAddressEditorViewController::ShippingAddressEditorViewController(
......@@ -89,11 +116,11 @@ base::string16 ShippingAddressEditorViewController::GetInitialValueForType(
autofill::l10n::CaseInsensitiveCompare compare;
for (const auto& region : region_model_->GetRegions()) {
base::string16 region_code = base::UTF8ToUTF16(region.first);
if (compare.StringsEqual(initial_region, region_code) ||
if (compare.StringsEqual(initial_region,
base::UTF8ToUTF16(region.first)) ||
compare.StringsEqual(initial_region,
base::UTF8ToUTF16(region.second))) {
return region_code;
return base::UTF8ToUTF16(region.second);
}
}
......@@ -372,6 +399,17 @@ void ShippingAddressEditorViewController::UpdateEditorFields() {
void ShippingAddressEditorViewController::OnDataChanged(bool synchronous) {
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();
if (synchronous) {
UpdateEditorView();
......
......@@ -297,8 +297,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, AsyncData) {
SetCommonFields();
SetComboboxValue(base::UTF8ToUTF16("United States"),
autofill::ADDRESS_HOME_COUNTRY);
SetComboboxValue(base::UTF8ToUTF16(kAnyStateCode),
autofill::ADDRESS_HOME_STATE);
SetComboboxValue(base::UTF8ToUTF16(kAnyState), autofill::ADDRESS_HOME_STATE);
std::string country_code(GetSelectedCountryCode());
......@@ -320,7 +319,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, AsyncData) {
DCHECK(profile);
EXPECT_EQ(base::ASCIIToUTF16(country_code),
profile->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
EXPECT_EQ(base::ASCIIToUTF16(kAnyStateCode),
EXPECT_EQ(base::ASCIIToUTF16(kAnyState),
profile->GetRawInfo(autofill::ADDRESS_HOME_STATE));
ExpectExistingRequiredFields(/*unset_types=*/nullptr,
/*accept_empty_phone_number=*/false);
......@@ -376,12 +375,12 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
if (use_regions1) {
ASSERT_EQ(2, region_model->GetItemCount());
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 {
ASSERT_EQ(3, region_model->GetItemCount());
EXPECT_EQ(base::ASCIIToUTF16("---"), region_model->GetItemAt(0));
EXPECT_EQ(base::ASCIIToUTF16("2a"), region_model->GetItemAt(1));
EXPECT_EQ(base::ASCIIToUTF16("2b"), region_model->GetItemAt(2));
EXPECT_EQ(base::ASCIIToUTF16("region2a"), region_model->GetItemAt(1));
EXPECT_EQ(base::ASCIIToUTF16("region2b"), region_model->GetItemAt(2));
}
use_regions1 = !use_regions1;
}
......@@ -653,7 +652,8 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
OpenShippingAddressEditorScreen();
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.
SetEditorTextfieldValue(base::UTF8ToUTF16("+61 2 9374 4000"),
......@@ -993,7 +993,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
EXPECT_FALSE(GetComboboxValue(autofill::ADDRESS_HOME_COUNTRY).empty());
// Expect that the state was selected.
EXPECT_EQ(base::ASCIIToUTF16("CA"),
EXPECT_EQ(base::ASCIIToUTF16("California"),
GetComboboxValue(autofill::ADDRESS_HOME_STATE));
// Expect that the save button is enabled, since the profile is now valid.
......@@ -1045,7 +1045,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
EXPECT_FALSE(GetComboboxValue(autofill::ADDRESS_HOME_COUNTRY).empty());
// Expect that the state was selected.
EXPECT_EQ(base::ASCIIToUTF16("CA"),
EXPECT_EQ(base::ASCIIToUTF16("California"),
GetComboboxValue(autofill::ADDRESS_HOME_STATE));
// Expect that the save button is enabled, since the profile is now valid.
......@@ -1081,7 +1081,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
DialogViewID::SHIPPING_ADDRESS_SHEET_LIST_VIEW);
// Expect that the state was selected.
EXPECT_EQ(base::ASCIIToUTF16("CA"),
EXPECT_EQ(base::ASCIIToUTF16("California"),
GetComboboxValue(autofill::ADDRESS_HOME_STATE));
}
......@@ -1112,7 +1112,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
DialogViewID::SHIPPING_ADDRESS_SHEET_LIST_VIEW);
// Expect that the state was selected.
EXPECT_EQ(base::ASCIIToUTF16("CA"),
EXPECT_EQ(base::ASCIIToUTF16("California"),
GetComboboxValue(autofill::ADDRESS_HOME_STATE));
}
......
......@@ -53,8 +53,8 @@ base::string16 RegionComboboxModel::GetItemAt(int index) {
if (static_cast<size_t>(index) >= regions_.size())
return l10n_util::GetStringUTF16(IDS_AUTOFILL_LOADING_REGIONS);
if (!regions_[index].first.empty())
return base::UTF8ToUTF16(regions_[index].first);
if (!regions_[index].second.empty())
return base::UTF8ToUTF16(regions_[index].second);
// The separator item. Implemented for platforms that don't yet support
// IsItemSeparatorAt().
......
......@@ -20,7 +20,9 @@ namespace autofill {
// Strings used in more than one place and must be the same everywhere.
const char kQuebecCode[] = "QC";
const char kQuebecName[] = "Quebec";
const char kOntarioCode[] = "ON";
const char kOntarioName[] = "Ontario";
// Make sure the two regions returned by the source are properly set in the
// model.
......@@ -30,15 +32,15 @@ TEST(RegionComboboxModelTest, QuebecOntarioRegions) {
model.LoadRegionData("", &test_region_data_loader, 0);
std::vector<std::pair<std::string, std::string>> regions;
regions.push_back(std::make_pair(kQuebecCode, "Quebec"));
regions.push_back(std::make_pair(kOntarioCode, "Ontario"));
regions.push_back(std::make_pair(kQuebecCode, kQuebecName));
regions.push_back(std::make_pair(kOntarioCode, kOntarioName));
test_region_data_loader.SendAsynchronousData(regions);
EXPECT_EQ(3, model.GetItemCount());
EXPECT_EQ(base::ASCIIToUTF16("---"), model.GetItemAt(0));
EXPECT_EQ(base::ASCIIToUTF16(kQuebecCode), model.GetItemAt(1));
EXPECT_EQ(base::ASCIIToUTF16(kOntarioCode), model.GetItemAt(2));
EXPECT_EQ(base::ASCIIToUTF16(kQuebecName), model.GetItemAt(1));
EXPECT_EQ(base::ASCIIToUTF16(kOntarioName), model.GetItemAt(2));
EXPECT_FALSE(model.failed_to_load_data());
}
......
......@@ -254,6 +254,10 @@ bool PaymentRequestState::IsPaymentAppInvoked() const {
return !!response_helper_;
}
AddressNormalizer* PaymentRequestState::GetAddressNormalizer() {
return payment_request_delegate_->GetAddressNormalizer();
}
void PaymentRequestState::PopulateProfileCache() {
std::vector<autofill::AutofillProfile*> profiles =
personal_data_manager_->GetProfilesToSuggest();
......
......@@ -185,6 +185,8 @@ class PaymentRequestState : public PaymentResponseHelper::Delegate,
// generation has begun. False otherwise.
bool IsPaymentAppInvoked() const;
AddressNormalizer* GetAddressNormalizer();
private:
// Fetches the Autofill Profiles for this user from the PersonalDataManager,
// 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