1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/fast_checkout/fast_checkout_personal_data_helper_impl.h"
#include "base/uuid.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/autofill/core/browser/data_manager/addresses/address_data_manager.h"
#include "components/autofill/core/browser/data_manager/payments/payments_data_manager.h"
#include "components/autofill/core/browser/data_manager/test_personal_data_manager.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using ::autofill::AutofillProfile;
using ::autofill::CreditCard;
using ::testing::Eq;
CreditCard GetEmptyCreditCard() {
CreditCard credit_card(base::Uuid::GenerateRandomV4().AsLowercaseString(),
"");
autofill::test::SetCreditCardInfo(&credit_card, /*name_on_card=*/"",
/*card_number=*/"",
autofill::test::NextMonth().c_str(),
autofill::test::NextYear().c_str(), "1");
return credit_card;
}
const AutofillProfile kProfile = autofill::test::GetFullProfile();
const AutofillProfile kIncompleteProfile =
autofill::test::GetIncompleteProfile1();
const CreditCard kCreditCard = autofill::test::GetCreditCard();
const CreditCard kEmptyCreditCard = GetEmptyCreditCard();
std::unique_ptr<KeyedService> BuildTestPersonalDataManager(
content::BrowserContext* context) {
auto personal_data_manager =
std::make_unique<autofill::TestPersonalDataManager>();
personal_data_manager->test_address_data_manager().SetAutofillProfileEnabled(
true);
personal_data_manager->test_payments_data_manager()
.SetAutofillPaymentMethodsEnabled(true);
personal_data_manager->test_payments_data_manager()
.SetAutofillWalletImportEnabled(true);
return personal_data_manager;
}
class FastCheckoutPersonalDataHelperTest
: public ChromeRenderViewHostTestHarness {
protected:
void SetUp() override {
content::RenderViewHostTestHarness::SetUp();
autofill::PersonalDataManagerFactory::GetInstance()->SetTestingFactory(
GetBrowserContext(),
base::BindRepeating(&BuildTestPersonalDataManager));
personal_data_helper_ =
std::make_unique<FastCheckoutPersonalDataHelperImpl>(web_contents());
}
FastCheckoutPersonalDataHelperImpl* personal_data_helper() {
return personal_data_helper_.get();
}
private:
std::unique_ptr<FastCheckoutPersonalDataHelperImpl> personal_data_helper_;
};
TEST_F(FastCheckoutPersonalDataHelperTest,
GetPersonalDataManager_ReturnsValidPointer) {
EXPECT_TRUE(personal_data_helper()->GetPersonalDataManager());
}
TEST_F(FastCheckoutPersonalDataHelperTest,
NoValidAddressProfiles_HasValidProfiles_ReturnsFalse) {
personal_data_helper()
->GetPersonalDataManager()
->address_data_manager()
.AddProfile(kProfile);
EXPECT_FALSE(personal_data_helper()->GetValidAddressProfiles().empty());
}
TEST_F(FastCheckoutPersonalDataHelperTest,
NoValidAddressProfiles_HasOnlyInvalidProfiles_ReturnsTrue) {
personal_data_helper()
->GetPersonalDataManager()
->address_data_manager()
.AddProfile(kIncompleteProfile);
EXPECT_TRUE(personal_data_helper()->GetValidAddressProfiles().empty());
}
TEST_F(FastCheckoutPersonalDataHelperTest,
NoValidAddressProfiles_HasValidAndInvalidProfiles_ReturnsFalse) {
personal_data_helper()
->GetPersonalDataManager()
->address_data_manager()
.AddProfile(kProfile);
personal_data_helper()
->GetPersonalDataManager()
->address_data_manager()
.AddProfile(kIncompleteProfile);
EXPECT_FALSE(personal_data_helper()->GetValidAddressProfiles().empty());
}
TEST_F(FastCheckoutPersonalDataHelperTest,
NoValidCreditCards_HasValidCreditCards_ReturnsFalse) {
personal_data_helper()
->GetPersonalDataManager()
->payments_data_manager()
.AddCreditCard(kCreditCard);
EXPECT_FALSE(personal_data_helper()->GetValidCreditCards().empty());
}
TEST_F(FastCheckoutPersonalDataHelperTest,
NoValidCreditCards_HasOnlyInvalidCreditCards_ReturnsTrue) {
personal_data_helper()
->GetPersonalDataManager()
->payments_data_manager()
.AddCreditCard(kEmptyCreditCard);
EXPECT_TRUE(personal_data_helper()->GetValidCreditCards().empty());
}
TEST_F(FastCheckoutPersonalDataHelperTest,
NoValidCreditCards_HasValidAndInvalidCreditCards_ReturnsFalse) {
personal_data_helper()
->GetPersonalDataManager()
->payments_data_manager()
.AddCreditCard(kCreditCard);
personal_data_helper()
->GetPersonalDataManager()
->payments_data_manager()
.AddCreditCard(kEmptyCreditCard);
EXPECT_FALSE(personal_data_helper()->GetValidCreditCards().empty());
}
TEST_F(FastCheckoutPersonalDataHelperTest,
GetCreditCardsToSuggest_ReturnsOnlyCardsWithNumber) {
personal_data_helper()
->GetPersonalDataManager()
->payments_data_manager()
.AddCreditCard(kCreditCard);
personal_data_helper()
->GetPersonalDataManager()
->payments_data_manager()
.AddCreditCard(kEmptyCreditCard);
std::vector<const autofill::CreditCard*> cards =
personal_data_helper()->GetCreditCardsToSuggest();
EXPECT_EQ(cards.size(), 1UL);
EXPECT_EQ(*cards.front(), kCreditCard);
}
TEST_F(FastCheckoutPersonalDataHelperTest,
GetProfilesToSuggest_ReturnsAllProfiles) {
personal_data_helper()
->GetPersonalDataManager()
->address_data_manager()
.AddProfile(kProfile);
personal_data_helper()
->GetPersonalDataManager()
->address_data_manager()
.AddProfile(kIncompleteProfile);
std::vector<const autofill::AutofillProfile*> profiles =
personal_data_helper()->GetProfilesToSuggest();
EXPECT_EQ(profiles.size(), 2UL);
}
} // namespace
|