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

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

#include <memory>
#include <string>
#include <string_view>

#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "chrome/browser/ui/autofill/payments/desktop_payments_window_manager.h"
#include "chrome/browser/ui/autofill/payments/desktop_payments_window_manager_test_api.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/test/test_browser_ui.h"
#include "chrome/browser/ui/views/autofill/payments/payments_window_user_consent_dialog_view.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "components/autofill/content/browser/test_autofill_client_injector.h"
#include "components/autofill/content/browser/test_content_autofill_client.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/metrics/payments/payments_window_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/payments/payments_window_manager.h"
#include "components/autofill/core/browser/payments/test_payments_autofill_client.h"
#include "components/autofill/core/browser/payments/test_payments_network_interface.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/window/dialog_client_view.h"

namespace autofill {

class TestContentAutofillClientForWindowManagerTest
    : public TestContentAutofillClient {};

namespace payments {

constexpr std::string_view kVcn3dsTestUrl =;
constexpr std::string_view kTestContextToken =;
constexpr std::string_view kVcn3dsFlowEventsHistogramName =;
constexpr std::string_view kVcn3dsFlowEventsConsentAlreadyGivenHistogramName =;
constexpr std::string_view kVcn3dsFlowEventsConsentNotGivenYetHistogramName =;
constexpr std::string_view kVcn3dsSuccessLatencyHistogramName =;
constexpr std::string_view kVcn3dsFailureLatencyHistogramName =;

class DesktopPaymentsWindowManagerInteractiveUiTest : public UiBrowserTest {};

// Tests that an error dialog is shown if there is no metadata returned from the
// server.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_EmptyMetadata_ErrorDialogShown) {}

// Tests that an error dialog is shown if there is no URL to open returned from
// the server.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_EmptyUrlToOpen_ErrorDialogShown) {}

// Tests that an error dialog is shown if there is no success query param name
// returned from the server.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_EmptySuccessQueryParamName_ErrorDialogShown) {}

// Tests that an error dialog is shown if there is no failure query param name
// returned from the server.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_EmptyFailureQueryParamName_ErrorDialogShown) {}

// Test that the VCN 3DS flow started and consent dialog skipped histogram
// buckets are logged to when the flow starts.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_Vcn3ds_FlowStartedHistogramBucketLogs) {}

// Test that the VCN 3DS pop-up is shown correctly, and on close an
// UnmaskCardRequest is triggered with the proper fields set if the right query
// params are present.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_Vcn3ds_QueryParamsPresent) {}

// Tests that the VCN 3DS flow succeeded histogram bucket is logged to when a
// successful flow is completed for VCN 3DS.
IN_PROC_BROWSER_TEST_F(
    DesktopPaymentsWindowManagerInteractiveUiTest,
    InvokeUi_Vcn3ds_QueryParamsPresent_SuccessHistogramBucketLogs) {}

// Tests that the VCN 3DS flow succeeded latency histogram bucket is logged to
// when a successful flow is completed for VCN 3DS.
IN_PROC_BROWSER_TEST_F(
    DesktopPaymentsWindowManagerInteractiveUiTest,
    InvokeUi_Vcn3ds_QueryParamsPresent_SuccessLatencyHistogramBucketLogs) {}

// Tests that the VCN 3DS flow failure latency histogram bucket is logged to
// when a failed flow is completed for VCN 3DS.
IN_PROC_BROWSER_TEST_F(
    DesktopPaymentsWindowManagerInteractiveUiTest,
    InvokeUi_Vcn3ds_QueryParamsPresent_FailureLatencyHistogramBucketLogs) {}

// Test that the VCN 3DS pop-up is shown correctly, and on close an
// UnmaskCardRequest is triggered with the proper fields set if the right query
// params are present. Then mock an UnmaskCardRequest failure, and check that
// the requester was notified of this failure.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_Vcn3ds_UnmaskCardRequestFailure) {}

// Tests that the VCN 3DS flow failed during second server call histogram bucket
// is logged to when a flow fails in the second UnmaskCardRequest.
IN_PROC_BROWSER_TEST_F(
    DesktopPaymentsWindowManagerInteractiveUiTest,
    InvokeUi_Vcn3ds_UnmaskCardRequestFailure_FailureHistogramBucketLogs) {}

// Test that the VCN 3DS pop-up is shown correctly, and on close an
// UnmaskCardRequest is not triggered if the query params indicate the
// authentication failed.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_Vcn3ds_QueryParams_AuthenticationFailed) {}

// Tests that the VCN 3DS authentication failed histogram bucket is logged to
// when the authentication inside of the pop-up failed for VCN 3DS.
IN_PROC_BROWSER_TEST_F(
    DesktopPaymentsWindowManagerInteractiveUiTest,
    InvokeUi_Vcn3ds_QueryParams_AuthenticationFailed_FailureHistogramBucketLogs) {}

// Test that the VCN 3DS pop-up is shown correctly, and on close an
// UnmaskCardRequest is not triggered if there are no query params present.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_Vcn3ds_NoQueryParamsAndPopupClosed) {}

// Tests that the VCN 3DS flow cancelled histogram bucket is logged to when the
// user closes the pop-up.
IN_PROC_BROWSER_TEST_F(
    DesktopPaymentsWindowManagerInteractiveUiTest,
    InvokeUi_Vcn3ds_NoQueryParamsAndPopupClosed_CancelledHistogramBucketLogs) {}

// Test that the VCN 3DS pop-up is shown correctly, and on close an
// UnmaskCardRequest is not triggered if the query params are invalid.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_Vcn3ds_InvalidQueryParams) {}

// Test that the VCN 3DS pop-up is shown correctly, and when the user cancels
// the progress dialog, the state of the PaymentsWindowManager in relation to
// the ongoing UnmaskCardRequest is reset.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_Vcn3ds_ProgressDialogCancelled) {}

// Tests that the VCN 3DS progress dialog cancelled histogram bucket is logged
// to when the progress dialog is cancelled during the VCN 3DS flow.
IN_PROC_BROWSER_TEST_F(
    DesktopPaymentsWindowManagerInteractiveUiTest,
    InvokeUi_Vcn3ds_ProgressDialogCancelled_ProgressDialogCancelledHistogramBucketLogs) {}

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
// Tests that if a VCN 3DS flow is ongoing, and the original tab is set active,
// the payments window manager popup's web contents are re-activated.
IN_PROC_BROWSER_TEST_F(DesktopPaymentsWindowManagerInteractiveUiTest,
                       InvokeUi_Vcn3ds_OriginalTabSetLastActive) {}
#endif  // #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)

// Integration test using Kombucha to ensure that the consent dialog creates a
// new pop-up when the ok button is clicked, and cancels the flow when the
// cancel button is clicked.
class PaymentsWindowUserConsentDialogIntegrationTest
    : public InteractiveBrowserTest {};

// Ensures that the flow started, consent not given yet histogram bucket is
// logged to when a payments window flow is started without consent already
// given.
IN_PROC_BROWSER_TEST_F(PaymentsWindowUserConsentDialogIntegrationTest,
                       FlowStartedConsentNotGivenYetHistogramBucketLogs) {}

// Ensures the UI can be shown, and verifies that accepting the dialog runs the
// accept callback and creates the pop-up.
IN_PROC_BROWSER_TEST_F(PaymentsWindowUserConsentDialogIntegrationTest,
                       DialogAccepted) {}

// Tests that the VCN 3DS consent dialog accepted histogram bucket is logged to
// when the consent dialog is accepted.
IN_PROC_BROWSER_TEST_F(PaymentsWindowUserConsentDialogIntegrationTest,
                       DialogAccepted_AcceptedHistogramBucketLogs) {}

// Ensures the UI can be shown, and verifies that cancelling the dialog runs the
// cancel callback and resets the state.
IN_PROC_BROWSER_TEST_F(PaymentsWindowUserConsentDialogIntegrationTest,
                       DialogCancelled) {}

// Tests that the VCN 3DS consent dialog declined histogram bucket is logged to
// when the consent dialog is declined.
IN_PROC_BROWSER_TEST_F(PaymentsWindowUserConsentDialogIntegrationTest,
                       DialogDeclined_DeclinedHistogramBucketLogs) {}

}  // namespace payments

}  // namespace autofill