chromium/chrome/browser/ui/autofill/payments/save_card_bubble_controller_impl_unittest.cc

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/autofill/payments/save_card_bubble_controller_impl.h"

#include <stddef.h>

#include <string>
#include <tuple>
#include <utility>

#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/ui/autofill/autofill_bubble_base.h"
#include "chrome/browser/ui/autofill/payments/save_card_ui.h"
#include "chrome/browser/ui/autofill/payments/save_payment_icon_controller.h"
#include "chrome/browser/ui/autofill/test/test_autofill_bubble_handler.h"
#include "chrome/browser/ui/hats/mock_trust_safety_sentiment_service.h"
#include "chrome/browser/ui/hats/trust_safety_sentiment_service_factory.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.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/metrics/payments/credit_card_save_metrics.h"
#include "components/autofill/core/browser/metrics/payments/manage_cards_prompt_metrics.h"
#include "components/autofill/core/browser/payments/payments_autofill_client.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/test_autofill_clock.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/test/mock_navigation_handle.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

Bucket;
ElementsAre;

namespace autofill {

CardSaveType;
SaveCreditCardOptions;

namespace {

const base::Time kArbitraryTime =;

std::unique_ptr<KeyedService> BuildTestPersonalDataManager(
    content::BrowserContext* context) {}

}  // namespace

// Test AutofillBubbleBase implementation which:
// - Notifies the controller when the bubble hides (to match prod).
// - Tracks the bubble's visibility.
class ObserveHideTestAutofillBubble : public AutofillBubbleBase {};

// TestAutofillBubbleHandler which provides access to bubbles it creates.
class ExposeBubbleAutofillBubbleHandler : public TestAutofillBubbleHandler {};

class TestBrowserWindowWithAutofillHandler : public TestBrowserWindow {};

class TestSaveCardBubbleControllerImpl : public SaveCardBubbleControllerImpl {};

class SaveCardBubbleControllerImplTest : public BrowserWithTestWindowTest {};

// Tests that the legal message lines vector is empty when doing a local save so
// that no legal messages will be shown to the user in that case.
TEST_F(SaveCardBubbleControllerImplTest, LegalMessageLinesEmptyOnLocalSave) {}

TEST_F(SaveCardBubbleControllerImplTest,
       PropagateShouldRequestNameFromUserWhenFalse) {}

TEST_F(SaveCardBubbleControllerImplTest,
       PropagateShouldRequestNameFromUserWhenTrue) {}

TEST_F(SaveCardBubbleControllerImplTest,
       PropagateShouldRequestExpirationDateFromUserWhenFalse) {}

TEST_F(SaveCardBubbleControllerImplTest,
       PropagateShouldRequestExpirationDateFromUserWhenTrue) {}

SaveCreditCardPromptResultMetricTestData;

// Test fixture to ensure the correct reporting of UMA metric
// Autofill.SaveCreditCardPromptResult{SaveDestination}.{UserGroup}.
class SaveCreditCardPromptResultMetricTest
    : public SaveCardBubbleControllerImplTest,
      public testing::WithParamInterface<
          SaveCreditCardPromptResultMetricTestData> {};

INSTANTIATE_TEST_SUITE_P();

// Tests that after the user interacts with a "save *local* card" dialog and
// *does not* have any card data on file, metrics
// Autofill.SaveCreditCardPromptResult.Local.Aggregate and .UserHasNoCards are
// recorded.
TEST_P(SaveCreditCardPromptResultMetricTest,
       EmitsSavePromptResultLocalHasNoCards) {}

// Tests that after the user interacts with a "save *server* card" dialog and
// *does not* have any card data on file, metrics
// Autofill.SaveCreditCardPromptResult.Upload.Aggregate and .UserHasNoCards are
// recorded.
TEST_P(SaveCreditCardPromptResultMetricTest,
       EmitsSavePromptResultUploadHasNoCards) {}

// Tests that after the user interacts with a "save *local* card" dialog and
// *does* have card data on file, metrics
// Autofill.SaveCreditCardPromptResult.Local.Aggregate and .UserHasSavedCards
// are recorded.
TEST_P(SaveCreditCardPromptResultMetricTest,
       EmitsSavePromptResultLocalHasSavedCards) {}

// Tests that after the user interacts with a "save *server* card" dialog and
// *does* have card data on file, metrics
// Autofill.SaveCreditCardPromptResult.Upload.Aggregate and .UserHasSavedCards
// are recorded.
TEST_P(SaveCreditCardPromptResultMetricTest,
       EmitsSavePromptResultUploadHasSavedCards) {}

// Param of the SaveCardBubbleSingletonTestData:
// -- bool first_shown_is_local;
// -- bool second_and_third_shown_are_local;
SaveCardBubbleSingletonTestData;

// One test case will be run several times till we cover all the param
// combinations of the |SaveCardBubbleSingletonTestData|. GetParam() will help
// get the specific param value for a particular run.
class SaveCardBubbleSingletonTest
    : public SaveCardBubbleControllerImplTest,
      public testing::WithParamInterface<SaveCardBubbleSingletonTestData> {};

INSTANTIATE_TEST_SUITE_P();

TEST_P(SaveCardBubbleSingletonTest, OnlyOneActiveBubble) {}

// Note that even though in prod the four options in the SaveCreditCardOptions
// struct can be true at the same time, we don't support that in the test case
// (by the way we create histogram name suffixes).
struct SaveCardOptionParam {};

const SaveCardOptionParam kSaveCardOptionParam[] =;

// Param of the SaveCardBubbleSingletonTestData:
// -- std::string save_destination
// -- std::string show_type
// -- SaveCardOptionParam save_card_option_param
SaveCardBubbleLoggingTestData;

// Test class to ensure the save card bubble events are logged correctly.
class SaveCardBubbleLoggingTest
    : public SaveCardBubbleControllerImplTest,
      public ::testing::WithParamInterface<SaveCardBubbleLoggingTestData> {};

INSTANTIATE_TEST_SUITE_P();

TEST_P(SaveCardBubbleLoggingTest, Metrics_ShowBubble) {}

TEST_P(SaveCardBubbleLoggingTest, Metrics_ShowIconOnly) {}

TEST_P(SaveCardBubbleLoggingTest, Metrics_SaveButton) {}

TEST_P(SaveCardBubbleLoggingTest, Metrics_CancelButton) {}

TEST_P(SaveCardBubbleLoggingTest, Metrics_Closed) {}

TEST_P(SaveCardBubbleLoggingTest, Metrics_NotInteracted) {}

TEST_P(SaveCardBubbleLoggingTest, Metrics_LostFocus) {}

TEST_P(SaveCardBubbleLoggingTest, Metrics_Unknown) {}

TEST_P(SaveCardBubbleLoggingTest, Metrics_LegalMessageLinkedClicked) {}

// Param of the SaveCvcBubbleLoggingTest:
// -- std::string show_type: decides if the view is shown first time or
// re-shown.
// -- std::string save_destination decides if card or CVC will be saved locally
// or to the server.
class SaveCvcBubbleLoggingTest
    : public SaveCardBubbleControllerImplTest,
      public testing::WithParamInterface<std::tuple<std::string, std::string>> {};

INSTANTIATE_TEST_SUITE_P();

TEST_P(SaveCvcBubbleLoggingTest, Metrics_ShowBubble) {}

TEST_P(SaveCvcBubbleLoggingTest, Metrics_ShowIconOnly) {}

TEST_P(SaveCvcBubbleLoggingTest, Metrics_SaveButton) {}

TEST_P(SaveCvcBubbleLoggingTest, Metrics_CancelButton) {}

TEST_P(SaveCvcBubbleLoggingTest, Metrics_Closed) {}

TEST_P(SaveCvcBubbleLoggingTest, Metrics_NotInteracted) {}

TEST_P(SaveCvcBubbleLoggingTest, Metrics_LostFocus) {}

TEST_P(SaveCvcBubbleLoggingTest, Metrics_Unknown) {}

TEST_F(SaveCardBubbleControllerImplTest, LocalCvcOnlySaveDialogContent) {}

TEST_F(SaveCardBubbleControllerImplTest, UploadCardSaveBubbleType) {}

TEST_F(SaveCardBubbleControllerImplTest, UploadCvcOnlySaveDialogContent) {}

TEST_F(SaveCardBubbleControllerImplTest,
       LocalCard_FirstShow_SaveButton_SigninPromo_Close_Reshow_ManageCards) {}

TEST_F(SaveCardBubbleControllerImplTest,
       LocalCvc_FirstShow_SaveButton_SigninPromo_Close_Reshow_ManageCards) {}

TEST_F(SaveCardBubbleControllerImplTest,
       Metrics_Local_ClickManageCardsDoneButton) {}

TEST_F(SaveCardBubbleControllerImplTest,
       Metrics_Local_ClickManageCardsManageCardsButton) {}

TEST_F(
    SaveCardBubbleControllerImplTest,
    Metrics_Local_FirstShow_SaveButton_Close_Reshow_Close_Reshow_ManageCards) {}

TEST_F(
    SaveCardBubbleControllerImplTest,
    Metrics_Local_FirstShow_SaveButton_SigninPromo_Close_Reshow_ManageCards) {}

TEST_F(SaveCardBubbleControllerImplTest,
       Upload_FirstShow_SaveButton_NoSigninPromo) {}

TEST_F(SaveCardBubbleControllerImplTest,
       Metrics_Upload_FirstShow_SaveButton_NoSigninPromo) {}

TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_FirstShow_ManageCards) {}

