#include "components/autofill/core/browser/autofill_external_delegate.h"
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/feature_list.h"
#include "base/i18n/rtl.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_move_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "components/autofill/core/browser/address_data_manager.h"
#include "components/autofill/core/browser/autofill_compose_delegate.h"
#include "components/autofill/core/browser/autofill_experiments.h"
#include "components/autofill/core/browser/autofill_form_test_utils.h"
#include "components/autofill/core/browser/autofill_granular_filling_utils.h"
#include "components/autofill/core/browser/autofill_plus_address_delegate.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/browser_autofill_manager.h"
#include "components/autofill/core/browser/browser_autofill_manager_test_api.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/metrics/autofill_in_devtools_metrics.h"
#include "components/autofill/core/browser/metrics/autofill_metrics.h"
#include "components/autofill/core/browser/metrics/granular_filling_metrics.h"
#include "components/autofill/core/browser/metrics/log_event.h"
#include "components/autofill/core/browser/metrics/suggestions_list_metrics.h"
#include "components/autofill/core/browser/mock_autofill_compose_delegate.h"
#include "components/autofill/core/browser/mock_autofill_plus_address_delegate.h"
#include "components/autofill/core/browser/mock_single_field_form_fill_router.h"
#include "components/autofill/core/browser/payments/mock_iban_access_manager.h"
#include "components/autofill/core/browser/payments/payments_autofill_client.h"
#include "components/autofill/core/browser/payments/test_payments_autofill_client.h"
#include "components/autofill/core/browser/payments_data_manager.h"
#include "components/autofill/core/browser/personal_data_manager_observer.h"
#include "components/autofill/core/browser/test_autofill_client.h"
#include "components/autofill/core/browser/test_autofill_driver.h"
#include "components/autofill/core/browser/test_browser_autofill_manager.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/autofill/core/browser/ui/suggestion.h"
#include "components/autofill/core/browser/ui/suggestion_test_helpers.h"
#include "components/autofill/core/browser/ui/suggestion_type.h"
#include "components/autofill/core/common/aliases.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_data_test_api.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/mojom/autofill_types.mojom-shared.h"
#include "components/autofill/core/common/password_form_fill_data.h"
#include "components/strings/grit/components_strings.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "url/origin.h"
namespace autofill {
namespace {
CreateTestAddressFormData;
CreateTestCreditCardFormData;
CreateTestPersonalInformationFormData;
CreateTestUnclassifiedFormData;
_;
AllOf;
AnyOf;
ElementsAre;
Field;
InSequence;
Matcher;
Mock;
NiceMock;
Property;
Return;
SizeIs;
StartsWith;
SuggestionPosition;
constexpr auto kDefaultTriggerSource = …;
Suggestion CreateFieldByFieldFillingSuggestion(const std::string& guid,
FieldType fbf_type_used) { … }
Matcher<const AutofillTriggerDetails&> EqualsAutofillTriggerDetails(
AutofillTriggerDetails details) { … }
template <typename SuggestionsMatcher>
auto PopupOpenArgsAre(
SuggestionsMatcher suggestions_matcher,
AutofillSuggestionTriggerSource trigger_source = kDefaultTriggerSource) { … }
class MockCreditCardAccessManager : public CreditCardAccessManager { … };
class MockPersonalDataManager : public TestPersonalDataManager { … };
class MockAddressDataManager : public TestAddressDataManager { … };
class MockAutofillDriver : public TestAutofillDriver { … };
class MockPaymentsAutofillClient : public payments::TestPaymentsAutofillClient { … };
class MockAutofillClient : public TestAutofillClient { … };
class MockBrowserAutofillManager : public TestBrowserAutofillManager { … };
}
class AutofillExternalDelegateUnitTest : public testing::Test { … };
class AutofillExternalDelegateCardsFromAccountTest
: public AutofillExternalDelegateUnitTest { … };
TEST_F(AutofillExternalDelegateUnitTest, GetMainFillingProduct) { … }
TEST_F(AutofillExternalDelegateUnitTest, ShowEditorForNonexistingProfile) { … }
TEST_F(AutofillExternalDelegateUnitTest, ShowEditorForExistingProfile) { … }
TEST_F(AutofillExternalDelegateUnitTest, UserCancelsEditing) { … }
TEST_F(AutofillExternalDelegateUnitTest, UserCancelsEditing_ManualFallback) { … }
TEST_F(AutofillExternalDelegateUnitTest, UserSavesEdits) { … }
TEST_F(AutofillExternalDelegateUnitTest,
UserOpensEditorTwiceBeforeProfileIsPersisted) { … }
TEST_F(AutofillExternalDelegateUnitTest,
DelegateIsDestroyedBeforeUpdateIsFinished) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ShowDeleteDialogForNonexistingProfile) { … }
TEST_F(AutofillExternalDelegateUnitTest, ShowDeleteDialog) { … }
TEST_F(AutofillExternalDelegateUnitTest, UserCancelsDeletion) { … }
TEST_F(AutofillExternalDelegateUnitTest, UserCancelsDeletion_ManualFallback) { … }
TEST_F(AutofillExternalDelegateUnitTest, UserAcceptsDeletion) { … }
TEST_F(AutofillExternalDelegateUnitTest,
UserOpensDeleteDialogTwiceBeforeProfileIsDeleted) { … }
TEST_F(AutofillExternalDelegateUnitTest, TestExternalDelegateVirtualCalls) { … }
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateDataList) { … }
TEST_F(AutofillExternalDelegateUnitTest, UpdateDataListWhileShowingPopup) { … }
TEST_F(AutofillExternalDelegateUnitTest, DuplicateAutofillDatalistValues) { … }
TEST_F(AutofillExternalDelegateUnitTest, DuplicateAutocompleteDatalistValues) { … }
TEST_F(AutofillExternalDelegateUnitTest, AutofillWarnings) { … }
TEST_F(AutofillExternalDelegateUnitTest,
AutofillWarningsNotShown_WithSuggestions) { … }
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateInvalidUniqueId) { … }
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateFillsIbanEntry) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateFillsMerchantPromoCodeEntry) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateMerchantPromoCodeSuggestionsFooter) { … }
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateClearPreviewedForm) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateHidePopupAfterEditing) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateAcceptDatalistSuggestion) { … }
TEST_F(AutofillExternalDelegateUnitTest,
AutofillSuggestionAvailability_ManuallFallback) { … }
TEST_F(AutofillExternalDelegateUnitTest,
AutofillSuggestionAvailability_Autofill) { … }
TEST_F(AutofillExternalDelegateUnitTest,
AutofillSuggestionAvailability_Autocomplete) { … }
struct FillingMethodMetricsTestParams { … };
class FillingMethodMetricsUnitTest
: public AutofillExternalDelegateUnitTest,
public ::testing::WithParamInterface<FillingMethodMetricsTestParams> { … };
const FillingMethodMetricsTestParams kFillingMethodMetricsTestCases[] = …;
TEST_P(FillingMethodMetricsUnitTest, RecordFillingMethodForPopupType) { … }
INSTANTIATE_TEST_SUITE_P(…);
struct GroupFillingTestParams { … };
class GroupFillingUnitTest
: public AutofillExternalDelegateUnitTest,
public ::testing::WithParamInterface<GroupFillingTestParams> { … };
const GroupFillingTestParams kGroupFillingTestCases[] = …;
TEST_P(GroupFillingUnitTest, GroupFillingTests_FillAndPreview) { … }
INSTANTIATE_TEST_SUITE_P(…);
TEST_F(AutofillExternalDelegateUnitTest, AcceptSuggestion) { … }
TEST_F(AutofillExternalDelegateUnitTest,
TestAddressSuggestionShown_MetricsEmitted) { … }
TEST_F(AutofillExternalDelegateUnitTest, TestAddressSuggestion_FillAndPreview) { … }
TEST_F(AutofillExternalDelegateUnitTest,
AcceptFirstPopupLevelSuggestion_LogSuggestionAcceptedMetric) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateAccept_FillEverythingSuggestion_FillAndPreview) { … }
TEST_F(AutofillExternalDelegateUnitTest, AcceptSuggestion_TriggerSource) { … }
TEST_F(AutofillExternalDelegateUnitTest,
FieldByFieldFilling_SubPopup_EmitsTypeMetric) { … }
TEST_F(AutofillExternalDelegateUnitTest,
FieldByFieldFilling_RootPopup_DoNotEmitTypeMetric) { … }
TEST_F(AutofillExternalDelegateUnitTest,
FieldByFieldFilling_PreviewCreditCard) { … }
TEST_F(AutofillExternalDelegateUnitTest,
FieldByFieldFilling_FillCreditCardName) { … }
TEST_F(AutofillExternalDelegateUnitTest,
FieldByFieldFilling_FillCreditCardNumber_FetchingFailed) { … }
TEST_F(AutofillExternalDelegateUnitTest,
FieldByFieldFilling_FillCreditCardNumber_Fetched) { … }
TEST_F(AutofillExternalDelegateUnitTest,
VirtualCreditCard_ManualFallback_CreditCardForm_Preview) { … }
TEST_F(AutofillExternalDelegateUnitTest,
VirtualCreditCard_ManualFallback_NonCreditCardForm_NoPreview) { … }
TEST_F(AutofillExternalDelegateUnitTest,
VirtualCreditCard_ManualFallback_CreditCardForm_FullFormFilling) { … }
TEST_F(AutofillExternalDelegateUnitTest,
VirtualCreditCard_ManualFallback_FetchingFails) { … }
TEST_F(AutofillExternalDelegateUnitTest,
VirtualCreditCard_ManualFallback_FetchingSucceeds) { … }
struct GetLastFieldTypesToFillForSectionTestParams { … };
class GetLastFieldTypesToFillUnitTest
: public AutofillExternalDelegateUnitTest,
public ::testing::WithParamInterface<
GetLastFieldTypesToFillForSectionTestParams> { … };
const GetLastFieldTypesToFillForSectionTestParams
kGetLastFieldTypesToFillForSectionTesCases[] = …;
TEST_P(GetLastFieldTypesToFillUnitTest, LastFieldTypesToFillForSection) { … }
INSTANTIATE_TEST_SUITE_P(…);
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateFillsExistingPlusAddress) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateOffersPlusAddressCreation) { … }
TEST_F(AutofillExternalDelegateUnitTest, PlusAddressInlineSuggestionSelected) { … }
TEST_F(AutofillExternalDelegateUnitTest,
PlusAddressInlineSuggestionSelectedWithNoAddress) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ComposeSuggestion_ComposeProactiveNudge_ForwardsCaretBoundsToClient) { … }
TEST_F(
AutofillExternalDelegateUnitTest,
ComposeSuggestion_ComposeProactiveNudge_ShouldAnchorNudgeOnCaretReturnsFalse_DoNotForwardsCaretBoundsToClient) { … }
TEST_F(
AutofillExternalDelegateUnitTest,
ComposeSuggestion_ComposeProactiveNudge_CaretOutsideField_DoNotSendCaretBoundsToClient) { … }
TEST_F(
AutofillExternalDelegateUnitTest,
NonComposeSuggestion_NonComposeProactiveNudge_DoNotForwardsCaretBoundsToClient) { … }
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateOpensComposeAndFills) { … }
TEST_F(AutofillExternalDelegateUnitTest,
Compose_AcceptDisable_CallsComposeDelegate) { … }
TEST_F(AutofillExternalDelegateUnitTest,
Compose_AcceptGoToSettings_CallsComposeDelegate) { … }
TEST_F(AutofillExternalDelegateUnitTest,
Compose_AcceptNeverShowOnThisWebsiteAgain_CallsComposeDelegate) { … }
#if !BUILDFLAG(IS_IOS)
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateUndoForm) { … }
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateUndoPreviewForm) { … }
#endif
TEST_F(AutofillExternalDelegateUnitTest, ScanCreditCardMenuItem) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ScanCreditCardMetrics_SuggestionShown) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ScanCreditCardMetrics_SuggestionAccepted) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ScanCreditCardMetrics_DifferentSuggestionAccepted) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ScanCreditCardMetrics_SuggestionNotShown) { … }
TEST_F(AutofillExternalDelegateUnitTest, AutocompleteShown_MetricsEmitted) { … }
MATCHER_P(CreditCardMatches, card, "") { … }
TEST_F(AutofillExternalDelegateUnitTest, FillCreditCardForm) { … }
TEST_F(AutofillExternalDelegateUnitTest, IgnoreAutocompleteOffForAutofill) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateFillFieldWithValue_Autocomplete) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateFillFieldWithValue_MerchantPromoCode) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateFillFieldWithValue_Iban) { … }
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateFillFieldWithValue_FieldByFieldFilling) { … }
TEST_F(AutofillExternalDelegateUnitTest, AcceptVirtualCardOptionItem) { … }
TEST_F(AutofillExternalDelegateUnitTest, SelectVirtualCardOptionItem) { … }
class AutofillExternalDelegate_RemoveSuggestionTest
: public AutofillExternalDelegateUnitTest,
public ::testing::WithParamInterface<SuggestionType> { … };
const SuggestionType kRemoveSuggestionTestCases[] = …;
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(AutofillExternalDelegate_RemoveSuggestionTest, RemoveSuggestion) { … }
TEST_F(AutofillExternalDelegateCardsFromAccountTest,
ShowCardsFromAccountMetrics) { … }
TEST_F(AutofillExternalDelegateUnitTest,
RecordSuggestionTypeOnSuggestionAccepted) { … }
TEST_F(AutofillExternalDelegateUnitTest, UpdateSuggestions) { … }
#if BUILDFLAG(IS_IOS)
TEST_F(AutofillExternalDelegateCardsFromAccountTest,
ShouldDiscardOutdatedSuggestions) {
FieldGlobalId old_field_id = test::MakeFieldGlobalId();
FieldGlobalId new_field_id = test::MakeFieldGlobalId();
client().set_last_queried_field(new_field_id);
IssueOnQuery();
EXPECT_CALL(client(), ShowAutofillSuggestions).Times(0);
OnSuggestionsReturned(old_field_id, std::vector<Suggestion>());
}
#endif
class AutofillExternalDelegateUnitTestWithNewSuggestionRankingAlgorithm
: public AutofillExternalDelegateUnitTest { … };
TEST_F(AutofillExternalDelegateUnitTestWithNewSuggestionRankingAlgorithm,
SuggestionAccepted_LogSuggestionRankingDifference_CreditCard) { … }
TEST_F(
AutofillExternalDelegateUnitTestWithNewSuggestionRankingAlgorithm,
SuggestionAccepted_LogSuggestionRankingDifference_NotLoggedWhenRankingsAreTheSame) { … }
}