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

[Web Payments] Use temporary autofill profile in shipping editor

Bug: 746544
Change-Id: I0b6813c118d90b47dd5b82c5721bac3d5e05aca1
Reviewed-on: https://chromium-review.googlesource.com/580070
Commit-Queue: Anthony Vallee-Dubois <anthonyvd@chromium.org>
Reviewed-by: default avatarMathieu Perreault <mathp@chromium.org>
Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488412}
parent 0e4939bc
......@@ -57,6 +57,8 @@ ShippingAddressEditorViewController::ShippingAddressEditorViewController(
profile_to_edit_(profile),
chosen_country_index_(kInvalidCountryIndex),
failed_to_load_region_data_(false) {
if (profile_to_edit_)
temporary_profile_ = *profile_to_edit_;
UpdateCountries(/*model=*/nullptr);
UpdateEditorFields();
}
......@@ -74,24 +76,15 @@ ShippingAddressEditorViewController::GetFieldDefinitions() {
base::string16 ShippingAddressEditorViewController::GetInitialValueForType(
autofill::ServerFieldType type) {
// Temporary profile has precedence over profile to edit since its existence
// is based on having unsaved stated to restore.
if (temporary_profile_.get())
return temporary_profile_->GetInfo(autofill::AutofillType(type),
state()->GetApplicationLocale());
if (!profile_to_edit_)
return base::string16();
if (type == autofill::PHONE_HOME_WHOLE_NUMBER) {
return data_util::GetFormattedPhoneNumberForDisplay(
*profile_to_edit_, state()->GetApplicationLocale());
temporary_profile_, state()->GetApplicationLocale());
}
if (type == autofill::ADDRESS_HOME_STATE) {
if (type == autofill::ADDRESS_HOME_STATE && region_model_) {
// For the state, check if the inital value matches either a region code or
// a region name.
base::string16 initial_region = profile_to_edit_->GetInfo(
base::string16 initial_region = temporary_profile_.GetInfo(
autofill::AutofillType(type), state()->GetApplicationLocale());
autofill::l10n::CaseInsensitiveCompare compare;
......@@ -107,8 +100,8 @@ base::string16 ShippingAddressEditorViewController::GetInitialValueForType(
return initial_region;
}
return profile_to_edit_->GetInfo(autofill::AutofillType(type),
state()->GetApplicationLocale());
return temporary_profile_.GetInfo(autofill::AutofillType(type),
state()->GetApplicationLocale());
}
bool ShippingAddressEditorViewController::ValidateModelAndSave() {
......@@ -213,6 +206,7 @@ void ShippingAddressEditorViewController::OnPerformAction(
}
void ShippingAddressEditorViewController::UpdateEditorView() {
region_model_ = nullptr;
EditorViewController::UpdateEditorView();
if (chosen_country_index_ > 0UL &&
chosen_country_index_ < countries_.size()) {
......@@ -228,8 +222,6 @@ void ShippingAddressEditorViewController::UpdateEditorView() {
} else {
chosen_country_index_ = kInvalidCountryIndex;
}
// Ignore temporary profile once the editor view has been updated.
temporary_profile_.reset();
}
base::string16 ShippingAddressEditorViewController::GetSheetTitle() {
......@@ -269,9 +261,9 @@ void ShippingAddressEditorViewController::UpdateCountries(
}
// If there is a profile to edit, make sure to use its country for the initial
// |chosen_country_index_|.
if (profile_to_edit_) {
if (IsEditingExistingItem()) {
autofill::AutofillType country_type(autofill::ADDRESS_HOME_COUNTRY);
base::string16 chosen_country(profile_to_edit_->GetInfo(
base::string16 chosen_country(temporary_profile_.GetInfo(
country_type, state()->GetApplicationLocale()));
for (chosen_country_index_ = 0; chosen_country_index_ < countries_.size();
++chosen_country_index_) {
......@@ -286,9 +278,9 @@ void ShippingAddressEditorViewController::UpdateCountries(
if (countries_.size() > 0) {
LOG(ERROR) << "Unexpected country: " << chosen_country;
chosen_country_index_ = 0;
profile_to_edit_->SetInfo(country_type,
countries_[chosen_country_index_].second,
state()->GetApplicationLocale());
temporary_profile_.SetInfo(country_type,
countries_[chosen_country_index_].second,
state()->GetApplicationLocale());
} else {
LOG(ERROR) << "Unexpected empty country list!";
chosen_country_index_ = kInvalidCountryIndex;
......@@ -378,8 +370,7 @@ void ShippingAddressEditorViewController::UpdateEditorFields() {
}
void ShippingAddressEditorViewController::OnDataChanged(bool synchronous) {
temporary_profile_.reset(new autofill::AutofillProfile);
SaveFieldsToProfile(temporary_profile_.get(), /*ignore_errors*/ true);
SaveFieldsToProfile(&temporary_profile_, /*ignore_errors*/ true);
UpdateEditorFields();
if (synchronous) {
......@@ -412,8 +403,6 @@ bool ShippingAddressEditorViewController::SaveFieldsToProfile(
<< "Can't set profile country to: " << country;
if (!success && !ignore_errors)
return false;
} else {
DCHECK_EQ(temporary_profile_.get(), profile);
}
bool success = true;
......
......@@ -109,7 +109,7 @@ class ShippingAddressEditorViewController : public EditorViewController {
// A temporary profile to keep unsaved data in between relayout (e.g., when
// the country is changed and fields set may be different).
std::unique_ptr<autofill::AutofillProfile> temporary_profile_;
autofill::AutofillProfile temporary_profile_;
// List of fields, reset everytime the current country changes.
std::vector<EditorField> editor_fields_;
......
......@@ -404,7 +404,11 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
static_cast<ValidatingTextfield*>(dialog_view()->GetViewByID(
EditorViewController::GetInputFieldViewId(type)));
if (textfield) {
EXPECT_TRUE(textfield->text().empty()) << type;
// The zip field will be populated after switching to a country for
// which the profile has a zip set.
EXPECT_TRUE(textfield->text().empty() ||
type == autofill::ADDRESS_HOME_ZIP)
<< type;
SetFieldTestValue(type);
set_types.insert(type);
}
......@@ -1112,4 +1116,29 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
GetComboboxValue(autofill::ADDRESS_HOME_STATE));
}
IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest,
NoCountryProfileDoesntSetCountryToLocale) {
// Add address without a country but a valid state for the default country.
autofill::AutofillProfile profile = autofill::test::GetFullProfile();
profile.SetInfo(autofill::AutofillType(autofill::NAME_FULL),
base::ASCIIToUTF16(kNameFull), kLocale);
profile.SetInfo(autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY),
base::ASCIIToUTF16(""), kLocale);
AddAutofillProfile(profile);
InvokePaymentRequestUI();
OpenShippingAddressSectionScreen();
ResetEventObserver(DialogEvent::SHIPPING_ADDRESS_EDITOR_OPENED);
ClickOnChildInListViewAndWait(/*child_index=*/0, /*num_children=*/1,
DialogViewID::SHIPPING_ADDRESS_SHEET_LIST_VIEW);
ClickOnBackArrow();
PaymentRequest* request = GetPaymentRequests(GetActiveWebContents()).front();
EXPECT_EQ(
base::ASCIIToUTF16(""),
request->state()->shipping_profiles()[0]->GetInfo(
autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY), kLocale));
}
} // namespace payments
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