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

// Copyright 2024 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/payments_data_manager.h"

#include <stddef.h>

#include <list>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/ranges/algorithm.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/gtest_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "components/autofill/core/browser/autofill_experiments.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/data_model/bank_account.h"
#include "components/autofill/core/browser/data_model/credit_card_art_image.h"
#include "components/autofill/core/browser/data_model/credit_card_benefit_test_api.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/metrics/autofill_metrics.h"
#include "components/autofill/core/browser/metrics/payments/mandatory_reauth_metrics.h"
#include "components/autofill/core/browser/payments_data_manager_test_api.h"
#include "components/autofill/core/browser/payments_data_manager_test_base.h"
#include "components/autofill/core/browser/personal_data_manager_test_utils.h"
#include "components/autofill/core/browser/test_autofill_clock.h"
#include "components/autofill/core/browser/ui/autofill_image_fetcher_base.h"
#include "components/autofill/core/browser/ui/suggestion.h"
#include "components/autofill/core/common/autofill_clock.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/credit_card_network_identifiers.h"
#include "components/autofill/core/common/form_data.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/signin/public/identity_manager/account_managed_status_finder.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/test/test_sync_service.h"
#include "components/version_info/version_info.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image_unittest_util.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#endif

namespace autofill {

namespace {

Pointee;

const base::Time kArbitraryTime =;
const base::Time kSomeLaterTime =;

template <typename T>
bool CompareElements(T* a, T* b) {}

template <typename T>
bool ElementsEqual(T* a, T* b) {}

// Verifies that two vectors have the same elements (according to T::Compare)
// while ignoring order. This is useful because multiple profiles or credit
// cards that are added to the SQLite DB within the same second will be returned
// in GUID (aka random) order.
template <typename T>
void ExpectSameElements(const std::vector<T*>& expectations,
                        const std::vector<T*>& results) {}

class MockPaymentsDataManagerObserver : public PaymentsDataManager::Observer {};

}  // anonymous namespace

class PaymentsDataManagerHelper : public PaymentsDataManagerTestBase {};

class MockAutofillImageFetcher : public AutofillImageFetcherBase {};
class PaymentsDataManagerTest : public PaymentsDataManagerHelper,
                                public testing::Test {};

class PaymentsDataManagerSyncTransportModeTest
    : public PaymentsDataManagerHelper,
      public testing::Test {};

// Test that server IBANs can be added and automatically loaded/cached.
TEST_F(PaymentsDataManagerTest, AddAndReloadServerIbans) {}

// Test that all (local and server) IBANs can be returned.
TEST_F(PaymentsDataManagerTest, GetIbans) {}

// Test that a local IBAN is removed from suggestions when it has a matching
// prefix and suffix (either equal or starting with) and the same length as a
// server IBAN.
TEST_F(PaymentsDataManagerTest,
       GetIbansToSuggestRemovesLocalIbanThatMatchesServerIban) {}

// Test that IBANs are ordered according to the frecency rating. All of the
// IBANs in this test case have the use count = 1.
TEST_F(PaymentsDataManagerTest, GetIbansToSuggestOrdersByFrecency) {}

TEST_F(PaymentsDataManagerTest, AddLocalIbans) {}

TEST_F(PaymentsDataManagerTest, NoIbansAddedIfDisabled) {}

TEST_F(PaymentsDataManagerTest, AddingIbanUpdatesPref) {}

TEST_F(PaymentsDataManagerTest, UpdateLocalIbans) {}

TEST_F(PaymentsDataManagerTest, RemoveLocalIbans) {}

TEST_F(PaymentsDataManagerTest, RecordIbanUsage_LocalIban) {}

TEST_F(PaymentsDataManagerTest, RecordIbanUsage_ServerIban) {}

TEST_F(PaymentsDataManagerTest, AddUpdateRemoveCreditCards) {}

TEST_F(PaymentsDataManagerTest, RecordUseOfCard) {}

// Test that UpdateLocalCvc function working as expected.
TEST_F(PaymentsDataManagerTest, UpdateLocalCvc) {}

// Test that verify add, update, remove server cvc function working as expected.
TEST_F(PaymentsDataManagerTest, ServerCvc) {}

// Test that verify clear server cvc function working as expected.
TEST_F(PaymentsDataManagerTest, ClearServerCvc) {}

// Test that a new credit card has its basic information set.
TEST_F(PaymentsDataManagerTest, AddCreditCard_BasicInformation) {}

// Test filling credit cards with unicode strings and crazy characters.
TEST_F(PaymentsDataManagerTest, AddCreditCard_CrazyCharacters) {}

// Test invalid credit card numbers typed in settings UI should be saved as-is.
TEST_F(PaymentsDataManagerTest, AddCreditCard_Invalid) {}

TEST_F(PaymentsDataManagerTest, GetCreditCardByServerId) {}

TEST_F(PaymentsDataManagerTest, UpdateUnverifiedCreditCards) {}

TEST_F(PaymentsDataManagerTest, SetUniqueCreditCardLabels) {}

TEST_F(PaymentsDataManagerTest, SetEmptyCreditCard) {}

// Tests that GetAutofillOffers returns all available offers.
TEST_F(PaymentsDataManagerTest, GetAutofillOffers) {}

// Tests that GetActiveAutofillPromoCodeOffersForOrigin returns only active and
// site-relevant promo code offers.
TEST_F(PaymentsDataManagerTest, GetActiveAutofillPromoCodeOffersForOrigin) {}

// Tests that GetAutofillOffers does not return any offers if
// |IsAutofillWalletImportEnabled()| returns |false|.
TEST_F(PaymentsDataManagerTest, GetAutofillOffers_WalletImportDisabled) {}

// Tests that GetAutofillOffers does not return any offers if
// `IsAutofillPaymentMethodsEnabled()` returns `false`.
TEST_F(PaymentsDataManagerTest, GetAutofillOffers_AutofillCreditCardDisabled) {}

// Tests that GetActiveAutofillPromoCodeOffersForOrigin does not return any
// promo code offers if |IsAutofillWalletImportEnabled()| returns |false|.
TEST_F(PaymentsDataManagerTest,
       GetActiveAutofillPromoCodeOffersForOrigin_WalletImportDisabled) {}

// Tests that GetActiveAutofillPromoCodeOffersForOrigin does not return any
// promo code offers if `IsAutofillPaymentMethodsEnabled()` returns `false`.
TEST_F(PaymentsDataManagerTest,
       GetActiveAutofillPromoCodeOffersForOrigin_AutofillCreditCardDisabled) {}

// Test that local credit cards are ordered as expected.
TEST_F(PaymentsDataManagerTest, GetCreditCardsToSuggest_LocalCardsRanking) {}

// Test that local and server cards are ordered as expected.
TEST_F(PaymentsDataManagerTest,
       GetCreditCardsToSuggest_LocalAndServerCardsRanking) {}

// Test that local and server cards are not shown if
// |kAutofillCreditCardEnabled| is set to |false|.
TEST_F(PaymentsDataManagerTest,
       GetCreditCardsToSuggest_CreditCardAutofillDisabled) {}

// Test that local and server cards are not loaded into memory on start-up if
// |kAutofillCreditCardEnabled| is set to |false|.
TEST_F(PaymentsDataManagerTest,
       GetCreditCardsToSuggest_NoCardsLoadedIfDisabled) {}

// Test that local credit cards are not added if |kAutofillCreditCardEnabled| is
// set to |false|.
TEST_F(PaymentsDataManagerTest,
       GetCreditCardsToSuggest_NoCreditCardsAddedIfDisabled) {}

// Tests that only the masked card is kept when deduping with a local duplicate
// of it or vice-versa. This is checked based on the value assigned during the
// for loop.
TEST_F(PaymentsDataManagerTest, DedupeCreditCardToSuggest_MaskedIsKept) {}

// Tests that different local and server credit cards are not deduped.
TEST_F(PaymentsDataManagerTest, DedupeCreditCardToSuggest_DifferentCards) {}

TEST_F(PaymentsDataManagerTest, DeleteLocalCreditCards) {}

TEST_F(PaymentsDataManagerTest, DeleteAllLocalCreditCards) {}

TEST_F(PaymentsDataManagerTest, LogStoredCreditCardMetrics) {}

// Test that setting a null sync service returns only local credit cards.
TEST_F(PaymentsDataManagerTest, GetCreditCards_NoSyncService) {}

// Sanity check that the mode where we use the regular, persistent storage for
// cards still works.
TEST_F(PaymentsDataManagerTest, UsePersistentServerStorage) {}

// Verify that PDM can switch at runtime between the different storages.
TEST_F(PaymentsDataManagerSyncTransportModeTest, SwitchServerStorages) {}

// Sanity check that the mode where we use the regular, persistent storage for
// cards still works.
TEST_F(PaymentsDataManagerSyncTransportModeTest,
       UseCorrectStorageForDifferentCards) {}

// Sync Transport mode is only for Win, Mac, and Linux.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
    BUILDFLAG(IS_CHROMEOS)
TEST_F(PaymentsDataManagerSyncTransportModeTest,
       ServerCardsShowInTransportMode) {}

// Make sure that the opt in is necessary to show server cards if the
// appropriate feature is disabled.
TEST_F(PaymentsDataManagerSyncTransportModeTest,
       ServerCardsShowInTransportMode_NeedOptIn) {}
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) ||
        // BUILDFLAG(IS_CHROMEOS)

