| 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" | 13 #include "components/payments/core/address_normalizer.h" |
| 14 #include "components/payments/core/payment_request_delegate.h" | 14 #include "components/payments/core/test_payment_request_delegate.h" |
| 15 #include "components/strings/grit/components_strings.h" | 15 #include "components/strings/grit/components_strings.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 | 18 |
| 19 namespace payments { | 19 namespace payments { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 class FakePaymentInstrumentDelegate : public PaymentInstrument::Delegate { | 23 class FakePaymentInstrumentDelegate : public PaymentInstrument::Delegate { |
| 24 public: | 24 public: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 bool WasOnInstrumentDetailsErrorCalled() { | 41 bool WasOnInstrumentDetailsErrorCalled() { |
| 42 return on_instrument_details_error_called_; | 42 return on_instrument_details_error_called_; |
| 43 } | 43 } |
| 44 | 44 |
| 45 private: | 45 private: |
| 46 bool on_instrument_details_ready_called_ = false; | 46 bool on_instrument_details_ready_called_ = false; |
| 47 bool on_instrument_details_error_called_ = false; | 47 bool on_instrument_details_error_called_ = false; |
| 48 }; | 48 }; |
| 49 | 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 | 50 } // namespace |
| 140 | 51 |
| 141 class AutofillPaymentInstrumentTest : public testing::Test { | 52 class AutofillPaymentInstrumentTest : public testing::Test { |
| 142 protected: | 53 protected: |
| 143 AutofillPaymentInstrumentTest() | 54 AutofillPaymentInstrumentTest() |
| 144 : address_(autofill::test::GetFullProfile()), | 55 : address_(autofill::test::GetFullProfile()), |
| 145 local_card_(autofill::test::GetCreditCard()), | 56 local_card_(autofill::test::GetCreditCard()), |
| 146 billing_profiles_({&address_}) { | 57 billing_profiles_({&address_}) { |
| 147 local_card_.set_billing_address_id(address_.guid()); | 58 local_card_.set_billing_address_id(address_.guid()); |
| 148 } | 59 } |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 card.SetNumber(base::ASCIIToUTF16("")); | 187 card.SetNumber(base::ASCIIToUTF16("")); |
| 277 AutofillPaymentInstrument instrument("visa", card, billing_profiles(), | 188 AutofillPaymentInstrument instrument("visa", card, billing_profiles(), |
| 278 "en-US", nullptr); | 189 "en-US", nullptr); |
| 279 EXPECT_FALSE(instrument.IsValidForCanMakePayment()); | 190 EXPECT_FALSE(instrument.IsValidForCanMakePayment()); |
| 280 } | 191 } |
| 281 | 192 |
| 282 // Tests that the autofill instrument only calls OnInstrumentDetailsReady when | 193 // Tests that the autofill instrument only calls OnInstrumentDetailsReady when |
| 283 // the billing address has been normalized and the card has been unmasked. | 194 // the billing address has been normalized and the card has been unmasked. |
| 284 TEST_F(AutofillPaymentInstrumentTest, | 195 TEST_F(AutofillPaymentInstrumentTest, |
| 285 InvokePaymentApp_NormalizationBeforeUnmask) { | 196 InvokePaymentApp_NormalizationBeforeUnmask) { |
| 286 FakePaymentRequestDelegate delegate; | 197 TestPaymentRequestDelegate delegate(/*personal_data_manager=*/nullptr); |
| 198 delegate.DelayFullCardRequestCompletion(); |
| 199 delegate.test_address_normalizer()->DelayNormalization(); |
| 287 | 200 |
| 288 autofill::CreditCard& card = local_credit_card(); | 201 autofill::CreditCard& card = local_credit_card(); |
| 289 card.SetNumber(base::ASCIIToUTF16("")); | 202 card.SetNumber(base::ASCIIToUTF16("")); |
| 290 AutofillPaymentInstrument instrument("visa", card, billing_profiles(), | 203 AutofillPaymentInstrument instrument("visa", card, billing_profiles(), |
| 291 "en-US", &delegate); | 204 "en-US", &delegate); |
| 292 | 205 |
| 293 FakePaymentInstrumentDelegate instrument_delegate; | 206 FakePaymentInstrumentDelegate instrument_delegate; |
| 294 | 207 |
| 295 instrument.InvokePaymentApp(&instrument_delegate); | 208 instrument.InvokePaymentApp(&instrument_delegate); |
| 296 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | 209 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); |
| 297 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | 210 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); |
| 298 | 211 |
| 299 delegate.GetTestAddressNormalizer()->CompleteAddressNormalization(); | 212 delegate.test_address_normalizer()->CompleteAddressNormalization(); |
| 300 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | 213 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); |
| 301 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | 214 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); |
| 302 | 215 |
| 303 delegate.CompleteFullCardRequest(); | 216 delegate.CompleteFullCardRequest(); |
| 304 EXPECT_TRUE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | 217 EXPECT_TRUE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); |
| 305 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | 218 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); |
| 306 } | 219 } |
| 307 | 220 |
| 308 // Tests that the autofill instrument only calls OnInstrumentDetailsReady when | 221 // Tests that the autofill instrument only calls OnInstrumentDetailsReady when |
| 309 // the billing address has been normalized and the card has been unmasked. | 222 // the billing address has been normalized and the card has been unmasked. |
| 310 TEST_F(AutofillPaymentInstrumentTest, | 223 TEST_F(AutofillPaymentInstrumentTest, |
| 311 InvokePaymentApp_UnmaskBeforeNormalization) { | 224 InvokePaymentApp_UnmaskBeforeNormalization) { |
| 312 FakePaymentRequestDelegate delegate; | 225 TestPaymentRequestDelegate delegate(/*personal_data_manager=*/nullptr); |
| 226 delegate.DelayFullCardRequestCompletion(); |
| 227 delegate.test_address_normalizer()->DelayNormalization(); |
| 313 | 228 |
| 314 autofill::CreditCard& card = local_credit_card(); | 229 autofill::CreditCard& card = local_credit_card(); |
| 315 card.SetNumber(base::ASCIIToUTF16("")); | 230 card.SetNumber(base::ASCIIToUTF16("")); |
| 316 AutofillPaymentInstrument instrument("visa", card, billing_profiles(), | 231 AutofillPaymentInstrument instrument("visa", card, billing_profiles(), |
| 317 "en-US", &delegate); | 232 "en-US", &delegate); |
| 318 | 233 |
| 319 FakePaymentInstrumentDelegate instrument_delegate; | 234 FakePaymentInstrumentDelegate instrument_delegate; |
| 320 | 235 |
| 321 instrument.InvokePaymentApp(&instrument_delegate); | 236 instrument.InvokePaymentApp(&instrument_delegate); |
| 322 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | 237 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); |
| 323 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | 238 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); |
| 324 | 239 |
| 325 delegate.CompleteFullCardRequest(); | 240 delegate.CompleteFullCardRequest(); |
| 326 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | 241 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); |
| 327 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | 242 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); |
| 328 | 243 |
| 329 delegate.GetTestAddressNormalizer()->CompleteAddressNormalization(); | 244 delegate.test_address_normalizer()->CompleteAddressNormalization(); |
| 330 EXPECT_TRUE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); | 245 EXPECT_TRUE(instrument_delegate.WasOnInstrumentDetailsReadyCalled()); |
| 331 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); | 246 EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled()); |
| 332 } | 247 } |
| 333 | 248 |
| 334 } // namespace payments | 249 } // namespace payments |
| OLD | NEW |