// Test that the credit card upload loading and confirmation metrics are
// recorded as false when the loading and confirmation views are not shown.
TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_LoadingConfirmation) {}

class SaveCardBubbleControllerImplTestWithCvCStorageAndFilling
    : public SaveCardBubbleControllerImplTest {};

TEST_F(SaveCardBubbleControllerImplTestWithCvCStorageAndFilling,
       LocalCardSaveOnlyDialogContent) {}

TEST_F(SaveCardBubbleControllerImplTestWithCvCStorageAndFilling,
       LocalCardSaveWithCvcDialogContent) {}

TEST_F(SaveCardBubbleControllerImplTestWithCvCStorageAndFilling,
       UploadCardSaveWithCvcDialogContent) {}

class SaveCardBubbleControllerImplTestWithLoadingAndConfirmation
    : public SaveCardBubbleControllerImplTest {};

// Test the entire upload save flow with the ShowConfirmationBubbleView()
// callback.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       Upload_OnSave_ShowConfirmationBubbleView) {}

// Test that when passing in "card_saved=false" for ShowConfirmationBubbleView()
// the confirmation UI model has "is_success" set to false.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       Upload_OnShowConfirmation_ShowFailureUIModel) {}

// Test that when showing the upload bubble when the confirmation bubble view is
// still up, the confirmation bubble view is closed and the upload bubble view
// is still shown.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       Upload_OnShowConfirmationBubbleView_ThenShowUploadView) {}