// Test that ensure local data is not lost on sign-in.
// Clearing/changing the primary account is not supported on CrOS.
#if !BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(PaymentsDataManagerTest, KeepExistingLocalDataOnSignIn) {}
#endif

// Tests that all the non settings origins of autofill credit cards are cleared
// even if sync is disabled.
TEST_F(
    PaymentsDataManagerTest,
    SyncServiceInitializedWithAutofillDisabled_ClearCreditCardNonSettingsOrigins) {}

TEST_F(PaymentsDataManagerTest, ClearAllCvcs) {}

// Tests that benefit getters return expected result for active benefits.
TEST_F(PaymentsDataManagerTest, GetActiveCreditCardBenefits) {}

// Tests benefit getters will not return inactive benefits.
TEST_F(PaymentsDataManagerTest, GetInactiveCreditCardBenefits) {}

// Tests benefit getters will not return expired benefits.
TEST_F(PaymentsDataManagerTest, GetExpiredCreditCardBenefits) {}

#if BUILDFLAG(IS_ANDROID)
TEST_F(PaymentsDataManagerTest, HasMaskedBankAccounts_ExpOff) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndDisableFeature(
      features::kAutofillEnableSyncingOfPixBankAccounts);
  BankAccount bank_account1 = test::CreatePixBankAccount(1234L);
  BankAccount bank_account2 = test::CreatePixBankAccount(5678L);
  ASSERT_TRUE(GetServerDataTable()->SetMaskedBankAccounts(
      {bank_account1, bank_account2}));
  // Refresh the PaymentsDataManager. Under normal circumstances with the flag
  // on, this step would load the bank accounts from the WebDatabase.
  payments_data_manager().Refresh();
  WaitForOnPaymentsDataChanged();

  // Verify that no bank accounts are loaded into PaymentsDataManager because
  // the experiment is turned off.
  EXPECT_FALSE(payments_data_manager().HasMaskedBankAccounts());
}

