chromium/components/autofill/core/browser/ui/payments/card_unmask_prompt_controller_impl_unittest.cc

// Copyright 2015 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/ui/payments/card_unmask_prompt_controller_impl.h"

#include <stddef.h>

#include <memory>
#include <string>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/metrics/autofill_metrics.h"
#include "components/autofill/core/browser/payments/card_unmask_challenge_option.h"
#include "components/autofill/core/browser/payments/payments_autofill_client.h"
#include "components/autofill/core/browser/ui/payments/card_unmask_prompt_options.h"
#include "components/autofill/core/browser/ui/payments/card_unmask_prompt_view.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace autofill {
namespace {

ASCIIToUTF16;
PaymentsRpcResult;

}  // namespace

class TestCardUnmaskDelegate : public CardUnmaskDelegate {};

class TestCardUnmaskPromptView : public CardUnmaskPromptView {};

class TestCardUnmaskPromptController : public CardUnmaskPromptControllerImpl {};

class CardUnmaskPromptControllerImplGenericTest : public testing::Test {};

#if BUILDFLAG(IS_ANDROID)
TEST_F(CardUnmaskPromptControllerImplGenericTest,
       FidoAuthOfferCheckboxStatePersistent) {
  ShowPromptAndSimulateResponse(/*enable_fido_auth=*/true);
  EXPECT_TRUE(pref_service_.GetBoolean(
      prefs::kAutofillCreditCardFidoAuthOfferCheckboxState));

  ShowPromptAndSimulateResponse(/*enable_fido_auth=*/false);
  EXPECT_FALSE(pref_service_.GetBoolean(
      prefs::kAutofillCreditCardFidoAuthOfferCheckboxState));
}

TEST_F(CardUnmaskPromptControllerImplGenericTest,
       FidoAuthOfferCheckboxStateUnchangedWhenInvisible) {
  pref_service_.SetBoolean(prefs::kAutofillCreditCardFidoAuthOfferCheckboxState,
                           true);
  ShowPromptAndSimulateResponse(/*enable_fido_auth=*/false,
                                /*should_unmask_virtual_card=*/false,
                                /*was_checkbox_visible=*/false);
  EXPECT_TRUE(pref_service_.GetBoolean(
      prefs::kAutofillCreditCardFidoAuthOfferCheckboxState));

  pref_service_.SetBoolean(prefs::kAutofillCreditCardFidoAuthOfferCheckboxState,
                           false);
  ShowPromptAndSimulateResponse(/*enable_fido_auth=*/true,
                                /*should_unmask_virtual_card=*/false,
                                /*was_checkbox_visible=*/false);
  EXPECT_FALSE(pref_service_.GetBoolean(
      prefs::kAutofillCreditCardFidoAuthOfferCheckboxState));
}

TEST_F(CardUnmaskPromptControllerImplGenericTest,
       PopulateCheckboxToUserProvidedUnmaskDetails) {
  ShowPromptAndSimulateResponse(/*enable_fido_auth=*/true);

  EXPECT_TRUE(delegate_.details().enable_fido_auth);
}
#endif

TEST_F(CardUnmaskPromptControllerImplGenericTest, LogRealPanResultSuccess) {}

TEST_F(CardUnmaskPromptControllerImplGenericTest, LogRealPanTryAgainFailure) {}

TEST_F(CardUnmaskPromptControllerImplGenericTest, LogRealPanClientSideTimeout) {}

TEST_F(CardUnmaskPromptControllerImplGenericTest,
       LogUnmaskingDurationResultSuccess) {}

TEST_F(CardUnmaskPromptControllerImplGenericTest,
       LogUnmaskingDurationTryAgainFailure) {}

TEST_F(CardUnmaskPromptControllerImplGenericTest,
       LogUnmaskingDurationVcnRetrievalPermanentFailure) {}

TEST_F(CardUnmaskPromptControllerImplGenericTest,
       LogUnmaskingDurationClientSideTimeout) {}

// Tests to ensure the UI text elements are shown correctly in the card unmask
// prompt.
// The parameter indicates whether the virtual card feature is enabled on Bling.
// It decides the text content we show in the promp.
class CardUnmaskPromptTextTest
    : public CardUnmaskPromptControllerImplGenericTest,
      public testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P();

// Ensures the card information is shown correctly.
TEST_P(CardUnmaskPromptTextTest, DisplayCardInformation) {}

// Tests the title and instructions message in the credit card unmask dialog.
TEST_P(CardUnmaskPromptTextTest, TitleAndInstructionMessage) {}

TEST_P(CardUnmaskPromptTextTest, TitleAndInstructionMessageAmex) {}

// Tests the title and instructions message in the credit card unmask dialog for
// expired cards.
TEST_P(CardUnmaskPromptTextTest, ExpiredCardTitleAndInstructionMessage) {}

