| Index: components/payments/core/autofill_payment_instrument_unittest.cc
|
| diff --git a/components/payments/core/autofill_payment_instrument_unittest.cc b/components/payments/core/autofill_payment_instrument_unittest.cc
|
| index 622a4e90f577f982879a1ed266e13b950e76f14a..6735be8965bd719434bf503f7138fd79574b5975 100644
|
| --- a/components/payments/core/autofill_payment_instrument_unittest.cc
|
| +++ b/components/payments/core/autofill_payment_instrument_unittest.cc
|
| @@ -10,12 +10,134 @@
|
| #include "components/autofill/core/browser/autofill_profile.h"
|
| #include "components/autofill/core/browser/autofill_test_utils.h"
|
| #include "components/autofill/core/browser/credit_card.h"
|
| +#include "components/payments/core/address_normalizer.h"
|
| +#include "components/payments/core/payment_request_delegate.h"
|
| #include "components/strings/grit/components_strings.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| #include "ui/base/l10n/l10n_util.h"
|
|
|
| namespace payments {
|
|
|
| +namespace {
|
| +
|
| +class FakePaymentInstrumentDelegate : public PaymentInstrument::Delegate {
|
| + public:
|
| + FakePaymentInstrumentDelegate() {}
|
| +
|
| + void OnInstrumentDetailsReady(
|
| + const std::string& method_name,
|
| + const std::string& stringified_details) override {
|
| + on_instrument_details_ready_called_ = true;
|
| + }
|
| +
|
| + void OnInstrumentDetailsError() override {
|
| + on_instrument_details_error_called_ = true;
|
| + }
|
| +
|
| + bool WasOnInstrumentDetailsReadyCalled() {
|
| + return on_instrument_details_ready_called_;
|
| + }
|
| +
|
| + bool WasOnInstrumentDetailsErrorCalled() {
|
| + return on_instrument_details_error_called_;
|
| + }
|
| +
|
| + private:
|
| + bool on_instrument_details_ready_called_ = false;
|
| + bool on_instrument_details_error_called_ = false;
|
| +};
|
| +
|
| +class FakeAddressNormalizer : public AddressNormalizer {
|
| + 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 {
|
| + profile_ = profile;
|
| + requester_ = requester;
|
| + }
|
| +
|
| + void OnAddressValidationRulesLoaded(const std::string& region_code,
|
| + bool success) override {}
|
| +
|
| + void CompleteAddressNormalization() {
|
| + requester_->OnAddressNormalized(profile_);
|
| + }
|
| +
|
| + private:
|
| + autofill::AutofillProfile profile_;
|
| + AddressNormalizer::Delegate* requester_;
|
| +};
|
| +
|
| +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 {
|
| + full_card_request_card_ = credit_card;
|
| + full_card_result_delegate_ = result_delegate;
|
| + }
|
| +
|
| + AddressNormalizer* GetAddressNormalizer() override {
|
| + return &address_normalizer_;
|
| + }
|
| +
|
| + FakeAddressNormalizer* GetTestAddressNormalizer() {
|
| + return &address_normalizer_;
|
| + }
|
| +
|
| + void CompleteFullCardRequest() {
|
| + full_card_result_delegate_->OnFullCardRequestSucceeded(
|
| + full_card_request_card_, base::ASCIIToUTF16("123"));
|
| + }
|
| +
|
| + 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 AutofillPaymentInstrumentTest : public testing::Test {
|
| protected:
|
| AutofillPaymentInstrumentTest()
|
| @@ -157,4 +279,56 @@ TEST_F(AutofillPaymentInstrumentTest, IsValidForCanMakePayment_NoNumber) {
|
| EXPECT_FALSE(instrument.IsValidForCanMakePayment());
|
| }
|
|
|
| +// Tests that the autofill instrument only calls OnInstrumentDetailsReady when
|
| +// the billing address has been normalized and the card has been unmasked.
|
| +TEST_F(AutofillPaymentInstrumentTest,
|
| + InvokePaymentApp_NormalizationBeforeUnmask) {
|
| + FakePaymentRequestDelegate delegate;
|
| +
|
| + autofill::CreditCard& card = local_credit_card();
|
| + card.SetNumber(base::ASCIIToUTF16(""));
|
| + AutofillPaymentInstrument instrument("visa", card, billing_profiles(),
|
| + "en-US", &delegate);
|
| +
|
| + FakePaymentInstrumentDelegate instrument_delegate;
|
| +
|
| + instrument.InvokePaymentApp(&instrument_delegate);
|
| + EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
|
| + EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());
|
| +
|
| + delegate.GetTestAddressNormalizer()->CompleteAddressNormalization();
|
| + EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
|
| + EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());
|
| +
|
| + delegate.CompleteFullCardRequest();
|
| + EXPECT_TRUE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
|
| + EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());
|
| +}
|
| +
|
| +// Tests that the autofill instrument only calls OnInstrumentDetailsReady when
|
| +// the billing address has been normalized and the card has been unmasked.
|
| +TEST_F(AutofillPaymentInstrumentTest,
|
| + InvokePaymentApp_UnmaskBeforeNormalization) {
|
| + FakePaymentRequestDelegate delegate;
|
| +
|
| + autofill::CreditCard& card = local_credit_card();
|
| + card.SetNumber(base::ASCIIToUTF16(""));
|
| + AutofillPaymentInstrument instrument("visa", card, billing_profiles(),
|
| + "en-US", &delegate);
|
| +
|
| + FakePaymentInstrumentDelegate instrument_delegate;
|
| +
|
| + instrument.InvokePaymentApp(&instrument_delegate);
|
| + EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
|
| + EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());
|
| +
|
| + delegate.CompleteFullCardRequest();
|
| + EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
|
| + EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());
|
| +
|
| + delegate.GetTestAddressNormalizer()->CompleteAddressNormalization();
|
| + EXPECT_TRUE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
|
| + EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());
|
| +}
|
| +
|
| } // namespace payments
|
|
|