#include "chrome/browser/ui/autofill/payments/virtual_card_enroll_bubble_controller_impl.h"
#include "base/check_op.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ui/autofill/payments/virtual_card_enroll_bubble_controller_impl_test_api.h"
#include "chrome/browser/ui/browser_list.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/payments/test_legal_message_line.h"
#include "components/autofill/core/browser/payments/virtual_card_enrollment_flow.h"
#include "components/autofill/core/browser/payments/virtual_card_enrollment_manager.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "content/public/browser/web_contents_user_data.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"
namespace autofill {
namespace {
VirtualCardEnrollmentFields CreateVirtualCardEnrollmentFields() { … }
#if BUILDFLAG(IS_ANDROID)
class ControllerTestSupport {
public:
explicit ControllerTestSupport(content::WebContents* web_contents)
: card_art_image_(gfx::test::CreateImage(100, 50).AsImageSkia()),
controller_(static_cast<VirtualCardEnrollBubbleControllerImpl*>(
VirtualCardEnrollBubbleControllerImpl::GetOrCreate(web_contents))) {
virtual_card_enrollment_fields_ = CreateVirtualCardEnrollmentFields();
}
~ControllerTestSupport() = default;
VirtualCardEnrollBubbleControllerImpl* controller() const {
return controller_;
}
const VirtualCardEnrollmentFields& virtual_card_enrollment_fields() const {
return virtual_card_enrollment_fields_;
}
private:
gfx::ImageSkia card_art_image_;
raw_ptr<VirtualCardEnrollBubbleControllerImpl> controller_;
VirtualCardEnrollmentFields virtual_card_enrollment_fields_;
};
class MockAutofillVCNEnrollBottomSheetBridge
: public AutofillVCNEnrollBottomSheetBridge {
public:
MockAutofillVCNEnrollBottomSheetBridge()
: AutofillSaveCardBottomSheetBridge() {}
MOCK_METHOD(void, Hide, (), (override));
};
class VirtualCardEnrollBubbleControllerImplBottomSheetTest
: public ChromeRenderViewHostTestHarness {
public:
VirtualCardEnrollBubbleControllerImplBottomSheetTest() = default;
};
TEST_F(VirtualCardEnrollBubbleControllerImplBottomSheetTest,
ShowBubbleSetsUiModel) {
ControllerTestSupport test_support(web_contents());
test_support.controller()->ShowBubble(
test_support.virtual_card_enrollment_fields(),
base::DoNothing(),
base::DoNothing());
auto expected_model = VirtualCardEnrollUiModel::Create(
test_support.virtual_card_enrollment_fields());
EXPECT_EQ(*test_support.controller()->GetUiModel(), *expected_model);
}
TEST_F(VirtualCardEnrollBubbleControllerImplBottomSheetTest, ShowBubble) {
ControllerTestSupport test_support(web_contents());
test_support.controller()->ShowBubble(
test_support.virtual_card_enrollment_fields(),
base::DoNothing(),
base::DoNothing());
EXPECT_TRUE(test_api(test_support.controller()).DidShowBottomSheet());
}
TEST_F(VirtualCardEnrollBubbleControllerImplBottomSheetTest,
ShowConfirmationBubbleView) {
ControllerTestSupport test_support(web_contents());
std::unique_ptr<MockAutofillVCNEnrollBottomSheetBridge> mock =
std::make_unique<MockAutofillVCNEnrollBottomSheetBridge>();
MockAutofillVCNEnrollBottomSheetBridge* bridge = mock.get();
test_api(test_support.controller())
.SetSetAutofillVCNEnrollBottomSheetBridge(mock);
EXPECT_CALL(*bridge, Hide());
test_support.controller()->ShowConfirmationBubbleView(
payments::PaymentsAutofillClient::PaymentsRpcResult::kSuccess);
}
#endif
class TestVirtualCardEnrollBubbleControllerImpl
: public VirtualCardEnrollBubbleControllerImpl { … };
class VirtualCardEnrollBubbleControllerImplBubbleViewTest
: public BrowserWithTestWindowTest { … };
TEST_F(VirtualCardEnrollBubbleControllerImplBubbleViewTest, ShowBubble) { … }
TEST_F(VirtualCardEnrollBubbleControllerImplBubbleViewTest,
ShowBubbleInLoadingState) { … }
#if !BUILDFLAG(IS_ANDROID)
TEST_F(VirtualCardEnrollBubbleControllerImplBubbleViewTest,
ShowBubbleInLoadingAndConfirmationState) { … }
TEST_F(VirtualCardEnrollBubbleControllerImplBubbleViewTest,
CloseBubbleInLoadingState_NoConfirmationBubble_ClientSideTimeout) { … }
TEST_F(VirtualCardEnrollBubbleControllerImplBubbleViewTest,
Metric_CloseConfirmationBubble_CardNotEnrolled) { … }
#endif
}
}