Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(384)

Unified Diff: components/payments/core/autofill_payment_instrument_unittest.cc

Issue 2842463002: [Payments] Normalize billing address for response on Desktop. (Closed)
Patch Set: iOS fix Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a52a71a75fc7a39c5d7ada31ee9ee7e8e002feef 100644
--- a/components/payments/core/autofill_payment_instrument_unittest.cc
+++ b/components/payments/core/autofill_payment_instrument_unittest.cc
@@ -10,12 +10,136 @@
#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()
+ : on_instrument_details_ready_called_(false),
+ on_instrument_details_error_called_(false) {}
+
+ 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_;
Mathieu 2017/04/25 19:27:42 can also use {false} or = false to initialize (not
sebsg 2017/04/25 19:35:23 Done.
+ bool on_instrument_details_error_called_;
+};
+
+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 +281,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

Powered by Google App Engine
This is Rietveld 408576698