TEST_F(PaymentsDataManagerTest, HasMaskedBankAccounts_PaymentMethodsDisabled) {
  base::test::ScopedFeatureList scoped_feature_list(
      features::kAutofillEnableSyncingOfPixBankAccounts);
  BankAccount bank_account1 = test::CreatePixBankAccount(1234L);
  BankAccount bank_account2 = test::CreatePixBankAccount(5678L);
  ASSERT_TRUE(GetServerDataTable()->SetMaskedBankAccounts(
      {bank_account1, bank_account2}));
  // We need to call `Refresh()` to ensure that the BankAccounts are loaded
  // again from the WebDatabase.
  payments_data_manager().Refresh();
  WaitForOnPaymentsDataChanged();

  // Disable payment methods prefs.
  prefs::SetAutofillPaymentMethodsEnabled(prefs_.get(), false);

  // Verify that no bank accounts are loaded into PaymentsDataManager because
  // the AutofillPaymentMethodsEnabled pref is set to false.
  EXPECT_FALSE(payments_data_manager().HasMaskedBankAccounts());
}

TEST_F(PaymentsDataManagerTest, HasMaskedBankAccounts_NoMaskedBankAccounts) {
  base::test::ScopedFeatureList scoped_feature_list(
      features::kAutofillEnableSyncingOfPixBankAccounts);

  // If the user doesn't have any masked bank accounts, or if the masked bank
  // accounts are not synced to PaymentsDatamanager, HasMaskedBankAccounts
  // should return false.
  EXPECT_FALSE(payments_data_manager().HasMaskedBankAccounts());
}

