#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;
}
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(true);
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kAutofillCreditCardFidoAuthOfferCheckboxState));
ShowPromptAndSimulateResponse(false);
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kAutofillCreditCardFidoAuthOfferCheckboxState));
}
TEST_F(CardUnmaskPromptControllerImplGenericTest,
FidoAuthOfferCheckboxStateUnchangedWhenInvisible) {
pref_service_.SetBoolean(prefs::kAutofillCreditCardFidoAuthOfferCheckboxState,
true);
ShowPromptAndSimulateResponse(false,
false,
false);
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kAutofillCreditCardFidoAuthOfferCheckboxState));
pref_service_.SetBoolean(prefs::kAutofillCreditCardFidoAuthOfferCheckboxState,
false);
ShowPromptAndSimulateResponse(true,
false,
false);
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kAutofillCreditCardFidoAuthOfferCheckboxState));
}
TEST_F(CardUnmaskPromptControllerImplGenericTest,
PopulateCheckboxToUserProvidedUnmaskDetails) {
ShowPromptAndSimulateResponse(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) { … }
class CardUnmaskPromptTextTest
: public CardUnmaskPromptControllerImplGenericTest,
public testing::WithParamInterface<bool> { … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(CardUnmaskPromptTextTest, DisplayCardInformation) { … }
TEST_P(CardUnmaskPromptTextTest, TitleAndInstructionMessage) { … }
TEST_P(CardUnmaskPromptTextTest, TitleAndInstructionMessageAmex) { … }
TEST_P(CardUnmaskPromptTextTest, ExpiredCardTitleAndInstructionMessage) { … }
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(false,
true);
base::HistogramTester histogram_tester;
controller_->OnVerificationResult(GetParam());
EXPECT_EQ(PaymentsRpcResult::kNone, controller_->GetVerificationResult());
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
}