chromium/components/autofill/core/browser/autofill_optimization_guide_unittest.cc

// 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 "components/autofill/core/browser/autofill_optimization_guide.h"

#include <memory>

#include "base/ranges/algorithm.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/country_type.h"
#include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/data_model/credit_card_benefit.h"
#include "components/autofill/core/browser/data_model/credit_card_test_api.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/form_structure_test_api.h"
#include "components/autofill/core/browser/payments/constants.h"
#include "components/autofill/core/browser/payments_data_manager.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/autofill/core/common/autofill_test_utils.h"
#include "components/autofill/core/common/credit_card_network_identifiers.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_data_test_api.h"
#include "components/optimization_guide/core/optimization_guide_decider.h"
#include "components/optimization_guide/core/optimization_guide_decision.h"
#include "components/optimization_guide/core/optimization_metadata.h"
#include "components/prefs/pref_service.h"
#include "components/sync/test/test_sync_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace autofill {

CreateTestCreditCardFormData;
CreateTestIbanFormData;

class MockOptimizationGuideDecider
    : public optimization_guide::OptimizationGuideDecider {};

class AutofillOptimizationGuideTest : public testing::Test {};

TEST_F(AutofillOptimizationGuideTest, EnsureIntegratorInitializedCorrectly) {}

// Test that the `IBAN_AUTOFILL_BLOCKED` optimization type is registered when we
// have seen an IBAN form.
TEST_F(AutofillOptimizationGuideTest, IbanFieldFound_IbanAutofillBlocked) {}

// Test that the corresponding optimization types are registered in the VCN
// merchant opt-out case when a credit card form is seen, and VCNs that have an
// associated optimization guide blocklist are present.
TEST_F(AutofillOptimizationGuideTest, CreditCardFormFound_VcnMerchantOptOut) {}

// Test that the `VCN_MERCHANT_OPT_OUT_VISA` optimization type is not registered
// when we have seen a credit card form, but the network is not Visa.
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_VcnMerchantOptOut_NotVisaNetwork) {}

// Test that the `VCN_MERCHANT_OPT_OUT_VISA` optimization type is not registered
// when we have seen a credit card form, but the virtual card is an issuer-level
// enrollment
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_VcnMerchantOptOut_IssuerEnrollment) {}

// Test that the `VCN_MERCHANT_OPT_OUT_VISA` optimization type is not registered
// when we have seen a credit card form, but we do not have a virtual card on
// the account.
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_VcnMerchantOptOut_NotEnrolledInVirtualCard) {}

// Test that no optimization type is registered when we have seen a credit card
// form, and meet all of the pre-requisites for the Visa merchant opt-out
// use-case, but there is no personal data manager present.
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_VcnMerchantOptOut_NoPersonalDataManager) {}

// Test that if the field type does not correlate to any optimization type we
// have, that no optimization type is registered.
TEST_F(AutofillOptimizationGuideTest, OptimizationTypeToRegisterNotFound) {}

// Test that if the form denotes that we need to register multiple optimization
// types, all of the optimization types that we need to register will be
// registered.
TEST_F(AutofillOptimizationGuideTest,
       FormWithMultipleOptimizationTypesToRegisterFound) {}

// Test that single field suggestions are blocked when we are about to display
// suggestions for an IBAN field but the OptimizationGuideDecider denotes that
// displaying the suggestion is not allowed for the `IBAN_AUTOFILL_BLOCKED`
// optimization type.
TEST_F(AutofillOptimizationGuideTest,
       ShouldBlockSingleFieldSuggestions_IbanAutofillBlocked) {}

// Test that single field suggestions are not blocked when we are about to
// display suggestions for an IBAN field and OptimizationGuideDecider denotes
// that displaying the suggestion is allowed for the `IBAN_AUTOFILL_BLOCKED`
// use-case.
TEST_F(AutofillOptimizationGuideTest,
       ShouldNotBlockSingleFieldSuggestions_IbanAutofillBlocked) {}

// Test that single field suggestions are not blocked for the
// `IBAN_AUTOFILL_BLOCKED` use-case when the field is not an IBAN field.
TEST_F(
    AutofillOptimizationGuideTest,
    ShouldNotBlockSingleFieldSuggestions_IbanAutofillBlocked_FieldTypeForBlockingNotFound) {}

// Test that blocking a virtual card suggestion works correctly in the VCN
// merchant opt-out use-case for Visa.
TEST_F(AutofillOptimizationGuideTest,
       ShouldBlockFormFieldSuggestion_VcnMerchantOptOutVisa) {}

// Test that blocking a virtual card suggestion works correctly in the VCN
// merchant opt-out use-case for Discover.
TEST_F(AutofillOptimizationGuideTest,
       ShouldBlockFormFieldSuggestion_VcnMerchantOptOutDiscover) {}