TEST_F(PaymentsDataManagerTest, HasMaskedBankAccounts_MaskedBankAccountsExist) {
  base::test::ScopedFeatureList scoped_feature_list(
      features::kAutofillEnableSyncingOfPixBankAccounts);
  BankAccount bank_account1 = test::CreatePixBankAccount(1234L);
  BankAccount bank_account2 = test::CreatePixBankAccount(5678L);
  ASSERT_TRUE(GetServerDataTable()->SetMaskedBankAccounts(
      {bank_account1, bank_account2}));

  // We need to call `Refresh()` to ensure that the BankAccounts are loaded
  // again from the WebDatabase.
  payments_data_manager().Refresh();
  WaitForOnPaymentsDataChanged();

  EXPECT_TRUE(payments_data_manager().HasMaskedBankAccounts());
}

TEST_F(PaymentsDataManagerTest, GetMaskedBankAccounts_ExpOff) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndDisableFeature(
      features::kAutofillEnableSyncingOfPixBankAccounts);
  BankAccount bank_account1 = test::CreatePixBankAccount(1234L);
  BankAccount bank_account2 = test::CreatePixBankAccount(5678L);
  ASSERT_TRUE(GetServerDataTable()->SetMaskedBankAccounts(
      {bank_account1, bank_account2}));
  base::span<const BankAccount> bank_accounts =
      payments_data_manager().GetMaskedBankAccounts();
  // Since the PaymentsDataManager was initialized before adding the masked
  // bank accounts to the WebDatabase, we expect GetMaskedBankAccounts to return
  // an empty list.
  EXPECT_EQ(0u, bank_accounts.size());

  // Refresh the PaymentsDataManager. Under normal circumstances with the flag
  // on, this step would load the bank accounts from the WebDatabase.
  payments_data_manager().Refresh();
  WaitForOnPaymentsDataChanged();

  // Verify that no bank accounts are loaded into PaymentsDataManager because
  // the experiment is turned off.
  bank_accounts = payments_data_manager().GetMaskedBankAccounts();
  EXPECT_EQ(0u, bank_accounts.size());
}

TEST_F(PaymentsDataManagerTest, GetMaskedBankAccounts_PaymentMethodsDisabled) {
  base::test::ScopedFeatureList scoped_feature_list(
      features::kAutofillEnableSyncingOfPixBankAccounts);
  BankAccount bank_account1 = test::CreatePixBankAccount(1234L);
  BankAccount bank_account2 = test::CreatePixBankAccount(5678L);
  ASSERT_TRUE(GetServerDataTable()->SetMaskedBankAccounts(
      {bank_account1, bank_account2}));
  // We need to call `Refresh()` to ensure that the BankAccounts are loaded
  // again from the WebDatabase.
  payments_data_manager().Refresh();
  WaitForOnPaymentsDataChanged();

  // Disable payment methods prefs.
  prefs::SetAutofillPaymentMethodsEnabled(prefs_.get(), false);

  // Verify that no bank accounts are loaded into PaymentsDataManager because
  // the AutofillPaymentMethodsEnabled pref is set to false.
  EXPECT_THAT(payments_data_manager().GetMaskedBankAccounts(),
              testing::IsEmpty());
}

