1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
// 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 "chrome/browser/facilitated_payments/ui/chrome_facilitated_payments_client.h"
#include <memory>
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/facilitated_payments/core/browser/pix_account_linking_manager.h"
#include "components/facilitated_payments/core/features/features.h"
#include "components/optimization_guide/core/hints/mock_optimization_guide_decider.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::Return;
class MockFacilitatedPaymentsController : public FacilitatedPaymentsController {
public:
explicit MockFacilitatedPaymentsController(content::WebContents* web_contents)
: FacilitatedPaymentsController(web_contents) {}
~MockFacilitatedPaymentsController() override = default;
MOCK_METHOD(bool, IsInLandscapeMode, (), (override));
MOCK_METHOD(void,
Show,
(base::span<const autofill::BankAccount> bank_account_suggestions,
base::OnceCallback<void(int64_t)> on_payment_account_selected),
(override));
MOCK_METHOD(void,
ShowForEwallet,
(base::span<const autofill::Ewallet> ewallet_suggestions,
base::OnceCallback<void(int64_t)> on_payment_account_selected),
(override));
MOCK_METHOD(void, ShowProgressScreen, (), (override));
MOCK_METHOD(void, ShowErrorScreen, (), (override));
MOCK_METHOD(void, Dismiss, (), (override));
MOCK_METHOD(void,
ShowPixAccountLinkingPrompt,
(base::OnceCallback<void()> on_accepted,
base::OnceCallback<void()> on_declined),
(override));
};
class MockPixAccountLinkingManager
: public payments::facilitated::PixAccountLinkingManager {
public:
explicit MockPixAccountLinkingManager(
payments::facilitated::FacilitatedPaymentsClient* client)
: PixAccountLinkingManager(client) {}
~MockPixAccountLinkingManager() override = default;
MOCK_METHOD(void, MaybeShowPixAccountLinkingPrompt, (), (override));
};
class ChromeFacilitatedPaymentsClientTest
: public ChromeRenderViewHostTestHarness {
public:
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
client_ = std::make_unique<ChromeFacilitatedPaymentsClient>(
web_contents(), &optimization_guide_decider_);
auto controller =
std::make_unique<MockFacilitatedPaymentsController>(web_contents());
controller_ = controller.get();
client_->SetFacilitatedPaymentsControllerForTesting(std::move(controller));
auto pix_account_linking_manager =
std::make_unique<MockPixAccountLinkingManager>(client_.get());
pix_account_linking_manager_ = pix_account_linking_manager.get();
client_->SetPixAccountLinkingManagerForTesting(
std::move(pix_account_linking_manager));
}
void TearDown() override {
ChromeRenderViewHostTestHarness::TearDown();
}
auto& base_client() {
return static_cast<payments::facilitated::FacilitatedPaymentsClient&>(
*client_);
}
MockFacilitatedPaymentsController& controller() { return *controller_; }
MockPixAccountLinkingManager& pix_account_linking_manager() {
return *pix_account_linking_manager_;
}
protected:
optimization_guide::MockOptimizationGuideDecider optimization_guide_decider_;
std::unique_ptr<ChromeFacilitatedPaymentsClient> client_;
raw_ptr<MockFacilitatedPaymentsController> controller_;
raw_ptr<MockPixAccountLinkingManager> pix_account_linking_manager_;
};
TEST_F(ChromeFacilitatedPaymentsClientTest, GetPaymentsDataManager) {
EXPECT_NE(nullptr, base_client().GetPaymentsDataManager());
}
TEST_F(ChromeFacilitatedPaymentsClientTest,
GetFacilitatedPaymentsNetworkInterface) {
EXPECT_NE(nullptr, base_client().GetFacilitatedPaymentsNetworkInterface());
}
// Test the client forwards call to show Pix FOP selector to the controller.
TEST_F(ChromeFacilitatedPaymentsClientTest,
ShowPixPaymentPrompt_ControllerDefaultTrue) {
EXPECT_CALL(controller(), Show);
base_client().ShowPixPaymentPrompt({}, base::DoNothing());
}
// Test that the `EWALLET_MERCHANT_ALLOWLIST` and
// `PIX_PAYMENT_MERCHANT_ALLOWLIST` optimization type is registered when the
// `ChromeFacilitatedPaymentClient` is created.
TEST_F(ChromeFacilitatedPaymentsClientTest, RegisterAllowlists) {
base::test::ScopedFeatureList feature_list(
payments::facilitated::kEwalletPayments);
EXPECT_CALL(optimization_guide_decider_,
RegisterOptimizationTypes(testing::ElementsAre(
optimization_guide::proto::PIX_MERCHANT_ORIGINS_ALLOWLIST)))
.Times(1);
EXPECT_CALL(optimization_guide_decider_,
RegisterOptimizationTypes(testing::ElementsAre(
optimization_guide::proto::EWALLET_MERCHANT_ALLOWLIST)))
.Times(1);
// Re-create the client; it should register the allowlist.
client_ = std::make_unique<ChromeFacilitatedPaymentsClient>(
web_contents(), &optimization_guide_decider_);
}
// Test that the `EWALLET_MERCHANT_ALLOWLIST` optimization type is not
// registered when when the `ChromeFacilitatedPaymentClient` is created and the
// eWallet experiment is disabled.
TEST_F(ChromeFacilitatedPaymentsClientTest, RegisterAllowlists_EWalletExpOff) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(payments::facilitated::kEwalletPayments);
EXPECT_CALL(optimization_guide_decider_,
RegisterOptimizationTypes(testing::ElementsAre(
optimization_guide::proto::PIX_MERCHANT_ORIGINS_ALLOWLIST)))
.Times(1);
EXPECT_CALL(optimization_guide_decider_,
RegisterOptimizationTypes(testing::ElementsAre(
optimization_guide::proto::EWALLET_MERCHANT_ALLOWLIST)))
.Times(0);
// Re-create the client; it should not register the allowlist.
client_ = std::make_unique<ChromeFacilitatedPaymentsClient>(
web_contents(), &optimization_guide_decider_);
}
// Test the client forwards call for showing the progress screen to the
// controller.
TEST_F(ChromeFacilitatedPaymentsClientTest, ShowProgressScreen) {
EXPECT_CALL(controller(), ShowProgressScreen);
base_client().ShowProgressScreen();
}
// Test the client forwards call for showing the error screen to the controller.
TEST_F(ChromeFacilitatedPaymentsClientTest, ShowErrorScreen) {
EXPECT_CALL(controller(), ShowErrorScreen);
base_client().ShowErrorScreen();
}
// Test that the controller is able to process requests to show different
// screens back to back.
TEST_F(ChromeFacilitatedPaymentsClientTest,
ControllerIsAbleToProcessBackToBackShowRequests) {
EXPECT_CALL(controller(), Show);
EXPECT_CALL(controller(), ShowProgressScreen);
base_client().ShowPixPaymentPrompt({}, base::DoNothing());
base_client().ShowProgressScreen();
}
// Test the client forwards call for closing the bottom sheet to the
// controller.
TEST_F(ChromeFacilitatedPaymentsClientTest, DismissPrompt) {
EXPECT_CALL(controller(), Dismiss);
base_client().DismissPrompt();
}
// Test the client forwards call to check the device screen orientation to the
// controller.
TEST_F(ChromeFacilitatedPaymentsClientTest, IsInLandscapeMode) {
EXPECT_CALL(controller(), IsInLandscapeMode);
base_client().IsInLandscapeMode();
}
// Test that the client forwards call to show eWallet FOP selector to the
// controller.
TEST_F(ChromeFacilitatedPaymentsClientTest,
ShowEwalletPaymentPrompt_ControllerInvoked) {
EXPECT_CALL(controller(), ShowForEwallet);
base_client().ShowEwalletPaymentPrompt({}, base::DoNothing());
}
// Test that the client forwards call to initiate Pix account linking flow to
// the Pix account linking manager.
TEST_F(ChromeFacilitatedPaymentsClientTest, InitPixAccountLinkingFlow) {
EXPECT_CALL(pix_account_linking_manager(), MaybeShowPixAccountLinkingPrompt);
base_client().InitPixAccountLinkingFlow();
}
// Test that the client forwards call to show Pix account linking prompt to the
// controller.
TEST_F(ChromeFacilitatedPaymentsClientTest, ShowPixAccountLinkingPrompt) {
EXPECT_CALL(controller(), ShowPixAccountLinkingPrompt);
base_client().ShowPixAccountLinkingPrompt(base::DoNothing(),
base::DoNothing());
}
|