| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/ui/views/payments/payment_request_browsertest_base.h" | 9 #include "chrome/browser/ui/views/payments/payment_request_browsertest_base.h" |
| 10 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" | 10 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
| 11 #include "chrome/browser/ui/views/payments/validating_textfield.h" | 11 #include "chrome/browser/ui/views/payments/validating_textfield.h" |
| 12 #include "components/autofill/core/browser/autofill_country.h" | 12 #include "components/autofill/core/browser/autofill_country.h" |
| 13 #include "components/autofill/core/browser/country_combobox_model.h" | 13 #include "components/autofill/core/browser/country_combobox_model.h" |
| 14 #include "components/autofill/core/browser/personal_data_manager.h" | 14 #include "components/autofill/core/browser/personal_data_manager.h" |
| 15 #include "components/autofill/core/browser/region_combobox_model.h" | 15 #include "components/autofill/core/browser/region_combobox_model.h" |
| 16 #include "components/autofill/core/browser/test_region_data_loader.h" | 16 #include "components/autofill/core/browser/test_region_data_loader.h" |
| 17 #include "components/payments/content/payment_request_spec.h" | 17 #include "components/payments/content/payment_request_spec.h" |
| 18 #include "content/public/test/browser_test_utils.h" | 18 #include "content/public/test/browser_test_utils.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_stora
ge.h" | 20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_stora
ge.h" |
| 21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" | 21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" |
| 22 #include "ui/views/controls/combobox/combobox.h" | 22 #include "ui/views/controls/combobox/combobox.h" |
| 23 | 23 |
| 24 namespace payments { | 24 namespace payments { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const char kLocale[] = "en_US"; |
| 28 const char kNameFull[] = "Bob Jones"; | 29 const char kNameFull[] = "Bob Jones"; |
| 29 const char kHomeAddress[] = "42 Answers-All Avenue"; | 30 const char kHomeAddress[] = "42 Answers-All Avenue"; |
| 30 const char kHomeCity[] = "Question-City"; | 31 const char kHomeCity[] = "Question-City"; |
| 31 const char kHomeZip[] = "ziiiiiip"; | 32 const char kHomeZip[] = "ziiiiiip"; |
| 32 const char kHomePhone[] = "5755555555"; // 5555555555 is invalid :-(. | 33 const char kHomePhone[] = "5755555555"; // 5555555555 is invalid :-(. |
| 33 const char kFormattedHomePhone[] = "(575) 555-5555"; | 34 const char kFormattedHomePhone[] = "(575) 555-5555"; |
| 34 const char kAnyState[] = "any state"; | 35 const char kAnyState[] = "any state"; |
| 36 const char kCountryWithoutStates[] = "Albania"; |
| 37 const char kCountryWithoutStatesPhoneNumber[] = "42223446"; |
| 35 | 38 |
| 36 } // namespace | 39 } // namespace |
| 37 | 40 |
| 38 class PaymentRequestShippingAddressEditorTest | 41 class PaymentRequestShippingAddressEditorTest |
| 39 : public PaymentRequestBrowserTestBase { | 42 : public PaymentRequestBrowserTestBase { |
| 40 protected: | 43 protected: |
| 41 PaymentRequestShippingAddressEditorTest() | 44 PaymentRequestShippingAddressEditorTest() |
| 42 : PaymentRequestBrowserTestBase( | 45 : PaymentRequestBrowserTestBase( |
| 43 "/payment_request_dynamic_shipping_test.html") {} | 46 "/payment_request_dynamic_shipping_test.html") {} |
| 44 | 47 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 } | 79 } |
| 77 | 80 |
| 78 void SetCommonFields() { | 81 void SetCommonFields() { |
| 79 SetFieldTestValue(autofill::NAME_FULL); | 82 SetFieldTestValue(autofill::NAME_FULL); |
| 80 SetFieldTestValue(autofill::ADDRESS_HOME_STREET_ADDRESS); | 83 SetFieldTestValue(autofill::ADDRESS_HOME_STREET_ADDRESS); |
| 81 SetFieldTestValue(autofill::ADDRESS_HOME_CITY); | 84 SetFieldTestValue(autofill::ADDRESS_HOME_CITY); |
| 82 SetFieldTestValue(autofill::ADDRESS_HOME_ZIP); | 85 SetFieldTestValue(autofill::ADDRESS_HOME_ZIP); |
| 83 SetFieldTestValue(autofill::PHONE_HOME_WHOLE_NUMBER); | 86 SetFieldTestValue(autofill::PHONE_HOME_WHOLE_NUMBER); |
| 84 } | 87 } |
| 85 | 88 |
| 86 // First check if the requested field of |type| exists, if so set it's value | 89 // First check if the requested field of |type| exists, if so, set its value |
| 87 // in |textfield_text| and return true. | 90 // in |textfield_text| if it's not null, and return true. |
| 88 bool GetEditorTextfieldValueIfExists(autofill::ServerFieldType type, | 91 bool GetEditorTextfieldValueIfExists(autofill::ServerFieldType type, |
| 89 base::string16* textfield_text) { | 92 base::string16* textfield_text) { |
| 90 ValidatingTextfield* textfield = static_cast<ValidatingTextfield*>( | 93 ValidatingTextfield* textfield = static_cast<ValidatingTextfield*>( |
| 91 dialog_view()->GetViewByID(static_cast<int>(type))); | 94 dialog_view()->GetViewByID(static_cast<int>(type))); |
| 92 if (!textfield) | 95 if (!textfield) |
| 93 return false; | 96 return false; |
| 94 *textfield_text = textfield->text(); | 97 if (textfield_text) |
| 98 *textfield_text = textfield->text(); |
| 95 return true; | 99 return true; |
| 96 } | 100 } |
| 97 | 101 |
| 98 // |unset_types| can be null, but when it's not, the fields that are not set | 102 // |unset_types| can be null, but when it's not, the fields that are not set |
| 99 // get their type added to it, so that the caller can tell which types are not | 103 // get their type added to it, so that the caller can tell which types are not |
| 100 // set for a given country. |accept_empty_phone_number| can be set to true to | 104 // set for a given country. |accept_empty_phone_number| can be set to true to |
| 101 // accept a phone type field set to empty, and mark it as unset. | 105 // accept a phone type field set to empty, and mark it as unset. |
| 102 void ExpectExistingRequiredFields( | 106 void ExpectExistingRequiredFields( |
| 103 std::set<autofill::ServerFieldType>* unset_types, | 107 std::set<autofill::ServerFieldType>* unset_types, |
| 104 bool accept_empty_phone_number) { | 108 bool accept_empty_phone_number) { |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 std::set<autofill::ServerFieldType> unset_types; | 333 std::set<autofill::ServerFieldType> unset_types; |
| 330 for (size_t country_index = 10; country_index < num_countries; | 334 for (size_t country_index = 10; country_index < num_countries; |
| 331 country_index += num_countries / 10) { | 335 country_index += num_countries / 10) { |
| 332 // The editor updates asynchronously when the country changes. | 336 // The editor updates asynchronously when the country changes. |
| 333 ResetEventObserver(DialogEvent::EDITOR_VIEW_UPDATED); | 337 ResetEventObserver(DialogEvent::EDITOR_VIEW_UPDATED); |
| 334 | 338 |
| 335 views::Combobox* region_combobox = | 339 views::Combobox* region_combobox = |
| 336 static_cast<views::Combobox*>(dialog_view()->GetViewByID( | 340 static_cast<views::Combobox*>(dialog_view()->GetViewByID( |
| 337 static_cast<int>(autofill::ADDRESS_HOME_STATE))); | 341 static_cast<int>(autofill::ADDRESS_HOME_STATE))); |
| 338 autofill::RegionComboboxModel* region_model = nullptr; | 342 autofill::RegionComboboxModel* region_model = nullptr; |
| 339 // Some countries don't have a state combo box. | 343 // Some countries don't have a state combobox. |
| 340 if (region_combobox) { | 344 if (region_combobox) { |
| 341 autofill::RegionComboboxModel* region_model = | 345 autofill::RegionComboboxModel* region_model = |
| 342 static_cast<autofill::RegionComboboxModel*>(region_combobox->model()); | 346 static_cast<autofill::RegionComboboxModel*>(region_combobox->model()); |
| 343 if (use_regions1) { | 347 if (use_regions1) { |
| 344 ASSERT_EQ(1, region_model->GetItemCount()); | 348 ASSERT_EQ(1, region_model->GetItemCount()); |
| 345 EXPECT_EQ(base::ASCIIToUTF16("region1a"), region_model->GetItemAt(0)); | 349 EXPECT_EQ(base::ASCIIToUTF16("region1a"), region_model->GetItemAt(0)); |
| 346 } else { | 350 } else { |
| 347 ASSERT_EQ(2, region_model->GetItemCount()); | 351 ASSERT_EQ(2, region_model->GetItemCount()); |
| 348 EXPECT_EQ(base::ASCIIToUTF16("region2a"), region_model->GetItemAt(0)); | 352 EXPECT_EQ(base::ASCIIToUTF16("region2a"), region_model->GetItemAt(0)); |
| 349 EXPECT_EQ(base::ASCIIToUTF16("region2b"), region_model->GetItemAt(1)); | 353 EXPECT_EQ(base::ASCIIToUTF16("region2b"), region_model->GetItemAt(1)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 370 if (textfield) { | 374 if (textfield) { |
| 371 EXPECT_TRUE(textfield->text().empty()) << type; | 375 EXPECT_TRUE(textfield->text().empty()) << type; |
| 372 SetFieldTestValue(type); | 376 SetFieldTestValue(type); |
| 373 set_types.insert(type); | 377 set_types.insert(type); |
| 374 } | 378 } |
| 375 } | 379 } |
| 376 for (auto type : set_types) { | 380 for (auto type : set_types) { |
| 377 unset_types.erase(type); | 381 unset_types.erase(type); |
| 378 } | 382 } |
| 379 | 383 |
| 380 // Make sure the country combo box was properly reset to the chosen country. | 384 // Make sure the country combobox was properly reset to the chosen country. |
| 381 country_combobox = static_cast<views::Combobox*>(dialog_view()->GetViewByID( | 385 country_combobox = static_cast<views::Combobox*>(dialog_view()->GetViewByID( |
| 382 static_cast<int>(autofill::ADDRESS_HOME_COUNTRY))); | 386 static_cast<int>(autofill::ADDRESS_HOME_COUNTRY))); |
| 383 DCHECK(country_combobox); | 387 DCHECK(country_combobox); |
| 384 EXPECT_EQ(country_index, | 388 EXPECT_EQ(country_index, |
| 385 static_cast<size_t>(country_combobox->GetSelectedRow())); | 389 static_cast<size_t>(country_combobox->GetSelectedRow())); |
| 386 country_model = | 390 country_model = |
| 387 static_cast<autofill::CountryComboboxModel*>(country_combobox->model()); | 391 static_cast<autofill::CountryComboboxModel*>(country_combobox->model()); |
| 388 ASSERT_EQ(num_countries, country_model->countries().size()); | 392 ASSERT_EQ(num_countries, country_model->countries().size()); |
| 389 | 393 |
| 390 // Update regions. | 394 // Update regions. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 profile->GetRawInfo(autofill::ADDRESS_HOME_STATE)); | 478 profile->GetRawInfo(autofill::ADDRESS_HOME_STATE)); |
| 475 ExpectExistingRequiredFields(/*unset_types=*/nullptr, | 479 ExpectExistingRequiredFields(/*unset_types=*/nullptr, |
| 476 /*accept_empty_phone_number=*/false); | 480 /*accept_empty_phone_number=*/false); |
| 477 } | 481 } |
| 478 | 482 |
| 479 IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, | 483 IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, |
| 480 SelectingIncompleteAddress) { | 484 SelectingIncompleteAddress) { |
| 481 // Add incomplete address. | 485 // Add incomplete address. |
| 482 autofill::AutofillProfile profile; | 486 autofill::AutofillProfile profile; |
| 483 profile.SetInfo(autofill::AutofillType(autofill::NAME_FULL), | 487 profile.SetInfo(autofill::AutofillType(autofill::NAME_FULL), |
| 484 base::ASCIIToUTF16(kNameFull), "fr_CA"); | 488 base::ASCIIToUTF16(kNameFull), kLocale); |
| 489 // Also set non-default country, to make sure proper fields will be used. |
| 490 profile.SetInfo(autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY), |
| 491 base::ASCIIToUTF16(kCountryWithoutStates), kLocale); |
| 485 AddAutofillProfile(profile); | 492 AddAutofillProfile(profile); |
| 486 | 493 |
| 487 InvokePaymentRequestUI(); | 494 InvokePaymentRequestUI(); |
| 488 | 495 |
| 489 // One shipping address is available, but it's not selected. | 496 // One shipping address is available, but it's not selected. |
| 490 PaymentRequest* request = GetPaymentRequests(GetActiveWebContents()).front(); | 497 PaymentRequest* request = GetPaymentRequests(GetActiveWebContents()).front(); |
| 491 EXPECT_EQ(1U, request->state()->shipping_profiles().size()); | 498 EXPECT_EQ(1U, request->state()->shipping_profiles().size()); |
| 492 EXPECT_EQ(nullptr, request->state()->selected_shipping_profile()); | 499 EXPECT_EQ(nullptr, request->state()->selected_shipping_profile()); |
| 493 | 500 |
| 494 OpenShippingAddressSectionScreen(); | 501 OpenShippingAddressSectionScreen(); |
| 495 | 502 |
| 496 ResetEventObserver(DialogEvent::SHIPPING_ADDRESS_EDITOR_OPENED); | 503 ResetEventObserver(DialogEvent::SHIPPING_ADDRESS_EDITOR_OPENED); |
| 497 ClickOnChildInListViewAndWait(/*child_index=*/0, /*num_children=*/1, | 504 ClickOnChildInListViewAndWait(/*child_index=*/0, /*num_children=*/1, |
| 498 DialogViewID::SHIPPING_ADDRESS_SHEET_LIST_VIEW); | 505 DialogViewID::SHIPPING_ADDRESS_SHEET_LIST_VIEW); |
| 499 | 506 |
| 500 EXPECT_EQ(base::ASCIIToUTF16(kNameFull), | 507 EXPECT_EQ(base::ASCIIToUTF16(kNameFull), |
| 501 GetEditorTextfieldValue(autofill::NAME_FULL)); | 508 GetEditorTextfieldValue(autofill::NAME_FULL)); |
| 502 EXPECT_EQ(base::ASCIIToUTF16(""), | 509 // There are no state field in |kCountryWithoutStates|. |
| 503 GetEditorTextfieldValue(autofill::ADDRESS_HOME_ZIP)); | 510 EXPECT_FALSE( |
| 511 GetEditorTextfieldValueIfExists(autofill::ADDRESS_HOME_STATE, nullptr)); |
| 504 | 512 |
| 505 // Set all required fields. | 513 // Set all required fields. |
| 506 SetCommonFields(); | 514 SetCommonFields(); |
| 515 // The phone number must be replaced by one that is valid for |
| 516 // |kCountryWithoutStates|. |
| 517 SetEditorTextfieldValue(base::ASCIIToUTF16(kCountryWithoutStatesPhoneNumber), |
| 518 autofill::PHONE_HOME_WHOLE_NUMBER); |
| 507 | 519 |
| 508 // Verifying the data is in the DB. | 520 // Verifying the data is in the DB. |
| 509 autofill::PersonalDataManager* personal_data_manager = GetDataManager(); | 521 autofill::PersonalDataManager* personal_data_manager = GetDataManager(); |
| 510 personal_data_manager->AddObserver(&personal_data_observer_); | 522 personal_data_manager->AddObserver(&personal_data_observer_); |
| 511 | 523 |
| 512 ResetEventObserver(DialogEvent::BACK_TO_PAYMENT_SHEET_NAVIGATION); | 524 ResetEventObserver(DialogEvent::BACK_TO_PAYMENT_SHEET_NAVIGATION); |
| 513 | 525 |
| 514 // Wait until the web database has been updated and the notification sent. | 526 // Wait until the web database has been updated and the notification sent. |
| 515 base::RunLoop data_loop; | 527 base::RunLoop data_loop; |
| 516 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) | 528 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 517 .WillOnce(QuitMessageLoop(&data_loop)); | 529 .WillOnce(QuitMessageLoop(&data_loop)); |
| 518 ClickOnDialogViewAndWait(DialogViewID::SAVE_ADDRESS_BUTTON); | 530 ClickOnDialogViewAndWait(DialogViewID::SAVE_ADDRESS_BUTTON); |
| 519 data_loop.Run(); | 531 data_loop.Run(); |
| 520 | 532 |
| 533 ASSERT_EQ(1UL, personal_data_manager->GetProfiles().size()); |
| 534 autofill::AutofillProfile* saved_profile = |
| 535 personal_data_manager->GetProfiles()[0]; |
| 536 DCHECK(saved_profile); |
| 537 EXPECT_EQ( |
| 538 base::ASCIIToUTF16(kCountryWithoutStates), |
| 539 saved_profile->GetInfo( |
| 540 autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY), kLocale)); |
| 521 ExpectExistingRequiredFields(/*unset_types=*/nullptr, | 541 ExpectExistingRequiredFields(/*unset_types=*/nullptr, |
| 522 /*accept_empty_phone_number=*/false); | 542 /*accept_empty_phone_number=*/false); |
| 523 | 543 |
| 524 // Still have one shipping address, but now it's selected. | 544 // Still have one shipping address, but now it's selected. |
| 525 EXPECT_EQ(1U, request->state()->shipping_profiles().size()); | 545 EXPECT_EQ(1U, request->state()->shipping_profiles().size()); |
| 526 EXPECT_EQ(request->state()->shipping_profiles().back(), | 546 EXPECT_EQ(request->state()->shipping_profiles().back(), |
| 527 request->state()->selected_shipping_profile()); | 547 request->state()->selected_shipping_profile()); |
| 528 } | 548 } |
| 529 | 549 |
| 530 } // namespace payments | 550 } // namespace payments |
| OLD | NEW |