TEST_F(PaymentsDataManagerTest, GetMaskedBankAccounts_DatabaseUpdated) {
  base::test::ScopedFeatureList scoped_feature_list(
      features::kAutofillEnableSyncingOfPixBankAccounts);
  BankAccount bank_account1 = test::CreatePixBankAccount(1234L);
  BankAccount bank_account2 = test::CreatePixBankAccount(5678L);
  ASSERT_TRUE(GetServerDataTable()->SetMaskedBankAccounts(
      {bank_account1, bank_account2}));

  // Since the PaymentsDataManager was initialized before adding the masked
  // bank accounts to the WebDatabase, we expect GetMaskedBankAccounts to return
  // an empty list.
  base::span<const BankAccount> bank_accounts =
      payments_data_manager().GetMaskedBankAccounts();
  EXPECT_EQ(0u, bank_accounts.size());

  // We need to call `Refresh()` to ensure that the BankAccounts are loaded
  // again from the WebDatabase.
  payments_data_manager().Refresh();
  WaitForOnPaymentsDataChanged();

  bank_accounts = payments_data_manager().GetMaskedBankAccounts();
  EXPECT_EQ(2u, bank_accounts.size());
}

TEST_F(PaymentsDataManagerTest,
       MaskedBankAccountsIconsFetched_DatabaseUpdated) {
  base::test::ScopedFeatureList scoped_feature_list(
      features::kAutofillEnableSyncingOfPixBankAccounts);
  MockAutofillImageFetcher mock_image_fetcher;
  test_api(payments_data_manager()).SetImageFetcher(&mock_image_fetcher);

  BankAccount bank_account1(1234L, u"nickname", GURL("http://www.example1.com"),
                            u"bank_name", u"account_number",
                            BankAccount::AccountType::kChecking);
  BankAccount bank_account2(5678L, u"nickname", GURL("http://www.example2.com"),
                            u"bank_name", u"account_number",
                            BankAccount::AccountType::kChecking);
  ASSERT_TRUE(GetServerDataTable()->SetMaskedBankAccounts(
      {bank_account1, bank_account2}));

  EXPECT_CALL(mock_image_fetcher, FetchImagesForURLs);

  // We need to call `Refresh()` to ensure that the BankAccounts are loaded
  // again from the WebDatabase which triggers the call to fetch icons from
  // image fetcher.
  payments_data_manager().Refresh();
  WaitForOnPaymentsDataChanged();
}
#endif  // BUILDFLAG(IS_ANDROID)

TEST_F(PaymentsDataManagerTest,
       OnBenefitsPrefChange_PrefIsOn_LoadsCardBenefits) {}

TEST_F(PaymentsDataManagerTest,
       OnAutofillPaymentsCardBenefitsPrefChange_PrefIsOff_ClearsCardBenefits) {}

// Tests that card benefits are not saved in PaymentsDataManager if the card
// benefits pref is disabled.
TEST_F(PaymentsDataManagerTest,
       OnAutofillPaymentsCardBenefits_PrefIsOff_BenefitsAreNotReturned) {}

// Tests that card benefits are not saved in PaymentsDataManager if the card
// benefits sync flag is disabled.
TEST_F(PaymentsDataManagerTest,
       OnAutofillPaymentsCardBenefits_SyncFlagIsOff_BenefitsAreNotReturned) {}

#if !BUILDFLAG(IS_IOS)
TEST_F(PaymentsDataManagerTest, AddAndGetCreditCardArtImage) {}

TEST_F(PaymentsDataManagerTest,
       TestNoImageFetchingAttemptForCardsWithInvalidCardArtUrls) {}

TEST_F(PaymentsDataManagerTest, ProcessCardArtUrlChanges) {}
#endif

// Params:
// 1. Whether the benefits toggle is turned on or off.
// 2. Whether the American Express benefits flag is enabled.
// 3. Whether the Capital One benefits flag is enabled.
class PaymentsDataManagerStartupBenefitsTest
    : public PaymentsDataManagerHelper,
      public testing::Test,
      public testing::WithParamInterface<std::tuple<bool, bool, bool>> {};

