Chromium Code Reviews| Index: chrome/browser/ui/views/payments/shipping_address_editor_view_controller_browsertest.cc |
| diff --git a/chrome/browser/ui/views/payments/shipping_address_editor_view_controller_browsertest.cc b/chrome/browser/ui/views/payments/shipping_address_editor_view_controller_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..07ad76621c73ab7f559d0977a59a389f5811eb74 |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/payments/shipping_address_editor_view_controller_browsertest.cc |
| @@ -0,0 +1,201 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <algorithm> |
| + |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/ui/views/payments/payment_request_browsertest_base.h" |
| +#include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
| +#include "chrome/browser/ui/views/payments/validating_textfield.h" |
| +#include "components/autofill/core/browser/autofill_country.h" |
| +#include "components/autofill/core/browser/country_combobox_model.h" |
| +#include "components/autofill/core/browser/personal_data_manager.h" |
| +#include "components/autofill/core/browser/region_combobox_model.h" |
| +#include "components/payments/content/payment_request_spec.h" |
| +#include "content/public/test/browser_test_utils.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/views/controls/combobox/combobox.h" |
| + |
| +namespace payments { |
| + |
| +class PaymentRequestShippingAddressEditorTest |
| + : public PaymentRequestBrowserTestBase { |
| + protected: |
| + PaymentRequestShippingAddressEditorTest() |
| + : PaymentRequestBrowserTestBase( |
| + "/payment_request_dynamic_shipping_test.html") {} |
| + |
| + PersonalDataLoadedObserverMock personal_data_observer_; |
| + |
| + void SetRequiredFields() { |
| + SetEditorTextfieldValue(base::ASCIIToUTF16(kNameFull), autofill::NAME_FULL); |
| + SetEditorTextfieldValue(base::ASCIIToUTF16(kHomeAddress), |
| + autofill::ADDRESS_HOME_STREET_ADDRESS); |
| + SetEditorTextfieldValue(base::ASCIIToUTF16(kHomeCity), |
| + autofill::ADDRESS_HOME_CITY); |
| + SetEditorTextfieldValue(base::ASCIIToUTF16(kHomeZip), |
| + autofill::ADDRESS_HOME_ZIP); |
| + } |
| + |
| + std::string GetSelectedCountryCode() { |
| + views::Combobox* country_combobox = |
| + static_cast<views::Combobox*>(dialog_view()->GetViewByID( |
| + static_cast<int>(autofill::ADDRESS_HOME_COUNTRY))); |
| + DCHECK(country_combobox); |
| + int selected_country_row = country_combobox->GetSelectedRow(); |
| + autofill::CountryComboboxModel* model = |
| + static_cast<autofill::CountryComboboxModel*>(country_combobox->model()); |
| + |
| + return model->countries()[selected_country_row]->country_code(); |
| + } |
| + |
| + static const char kNameFull[]; |
|
please use gerrit instead
2017/04/11 21:05:35
In general, I prefer to use literal strings instea
MAD
2017/04/12 02:46:08
I prefer to use constants when the string is used
|
| + static const char kHomeAddress[]; |
| + static const char kHomeCity[]; |
| + static const char kHomeZip[]; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(PaymentRequestShippingAddressEditorTest); |
| +}; |
| + |
| +const char PaymentRequestShippingAddressEditorTest::kNameFull[] = "Bob Jones"; |
| +const char PaymentRequestShippingAddressEditorTest::kHomeAddress[] = |
| + "42 Answers-All Avenue"; |
| +const char PaymentRequestShippingAddressEditorTest::kHomeCity[] = |
| + "Question-City"; |
| +const char PaymentRequestShippingAddressEditorTest::kHomeZip[] = "ziiiiiip"; |
| + |
| +IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, |
| + EnteringValidDataWithDefaultCountry) { |
| + InvokePaymentRequestUI(); |
| + |
| + OpenShippingOptionSectionScreen(); |
| + |
| + OpenShippingAddressEditorScreen(); |
| + |
| + std::string country_code(GetSelectedCountryCode()); |
| + |
| + SetRequiredFields(); |
| + |
| + ResetEventObserver(DialogEvent::BACK_NAVIGATION); |
| + |
| + // Verifying the data is in the DB. |
| + autofill::PersonalDataManager* personal_data_manager = GetDataManager(); |
| + personal_data_manager->AddObserver(&personal_data_observer_); |
| + |
| + // Wait until the web database has been updated and the notification sent. |
| + base::RunLoop data_loop; |
| + EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| + .WillOnce(QuitMessageLoop(&data_loop)); |
| + ClickOnDialogViewAndWait(DialogViewID::EDITOR_SAVE_BUTTON); |
| + data_loop.Run(); |
| + |
| + ASSERT_EQ(1UL, personal_data_manager->GetProfiles().size()); |
| + autofill::AutofillProfile* profile = personal_data_manager->GetProfiles()[0]; |
| + DCHECK(profile); |
| + EXPECT_EQ(base::ASCIIToUTF16(country_code), |
| + profile->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); |
| + EXPECT_EQ(base::ASCIIToUTF16(kNameFull), |
| + profile->GetRawInfo(autofill::NAME_FULL)); |
| + EXPECT_EQ(base::ASCIIToUTF16(kHomeAddress), |
| + profile->GetRawInfo(autofill::ADDRESS_HOME_STREET_ADDRESS)); |
| + EXPECT_EQ(base::ASCIIToUTF16(kHomeCity), |
| + profile->GetRawInfo(autofill::ADDRESS_HOME_CITY)); |
| + EXPECT_EQ(base::ASCIIToUTF16(kHomeZip), |
| + profile->GetRawInfo(autofill::ADDRESS_HOME_ZIP)); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, |
| + SwitchingCountryUpdatesView) { |
| + InvokePaymentRequestUI(); |
| + |
| + OpenShippingOptionSectionScreen(); |
| + |
| + OpenShippingAddressEditorScreen(); |
| + |
| + views::Combobox* country_combobox = |
| + static_cast<views::Combobox*>(dialog_view()->GetViewByID( |
| + static_cast<int>(autofill::ADDRESS_HOME_COUNTRY))); |
| + ASSERT_NE(nullptr, country_combobox); |
| + ASSERT_EQ(0, country_combobox->GetSelectedRow()); |
| + autofill::CountryComboboxModel* model = |
| + static_cast<autofill::CountryComboboxModel*>(country_combobox->model()); |
| + size_t num_countries = model->countries().size(); |
| + ASSERT_GT(num_countries, 10UL); |
| + for (size_t country_index = 1; country_index < num_countries; |
| + country_index += num_countries / 10) { |
| + // The editor updates asynchronously when the country changes. |
| + ResetEventObserver(DialogEvent::EDITOR_VIEW_UPDATED); |
| + country_combobox->SetSelectedRow(country_index); |
| + country_combobox->OnBlur(); |
| + |
| + // The view update will invalidate the country_combobox / model pointers. |
| + country_combobox = nullptr; |
| + model = nullptr; |
| + WaitForObservedEvent(); |
| + |
| + // Make sure the country combo box was properly reset to the chosen country. |
| + country_combobox = static_cast<views::Combobox*>(dialog_view()->GetViewByID( |
| + static_cast<int>(autofill::ADDRESS_HOME_COUNTRY))); |
| + DCHECK(country_combobox); |
| + EXPECT_EQ(country_index, |
| + static_cast<size_t>(country_combobox->GetSelectedRow())); |
| + |
| + country_combobox = static_cast<views::Combobox*>(dialog_view()->GetViewByID( |
| + static_cast<int>(autofill::ADDRESS_HOME_COUNTRY))); |
| + DCHECK(country_combobox); |
| + model = |
| + static_cast<autofill::CountryComboboxModel*>(country_combobox->model()); |
| + ASSERT_EQ(num_countries, model->countries().size()); |
| + } |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressEditorTest, |
| + FailToLoadRegionData) { |
| + InvokePaymentRequestUI(); |
| + |
| + OpenShippingOptionSectionScreen(); |
| + |
| + OpenShippingAddressEditorScreen(); |
| + |
| + // The editor updates asynchronously when the regions fail to load. |
| + ResetEventObserver(DialogEvent::EDITOR_VIEW_UPDATED); |
| + views::Combobox* country_combobox = |
| + static_cast<views::Combobox*>(dialog_view()->GetViewByID( |
| + static_cast<int>(autofill::ADDRESS_HOME_STATE))); |
| + ASSERT_NE(nullptr, country_combobox); |
| + autofill::RegionComboboxModel* model = |
| + static_cast<autofill::RegionComboboxModel*>(country_combobox->model()); |
| + model->SetFailureModeForTests(true); |
| + |
| + // The view update will invalidate the country_combobox / model pointers. |
| + country_combobox = nullptr; |
| + model = nullptr; |
| + WaitForObservedEvent(); |
| + |
| + // Now any textual value can be set as the state. |
| + SetEditorTextfieldValue(base::ASCIIToUTF16("any state"), |
| + autofill::ADDRESS_HOME_STATE); |
| + SetRequiredFields(); |
| + ResetEventObserver(DialogEvent::BACK_NAVIGATION); |
| + |
| + // Verifying the data is in the DB. |
| + autofill::PersonalDataManager* personal_data_manager = GetDataManager(); |
| + personal_data_manager->AddObserver(&personal_data_observer_); |
| + |
| + // Wait until the web database has been updated and the notification sent. |
| + base::RunLoop data_loop; |
| + EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| + .WillOnce(QuitMessageLoop(&data_loop)); |
| + ClickOnDialogViewAndWait(DialogViewID::EDITOR_SAVE_BUTTON); |
| + data_loop.Run(); |
| + |
| + ASSERT_EQ(1UL, personal_data_manager->GetProfiles().size()); |
| + autofill::AutofillProfile* profile = personal_data_manager->GetProfiles()[0]; |
| + DCHECK(profile); |
| + EXPECT_EQ(base::ASCIIToUTF16("any state"), |
| + profile->GetRawInfo(autofill::ADDRESS_HOME_STATE)); |
| +} |
| + |
| +} // namespace payments |