Chromium Code Reviews| Index: components/payments/content/payment_request_state_unittest.cc |
| diff --git a/components/payments/content/payment_request_state_unittest.cc b/components/payments/content/payment_request_state_unittest.cc |
| index 6b18b1775a1489abcc864655640529fa5d8844e7..6b8409e75df55581803c961e7878be4c6344381a 100644 |
| --- a/components/payments/content/payment_request_state_unittest.cc |
| +++ b/components/payments/content/payment_request_state_unittest.cc |
| @@ -13,11 +13,108 @@ |
| #include "components/autofill/core/browser/credit_card.h" |
| #include "components/autofill/core/browser/test_personal_data_manager.h" |
| #include "components/payments/content/payment_request_spec.h" |
| +#include "components/payments/core/address_normalizer.h" |
| +#include "components/payments/core/payment_request_delegate.h" |
| #include "components/payments/mojom/payment_request.mojom.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace payments { |
| +namespace { |
| + |
| +class FakeAddressNormalizer : public AddressNormalizer { |
|
Mathieu
2017/04/26 13:09:49
nit: I prefer TestAddressNormalizer instead of Fak
sebsg
2017/04/26 19:28:54
Done.
|
| + public: |
| + FakeAddressNormalizer() {} |
| + |
| + void LoadRulesForRegion(const std::string& region_code) override {} |
| + |
| + bool AreRulesLoadedForRegion(const std::string& region_code) override { |
| + return true; |
| + } |
| + |
| + void StartAddressNormalization( |
| + const autofill::AutofillProfile& profile, |
| + const std::string& region_code, |
| + int timeout_seconds, |
| + AddressNormalizer::Delegate* requester) override { |
| + if (immediate_normalization_) { |
| + requester->OnAddressNormalized(profile_); |
| + return; |
| + } |
| + |
| + // Setup the necessary variables for the delayed normalization. |
| + profile_ = profile; |
| + requester_ = requester; |
| + } |
| + |
| + void OnAddressValidationRulesLoaded(const std::string& region_code, |
| + bool success) override {} |
| + |
| + void DelayNormalization() { immediate_normalization_ = false; } |
| + |
| + void CompleteAddressNormalization() { |
| + requester_->OnAddressNormalized(profile_); |
| + } |
| + |
| + private: |
| + autofill::AutofillProfile profile_; |
| + AddressNormalizer::Delegate* requester_; |
| + |
| + bool immediate_normalization_ = true; |
| +}; |
| + |
| +class FakePaymentRequestDelegate : public PaymentRequestDelegate { |
| + public: |
| + FakePaymentRequestDelegate() |
| + : locale_("en-US"), last_committed_url_("https://shop.com") {} |
| + void ShowDialog(PaymentRequest* request) override {} |
| + |
| + void CloseDialog() override {} |
| + |
| + void ShowErrorMessage() override {} |
| + |
| + autofill::PersonalDataManager* GetPersonalDataManager() override { |
| + return nullptr; |
| + } |
| + |
| + const std::string& GetApplicationLocale() const override { return locale_; } |
| + |
| + bool IsIncognito() const override { return false; } |
| + |
| + bool IsSslCertificateValid() override { return true; } |
| + |
| + const GURL& GetLastCommittedURL() const override { |
| + return last_committed_url_; |
| + } |
| + |
| + void DoFullCardRequest( |
| + const autofill::CreditCard& credit_card, |
| + base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate> |
| + result_delegate) override {} |
| + |
| + AddressNormalizer* GetAddressNormalizer() override { |
| + return &address_normalizer_; |
| + } |
| + |
| + FakeAddressNormalizer* GetTestAddressNormalizer() { |
| + return &address_normalizer_; |
| + } |
| + |
| + autofill::RegionDataLoader* GetRegionDataLoader() override { return nullptr; } |
| + |
| + private: |
| + std::string locale_; |
| + const GURL last_committed_url_; |
| + FakeAddressNormalizer address_normalizer_; |
| + |
| + autofill::CreditCard full_card_request_card_; |
| + base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate> |
| + full_card_result_delegate_; |
| + DISALLOW_COPY_AND_ASSIGN(FakePaymentRequestDelegate); |
| +}; |
| + |
| +} // namespace |
| + |
| class PaymentRequestStateTest : public testing::Test, |
| public PaymentRequestState::Observer, |
| public PaymentRequestState::Delegate { |
| @@ -43,7 +140,9 @@ class PaymentRequestStateTest : public testing::Test, |
| payment_response_ = std::move(response); |
| }; |
| void OnShippingOptionIdSelected(std::string shipping_option_id) override {} |
| - void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {} |
| + void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override { |
| + selected_shipping_address_ = std::move(address); |
| + } |
| void RecreateStateWithOptionsAndDetails( |
| mojom::PaymentOptionsPtr options, |
| @@ -54,7 +153,8 @@ class PaymentRequestStateTest : public testing::Test, |
| std::move(options), std::move(details), std::move(method_data), nullptr, |
| "en-US"); |
| state_ = base::MakeUnique<PaymentRequestState>( |
| - spec_.get(), this, "en-US", &test_personal_data_manager_, nullptr); |
| + spec_.get(), this, "en-US", &test_personal_data_manager_, |
| + &test_payment_request_delegate_); |
| state_->AddObserver(this); |
| } |
| @@ -85,18 +185,26 @@ class PaymentRequestStateTest : public testing::Test, |
| PaymentRequestState* state() { return state_.get(); } |
| const mojom::PaymentResponsePtr& response() { return payment_response_; } |
| + const mojom::PaymentAddressPtr& selected_shipping_address() { |
| + return selected_shipping_address_; |
| + } |
| int num_on_selected_information_changed_called() { |
| return num_on_selected_information_changed_called_; |
| } |
| autofill::AutofillProfile* test_address() { return &address_; } |
| + FakePaymentRequestDelegate* test_payment_request_delegate() { |
| + return &test_payment_request_delegate_; |
| + } |
| private: |
| std::unique_ptr<PaymentRequestState> state_; |
| std::unique_ptr<PaymentRequestSpec> spec_; |
| int num_on_selected_information_changed_called_; |
| mojom::PaymentResponsePtr payment_response_; |
| + mojom::PaymentAddressPtr selected_shipping_address_; |
| autofill::TestPersonalDataManager test_personal_data_manager_; |
| + FakePaymentRequestDelegate test_payment_request_delegate_; |
| // Test data. |
| autofill::AutofillProfile address_; |
| @@ -247,4 +355,40 @@ TEST_F(PaymentRequestStateTest, ReadyToPay_ContactInfo) { |
| EXPECT_TRUE(state()->is_ready_to_pay()); |
| } |
| +TEST_F(PaymentRequestStateTest, SelectedShippingAddressMessage_Normalized) { |
|
Mathieu
2017/04/26 13:09:49
can you test the is_ready_to_pay state as well
sebsg
2017/04/26 19:28:54
Done.
|
| + mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); |
| + options->request_shipping = true; |
| + RecreateStateWithOptions(std::move(options)); |
| + |
| + // Make the normalization not be instantaneous. |
| + test_payment_request_delegate() |
| + ->GetTestAddressNormalizer() |
| + ->DelayNormalization(); |
| + |
| + // Select an address, nothing should happen until the normalization is |
| + // completed. |
| + state()->SetSelectedShippingProfile(test_address()); |
| + EXPECT_EQ(0, num_on_selected_information_changed_called()); |
| + |
| + // Complete the normalization. |
| + test_payment_request_delegate() |
| + ->GetTestAddressNormalizer() |
| + ->CompleteAddressNormalization(); |
| + EXPECT_EQ(1, num_on_selected_information_changed_called()); |
| + |
| + // Check that all the expected values were set for the shipping address. |
| + EXPECT_EQ("US", selected_shipping_address()->country); |
| + EXPECT_EQ("666 Erebus St.", selected_shipping_address()->address_line[0]); |
| + EXPECT_EQ("Apt 8", selected_shipping_address()->address_line[1]); |
| + EXPECT_EQ("CA", selected_shipping_address()->region); |
| + EXPECT_EQ("Elysium", selected_shipping_address()->city); |
| + EXPECT_EQ("", selected_shipping_address()->dependent_locality); |
| + EXPECT_EQ("91111", selected_shipping_address()->postal_code); |
| + EXPECT_EQ("", selected_shipping_address()->sorting_code); |
| + EXPECT_EQ("", selected_shipping_address()->language_code); |
| + EXPECT_EQ("Underworld", selected_shipping_address()->organization); |
| + EXPECT_EQ("John H. Doe", selected_shipping_address()->recipient); |
| + EXPECT_EQ("16502111111", selected_shipping_address()->phone); |
| +} |
| + |
| } // namespace payments |