Chromium Code Reviews| 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 "components/payments/core/autofill_payment_instrument.h" | 5 #include "components/payments/core/autofill_payment_instrument.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_profile.h" | 10 #include "components/autofill/core/browser/autofill_profile.h" |
| 11 #include "components/autofill/core/browser/autofill_test_utils.h" | 11 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" | 12 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/payments/core/address_normalizer.h" | |
| 14 #include "components/payments/core/payment_request_delegate.h" | |
| 13 #include "components/strings/grit/components_strings.h" | 15 #include "components/strings/grit/components_strings.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 16 | 18 |
| 17 namespace payments { | 19 namespace payments { |
| 18 | 20 |
| 21 namespace { | |
| 22 | |
| 23 class FakePaymentInstrumentDelegate : public PaymentInstrument::Delegate { | |
| 24 public: | |
| 25 FakePaymentInstrumentDelegate() | |
| 26 : on_instrument_details_ready_called_(false), | |
| 27 on_instrument_details_error_called_(false) {} | |
| 28 | |
| 29 void OnInstrumentDetailsReady( | |
| 30 const std::string& method_name, | |
| 31 const std::string& stringified_details) override { | |
| 32 on_instrument_details_ready_called_ = true; | |
| 33 } | |
| 34 | |
| 35 void OnInstrumentDetailsError() override { | |
| 36 on_instrument_details_error_called_ = true; | |
| 37 } | |
| 38 | |
| 39 bool WasOnInstrumentDetailsReadyCalled() { | |
| 40 return on_instrument_details_ready_called_; | |
| 41 } | |
| 42 | |
| 43 bool WasOnInstrumentDetailsErrorCalled() { | |
| 44 return on_instrument_details_error_called_; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 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.
| |
| 49 bool on_instrument_details_error_called_; | |
| 50 }; | |
| 51 | |
| 52 class FakeAddressNormalizer : public AddressNormalizer { | |
| 53 public: | |
| 54 FakeAddressNormalizer() {} | |
| 55 | |
| 56 void LoadRulesForRegion(const std::string& region_code) override {} | |
| 57 | |
| 58 bool AreRulesLoadedForRegion(const std::string& region_code) override { | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 void StartAddressNormalization( | |
| 63 const autofill::AutofillProfile& profile, | |
| 64 const std::string& region_code, | |
| 65 int timeout_seconds, | |
| 66 AddressNormalizer::Delegate* requester) override { | |
| 67 profile_ = profile; | |
| 68 requester_ = requester; | |
| 69 } | |
| 70 | |
| 71 void OnAddressValidationRulesLoaded(const std::string& region_code, | |
| 72 bool success) override {} | |
| 73 | |
| 74 void CompleteAddressNormalization() { | |
| 75 requester_->OnAddressNormalized(profile_); | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 autofill::AutofillProfile profile_; | |
| 80 AddressNormalizer::Delegate* requester_; | |
| 81 }; | |
| 82 | |
| 83 class FakePaymentRequestDelegate : public PaymentRequestDelegate { | |
| 84 public: | |
| 85 FakePaymentRequestDelegate() | |
| 86 : locale_("en-US"), last_committed_url_("https://shop.com") {} | |
| 87 void ShowDialog(PaymentRequest* request) override {} | |
| 88 | |
| 89 void CloseDialog() override {} | |
| 90 | |
| 91 void ShowErrorMessage() override {} | |
| 92 | |
| 93 autofill::PersonalDataManager* GetPersonalDataManager() override { | |
| 94 return nullptr; | |
| 95 } | |
| 96 | |
| 97 const std::string& GetApplicationLocale() const override { return locale_; } | |
| 98 | |
| 99 bool IsIncognito() const override { return false; } | |
| 100 | |
| 101 bool IsSslCertificateValid() override { return true; } | |
| 102 | |
| 103 const GURL& GetLastCommittedURL() const override { | |
| 104 return last_committed_url_; | |
| 105 } | |
| 106 | |
| 107 void DoFullCardRequest( | |
| 108 const autofill::CreditCard& credit_card, | |
| 109 base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate> | |
| 110 result_delegate) override { | |
| 111 full_card_request_card_ = credit_card; | |
| 112 full_card_result_delegate_ = result_delegate; | |
| 113 } | |
| 114 | |
| 115 AddressNormalizer* GetAddressNormalizer() override { | |
| 116 return &address_normalizer_; | |
| 117 } | |
| 118 | |
| 119 FakeAddressNormalizer* GetTestAddressNormalizer() { | |
| 120 return &address_normalizer_; | |
| 121 } | |
| 122 | |
| 123 void CompleteFullCardRequest() { | |
| 124 full_card_result_delegate_->OnFullCardRequestSucceeded( | |
| 125 full_card_request_card_, base::ASCIIToUTF16("123")); | |
| 126 } | |
| 127 | |
| 128 autofill::RegionDataLoader* GetRegionDataLoader() override { return nullptr; } | |
| 129 | |
| 130 private: | |
| 131 std::string locale_; | |
| 132 const GURL last_committed_url_; | |
| 133 FakeAddressNormalizer address_normalizer_; | |
| 134 | |
| 135 autofill::CreditCard full_card_request_card_; | |
| 136 base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate> | |
| 137 full_card_result_delegate_; | |
| 138 DISALLOW_COPY_AND_ASSIGN(FakePaymentRequestDelegate); | |
| 139 }; | |
| 140 | |
| 141 } // namespace | |
| 142 | |
| 19 class AutofillPaymentInstrumentTest : public testing::Test { | 143 class AutofillPaymentInstrumentTest : public testing::Test { |
| 20 protected: | 144 protected: |
| 21 AutofillPaymentInstrumentTest() | 145 AutofillPaymentInstrumentTest() |
| 22 : address_(autofill::test::GetFullProfile()), | 146 : address_(autofill::test::GetFullProfile()), |
| 23 local_card_(autofill::test::GetCreditCard()), | 147 local_card_(autofill::test::GetCreditCard()), |
| 24 billing_profiles_({&address_}) { | 148 billing_profiles_({&address_}) { |
| 25 local_card_.set_billing_address_id(address_.guid()); | 149 local_card_.set_billing_address_id(address_.guid()); |
| 26 } | 150 } |
| 27 | 151 |
| 28 autofill::CreditCard& local_credit_card() { return local_card_; } | 152 autofill::CreditCard& local_credit_card() { return local_card_; } |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 | 274 |
| 151 // A card with no number is not a valid instrument for canMakePayment. | 275 // A card with no number is not a valid instrument for canMakePayment. |
| 152 TEST_F(AutofillPaymentInstrumentTest, IsValidForCanMakePayment_NoNumber) { | 276 TEST_F(AutofillPaymentInstrumentTest, IsValidForCanMakePayment_NoNumber) { |
| 153 autofill::CreditCard& card = local_credit_card(); | 277 autofill::CreditCard& card = local_credit_card(); |
| 154 card.SetNumber(base::ASCIIToUTF16("")); | 278 card.SetNumber(base::ASCIIToUTF16("")); |
| 155 AutofillPaymentInstrument instrument("visa", card, billing_profiles(), | 279 AutofillPaymentInstrument instrument("visa", card, billing_profiles(), |
| 156 "en-US", nullptr); | 280 "en-US", nullptr); |
| 157 EXPECT_FALSE(instrument.IsValidForCanMakePayment()); | 281 EXPECT_FALSE(instrument.IsValidForCanMakePayment()); |
| 158 } | 282 } |
| 159 | 283 |
| 284 // Tests that the autofill instrument only calls OnInstrumentDetailsReady when | |
| 285 // the billing address has been normalized and the card has been unmasked. | |
| 286 TEST_F(AutofillPaymentInstrumentTest, | |
| 287 InvokePaymentApp_NormalizationBeforeUnmask) { | |
| 288 FakePaymentRequestDelegate delegate; | |
| 289 | |
| 290 autofill::CreditCard& card = local_credit_card(); | |
| 291 card.SetNumber(base::ASCIIToUTF16("")); | |
| 292 AutofillPaymentInstrument instrument("visa", card, billing_profiles(), | |
| 293 "en-US", &delegate); | |
| 294 | |
| 295 FakePaymentInstrumentDelegate instrument_delegate; | |
| 296 | |
| 297 instrument.InvokePaymentApp(&instrument_delegate); | |
| 298 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | |
| 299 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | |
| 300 | |
| 301 delegate.GetTestAddressNormalizer()->CompleteAddressNormalization(); | |
| 302 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | |
| 303 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | |
| 304 | |
| 305 delegate.CompleteFullCardRequest(); | |
| 306 EXPECT_TRUE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | |
| 307 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | |
| 308 } | |
| 309 | |
| 310 // Tests that the autofill instrument only calls OnInstrumentDetailsReady when | |
| 311 // the billing address has been normalized and the card has been unmasked. | |
| 312 TEST_F(AutofillPaymentInstrumentTest, | |
| 313 InvokePaymentApp_UnmaskBeforeNormalization) { | |
| 314 FakePaymentRequestDelegate delegate; | |
| 315 | |
| 316 autofill::CreditCard& card = local_credit_card(); | |
| 317 card.SetNumber(base::ASCIIToUTF16("")); | |
| 318 AutofillPaymentInstrument instrument("visa", card, billing_profiles(), | |
| 319 "en-US", &delegate); | |
| 320 | |
| 321 FakePaymentInstrumentDelegate instrument_delegate; | |
| 322 | |
| 323 instrument.InvokePaymentApp(&instrument_delegate); | |
| 324 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | |
| 325 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | |
| 326 | |
| 327 delegate.CompleteFullCardRequest(); | |
| 328 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | |
| 329 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | |
| 330 | |
| 331 delegate.GetTestAddressNormalizer()->CompleteAddressNormalization(); | |
| 332 EXPECT_TRUE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | |
| 333 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | |
| 334 } | |
| 335 | |
| 160 } // namespace payments | 336 } // namespace payments |
| OLD | NEW |