INSTANTIATE_TEST_SUITE_P();

// Tests that on startup we log the value of the card benefits pref.
TEST_P(PaymentsDataManagerStartupBenefitsTest,
       LogIsCreditCardBenefitsEnabledAtStartup) {}

// Tests that on startup if payment methods are disabled we don't log if
// benefits are enabled/disabled.
TEST_F(PaymentsDataManagerTest,
       LogIsCreditCardBenefitsEnabledAtStartup_PaymentMethodsDisabled) {}

// Tests that on startup if there is no pref service for the PaymentsDataManager
// we don't log if benefits are enabled/disabled.
TEST_F(PaymentsDataManagerTest,
       LogIsCreditCardBenefitsEnabledAtStartup_NullPrefService) {}

// Ensure that verified credit cards can be saved via
// OnAcceptedLocalCreditCardSave.
TEST_F(PaymentsDataManagerTest, OnAcceptedLocalCreditCardSaveWithVerifiedData) {}

// Ensure that new IBANs can be updated and saved via
// `OnAcceptedLocalIbanSave()`.
TEST_F(PaymentsDataManagerTest, OnAcceptedLocalIbanSave) {}

TEST_F(PaymentsDataManagerTest, IsKnownCard_MatchesMaskedServerCard) {}

TEST_F(PaymentsDataManagerTest, IsKnownCard_MatchesLocalCard) {}

TEST_F(PaymentsDataManagerTest, IsKnownCard_TypeDoesNotMatch) {}

TEST_F(PaymentsDataManagerTest, IsKnownCard_LastFourDoesNotMatch) {}

TEST_F(PaymentsDataManagerTest, IsServerCard_DuplicateOfMaskedServerCard) {}

TEST_F(PaymentsDataManagerTest, IsServerCard_AlreadyServerCard) {}

TEST_F(PaymentsDataManagerTest, IsServerCard_UniqueLocalCard) {}

#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(PaymentsDataManagerSyncTransportModeTest,
       ShouldShowCardsFromAccountOption_FlagOff) {}

TEST_F(PaymentsDataManagerSyncTransportModeTest,
       ShouldShowCardsFromAccountOption_FlagOn) {}

TEST_F(PaymentsDataManagerSyncTransportModeTest,
       ShouldSuggestServerPaymentMethods_FlagOff) {}

TEST_F(PaymentsDataManagerSyncTransportModeTest,
       ShouldSuggestServerPaymentMethods_FlagOn) {}

#else   // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS) &&
        // !BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(PaymentsDataManagerSyncTransportModeTest,
       ShouldShowCardsFromAccountOption) {
  // The method should return false if one of these is not respected:
  //   * The sync_service is not null
  //   * The sync feature is not enabled
  //   * The user has server cards
  //   * The user has not opted-in to seeing their account cards
  // Start by setting everything up, then making each of these conditions false
  // independently, one by one.

  // Set everything up so that the proposition should be shown on Desktop.

  // Set a server credit card.
  std::vector<CreditCard> server_cards;
  server_cards.emplace_back(CreditCard::RecordType::kMaskedServerCard, "c789");
  test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
                          "0005" /* American Express */, "04", "2999", "1");
  server_cards.back().SetNetworkForMaskedCard(kMasterCard);
  SetServerCards(server_cards);
  payments_data_manager().Refresh();
  WaitForOnPaymentsDataChanged();

  // Make sure the function returns false.
  EXPECT_FALSE(payments_data_manager().ShouldShowCardsFromAccountOption());

  // Set that the user already opted-in. Check that the function still returns
  // false.
  CoreAccountId account_id =
      identity_test_env_.identity_manager()->GetPrimaryAccountId(
          signin::ConsentLevel::kSignin);
  ::autofill::prefs::SetUserOptedInWalletSyncTransport(prefs_.get(), account_id,
                                                       true);
  EXPECT_FALSE(payments_data_manager().ShouldShowCardsFromAccountOption());

  // Re-opt the user out. Check that the function now returns true.
  ::autofill::prefs::SetUserOptedInWalletSyncTransport(prefs_.get(), account_id,
                                                       false);
  EXPECT_FALSE(payments_data_manager().ShouldShowCardsFromAccountOption());

  // Set that the user has no server cards. Check that the function still
  // returns false.
  SetServerCards({});
  payments_data_manager().Refresh();
  WaitForOnPaymentsDataChanged();
  EXPECT_FALSE(payments_data_manager().ShouldShowCardsFromAccountOption());

  // Re-set some server cards. Check that the function still returns false.
  SetServerCards(server_cards);
  payments_data_manager().Refresh();
  WaitForOnPaymentsDataChanged();
  EXPECT_FALSE(payments_data_manager().ShouldShowCardsFromAccountOption());

  // Set that the user enabled the sync feature. Check that the function still
  // returns false.
  sync_service_.SetSignedIn(signin::ConsentLevel::kSync);
  EXPECT_FALSE(payments_data_manager().ShouldShowCardsFromAccountOption());

  // Re-disable the sync feature. Check that the function still returns false.
  sync_service_.SetSignedIn(signin::ConsentLevel::kSignin);
  EXPECT_FALSE(payments_data_manager().ShouldShowCardsFromAccountOption());

  // Set a null sync service. Check that the function still returns false.
  payments_data_manager().SetSyncServiceForTest(nullptr);
  EXPECT_FALSE(payments_data_manager().ShouldShowCardsFromAccountOption());
}
#endif  // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS) &&
        // !BUILDFLAG(IS_CHROMEOS_ASH)

