OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <vector> |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/ui/browser_commands.h" |
| 10 #include "chrome/browser/ui/views/payments/payment_request_browsertest_base.h" |
| 11 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
| 12 #include "chrome/test/base/ui_test_utils.h" |
| 13 #include "components/autofill/core/browser/autofill_profile.h" |
| 14 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 15 #include "components/autofill/core/browser/credit_card.h" |
| 16 #include "components/autofill/core/browser/personal_data_manager.h" |
| 17 #include "components/autofill/core/browser/test_autofill_clock.h" |
| 18 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 19 #include "content/public/test/browser_test_utils.h" |
| 20 |
| 21 namespace payments { |
| 22 |
| 23 namespace { |
| 24 |
| 25 const base::Time kSomeDate = base::Time::FromDoubleT(1484505871); |
| 26 const base::Time kSomeLaterDate = base::Time::FromDoubleT(1497552271); |
| 27 |
| 28 } // namespace |
| 29 |
| 30 class PaymentRequestAutofillInstrumentUseStatsTest |
| 31 : public PaymentRequestBrowserTestBase { |
| 32 protected: |
| 33 PaymentRequestAutofillInstrumentUseStatsTest() |
| 34 : PaymentRequestBrowserTestBase( |
| 35 "/payment_request_no_shipping_test.html") {} |
| 36 |
| 37 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(PaymentRequestAutofillInstrumentUseStatsTest); |
| 39 }; |
| 40 |
| 41 // Tests that use stats for the autofill payment instrument used in a Payment |
| 42 // Request are properly updated upon completion. |
| 43 IN_PROC_BROWSER_TEST_F(PaymentRequestAutofillInstrumentUseStatsTest, |
| 44 RecordUse) { |
| 45 autofill::TestAutofillClock test_clock; |
| 46 test_clock.SetNow(kSomeDate); |
| 47 |
| 48 // Setup a credit card with an associated billing address. |
| 49 autofill::AutofillProfile billing_address = autofill::test::GetFullProfile(); |
| 50 AddAutofillProfile(billing_address); |
| 51 autofill::CreditCard card = autofill::test::GetCreditCard(); |
| 52 card.set_billing_address_id(billing_address.guid()); |
| 53 AddCreditCard(card); // Visa. |
| 54 |
| 55 // Check that the initial use stats were set correctly. |
| 56 autofill::CreditCard* initial_card = |
| 57 GetDataManager()->GetCreditCardByGUID(card.guid()); |
| 58 EXPECT_EQ(1U, initial_card->use_count()); |
| 59 EXPECT_EQ(kSomeDate, initial_card->use_date()); |
| 60 |
| 61 autofill::AutofillProfile* initial_address = |
| 62 GetDataManager()->GetProfileByGUID(billing_address.guid()); |
| 63 EXPECT_EQ(1U, initial_address->use_count()); |
| 64 EXPECT_EQ(kSomeDate, initial_address->use_date()); |
| 65 |
| 66 // Complete the Payment Request. |
| 67 test_clock.SetNow(kSomeLaterDate); |
| 68 InvokePaymentRequestUI(); |
| 69 ResetEventObserver(DialogEvent::DIALOG_CLOSED); |
| 70 PayWithCreditCardAndWait(base::ASCIIToUTF16("123")); |
| 71 |
| 72 // Check that the usage of the card and billing address were recorded. |
| 73 autofill::CreditCard* updated_card = |
| 74 GetDataManager()->GetCreditCardByGUID(card.guid()); |
| 75 EXPECT_EQ(2U, updated_card->use_count()); |
| 76 EXPECT_EQ(kSomeLaterDate, updated_card->use_date()); |
| 77 |
| 78 autofill::AutofillProfile* updated_address = |
| 79 GetDataManager()->GetProfileByGUID(billing_address.guid()); |
| 80 EXPECT_EQ(2U, updated_address->use_count()); |
| 81 EXPECT_EQ(kSomeLaterDate, updated_address->use_date()); |
| 82 } |
| 83 |
| 84 class PaymentRequestShippingAddressUseStatsTest |
| 85 : public PaymentRequestBrowserTestBase { |
| 86 protected: |
| 87 PaymentRequestShippingAddressUseStatsTest() |
| 88 : PaymentRequestBrowserTestBase( |
| 89 "/payment_request_free_shipping_test.html") {} |
| 90 |
| 91 private: |
| 92 DISALLOW_COPY_AND_ASSIGN(PaymentRequestShippingAddressUseStatsTest); |
| 93 }; |
| 94 |
| 95 // Tests that use stats for the shipping address used in a Payment Request are |
| 96 // properly updated upon completion. |
| 97 IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressUseStatsTest, RecordUse) { |
| 98 autofill::TestAutofillClock test_clock; |
| 99 test_clock.SetNow(kSomeDate); |
| 100 |
| 101 // Create a billing address and a card that uses it. |
| 102 autofill::AutofillProfile billing_address = autofill::test::GetFullProfile(); |
| 103 AddAutofillProfile(billing_address); |
| 104 autofill::CreditCard card = autofill::test::GetCreditCard(); |
| 105 card.set_billing_address_id(billing_address.guid()); |
| 106 AddCreditCard(card); // Visa. |
| 107 |
| 108 // Create a shipping address with a higher frecency score, so that it is |
| 109 // selected as the default shipping address. |
| 110 autofill::AutofillProfile shipping_address = |
| 111 autofill::test::GetFullProfile2(); |
| 112 shipping_address.set_use_count(3); |
| 113 AddAutofillProfile(shipping_address); |
| 114 |
| 115 // Check that the initial use stats were set correctly. |
| 116 autofill::AutofillProfile* initial_shipping = |
| 117 GetDataManager()->GetProfileByGUID(shipping_address.guid()); |
| 118 EXPECT_EQ(3U, initial_shipping->use_count()); |
| 119 EXPECT_EQ(kSomeDate, initial_shipping->use_date()); |
| 120 |
| 121 // Complete the Payment Request. |
| 122 test_clock.SetNow(kSomeLaterDate); |
| 123 InvokePaymentRequestUI(); |
| 124 ResetEventObserver(DialogEvent::DIALOG_CLOSED); |
| 125 PayWithCreditCardAndWait(base::ASCIIToUTF16("123")); |
| 126 |
| 127 // Check that the usage of the profile was recorded. |
| 128 autofill::AutofillProfile* updated_shipping = |
| 129 GetDataManager()->GetProfileByGUID(shipping_address.guid()); |
| 130 EXPECT_EQ(4U, updated_shipping->use_count()); |
| 131 EXPECT_EQ(kSomeLaterDate, updated_shipping->use_date()); |
| 132 } |
| 133 |
| 134 class PaymentRequestContactAddressUseStatsTest |
| 135 : public PaymentRequestBrowserTestBase { |
| 136 protected: |
| 137 PaymentRequestContactAddressUseStatsTest() |
| 138 : PaymentRequestBrowserTestBase("/payment_request_name_test.html") {} |
| 139 |
| 140 private: |
| 141 DISALLOW_COPY_AND_ASSIGN(PaymentRequestContactAddressUseStatsTest); |
| 142 }; |
| 143 |
| 144 // Tests that use stats for the contact address used in a Payment Request are |
| 145 // properly updated upon completion. |
| 146 IN_PROC_BROWSER_TEST_F(PaymentRequestContactAddressUseStatsTest, RecordUse) { |
| 147 autofill::TestAutofillClock test_clock; |
| 148 test_clock.SetNow(kSomeDate); |
| 149 |
| 150 // Setup a credit card with an associated billing address. |
| 151 autofill::AutofillProfile billing_address = autofill::test::GetFullProfile(); |
| 152 AddAutofillProfile(billing_address); |
| 153 autofill::CreditCard card = autofill::test::GetCreditCard(); |
| 154 card.set_billing_address_id(billing_address.guid()); |
| 155 AddCreditCard(card); // Visa. |
| 156 |
| 157 // Create a contact address with a higher frecency score, so that it is |
| 158 // selected as the default contact address. |
| 159 autofill::AutofillProfile contact_address = autofill::test::GetFullProfile2(); |
| 160 contact_address.set_use_count(3); |
| 161 AddAutofillProfile(contact_address); |
| 162 |
| 163 // Check that the initial use stats were set correctly. |
| 164 autofill::AutofillProfile* initial_contact = |
| 165 GetDataManager()->GetProfileByGUID(contact_address.guid()); |
| 166 EXPECT_EQ(3U, initial_contact->use_count()); |
| 167 EXPECT_EQ(kSomeDate, initial_contact->use_date()); |
| 168 |
| 169 // Complete the Payment Request. |
| 170 test_clock.SetNow(kSomeLaterDate); |
| 171 InvokePaymentRequestUI(); |
| 172 ResetEventObserver(DialogEvent::DIALOG_CLOSED); |
| 173 PayWithCreditCardAndWait(base::ASCIIToUTF16("123")); |
| 174 |
| 175 // Check that the usage of the profile was recorded. |
| 176 autofill::AutofillProfile* updated_contact = |
| 177 GetDataManager()->GetProfileByGUID(contact_address.guid()); |
| 178 EXPECT_EQ(4U, updated_contact->use_count()); |
| 179 EXPECT_EQ(kSomeLaterDate, updated_contact->use_date()); |
| 180 } |
| 181 |
| 182 class PaymentRequestSameShippingAndContactAddressUseStatsTest |
| 183 : public PaymentRequestBrowserTestBase { |
| 184 protected: |
| 185 PaymentRequestSameShippingAndContactAddressUseStatsTest() |
| 186 : PaymentRequestBrowserTestBase( |
| 187 "/payment_request_contact_details_and_free_shipping_test.html") {} |
| 188 |
| 189 private: |
| 190 DISALLOW_COPY_AND_ASSIGN( |
| 191 PaymentRequestSameShippingAndContactAddressUseStatsTest); |
| 192 }; |
| 193 |
| 194 // Tests that use stats for an address that was used both as a shipping and |
| 195 // contact address in a Payment Request are properly updated upon completion. |
| 196 IN_PROC_BROWSER_TEST_F(PaymentRequestSameShippingAndContactAddressUseStatsTest, |
| 197 RecordUse) { |
| 198 autofill::TestAutofillClock test_clock; |
| 199 test_clock.SetNow(kSomeDate); |
| 200 |
| 201 // Setup a credit card with an associated billing address. |
| 202 autofill::AutofillProfile billing_address = autofill::test::GetFullProfile(); |
| 203 AddAutofillProfile(billing_address); |
| 204 autofill::CreditCard card = autofill::test::GetCreditCard(); |
| 205 card.set_billing_address_id(billing_address.guid()); |
| 206 AddCreditCard(card); // Visa. |
| 207 |
| 208 // Create an address with a higher frecency score, so that it is selected as |
| 209 // the default shipping and contact address. |
| 210 autofill::AutofillProfile multi_address = autofill::test::GetFullProfile2(); |
| 211 multi_address.set_use_count(3); |
| 212 AddAutofillProfile(multi_address); |
| 213 |
| 214 // Check that the initial use stats were set correctly. |
| 215 autofill::AutofillProfile* initial_multi = |
| 216 GetDataManager()->GetProfileByGUID(multi_address.guid()); |
| 217 EXPECT_EQ(3U, initial_multi->use_count()); |
| 218 EXPECT_EQ(kSomeDate, initial_multi->use_date()); |
| 219 |
| 220 // Complete the Payment Request. |
| 221 test_clock.SetNow(kSomeLaterDate); |
| 222 InvokePaymentRequestUI(); |
| 223 ResetEventObserver(DialogEvent::DIALOG_CLOSED); |
| 224 PayWithCreditCardAndWait(base::ASCIIToUTF16("123")); |
| 225 |
| 226 // Check that the usage of the profile was only recorded once. |
| 227 autofill::AutofillProfile* updated_multi = |
| 228 GetDataManager()->GetProfileByGUID(multi_address.guid()); |
| 229 EXPECT_EQ(4U, updated_multi->use_count()); |
| 230 EXPECT_EQ(kSomeLaterDate, updated_multi->use_date()); |
| 231 } |
| 232 |
| 233 } // namespace payments |
OLD | NEW |