// Test that blocking a virtual card suggestion works correctly in the VCN
// merchant opt-out use-case for Mastercard.
TEST_F(AutofillOptimizationGuideTest,
       ShouldBlockFormFieldSuggestion_VcnMerchantOptOutMastercard) {}

// Test that if the URL is not blocklisted, we do not block a virtual card
// suggestion in the VCN merchant opt-out use-case.
TEST_F(AutofillOptimizationGuideTest,
       ShouldNotBlockFormFieldSuggestion_VcnMerchantOptOut_UrlNotBlocked) {}

// Test that we do not block virtual card suggestions in the VCN merchant
// opt-out use-case if the card is an issuer-level enrollment.
TEST_F(AutofillOptimizationGuideTest,
       ShouldNotBlockFormFieldSuggestion_VcnMerchantOptOut_IssuerEnrollment) {}

// Test that we do not block the virtual card suggestion from being shown in the
// VCN merchant opt-out use-case if the network does not have a VCN merchant
// opt-out blocklist.
TEST_F(
    AutofillOptimizationGuideTest,
    ShouldNotBlockFormFieldSuggestion_VcnMerchantOptOut_NetworkDoesNotHaveBlocklist) {}

// Test that we block benefits suggestions for Capital One cards on blocked
// URLs.
TEST_F(AutofillOptimizationGuideTest,
       ShouldBlockBenefitSuggestionLabelsForCardAndUrl_CapitalOne_BlockedUrl) {}

// Test that we do not block benefits suggestions for Capital One cards on
// unblocked URLs.
TEST_F(
    AutofillOptimizationGuideTest,
    ShouldNotBlockBenefitSuggestionLabelsForCardAndUrl_CapitalOne_UnblockedUrl) {}

// Test that we do not block benefits suggestions when a kUnknown decision is
// returned.
TEST_F(
    AutofillOptimizationGuideTest,
    ShouldNotBlockBenefitSuggestionLabelsForCardAndUrl_CapitalOne_UnknownDecision) {}

// Test that we do not block benefits suggestions for non-Capital One cards on
// blocked URLs.
TEST_F(
    AutofillOptimizationGuideTest,
    ShouldNotBlockBenefitSuggestionLabelsForCardAndUrl_NonCapitalOne_BlockedUrl) {}

// Test that we do not block benefits suggestions for non-Capital One cards on
// unblocked URLs.
TEST_F(
    AutofillOptimizationGuideTest,
    ShouldNotBlockBenefitSuggestionLabelsForCardAndUrl_NonCapitalOne_UnblockedUrl) {}

// Test that the Amex category-benefit optimization types are registered when we
// have seen a credit card form and the user has an Amex card.
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_AmexCategoryBenefits) {}

// Test that the Capital One category-benefit optimization types are registered
// when we have seen a credit card form and the user has a Capital One card.
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_CapitalOneCategoryBenefits) {}

// Test that the Amex category-benefit optimization types are not registered
// when the kAutofillEnableCardBenefitsSync experiment is disabled.
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_AmexCategoryBenefits_ExperimentDisabled) {}

// Test that the Capital One category-benefit optimization types are not
// registered when the kAutofillEnableCardBenefitsSync experiment is disabled.
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_CapitalOneCategoryBenefits_ExperimentDisabled) {}

// Test the `AMOUNT_EXTRACTION_ALLOWLIST` optimization type is registered
// when the amount extraction experiment is enabled and there is at least one
// server credit card.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
    BUILDFLAG(IS_CHROMEOS)
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_AmountExtractionAllowed) {}

// Test the `AMOUNT_EXTRACTION_ALLOWLIST` optimization type is not registered
// when the amount extraction experiment is off.
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_AmountExtractionAllowed_FlagOff) {}

// Test the `AMOUNT_EXTRACTION_ALLOWLIST` optimization type is not registered
// when there is no serever credit card.
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_AmountExtractionAllowed_NoServerCreditCardFound) {}

// Test the `AMOUNT_EXTRACTION_ALLOWLIST` optimization type is not registered
// when there is no personal data manager present.
TEST_F(AutofillOptimizationGuideTest,
       CreditCardFormFound_AmountExtractionAllowed_NoPersonalDataManager) {}
#endif

// Test that the ablation site lists are registered in case the ablation
// experiment is enabled.
TEST_F(AutofillOptimizationGuideTest, AutofillAblation) {}

struct BenefitOptimizationToBenefitCategoryTestCase {};

class BenefitOptimizationToBenefitCategoryTest
    : public AutofillOptimizationGuideTest,
      public testing::WithParamInterface<
          BenefitOptimizationToBenefitCategoryTestCase> {};

// Tests that the correct benefit category is returned when a benefit
// optimization is found for a particular credit card issuer and url.
TEST_P(BenefitOptimizationToBenefitCategoryTest,
       GetBenefitCategoryForOptimizationType) {}

INSTANTIATE_TEST_SUITE_P();

}  // namespace autofill