| 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 MockPaymentOptionsProvider provider(kRequestPayerName | kRequestPayerEmail | |
| 91 kRequestPayerPhone); |
| 92 std::vector<AutofillProfile*> filtered = |
| 93 FilterProfilesForContact(profiles, "en-US", provider); |
| 94 |
| 95 ASSERT_EQ(3u, filtered.size()); |
| 96 EXPECT_EQ(&include_1, filtered[0]); |
| 97 EXPECT_EQ(&include_3, filtered[1]); |
| 98 EXPECT_EQ(&include_2, filtered[2]); |
| 99 |
| 100 // Repeat the filter using a provider set to only request phone numbers. |
| 101 // Under these rules, since all profiles have the same (or no) phone number, |
| 102 // we should only see the first profile with a phone number, |exclude_1|. |
| 103 MockPaymentOptionsProvider phone_only_provider(kRequestPayerPhone); |
| 104 std::vector<AutofillProfile*> filtered_phones = |
| 105 FilterProfilesForContact(profiles, "en-US", phone_only_provider); |
| 106 ASSERT_EQ(1u, filtered_phones.size()); |
| 107 EXPECT_EQ(&exclude_1, filtered_phones[0]); |
| 108 } |
| 109 |
| 110 TEST(PaymentRequestProfileUtilTest, IsContactEqualOrSuperset) { |
| 111 MockPaymentOptionsProvider provider(kRequestPayerName | kRequestPayerEmail | |
| 112 kRequestPayerPhone); |
| 113 PaymentsProfileComparator comp("en-US", provider); |
| 114 |
| 115 AutofillProfile p1 = |
| 116 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); |
| 117 |
| 118 // Candidate subset profile is equal. |
| 119 AutofillProfile p2 = |
| 120 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); |
| 121 EXPECT_TRUE(comp.IsContactEqualOrSuperset(p1, p2)); |
| 122 EXPECT_TRUE(comp.IsContactEqualOrSuperset(p2, p1)); |
| 123 |
| 124 // Candidate subset profile has non-matching fields. |
| 125 AutofillProfile p3 = CreateProfileWithContactInfo( |
| 126 "Homer", "homer@springfieldnuclear.gov", "5551234567"); |
| 127 EXPECT_FALSE(comp.IsContactEqualOrSuperset(p1, p3)); |
| 128 EXPECT_FALSE(comp.IsContactEqualOrSuperset(p3, p1)); |
| 129 |
| 130 // Candidate subset profile is equal, except for missing fields. |
| 131 AutofillProfile p4 = |
| 132 CreateProfileWithContactInfo("Homer", "homer@simpson.net", ""); |
| 133 EXPECT_TRUE(comp.IsContactEqualOrSuperset(p1, p4)); |
| 134 EXPECT_FALSE(comp.IsContactEqualOrSuperset(p4, p1)); |
| 135 |
| 136 // One field is common, but each has a field which the other is missing. |
| 137 AutofillProfile p5 = |
| 138 CreateProfileWithContactInfo("Homer", "homer@simpson.net", ""); |
| 139 AutofillProfile p6 = CreateProfileWithContactInfo("Homer", "", "5551234567"); |
| 140 EXPECT_FALSE(comp.IsContactEqualOrSuperset(p5, p6)); |
| 141 EXPECT_FALSE(comp.IsContactEqualOrSuperset(p6, p5)); |
| 142 } |
| 143 |
| 144 TEST(PaymentRequestProfileUtilTest, IsContactEqualOrSuperset_WithFieldIgnored) { |
| 145 // Discrepancies in email should be ignored throughout this test. |
| 146 MockPaymentOptionsProvider provider(kRequestPayerName | kRequestPayerPhone); |
| 147 PaymentsProfileComparator comp("en-US", provider); |
| 148 |
| 149 AutofillProfile p1 = |
| 150 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); |
| 151 |
| 152 // Candidate subset profile is equal. |
| 153 AutofillProfile p2 = |
| 154 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); |
| 155 EXPECT_TRUE(comp.IsContactEqualOrSuperset(p1, p2)); |
| 156 EXPECT_TRUE(comp.IsContactEqualOrSuperset(p2, p1)); |
| 157 |
| 158 // Email fields don't match, but profiles are still equal. |
| 159 AutofillProfile p3 = CreateProfileWithContactInfo( |
| 160 "Homer", "homer@springfieldnuclear.gov", "5551234567"); |
| 161 EXPECT_TRUE(comp.IsContactEqualOrSuperset(p1, p3)); |
| 162 EXPECT_TRUE(comp.IsContactEqualOrSuperset(p3, p1)); |
| 163 |
| 164 // Profile without an email is mutual subset of profile with an email. |
| 165 AutofillProfile p4 = CreateProfileWithContactInfo("Homer", "", "5551234567"); |
| 166 EXPECT_TRUE(comp.IsContactEqualOrSuperset(p1, p4)); |
| 167 EXPECT_TRUE(comp.IsContactEqualOrSuperset(p4, p1)); |
| 168 } |
| 169 |
| 170 TEST(PaymentRequestProfileUtilTest, GetContactCompletenessScore) { |
| 171 MockPaymentOptionsProvider provider(kRequestPayerName | kRequestPayerPhone); |
| 172 PaymentsProfileComparator comp("en-US", provider); |
| 173 |
| 174 // Two completeness points: One each for name and phone number, but not email |
| 175 // as it was not requested. |
| 176 AutofillProfile p1 = |
| 177 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); |
| 178 EXPECT_EQ(2, comp.GetContactCompletenessScore(&p1)); |
| 179 |
| 180 // One completeness point for name, no points for phone number (missing) or |
| 181 // email (not requested). |
| 182 AutofillProfile p2 = |
| 183 CreateProfileWithContactInfo("Homer", "homer@simpson.net", ""); |
| 184 EXPECT_EQ(1, comp.GetContactCompletenessScore(&p2)); |
| 185 |
| 186 // No completeness points, as the only field present was not requested. |
| 187 AutofillProfile p3 = |
| 188 CreateProfileWithContactInfo("", "homer@simpson.net", ""); |
| 189 EXPECT_EQ(0, comp.GetContactCompletenessScore(&p3)); |
| 190 |
| 191 // Null profile returns 0. |
| 192 EXPECT_EQ(0, comp.GetContactCompletenessScore(nullptr)); |
| 193 } |
| 194 |
| 195 TEST(PaymentRequestProfileUtilTest, IsContactInfoComplete) { |
| 196 MockPaymentOptionsProvider provider(kRequestPayerName | kRequestPayerEmail); |
| 197 PaymentsProfileComparator comp("en-US", provider); |
| 198 |
| 199 // If name and email are present, return true regardless of the (ignored) |
| 200 // phone value. |
| 201 AutofillProfile p1 = |
| 202 CreateProfileWithContactInfo("Homer", "homer@simpson.net", "5551234567"); |
| 203 AutofillProfile p2 = |
| 204 CreateProfileWithContactInfo("Homer", "homer@simpson.net", ""); |
| 205 |
| 206 EXPECT_TRUE(comp.IsContactInfoComplete(&p1)); |
| 207 EXPECT_TRUE(comp.IsContactInfoComplete(&p2)); |
| 208 |
| 209 // If name is not present, return false regardless of the (ignored) |
| 210 // phone value. |
| 211 AutofillProfile p3 = |
| 212 CreateProfileWithContactInfo("", "homer@simpson.net", "5551234567"); |
| 213 AutofillProfile p4 = |
| 214 CreateProfileWithContactInfo("", "homer@simpson.net", ""); |
| 215 |
| 216 EXPECT_FALSE(comp.IsContactInfoComplete(&p3)); |
| 217 EXPECT_FALSE(comp.IsContactInfoComplete(&p4)); |
| 218 |
| 219 // If no fields are requested, any profile (even empty or null) is complete. |
| 220 MockPaymentOptionsProvider empty_provider(0); |
| 221 PaymentsProfileComparator empty_comp("en-US", empty_provider); |
| 222 |
| 223 AutofillProfile p5 = CreateProfileWithContactInfo("", "", ""); |
| 224 |
| 225 EXPECT_TRUE(empty_comp.IsContactInfoComplete(&p1)); |
| 226 EXPECT_TRUE(empty_comp.IsContactInfoComplete(&p5)); |
| 227 EXPECT_TRUE(empty_comp.IsContactInfoComplete(nullptr)); |
| 228 } |
| 229 |
| 230 } // namespace profile_util |
| 231 } // namespace payments |
| OLD | NEW |