chromium/components/autofill/core/browser/webdata/payments/payments_sync_bridge_util_unittest.cc

// Copyright 2018 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/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 {};

// Tests that PopulateWalletTypesFromSyncData behaves as expected.
TEST_F(PaymentsSyncBridgeUtilTest, PopulateWalletTypesFromSyncData) {}

// Test suite for benefit syncing helpers that takes a bool indicating
// whether benefit syncing should be enabled for testing.
class PaymentsSyncBridgeUtilCardBenefitsTest
    : public PaymentsSyncBridgeUtilTest,
      public testing::WithParamInterface<bool> {};

// Initializes the parameterized test suite with a bool indicating whether
// benefit syncing should be enabled for testing.
INSTANTIATE_TEST_SUITE_P();

// Tests that PopulateWalletTypesFromSyncData behaves as expected for wallet
// card and card benefit.
TEST_P(PaymentsSyncBridgeUtilCardBenefitsTest,
       PopulateWalletTypesFromSyncDataForCreditCardBenefits) {}

// Verify that the billing address id from the card saved on disk is kept if it
// is a local profile guid.
TEST_F(PaymentsSyncBridgeUtilTest,
       CopyRelevantWalletMetadataAndCvc_KeepLocalAddresses) {}

// Verify that the billing address id from the card saved on disk is overwritten
// if it does not refer to a local profile.
TEST_F(PaymentsSyncBridgeUtilTest,
       CopyRelevantWalletMetadataAndCvc_OverwriteOtherAddresses) {}

// Verify that the use stats on disk are kept when server cards are synced.
TEST_F(PaymentsSyncBridgeUtilTest,
       CopyRelevantWalletMetadataAndCvc_KeepUseStats) {}

// Verify that the credential data on disk are kept when server cards are
// synced.
TEST_F(PaymentsSyncBridgeUtilTest,
       CopyRelevantWalletMetadataAndCvc_KeepCredentialData) {}

// Test to ensure the general-purpose fields from an AutofillOfferData are
// correctly converted to an AutofillOfferSpecifics.
TEST_F(PaymentsSyncBridgeUtilTest, OfferSpecificsFromOfferData) {}

// Test to ensure the card-linked offer-specific fields from an
// AutofillOfferData are correctly converted to an AutofillOfferSpecifics.
TEST_F(PaymentsSyncBridgeUtilTest, OfferSpecificsFromCardLinkedOfferData) {}

// Test to ensure the promo code offer-specific fields from an AutofillOfferData
// are correctly converted to an AutofillOfferSpecifics.
TEST_F(PaymentsSyncBridgeUtilTest, OfferSpecificsFromPromoCodeOfferData) {}

// Ensures that the ShouldResetAutofillWalletData function works correctly, if
// the two given data sets have the same size.
TEST_F(PaymentsSyncBridgeUtilTest,
       ShouldResetAutofillWalletData_SameDataSetSize) {}

// Ensures that the ShouldResetAutofillWalletData function works correctly, if
// the two given data sets have different size.
TEST_F(PaymentsSyncBridgeUtilTest,
       ShouldResetAutofillWalletData_DifferentDataSetSize) {}

// Ensures that function IsOfferSpecificsValid is working correctly.
TEST_F(PaymentsSyncBridgeUtilTest, IsOfferSpecificsValid) {}

// Test to ensure that Wallet Usage Data for virtual card retrieval is correctly
// converted to AutofillWalletUsageSpecifics.
TEST_F(PaymentsSyncBridgeUtilTest, WalletUsageSpecificsFromWalletUsageData) {}

// Test to ensure that Wallet Usage Data for virtual card retrieval is correctly
// converted to AutofillWalletUsageSpecifics.
TEST_F(PaymentsSyncBridgeUtilTest, VirtualCardUsageDataFromUsageSpecifics) {}

// Test to ensure that WalletCredential struct data for CVV storage is correctly
// converted to AutofillWalletCredentialSpecifics.
TEST_F(PaymentsSyncBridgeUtilTest,
       AutofillWalletCredentialSpecificsFromStructData) {}

TEST_F(PaymentsSyncBridgeUtilTest, AutofillWalletStructDataFromUsageSpecifics) {}

// Test to ensure that CreditCardBenefits can be correctly converted to
// AutofillWalletSpecifics.
TEST_F(PaymentsSyncBridgeUtilTest, SetAutofillWalletSpecificsFromCardBenefit) {}

// Round trip test to ensure that WalletCredential struct data for CVV storage
// is correctly converted to AutofillWalletCredentialSpecifics and then from
// the converted AutofillWalletCredentialSpecifics to WalletCredential struct
// data. In the end we compare the original and the converted struct data.
TEST_F(PaymentsSyncBridgeUtilTest,
       AutofillWalletCredentialStructDataRoundTripTest) {}