TEST_F(PaymentsDataManagerSyncTransportModeTest,
       GetPaymentsSigninStateForMetrics) {}

// On mobile, no dedicated opt-in is required for WalletSyncTransport - the
// user is always considered opted-in and thus this test doesn't make sense.
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
TEST_F(PaymentsDataManagerSyncTransportModeTest, OnUserAcceptedUpstreamOffer) {}
#endif  // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)

#if BUILDFLAG(IS_ANDROID)
TEST_F(PaymentsDataManagerTest,
       AutofillPaymentMethodsMandatoryReauthAlwaysEnabledOnAutomotive) {
  if (!base::android::BuildInfo::GetInstance()->is_automotive()) {
    GTEST_SKIP() << "This test should only run on automotive.";
  }

  EXPECT_TRUE(payments_data_manager().IsPaymentMethodsMandatoryReauthEnabled());

  EXPECT_CHECK_DEATH_WITH(
      {
        payments_data_manager().SetPaymentMethodsMandatoryReauthEnabled(false);
      },
      "This feature should not be able to be turned off on automotive "
      "devices.");

  EXPECT_TRUE(payments_data_manager().IsPaymentMethodsMandatoryReauthEnabled());
}
#endif  // BUILDFLAG(IS_ANDROID)

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
// Test that setting the `kAutofillEnablePaymentsMandatoryReauth` pref works
// correctly.
TEST_F(PaymentsDataManagerTest, AutofillPaymentMethodsMandatoryReauthEnabled) {
#if BUILDFLAG(IS_ANDROID)
  if (base::android::BuildInfo::GetInstance()->is_automotive()) {
    GTEST_SKIP() << "This test should not run on automotive.";
  }
#endif  // BUILDFLAG(IS_ANDROID)

  EXPECT_FALSE(
      payments_data_manager().IsPaymentMethodsMandatoryReauthEnabled());
  payments_data_manager().SetPaymentMethodsMandatoryReauthEnabled(true);
  EXPECT_TRUE(payments_data_manager().IsPaymentMethodsMandatoryReauthEnabled());
  payments_data_manager().SetPaymentMethodsMandatoryReauthEnabled(false);
  EXPECT_FALSE(
      payments_data_manager().IsPaymentMethodsMandatoryReauthEnabled());
}

