#include "components/autofill/core/browser/webdata/payments/payments_sync_bridge_util.h"
#include <vector>
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/data_model/autofill_offer_data.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/data_model/autofill_wallet_usage_data.h"
#include "components/autofill/core/browser/data_model/bank_account.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_benefit_test_api.h"
#include "components/autofill/core/browser/data_model/credit_card_cloud_token_data.h"
#include "components/autofill/core/browser/payments/payments_customer_data.h"
#include "components/autofill/core/browser/webdata/payments/payments_autofill_table.h"
#include "components/autofill/core/browser/webdata/payments/payments_sync_bridge_test_util.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/autofill/core/common/credit_card_network_identifiers.h"
#include "components/sync/base/client_tag_hash.h"
#include "components/sync/protocol/autofill_offer_specifics.pb.h"
#include "components/sync/protocol/autofill_specifics.pb.h"
#include "components/sync/protocol/autofill_wallet_usage_specifics.pb.h"
#include "components/sync/protocol/entity_data.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
namespace {
EntityChange;
EntityData;
class TestPaymentsAutofillTable : public PaymentsAutofillTable { … };
EntityData SpecificsToEntity(const sync_pb::AutofillWalletSpecifics& specifics,
const std::string& client_tag) { … }
class PaymentsSyncBridgeUtilTest : public testing::Test { … };
TEST_F(PaymentsSyncBridgeUtilTest, PopulateWalletTypesFromSyncData) { … }
class PaymentsSyncBridgeUtilCardBenefitsTest
: public PaymentsSyncBridgeUtilTest,
public testing::WithParamInterface<bool> { … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(PaymentsSyncBridgeUtilCardBenefitsTest,
PopulateWalletTypesFromSyncDataForCreditCardBenefits) { … }
TEST_F(PaymentsSyncBridgeUtilTest,
CopyRelevantWalletMetadataAndCvc_KeepLocalAddresses) { … }
TEST_F(PaymentsSyncBridgeUtilTest,
CopyRelevantWalletMetadataAndCvc_OverwriteOtherAddresses) { … }
TEST_F(PaymentsSyncBridgeUtilTest,
CopyRelevantWalletMetadataAndCvc_KeepUseStats) { … }
TEST_F(PaymentsSyncBridgeUtilTest,
CopyRelevantWalletMetadataAndCvc_KeepCredentialData) { … }
TEST_F(PaymentsSyncBridgeUtilTest, OfferSpecificsFromOfferData) { … }
TEST_F(PaymentsSyncBridgeUtilTest, OfferSpecificsFromCardLinkedOfferData) { … }
TEST_F(PaymentsSyncBridgeUtilTest, OfferSpecificsFromPromoCodeOfferData) { … }
TEST_F(PaymentsSyncBridgeUtilTest,
ShouldResetAutofillWalletData_SameDataSetSize) { … }
TEST_F(PaymentsSyncBridgeUtilTest,
ShouldResetAutofillWalletData_DifferentDataSetSize) { … }
TEST_F(PaymentsSyncBridgeUtilTest, IsOfferSpecificsValid) { … }
TEST_F(PaymentsSyncBridgeUtilTest, WalletUsageSpecificsFromWalletUsageData) { … }
TEST_F(PaymentsSyncBridgeUtilTest, VirtualCardUsageDataFromUsageSpecifics) { … }
TEST_F(PaymentsSyncBridgeUtilTest,
AutofillWalletCredentialSpecificsFromStructData) { … }
TEST_F(PaymentsSyncBridgeUtilTest, AutofillWalletStructDataFromUsageSpecifics) { … }
TEST_F(PaymentsSyncBridgeUtilTest, SetAutofillWalletSpecificsFromCardBenefit) { … }
TEST_F(PaymentsSyncBridgeUtilTest,
AutofillWalletCredentialStructDataRoundTripTest) { … }
#if BUILDFLAG(IS_ANDROID)
TEST_F(PaymentsSyncBridgeUtilTest, PopulateBankAccountFromSyncData) {
base::test::ScopedFeatureList scoped_feature_list(
features::kAutofillEnableSyncingOfPixBankAccounts);
syncer::EntityChangeList entity_data;
std::string bank_account_id = "payment_instrument:123545";
sync_pb::AutofillWalletSpecifics payment_instrument_bank_account_specifics =
CreateAutofillWalletSpecificsForBankAccount(
bank_account_id, "Pix bank account",
GURL("http://www.google.com"),
"ABC Bank",
"1234",
sync_pb::BankAccountDetails_AccountType_CHECKING);
entity_data.push_back(EntityChange::CreateAdd(
bank_account_id,
SpecificsToEntity(payment_instrument_bank_account_specifics,
"bank_account")));
BankAccount expected_bank_account(
123545, u"Pix bank account",
GURL("http://www.google.com"),
u"ABC Bank",
u"1234", BankAccount::AccountType::kChecking);
std::vector<CreditCard> wallet_cards;
std::vector<Iban> wallet_ibans;
std::vector<PaymentsCustomerData> customer_data;
std::vector<CreditCardCloudTokenData> cloud_token_data;
std::vector<BankAccount> bank_accounts;
std::vector<CreditCardBenefit> benefits;
std::vector<sync_pb::PaymentInstrument> payment_instruments;
PopulateWalletTypesFromSyncData(entity_data, wallet_cards, wallet_ibans,
customer_data, cloud_token_data,
bank_accounts, benefits, payment_instruments);
ASSERT_EQ(1u, bank_accounts.size());
EXPECT_EQ(expected_bank_account, bank_accounts.at(0));
}
TEST_F(PaymentsSyncBridgeUtilTest,
PopulateBankAccountFromSyncDataExperimentOff) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(
features::kAutofillEnableSyncingOfPixBankAccounts);
syncer::EntityChangeList entity_data;
std::string bank_account_id = "payment_instrument:123545";
sync_pb::AutofillWalletSpecifics payment_instrument_bank_account_specifics =
CreateAutofillWalletSpecificsForBankAccount(
bank_account_id, "Pix bank account",
GURL("http://www.google.com"),
"ABC Bank",
"1234",
sync_pb::BankAccountDetails_AccountType_CHECKING);
entity_data.push_back(EntityChange::CreateAdd(
bank_account_id,
SpecificsToEntity(payment_instrument_bank_account_specifics,
"bank_account")));
std::vector<CreditCard> wallet_cards;
std::vector<Iban> wallet_ibans;
std::vector<PaymentsCustomerData> customer_data;
std::vector<CreditCardCloudTokenData> cloud_token_data;
std::vector<BankAccount> bank_accounts;
std::vector<CreditCardBenefit> benefits;
std::vector<sync_pb::PaymentInstrument> payment_instruments;
PopulateWalletTypesFromSyncData(entity_data, wallet_cards, wallet_ibans,
customer_data, cloud_token_data,
bank_accounts, benefits, payment_instruments);
EXPECT_EQ(0u, bank_accounts.size());
}
TEST_F(PaymentsSyncBridgeUtilTest, BankAccountFromWalletSpecifics) {
sync_pb::AutofillWalletSpecifics payment_instrument_bank_account_specifics =
CreateAutofillWalletSpecificsForBankAccount(
"payment_instrument:123545",
"Pix bank account",
GURL("http://www.google.com"),
"ABC Bank",
"1234",
sync_pb::BankAccountDetails_AccountType_CHECKING);
BankAccount expected_bank_account(
123545, u"Pix bank account",
GURL("http://www.google.com"),
u"ABC Bank",
u"1234", BankAccount::AccountType::kChecking);
EXPECT_EQ(
expected_bank_account,
BankAccountFromWalletSpecifics(
payment_instrument_bank_account_specifics.payment_instrument()));
}
TEST_F(PaymentsSyncBridgeUtilTest,
PopulatePaymentInstrumentsFromSyncData_EwalletAccounts) {
base::test::ScopedFeatureList scoped_feature_list(
features::kAutofillSyncEwalletAccounts);
syncer::EntityChangeList entity_data;
std::string ewallet_account_id = "payment_instrument:123545";
sync_pb::AutofillWalletSpecifics
payment_instrument_ewallet_account_specifics =
CreateAutofillWalletSpecificsForEwalletAccount(
ewallet_account_id, "eWallet account",
GURL("http://www.google.com"),
"ABC Pay",
"1234",
false);
entity_data.push_back(EntityChange::CreateAdd(
ewallet_account_id,
SpecificsToEntity(payment_instrument_ewallet_account_specifics,
"ewallet_account")));
std::vector<CreditCard> wallet_cards;
std::vector<Iban> wallet_ibans;
std::vector<PaymentsCustomerData> customer_data;
std::vector<CreditCardCloudTokenData> cloud_token_data;
std::vector<BankAccount> bank_accounts;
std::vector<CreditCardBenefit> benefits;
std::vector<sync_pb::PaymentInstrument> payment_instruments;
PopulateWalletTypesFromSyncData(entity_data, wallet_cards, wallet_ibans,
customer_data, cloud_token_data,
bank_accounts, benefits, payment_instruments);
ASSERT_EQ(1u, payment_instruments.size());
sync_pb::PaymentInstrument payment_instrument = payment_instruments.at(0);
EXPECT_EQ(payment_instrument_ewallet_account_specifics.payment_instrument()
.instrument_id(),
payment_instrument.instrument_id());
EXPECT_EQ(payment_instrument_ewallet_account_specifics.payment_instrument()
.nickname(),
payment_instrument.nickname());
EXPECT_EQ(payment_instrument_ewallet_account_specifics.payment_instrument()
.display_icon_url(),
payment_instrument.display_icon_url());
EXPECT_EQ(payment_instrument_ewallet_account_specifics.payment_instrument()
.supported_rails()
.at(0),
payment_instrument.supported_rails().at(0));
EXPECT_EQ(payment_instrument_ewallet_account_specifics.payment_instrument()
.ewallet_details()
.ewallet_name(),
payment_instrument.ewallet_details().ewallet_name());
EXPECT_EQ(payment_instrument_ewallet_account_specifics.payment_instrument()
.ewallet_details()
.account_display_name(),
payment_instrument.ewallet_details().account_display_name());
EXPECT_EQ(
payment_instrument_ewallet_account_specifics.payment_instrument()
.ewallet_details()
.supported_payment_link_uris()
.at(0),
payment_instrument.ewallet_details().supported_payment_link_uris().at(0));
EXPECT_EQ(payment_instrument_ewallet_account_specifics.payment_instrument()
.device_details()
.is_fido_enrolled(),
payment_instrument.device_details().is_fido_enrolled());
}
TEST_F(PaymentsSyncBridgeUtilTest,
PopulatePaymentInstrumentsFromSyncDataExperimentOff_EwalletAccounts) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(
features::kAutofillSyncEwalletAccounts);
syncer::EntityChangeList entity_data;
std::string ewallet_account_id = "payment_instrument:123545";
sync_pb::AutofillWalletSpecifics
payment_instrument_ewallet_account_specifics =
CreateAutofillWalletSpecificsForEwalletAccount(
ewallet_account_id, "eWallet account",
GURL("http://www.google.com"),
"ABC Pay",
"1234",
false);
entity_data.push_back(EntityChange::CreateAdd(
ewallet_account_id,
SpecificsToEntity(payment_instrument_ewallet_account_specifics,
"ewallet_account")));
std::vector<CreditCard> wallet_cards;
std::vector<Iban> wallet_ibans;
std::vector<PaymentsCustomerData> customer_data;
std::vector<CreditCardCloudTokenData> cloud_token_data;
std::vector<BankAccount> bank_accounts;
std::vector<CreditCardBenefit> benefits;
std::vector<sync_pb::PaymentInstrument> payment_instruments;
PopulateWalletTypesFromSyncData(entity_data, wallet_cards, wallet_ibans,
customer_data, cloud_token_data,
bank_accounts, benefits, payment_instruments);
EXPECT_EQ(0u, payment_instruments.size());
}
#endif
struct WalletCardTypeMapping { … };
class PaymentsSyncBridgeUtilTest_WalletCardMapping
: public ::testing::TestWithParam<WalletCardTypeMapping> { … };
TEST_P(PaymentsSyncBridgeUtilTest_WalletCardMapping,
VerifyCardTypeMappingFromCardNetwork) { … }
TEST_P(PaymentsSyncBridgeUtilTest_WalletCardMapping,
VerifyCardNetworkMappingFromCardType) { … }
INSTANTIATE_TEST_SUITE_P(…);
TEST_F(PaymentsSyncBridgeUtilTest,
VerifyCardNetworkMappingFromCardType_ForVerve_WithFlagOff) { … }
TEST_F(PaymentsSyncBridgeUtilTest,
VerifyCardTypeMappingFromCardNetwork_ForVerve_WithFlagOff) { … }
}
}