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