// Ensures the instruction message and window title is correctly displayed when
// showing the card unmask prompt for a virtual card. This test also checks that
// the expected CVC length is correctly set for virtual cards. Virtual cards are
// not currently supported on iOS, so we don't test on the platform.
TEST_P(CardUnmaskPromptTextTest,
       ChallengeOptionInstructionMessageAndWindowTitleAndExpectedCvcLength) {}

TEST_P(
    CardUnmaskPromptTextTest,
    ChallengeOptionInstructionMessageAndWindowTitleAndExpectedCvcLengthAmex) {}

class LoggingValidationTestForNickname
    : public CardUnmaskPromptControllerImplGenericTest,
      public testing::WithParamInterface<bool> {};

TEST_P(LoggingValidationTestForNickname,
       MaskedServerCard_LogUnmaskPromptShown) {}

TEST_P(LoggingValidationTestForNickname, VirtualCard_LogUnmaskPromptShown) {}

TEST_P(LoggingValidationTestForNickname, LogClosedNoAttempts) {}

TEST_P(LoggingValidationTestForNickname, LogClosedAbandonUnmasking) {}

TEST_P(LoggingValidationTestForNickname, LogClosedFailedToUnmaskRetriable) {}

TEST_P(LoggingValidationTestForNickname, LogClosedFailedToUnmaskNonRetriable) {}

TEST_P(LoggingValidationTestForNickname, LogUnmaskedCardFirstAttempt) {}

TEST_P(LoggingValidationTestForNickname, LogUnmaskedCardAfterFailure) {}

TEST_P(LoggingValidationTestForNickname, LogDurationNoAttempts) {}

TEST_P(LoggingValidationTestForNickname, LogDurationAbandonUnmasking) {}

TEST_P(LoggingValidationTestForNickname, LogDurationFailedToUnmaskRetriable) {}

TEST_P(LoggingValidationTestForNickname,
       LogDurationFailedToUnmaskNonRetriable) {}

TEST_P(LoggingValidationTestForNickname, LogDurationCardFirstAttempt) {}

TEST_P(LoggingValidationTestForNickname, LogDurationUnmaskedCardAfterFailure) {}

TEST_P(LoggingValidationTestForNickname, LogTimeBeforeAbandonUnmasking) {}

INSTANTIATE_TEST_SUITE_P();

struct CvcCase {};

class CvcInputValidationTest : public CardUnmaskPromptControllerImplGenericTest,
                               public testing::WithParamInterface<CvcCase> {};

TEST_P(CvcInputValidationTest, CvcInputValidation) {}

INSTANTIATE_TEST_SUITE_P();

class CvcInputAmexValidationTest
    : public CardUnmaskPromptControllerImplGenericTest,
      public testing::WithParamInterface<CvcCase> {};

TEST_P(CvcInputAmexValidationTest, CvcInputValidation) {}

INSTANTIATE_TEST_SUITE_P();

struct ExpirationDateTestCase {};

class ExpirationDateValidationTest
    : public CardUnmaskPromptControllerImplGenericTest,
      public testing::WithParamInterface<ExpirationDateTestCase> {};

TEST_P(ExpirationDateValidationTest, ExpirationDateValidation) {}

INSTANTIATE_TEST_SUITE_P();

class VirtualCardErrorTest
    : public CardUnmaskPromptControllerImplGenericTest,
      public testing::WithParamInterface<PaymentsRpcResult> {};

#if BUILDFLAG(IS_ANDROID)
TEST_P(VirtualCardErrorTest, VirtualCardFailureDismissesUnmaskPrompt) {
  ShowPromptAndSimulateResponse(/*enable_fido_auth=*/false,
                                /*should_unmask_virtual_card=*/true);
  base::HistogramTester histogram_tester;

  controller_->OnVerificationResult(GetParam());

  // Verify that the dialog is closed by checking the state.
  EXPECT_EQ(PaymentsRpcResult::kNone, controller_->GetVerificationResult());
  // Verify that prompt closing metrics are logged.
  histogram_tester.ExpectBucketCount(
      "Autofill.UnmaskPrompt.VirtualCard.Events",
      AutofillMetrics::
          UNMASK_PROMPT_CLOSED_FAILED_TO_UNMASK_NON_RETRIABLE_FAILURE,
      1);
  histogram_tester.ExpectTotalCount("Autofill.UnmaskPrompt.Duration", 1);
  histogram_tester.ExpectTotalCount("Autofill.UnmaskPrompt.Duration.Failure",
                                    1);
}

INSTANTIATE_TEST_SUITE_P(
    CardUnmaskPromptControllerImplGenericTest,
    VirtualCardErrorTest,
    testing::Values(PaymentsRpcResult::kVcnRetrievalPermanentFailure,
                    PaymentsRpcResult::kVcnRetrievalTryAgainFailure));
#endif  // BUILDFLAG(IS_ANDROID)

}  // namespace autofill