#if BUILDFLAG(IS_ANDROID)
// Tests that PopulateWalletTypesFromSyncData populates BankAccounts.
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(
          /*client_tag=*/bank_account_id, /*nickname=*/"Pix bank account",
          /*display_icon_url=*/GURL("http://www.google.com"),
          /*bank_name=*/"ABC Bank",
          /*account_number_suffix=*/"1234",
          sync_pb::BankAccountDetails_AccountType_CHECKING);
  entity_data.push_back(EntityChange::CreateAdd(
      bank_account_id,
      SpecificsToEntity(payment_instrument_bank_account_specifics,
                        /*client_tag=*/"bank_account")));
  BankAccount expected_bank_account(
      /*instrument_id=*/123545, /*nickname=*/u"Pix bank account",
      /*display_icon_url=*/GURL("http://www.google.com"),
      /*bank_name=*/u"ABC Bank",
      /*account_number_suffix=*/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));
}

// Tests that PopulateWalletTypesFromSyncData does not BankAccounts if Pix
// experiment flag is disabled.
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(
          /*client_tag=*/bank_account_id, /*nickname=*/"Pix bank account",
          /*display_icon_url=*/GURL("http://www.google.com"),
          /*bank_name=*/"ABC Bank",
          /*account_number_suffix=*/"1234",
          sync_pb::BankAccountDetails_AccountType_CHECKING);
  entity_data.push_back(EntityChange::CreateAdd(
      bank_account_id,
      SpecificsToEntity(payment_instrument_bank_account_specifics,
                        /*client_tag=*/"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(
          /*client_tag=*/"payment_instrument:123545",
          /*nickname=*/"Pix bank account",
          /*display_icon_url=*/GURL("http://www.google.com"),
          /*bank_name=*/"ABC Bank",
          /*account_number_suffix=*/"1234",
          sync_pb::BankAccountDetails_AccountType_CHECKING);
  BankAccount expected_bank_account(
      /*instrument_id=*/123545, /*nickname=*/u"Pix bank account",
      /*display_icon_url=*/GURL("http://www.google.com"),
      /*bank_name=*/u"ABC Bank",
      /*account_number_suffix=*/u"1234", BankAccount::AccountType::kChecking);

  EXPECT_EQ(
      expected_bank_account,
      BankAccountFromWalletSpecifics(
          payment_instrument_bank_account_specifics.payment_instrument()));
}

// Tests that PopulateWalletTypesFromSyncData populates PaymentInstruments for
// eWallet accounts.
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(
              /*client_tag=*/ewallet_account_id, /*nickname=*/"eWallet account",
              /*display_icon_url=*/GURL("http://www.google.com"),
              /*ewallet_name=*/"ABC Pay",
              /*account_display_name=*/"1234",
              /*is_fido_enrolled=*/false);
  entity_data.push_back(EntityChange::CreateAdd(
      ewallet_account_id,
      SpecificsToEntity(payment_instrument_ewallet_account_specifics,
                        /*client_tag=*/"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());
}

// Tests that PopulateWalletTypesFromSyncData does not populate
// PaymentInstruments for eWallet accounts if eWallet sync experiment flag is
// disabled.
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(
              /*client_tag=*/ewallet_account_id, /*nickname=*/"eWallet account",
              /*display_icon_url=*/GURL("http://www.google.com"),
              /*ewallet_name=*/"ABC Pay",
              /*account_display_name=*/"1234",
              /*is_fido_enrolled=*/false);
  entity_data.push_back(EntityChange::CreateAdd(
      ewallet_account_id,
      SpecificsToEntity(payment_instrument_ewallet_account_specifics,
                        /*client_tag=*/"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  // BUILDFLAG(IS_ANDROID)

struct WalletCardTypeMapping {};

class PaymentsSyncBridgeUtilTest_WalletCardMapping
    : public ::testing::TestWithParam<WalletCardTypeMapping> {};

// Test to verify the correct mapping of CardType to the card network.
TEST_P(PaymentsSyncBridgeUtilTest_WalletCardMapping,
       VerifyCardTypeMappingFromCardNetwork) {}

// Test to verify the correct mapping of the card network to CardType.
TEST_P(PaymentsSyncBridgeUtilTest_WalletCardMapping,
       VerifyCardNetworkMappingFromCardType) {}

INSTANTIATE_TEST_SUITE_P();

// These two tests verify the same case as
// `PaymentsSyncBridgeUtilTest_WalletCardMapping` but with the added caveat of
// checking the Verve conversion values with `kAutofillEnableVerveCardSupport`
// flag off.
TEST_F(PaymentsSyncBridgeUtilTest,
       VerifyCardNetworkMappingFromCardType_ForVerve_WithFlagOff) {}

TEST_F(PaymentsSyncBridgeUtilTest,
       VerifyCardTypeMappingFromCardNetwork_ForVerve_WithFlagOff) {}

}  // namespace
}  // namespace autofill