File: android_payments_window_manager_unittest.cc

package info (click to toggle)
chromium 140.0.7339.127-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,772 kB
  • sloc: cpp: 35,093,800; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,798; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (210 lines) | stat: -rw-r--r-- 8,290 bytes parent folder | download | duplicates (4)
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
// Copyright 2025 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/android_payments_window_manager.h"

#include <memory>
#include <string_view>

#include "base/test/mock_callback.h"
#include "chrome/browser/ui/autofill/payments/android_payments_window_manager_test_api.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.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/payments/payments_window_manager.h"
#include "components/autofill/core/browser/payments/test_payments_autofill_client.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace autofill {

class TestContentAutofillClientForWindowManagerTest
    : public TestContentAutofillClient {
 public:
  explicit TestContentAutofillClientForWindowManagerTest(
      content::WebContents* web_contents)
      : TestContentAutofillClient(web_contents) {
    GetPaymentsAutofillClient()->set_payments_window_manager(
        std::make_unique<payments::AndroidPaymentsWindowManager>(this));
  }

  ~TestContentAutofillClientForWindowManagerTest() override = default;
};

namespace payments {

constexpr std::string_view kBnplInitialUrl = "https://www.bnplinitialurl.com/";
constexpr std::string_view kBnplSuccessUrlPrefix =
    "https://www.bnplsuccess.com/";
constexpr std::string_view kBnplFailureUrlPrefix =
    "https://www.bnplfailure.com/";

class AndroidPaymentsWindowManagerTest
    : public ChromeRenderViewHostTestHarness {
 protected:
  TestContentAutofillClientForWindowManagerTest* client() {
    return test_autofill_client_injector_[web_contents()];
  }

  AndroidPaymentsWindowManager& window_manager() {
    return *static_cast<AndroidPaymentsWindowManager*>(
        client()->GetPaymentsAutofillClient()->GetPaymentsWindowManager());
  }

  void InitBnplFlowForTest() {
    PaymentsWindowManager::BnplContext context;
    context.issuer_id = BnplIssuer::IssuerId::kBnplAffirm;
    context.success_url_prefix = GURL(kBnplSuccessUrlPrefix);
    context.failure_url_prefix = GURL(kBnplFailureUrlPrefix);
    context.initial_url = GURL(kBnplInitialUrl);
    context.completion_callback = bnpl_tab_closed_callback_.Get();
    window_manager().InitBnplFlow(std::move(context));
  }

  base::MockCallback<PaymentsWindowManager::OnBnplPopupClosedCallback>
      bnpl_tab_closed_callback_;
  base::HistogramTester histogram_tester_;

 private:
  TestAutofillClientInjector<TestContentAutofillClientForWindowManagerTest>
      test_autofill_client_injector_;
};

// Test that calling InitBnplFlow() correctly sets the internal state of the
// window manager.
TEST_F(AndroidPaymentsWindowManagerTest, InitBnplFlow) {
  // The flow should not be ongoing initially.
  EXPECT_TRUE(test_api(window_manager()).NoOngoingFlow());

  InitBnplFlowForTest();

  // After initialization, a BNPL flow should be ongoing.
  EXPECT_FALSE(test_api(window_manager()).NoOngoingFlow());
  ASSERT_TRUE(test_api(window_manager()).GetBnplContext().has_value());
  EXPECT_EQ(test_api(window_manager()).GetBnplContext()->initial_url,
            GURL(kBnplInitialUrl));
  EXPECT_EQ(test_api(window_manager()).GetBnplContext()->success_url_prefix,
            GURL(kBnplSuccessUrlPrefix));
  EXPECT_EQ(test_api(window_manager()).GetBnplContext()->failure_url_prefix,
            GURL(kBnplFailureUrlPrefix));
  histogram_tester_.ExpectUniqueSample("Autofill.Bnpl.PopupWindowShown.Affirm",
                                       true, 1);
}

// Test that when the web contents are destroyed during a BNPL flow after a
// successful navigation, the correct result is propagated and metrics are
// logged.
TEST_F(AndroidPaymentsWindowManagerTest, WebContentsDestroyed_Bnpl_Success) {
  InitBnplFlowForTest();

  // Simulate navigation to the success URL.
  const GURL success_url =
      GURL(std::string(kBnplSuccessUrlPrefix) + "?status=success");
  window_manager().OnDidFinishNavigationForBnpl(success_url);
  EXPECT_EQ(test_api(window_manager()).GetMostRecentUrlNavigation(),
            success_url);

  EXPECT_CALL(
      bnpl_tab_closed_callback_,
      Run(PaymentsWindowManager::BnplFlowResult::kSuccess, success_url));

  // Simulate destruction of the tab.
  window_manager().WebContentsDestroyed();

  // Verify metrics and state reset.
  histogram_tester_.ExpectUniqueSample(
      "Autofill.Bnpl.PopupWindowResult.Affirm",
      PaymentsWindowManager::BnplFlowResult::kSuccess, 1);
  histogram_tester_.ExpectTotalCount(
      "Autofill.Bnpl.PopupWindowLatency.Affirm.Success", 1);
  EXPECT_TRUE(test_api(window_manager()).NoOngoingFlow());
}

// Test that when the web contents are destroyed during a BNPL flow after a
// failed navigation, the correct result is propagated and metrics are logged.
TEST_F(AndroidPaymentsWindowManagerTest, WebContentsDestroyed_Bnpl_Failure) {
  InitBnplFlowForTest();

  // Simulate navigation to the failure URL.
  const GURL failure_url =
      GURL(std::string(kBnplFailureUrlPrefix) + "?status=failure");
  window_manager().OnDidFinishNavigationForBnpl(failure_url);
  EXPECT_EQ(test_api(window_manager()).GetMostRecentUrlNavigation(),
            failure_url);

  EXPECT_CALL(
      bnpl_tab_closed_callback_,
      Run(PaymentsWindowManager::BnplFlowResult::kFailure, failure_url));

  // Simulate destruction of the tab.
  window_manager().WebContentsDestroyed();

  // Verify metrics and state reset.
  histogram_tester_.ExpectUniqueSample(
      "Autofill.Bnpl.PopupWindowResult.Affirm",
      PaymentsWindowManager::BnplFlowResult::kFailure, 1);
  histogram_tester_.ExpectTotalCount(
      "Autofill.Bnpl.PopupWindowLatency.Affirm.Failure", 1);
  EXPECT_TRUE(test_api(window_manager()).NoOngoingFlow());
}

// Test that when the web contents are destroyed during a BNPL flow before the
// flow completes, the correct result is propagated and metrics are logged.
TEST_F(AndroidPaymentsWindowManagerTest, WebContentsDestroyed_Bnpl_UserClosed) {
  InitBnplFlowForTest();

  // Simulate navigation to some intermediate URL.
  const GURL intermediate_url("https://www.bnpl.com/intermediate-step");
  window_manager().OnDidFinishNavigationForBnpl(intermediate_url);
  EXPECT_EQ(test_api(window_manager()).GetMostRecentUrlNavigation(),
            intermediate_url);

  EXPECT_CALL(bnpl_tab_closed_callback_,
              Run(PaymentsWindowManager::BnplFlowResult::kUserClosed,
                  intermediate_url));

  // Simulate destruction of the tab.
  window_manager().WebContentsDestroyed();

  // Verify metrics and state reset.
  histogram_tester_.ExpectUniqueSample(
      "Autofill.Bnpl.PopupWindowResult.Affirm",
      PaymentsWindowManager::BnplFlowResult::kUserClosed, 1);
  histogram_tester_.ExpectTotalCount(
      "Autofill.Bnpl.PopupWindowLatency.Affirm.UserClosed", 1);
  EXPECT_TRUE(test_api(window_manager()).NoOngoingFlow());
}

// Test that if the tab is closed by the user before any navigation
// completes, it's handled as a user cancellation.
TEST_F(AndroidPaymentsWindowManagerTest,
       WebContentsDestroyed_Bnpl_UserClosed_NoNavigation) {
  InitBnplFlowForTest();

  // No navigation occurs. Before any navigation,
  // `most_recent_url_navigation` is empty. The completion callback will
  // receive an empty GURL.
  EXPECT_TRUE(
      test_api(window_manager()).GetMostRecentUrlNavigation().is_empty());

  // Expect the completion callback to be called with a user-closed result.
  // Since no navigation finished, the URL passed should be empty.
  EXPECT_CALL(bnpl_tab_closed_callback_,
              Run(PaymentsWindowManager::BnplFlowResult::kUserClosed, GURL()));

  // Simulate destruction of the tab.
  window_manager().WebContentsDestroyed();

  // Verify metrics and state reset.
  histogram_tester_.ExpectUniqueSample(
      "Autofill.Bnpl.PopupWindowResult.Affirm",
      PaymentsWindowManager::BnplFlowResult::kUserClosed, 1);
  histogram_tester_.ExpectTotalCount(
      "Autofill.Bnpl.PopupWindowLatency.Affirm.UserClosed", 1);
  EXPECT_TRUE(test_api(window_manager()).NoOngoingFlow());
}

}  // namespace payments

}  // namespace autofill