// Test that
// `PaymentsDataManager::ShouldShowPaymentMethodsMandatoryReauthPromo()`
// only returns that we should show the promo when we are below the max counter
// limit for showing the promo.
TEST_F(
    PaymentsDataManagerTest,
    ShouldShowPaymentMethodsMandatoryReauthPromo_MaxValueForPromoShownCounterReached) {
#if BUILDFLAG(IS_ANDROID)
  if (base::android::BuildInfo::GetInstance()->is_automotive()) {
    GTEST_SKIP() << "This test should not run on automotive.";
  }
#endif  // BUILDFLAG(IS_ANDROID)

  base::HistogramTester histogram_tester;
  for (int i = 0; i < prefs::kMaxValueForMandatoryReauthPromoShownCounter;
       i++) {
    // This also verifies that ShouldShowPaymentMethodsMandatoryReauthPromo()
    // works as expected when below the max cap.
    EXPECT_TRUE(
        payments_data_manager().ShouldShowPaymentMethodsMandatoryReauthPromo());
    payments_data_manager()
        .IncrementPaymentMethodsMandatoryReauthPromoShownCounter();
  }

  EXPECT_FALSE(
      payments_data_manager().ShouldShowPaymentMethodsMandatoryReauthPromo());
  histogram_tester.ExpectUniqueSample(
      "Autofill.PaymentMethods.MandatoryReauth.CheckoutFlow."
      "ReauthOfferOptInDecision2",
      autofill_metrics::MandatoryReauthOfferOptInDecision::
          kBlockedByStrikeDatabase,
      1);
}

// Test that
// `PaymentsDataManager::ShouldShowPaymentMethodsMandatoryReauthPromo()`
// returns that we should not show the promo if the user already opted in.
TEST_F(PaymentsDataManagerTest,
       ShouldShowPaymentMethodsMandatoryReauthPromo_UserOptedInAlready) {
#if BUILDFLAG(IS_ANDROID)
  // Opt-in prompts are not shown on automotive as mandatory reauth is always
  // enabled.
  if (base::android::BuildInfo::GetInstance()->is_automotive()) {
    GTEST_SKIP() << "This test should not run on automotive.";
  }
#endif  // BUILDFLAG(IS_ANDROID)

  base::HistogramTester histogram_tester;
  // Simulate user is already opted in.
  payments_data_manager().SetPaymentMethodsMandatoryReauthEnabled(true);

  EXPECT_FALSE(
      payments_data_manager().ShouldShowPaymentMethodsMandatoryReauthPromo());
  histogram_tester.ExpectUniqueSample(
      "Autofill.PaymentMethods.MandatoryReauth.CheckoutFlow."
      "ReauthOfferOptInDecision2",
      autofill_metrics::MandatoryReauthOfferOptInDecision::kAlreadyOptedIn, 1);
}

// Test that
// `PaymentsDataManager::ShouldShowPaymentMethodsMandatoryReauthPromo()`
// returns that we should not show the promo if the user has already opted out.
TEST_F(PaymentsDataManagerTest,
       ShouldShowPaymentMethodsMandatoryReauthPromo_UserOptedOut) {
#if BUILDFLAG(IS_ANDROID)
  if (base::android::BuildInfo::GetInstance()->is_automotive()) {
    GTEST_SKIP() << "This test should not run on automotive.";
  }
#endif  // BUILDFLAG(IS_ANDROID)

  base::HistogramTester histogram_tester;
  // Simulate user is already opted out.
  payments_data_manager().SetPaymentMethodsMandatoryReauthEnabled(false);

  EXPECT_FALSE(
      payments_data_manager().ShouldShowPaymentMethodsMandatoryReauthPromo());
  histogram_tester.ExpectUniqueSample(
      "Autofill.PaymentMethods.MandatoryReauth.CheckoutFlow."
      "ReauthOfferOptInDecision2",
      autofill_metrics::MandatoryReauthOfferOptInDecision::kAlreadyOptedOut, 1);
}

#endif  // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)

TEST_F(PaymentsDataManagerTest, SaveCardLocallyIfNewWithNewCard) {}

TEST_F(PaymentsDataManagerTest, SaveCardLocallyIfNewWithExistingCard) {}

TEST_F(PaymentsDataManagerTest, GetAccountInfoForPaymentsServer) {}

TEST_F(PaymentsDataManagerTest, OnAccountsCookieDeletedByUserAction) {}

}  // namespace autofill