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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
// 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_trigger_validator_impl.h"
#include "chrome/browser/fast_checkout/fast_checkout_personal_data_helper.h"
#include "chrome/browser/fast_checkout/mock_fast_checkout_capabilities_fetcher.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/autofill/content/browser/test_autofill_client_injector.h"
#include "components/autofill/content/browser/test_autofill_driver_injector.h"
#include "components/autofill/content/browser/test_autofill_manager_injector.h"
#include "components/autofill/content/browser/test_content_autofill_client.h"
#include "components/autofill/content/browser/test_content_autofill_driver.h"
#include "components/autofill/core/browser/data_manager/test_personal_data_manager.h"
#include "components/autofill/core/browser/foundations/test_autofill_client.h"
#include "components/autofill/core/browser/foundations/test_autofill_driver.h"
#include "components/autofill/core/browser/foundations/test_browser_autofill_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::FastCheckoutTriggerOutcome;
using ::autofill::FastCheckoutUIState;
using ::testing::Return;
class MockBrowserAutofillManager : public autofill::TestBrowserAutofillManager {
public:
using autofill::TestBrowserAutofillManager::TestBrowserAutofillManager;
MOCK_METHOD(bool, CanShowAutofillUi, (), (const override));
};
class MockAutofillClient : public autofill::TestContentAutofillClient {
public:
using autofill::TestContentAutofillClient::TestContentAutofillClient;
MOCK_METHOD(autofill::LogManager*, GetCurrentLogManager, (), (override));
MOCK_METHOD(bool, IsContextSecure, (), (const override));
MOCK_METHOD(GeoIpCountryCode,
GetVariationConfigCountryCode,
(),
(const override));
};
class MockPersonalDataHelper : public FastCheckoutPersonalDataHelper {
public:
MockPersonalDataHelper() = default;
~MockPersonalDataHelper() override = default;
MOCK_METHOD(std::vector<const autofill::CreditCard*>,
GetValidCreditCards,
(),
(const override));
MOCK_METHOD(std::vector<const autofill::AutofillProfile*>,
GetValidAddressProfiles,
(),
(const override));
MOCK_METHOD(autofill::PersonalDataManager*,
GetPersonalDataManager,
(),
(const override));
MOCK_METHOD(std::vector<const autofill::AutofillProfile*>,
GetProfilesToSuggest,
(),
(const override));
MOCK_METHOD(std::vector<const autofill::CreditCard*>,
GetCreditCardsToSuggest,
(),
(const override));
};
class FastCheckoutTriggerValidatorTest
: public ChromeRenderViewHostTestHarness {
public:
FastCheckoutTriggerValidatorTest() = default;
protected:
void SetUp() override {
content::RenderViewHostTestHarness::SetUp();
capabilities_fetcher_ =
std::make_unique<MockFastCheckoutCapabilitiesFetcher>();
personal_data_helper_ = std::make_unique<MockPersonalDataHelper>();
validator_ = std::make_unique<FastCheckoutTriggerValidatorImpl>(
autofill_client(), capabilities_fetcher(), personal_data_helper());
credit_card_ = autofill::test::GetCreditCard();
profile_ = autofill::test::GetFullProfile();
// Creates the AutofillDriver and AutofillManager.
NavigateAndCommit(GURL("about:blank"));
ON_CALL(*autofill_manager(), CanShowAutofillUi).WillByDefault(Return(true));
ON_CALL(*capabilities_fetcher(), IsTriggerFormSupported)
.WillByDefault(Return(true));
ON_CALL(*personal_data_helper(), GetValidCreditCards)
.WillByDefault(
Return(std::vector<const autofill::CreditCard*>{&credit_card_}));
ON_CALL(*personal_data_helper(), GetValidAddressProfiles)
.WillByDefault(
Return(std::vector<const autofill::AutofillProfile*>{&profile_}));
ON_CALL(*personal_data_helper(), GetPersonalDataManager)
.WillByDefault(Return(&pdm()));
ON_CALL(*autofill_client(), IsContextSecure).WillByDefault(Return(true));
ON_CALL(*autofill_client(), GetVariationConfigCountryCode)
.WillByDefault(Return(GeoIpCountryCode("US")));
pdm().test_address_data_manager().SetAutofillProfileEnabled(true);
pdm().test_payments_data_manager().SetAutofillPaymentMethodsEnabled(true);
}
autofill::TestPersonalDataManager& pdm() { return pdm_; }
MockAutofillClient* autofill_client() {
return autofill_client_injector_[web_contents()];
}
MockFastCheckoutCapabilitiesFetcher* capabilities_fetcher() {
return capabilities_fetcher_.get();
}
MockPersonalDataHelper* personal_data_helper() {
return personal_data_helper_.get();
}
MockBrowserAutofillManager* autofill_manager() {
return autofill_manager_injector_[web_contents()];
}
FastCheckoutTriggerValidatorImpl* validator() { return validator_.get(); }
FastCheckoutTriggerOutcome ShouldRun() {
return validator()->ShouldRun(form_, field_, ui_state_, is_running_,
*autofill_manager());
}
// Protected for access in tests below.
autofill::FormFieldData field_;
FastCheckoutUIState ui_state_ = FastCheckoutUIState::kNotShownYet;
bool is_running_ = false;
base::HistogramTester histogram_tester_;
private:
autofill::AutofillProfile profile_{
autofill::i18n_model_definition::kLegacyHierarchyCountryCode};
autofill::CreditCard credit_card_;
autofill::FormData form_;
autofill::TestAutofillClientInjector<MockAutofillClient>
autofill_client_injector_;
autofill::TestAutofillDriverInjector<autofill::TestContentAutofillDriver>
autofill_driver_injector_;
autofill::TestAutofillManagerInjector<MockBrowserAutofillManager>
autofill_manager_injector_;
std::unique_ptr<FastCheckoutTriggerValidatorImpl> validator_;
std::unique_ptr<MockFastCheckoutCapabilitiesFetcher> capabilities_fetcher_;
std::unique_ptr<MockPersonalDataHelper> personal_data_helper_;
autofill::TestPersonalDataManager pdm_;
};
TEST_F(FastCheckoutTriggerValidatorTest, ShouldRun_AllChecksPass_ReturnsTrue) {
EXPECT_EQ(ShouldRun(), FastCheckoutTriggerOutcome::kSuccess);
}
TEST_F(FastCheckoutTriggerValidatorTest,
ShouldRun_AlreadyRunning_ReturnsFalse) {
is_running_ = true;
EXPECT_EQ(ShouldRun(), FastCheckoutTriggerOutcome::kUnsupportedFieldType);
}
TEST_F(FastCheckoutTriggerValidatorTest,
ShouldRun_NotContextSecure_ReturnsFalse) {
ON_CALL(*autofill_client(), IsContextSecure).WillByDefault(Return(false));
EXPECT_EQ(ShouldRun(), FastCheckoutTriggerOutcome::kUnsupportedFieldType);
}
TEST_F(FastCheckoutTriggerValidatorTest, ShouldRun_NoTriggerForm_ReturnsFalse) {
ON_CALL(*capabilities_fetcher(), IsTriggerFormSupported)
.WillByDefault(Return(false));
EXPECT_EQ(ShouldRun(), FastCheckoutTriggerOutcome::kUnsupportedFieldType);
}
TEST_F(FastCheckoutTriggerValidatorTest, ShouldRun_UiIsShowing_ReturnsFalse) {
ui_state_ = FastCheckoutUIState::kIsShowing;
EXPECT_EQ(ShouldRun(), FastCheckoutTriggerOutcome::kFailureShownBefore);
}
TEST_F(FastCheckoutTriggerValidatorTest, ShouldRun_UiWasShown_ReturnsFalse) {
ui_state_ = FastCheckoutUIState::kWasShown;
EXPECT_EQ(ShouldRun(), FastCheckoutTriggerOutcome::kFailureShownBefore);
}
TEST_F(FastCheckoutTriggerValidatorTest,
ShouldRun_FieldNotFocusable_ReturnsFalse) {
field_.set_is_focusable(false);
EXPECT_EQ(ShouldRun(), FastCheckoutTriggerOutcome::kFailureFieldNotFocusable);
}
TEST_F(FastCheckoutTriggerValidatorTest, ShouldRun_FieldHasValue_ReturnsFalse) {
field_.set_value(u"value");
EXPECT_EQ(ShouldRun(), FastCheckoutTriggerOutcome::kFailureFieldNotEmpty);
}
TEST_F(FastCheckoutTriggerValidatorTest,
ShouldRun_CannotShowAutofillUi_ReturnsFalse) {
ON_CALL(*autofill_manager(), CanShowAutofillUi).WillByDefault(Return(false));
EXPECT_EQ(ShouldRun(),
FastCheckoutTriggerOutcome::kFailureCannotShowAutofillUi);
}
TEST_F(FastCheckoutTriggerValidatorTest,
ShouldRun_AutofillProfileDisabled_ReturnsFalse) {
pdm().test_address_data_manager().SetAutofillProfileEnabled(false);
EXPECT_EQ(ShouldRun(),
FastCheckoutTriggerOutcome::kFailureAutofillProfileDisabled);
}
TEST_F(FastCheckoutTriggerValidatorTest,
ShouldRun_CreditCardDisabled_ReturnsFalse) {
pdm().test_payments_data_manager().SetAutofillPaymentMethodsEnabled(false);
EXPECT_EQ(ShouldRun(),
FastCheckoutTriggerOutcome::kFailureAutofillCreditCardDisabled);
}
TEST_F(FastCheckoutTriggerValidatorTest,
ShouldRun_NoValidAddressProfiles_ReturnsFalse) {
ON_CALL(*personal_data_helper(), GetValidAddressProfiles)
.WillByDefault(Return(std::vector<const autofill::AutofillProfile*>{}));
EXPECT_EQ(ShouldRun(),
FastCheckoutTriggerOutcome::kFailureNoValidAutofillProfile);
}
TEST_F(FastCheckoutTriggerValidatorTest,
ShouldRun_NoValidCreditCards_ReturnsFalse) {
ON_CALL(*personal_data_helper(), GetValidCreditCards)
.WillByDefault(Return(std::vector<const autofill::CreditCard*>{}));
EXPECT_EQ(ShouldRun(), FastCheckoutTriggerOutcome::kFailureNoValidCreditCard);
}
} // namespace
|