// Test that the `Accepted` upload result metric is recorded on upload card save
// and that upload result metrics are not recorded but the confirmation shown &
// result metrics are recorded when the save card bubble is closed after the
// save card upload completes.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       Metrics_Upload_AfterSave_OnClose) {}

// Test that the `CardNotUploaded` confirmation shown & result metrics are
// recorded when the save card bubble is closed after the save card upload
// completes without the card being saved.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       Metrics_Upload_AfterFailedSave_OnClose) {}

// Test that the `Accepted` upload result metric is not recorded and the loading
// view shown & closed metrics are recorded when the save card bubble is closed
// before the save card upload completes.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       Metrics_Upload_DuringSave_OnClose) {}

// Test that metrics are not recorded in
// SaveCardBubbleController::OnSaveButton() on local card save.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       Metrics_Local_OnSave) {}

// Test that after changing tabs, when returning to the tab with the save card,
// the bubble view is no longer showing but can be accessed through the icon.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       VisibilityChange_Upload_HideBubble) {}

// Test that after a link is clicked in the save card bubble view; and one
// returns to the tab with the save card, the bubble view is automatically
// re-shown without user prompt.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       VisibilityChange_Upload_ReshowAfterLinkClick) {}

// Test that while in the UPLOAD_IN_PROGRESS state, after changing tabs and
// returning to the tab with the save card, the state will remain as
// UPLOAD_IN_PROGRESS.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       VisibilityChange_Upload_InProgressState_Retained) {}

// Test that while in the UPLOAD_IN_PROGRESS state, if the tab is changed and
// the upload is completed, upon returning to the original tab with the save
// card, the confirmation bubble will be showing.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       VisibilityChange_Upload_InProgressStateTransitionIntoCompletedState) {}

// Test the metrics for reshowing the bubble view after a link is clicked.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       Metrics_VisibilityChange_Upload_ReshowAfterLinkClick) {}

// Test that `HideSaveCardBubble()` hides save card offer and confirmation
// bubble.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       HideSaveCardBubble) {}

// Test that `OnConfirmationClosedCallback` runs when confirmation prompt
// is closed by user.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       OnConfirmationPromptClosedByUser_RunCallback) {}

// Test that `OnConfirmationClosedCallback` runs when confirmation prompt is
// auto-closed in 3 sec.
TEST_F(SaveCardBubbleControllerImplTestWithLoadingAndConfirmation,
       OnConfirmationPromptAutoClosed_RunCallback) {}

UploadCardUpdatedDesktopUiTestData;

// Ensures that the AutofillUpstreamUpdatedUi feature displays the correct UI
// based on which treatment arm the user is in.
// Param of the UploadCardUpdatedDesktopUiTest:
// -- UploadCardUpdatedDesktopUiTestData: A trio of 1) which arm of the
//    experiment is active, 2) the expected bubble title text message ID for
//    that arm, and 3) the expected bubble explanatory message ID for that arm.
class UploadCardUpdatedDesktopUiTest
    : public SaveCardBubbleControllerImplTest,
      public testing::WithParamInterface<UploadCardUpdatedDesktopUiTestData> {};

INSTANTIATE_TEST_SUITE_P();

TEST_P(UploadCardUpdatedDesktopUiTest, ReturnsApplicableWindowTitle) {}

TEST_P(UploadCardUpdatedDesktopUiTest, ReturnsApplicableExplanatoryMessage) {}

}  // namespace autofill