Chromium Code Reviews| 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 "components/payments/core/profile_util.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/guid.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "components/autofill/core/browser/autofill_profile.h" | |
| 14 #include "components/autofill/core/browser/autofill_test_utils.h" | |
| 15 #include "components/payments/core/payment_options_provider.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 using autofill::AutofillProfile; | |
| 19 | |
| 20 namespace payments { | |
| 21 namespace profile_util { | |
| 22 | |
| 23 constexpr uint32_t kRequestPayerName = 1 << 0; | |
| 24 constexpr uint32_t kRequestPayerEmail = 1 << 1; | |
| 25 constexpr uint32_t kRequestPayerPhone = 1 << 2; | |
| 26 constexpr uint32_t kRequestShipping = 1 << 3; | |
| 27 | |
| 28 class MockPaymentOptionsProvider : public PaymentOptionsProvider { | |
| 29 public: | |
| 30 MockPaymentOptionsProvider(uint32_t options) : options_(options) {} | |
| 31 | |
| 32 ~MockPaymentOptionsProvider() override {} | |
| 33 bool request_payer_name() const override { | |
| 34 return options_ & kRequestPayerName; | |
| 35 } | |
| 36 bool request_payer_email() const override { | |
| 37 return options_ & kRequestPayerEmail; | |
| 38 } | |
| 39 bool request_payer_phone() const override { | |
| 40 return options_ & kRequestPayerPhone; | |
| 41 } | |
| 42 bool request_shipping() const override { return options_ & kRequestShipping; } | |
| 43 PaymentShippingType shipping_type() const override { | |
| 44 return PaymentShippingType::SHIPPING; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 uint32_t options_; | |
| 49 }; | |
| 50 | |
| 51 AutofillProfile CreateProfileWithContactInfo(const char* name, | |
| 52 const char* email, | |
| 53 const char* phone) { | |
| 54 AutofillProfile profile(base::GenerateGUID(), "http://www.example.com/"); | |
| 55 autofill::test::SetProfileInfo(&profile, name, "", "", email, "", "", "", "", | |
| 56 "", "", "", phone); | |
| 57 return profile; | |
| 58 } | |
| 59 | |
| 60 TEST(PaymentRequestProfileUtilTest, FilterProfilesForContact) { | |
| 61 // These profiles are subset/equal, so only the first complete one is | |
| 62 // included. | |
| 63 AutofillProfile exclude_1 = | |
| 64 CreateProfileWithContactInfo("Homer", "", "5551234567"); | |
| 65 | |
| 66 AutofillProfile exclude_2 = | |
| 67 CreateProfileWithContactInfo("Homer", "homer@simpson.net", ""); | |
| 68 | |
| 69 AutofillProfile include_1 = | |
| 70 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); | |
| 71 | |
| 72 AutofillProfile exclude_3 = | |
| 73 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); | |
| 74 | |
| 75 // This profile is different, so it should also be included. Since it is | |
| 76 // less complete than |include_1|, it will appear after. | |
| 77 AutofillProfile include_2 = | |
| 78 CreateProfileWithContactInfo("Marge", "marge@simpson.net", ""); | |
| 79 | |
| 80 // This profile is different, so it should also be included. Since it is | |
| 81 // equally complete with |include_1|, it will appear before |include_2|, but | |
| 82 // after |include_1| since order is preserved amongst profiles of equal | |
| 83 // completeness. | |
| 84 AutofillProfile include_3 = CreateProfileWithContactInfo( | |
| 85 "Bart", "eatmyshorts@simpson.net", "5551234567"); | |
| 86 | |
| 87 std::vector<AutofillProfile*> profiles = {&exclude_1, &exclude_2, &include_1, | |
| 88 &exclude_3, &include_2, &include_3}; | |
| 89 | |
| 90 std::unique_ptr<PaymentOptionsProvider> provider = | |
| 91 base::MakeUnique<MockPaymentOptionsProvider>( | |
| 92 kRequestPayerName | kRequestPayerEmail | kRequestPayerPhone); | |
| 93 std::vector<AutofillProfile*> filtered = | |
| 94 FilterProfilesForContact(profiles, "en-US", provider.get()); | |
| 95 | |
| 96 ASSERT_EQ(3u, filtered.size()); | |
| 97 EXPECT_EQ(&include_1, filtered[0]); | |
| 98 EXPECT_EQ(&include_3, filtered[1]); | |
| 99 EXPECT_EQ(&include_2, filtered[2]); | |
| 100 | |
| 101 // Repeat the filter using a provider set to only request phone numbers. | |
| 102 // Under these rules, since all profiles have the same (or no) phone number, | |
| 103 // we should only see the first profile with a phone number, |exclude_1|. | |
| 104 std::unique_ptr<PaymentOptionsProvider> phone_only_provider = | |
| 105 base::MakeUnique<MockPaymentOptionsProvider>(kRequestPayerPhone); | |
| 106 std::vector<AutofillProfile*> filtered_phones = | |
| 107 FilterProfilesForContact(profiles, "en-US", phone_only_provider.get()); | |
| 108 ASSERT_EQ(1u, filtered_phones.size()); | |
| 109 EXPECT_EQ(&exclude_1, filtered_phones[0]); | |
| 110 } | |
| 111 | |
| 112 TEST(PaymentRequestProfileUtilTest, IsContactEqualOrSuperset) { | |
| 113 std::unique_ptr<PaymentOptionsProvider> provider = | |
| 114 base::MakeUnique<MockPaymentOptionsProvider>( | |
| 115 kRequestPayerName | kRequestPayerEmail | kRequestPayerPhone); | |
| 116 PaymentsProfileComparator comp("en-US", provider.get()); | |
| 117 | |
| 118 AutofillProfile p1 = | |
| 119 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); | |
| 120 | |
| 121 // Candidate subset profile is equal. | |
| 122 AutofillProfile p2 = | |
| 123 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); | |
| 124 EXPECT_TRUE(comp.IsContactEqualOrSuperset(&p1, &p2)); | |
| 125 EXPECT_TRUE(comp.IsContactEqualOrSuperset(&p2, &p1)); | |
| 126 | |
| 127 // Candidate subset profile has non-matching fields. | |
| 128 AutofillProfile p3 = CreateProfileWithContactInfo( | |
| 129 "Homer", "homer@springfieldnuclear.gov", "5551234567"); | |
| 130 EXPECT_FALSE(comp.IsContactEqualOrSuperset(&p1, &p3)); | |
| 131 EXPECT_FALSE(comp.IsContactEqualOrSuperset(&p3, &p1)); | |
| 132 | |
| 133 // Candidate subset profile is equal, except for missing fields. | |
| 134 AutofillProfile p4 = | |
| 135 CreateProfileWithContactInfo("Homer", "homer@simpson.net", ""); | |
| 136 EXPECT_TRUE(comp.IsContactEqualOrSuperset(&p1, &p4)); | |
| 137 EXPECT_FALSE(comp.IsContactEqualOrSuperset(&p4, &p1)); | |
| 138 | |
| 139 // One field is common, but each has a field which the other is missing. | |
| 140 AutofillProfile p5 = | |
| 141 CreateProfileWithContactInfo("Homer", "homer@simpson.net", ""); | |
| 142 AutofillProfile p6 = CreateProfileWithContactInfo("Homer", "", "5551234567"); | |
| 143 EXPECT_FALSE(comp.IsContactEqualOrSuperset(&p5, &p6)); | |
| 144 EXPECT_FALSE(comp.IsContactEqualOrSuperset(&p6, &p5)); | |
| 145 } | |
| 146 | |
| 147 TEST(PaymentRequestProfileUtilTest, IsContactEqualOrSuperset_WithFieldIgnored) { | |
| 148 // Discrepancies in email should be ignored throughout this test. | |
| 149 std::unique_ptr<PaymentOptionsProvider> provider = | |
| 150 base::MakeUnique<MockPaymentOptionsProvider>(kRequestPayerName | | |
| 151 kRequestPayerPhone); | |
| 152 PaymentsProfileComparator comp("en-US", provider.get()); | |
| 153 | |
| 154 AutofillProfile p1 = | |
| 155 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); | |
| 156 | |
| 157 // Candidate subset profile is equal. | |
| 158 AutofillProfile p2 = | |
| 159 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); | |
| 160 EXPECT_TRUE(comp.IsContactEqualOrSuperset(&p1, &p2)); | |
| 161 EXPECT_TRUE(comp.IsContactEqualOrSuperset(&p2, &p1)); | |
| 162 | |
| 163 // Email fields don't match, but profiles are still equal. | |
| 164 AutofillProfile p3 = CreateProfileWithContactInfo( | |
| 165 "Homer", "homer@springfieldnuclear.gov", "5551234567"); | |
| 166 EXPECT_TRUE(comp.IsContactEqualOrSuperset(&p1, &p3)); | |
| 167 EXPECT_TRUE(comp.IsContactEqualOrSuperset(&p3, &p1)); | |
| 168 | |
| 169 // Profile without an email is mutual subset of profile with an email. | |
| 170 AutofillProfile p4 = CreateProfileWithContactInfo("Homer", "", "5551234567"); | |
| 171 EXPECT_TRUE(comp.IsContactEqualOrSuperset(&p1, &p4)); | |
| 172 EXPECT_TRUE(comp.IsContactEqualOrSuperset(&p4, &p1)); | |
| 173 } | |
| 174 | |
| 175 TEST(PaymentRequestProfileUtilTest, GetContactCompletenessScore) { | |
| 176 std::unique_ptr<PaymentOptionsProvider> provider = | |
| 177 base::MakeUnique<MockPaymentOptionsProvider>(kRequestPayerName | | |
| 178 kRequestPayerPhone); | |
| 179 PaymentsProfileComparator comp("en-US", provider.get()); | |
| 180 | |
| 181 AutofillProfile p1 = | |
| 182 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); | |
| 183 // Two completeness points: One each for name and phone number, but not email | |
| 184 // as it was not requested. | |
| 185 EXPECT_EQ(2, comp.GetContactCompletenessScore(&p1)); | |
| 186 | |
|
sebsg
2017/04/04 22:17:17
Can you add one for where the profile only has eit
tmartino
2017/04/05 21:30:29
Done
| |
| 187 // Null profile returns 0. | |
| 188 EXPECT_EQ(0, comp.GetContactCompletenessScore(nullptr)); | |
| 189 } | |
| 190 | |
|
sebsg
2017/04/04 22:17:17
Can you also add a test for IsContactInfoComplete?
tmartino
2017/04/05 21:30:29
Done
| |
| 191 } // namespace profile_util | |
| 192 } // namespace payments | |
| OLD | NEW |