File: autofill_util_unittest.cc

package info (click to toggle)
chromium 140.0.7339.80-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,193,064 kB
  • sloc: cpp: 35,092,386; ansic: 7,161,671; javascript: 4,199,703; python: 1,441,797; asm: 949,904; xml: 747,409; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; 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 (173 lines) | stat: -rw-r--r-- 6,747 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
// Copyright 2023 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/extensions/api/autofill_private/autofill_util.h"

#include <memory>

#include "base/functional/callback_forward.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/autofill/core/browser/data_manager/payments/test_payments_data_manager.h"
#include "components/autofill/core/browser/data_manager/test_personal_data_manager.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/common/autofill_test_utils.h"
#include "components/autofill/core/common/dense_set.h"
#include "components/device_reauth/mock_device_authenticator.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/browser_test.h"

namespace extensions::autofill_util {

namespace {

using ::testing::_;
using ::testing::Return;
using ::testing::UnorderedElementsAre;
using MockCallbackAfterSuccessfulUserAuth =
    base::MockCallback<CallbackAfterSuccessfulUserAuth>;

MATCHER_P(MatchesIbanType, iban, "") {
  bool is_local = iban.record_type() == autofill::Iban::RecordType::kLocalIban;
  return arg.metadata->is_local == is_local;
}

MATCHER_P(MatchesBnplIssuer, bnpl_issuer, "") {
  return arg.issuer_id ==
         autofill::ConvertToBnplIssuerIdString(bnpl_issuer.issuer_id());
}

class AutofillUtilTest : public InProcessBrowserTest {
 public:
  AutofillUtilTest() = default;

  AutofillUtilTest(const AutofillUtilTest&) = delete;
  AutofillUtilTest& operator=(const AutofillUtilTest&) = delete;

  void SetUpOnMainThread() override {
    mock_device_authenticator_ =
        std::make_unique<device_reauth::MockDeviceAuthenticator>();
  }

 protected:
  autofill::test::AutofillBrowserTestEnvironment autofill_test_environment_;
  std::unique_ptr<device_reauth::MockDeviceAuthenticator>
      mock_device_authenticator_;
};

class AutofillUtilTestForBnpl : public AutofillUtilTest {
 public:
  AutofillUtilTestForBnpl() {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/
        {autofill::features::kAutofillEnableBuyNowPayLater,
         autofill::features::kAutofillEnableBuyNowPayLaterSyncing,
         autofill::features::kAutofillEnableBuyNowPayLaterForKlarna},
        /*disabled_features=*/{});
  }

  AutofillUtilTestForBnpl(const AutofillUtilTestForBnpl&) = delete;
  AutofillUtilTestForBnpl& operator=(const AutofillUtilTestForBnpl&) = delete;

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(AutofillUtilTest, GenerateIbanList) {
  autofill::TestPaymentsDataManager paydm;
  paydm.SetAutofillWalletImportEnabled(true);

  autofill::Iban local_iban = autofill::test::GetLocalIban();
  paydm.AddIbanForTest(std::make_unique<autofill::Iban>(local_iban));
  autofill::Iban server_iban = autofill::test::GetServerIban();
  paydm.AddServerIban(server_iban);

  IbanEntryList iban_list = GenerateIbanList(paydm);
  EXPECT_THAT(iban_list, UnorderedElementsAre(MatchesIbanType(local_iban),
                                              MatchesIbanType(server_iban)));
}

IN_PROC_BROWSER_TEST_F(AutofillUtilTest, AuthenticateUser_SuccessfulAuth) {
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  base::MockCallback<base::OnceCallback<void(bool)>> mock_result_callback;
  const std::u16string mock_prompt_message = u"This is a mock message";

  ON_CALL(*mock_device_authenticator_, AuthenticateWithMessage)
      .WillByDefault(
          testing::WithArg<1>([](base::OnceCallback<void(bool)> callback) {
            std::move(callback).Run(true);
          }));
  EXPECT_CALL(mock_result_callback, Run(true));
  EXPECT_CALL(*mock_device_authenticator_,
              AuthenticateWithMessage(mock_prompt_message, _));

  mock_device_authenticator_->AuthenticateWithMessage(
      mock_prompt_message, mock_result_callback.Get());
#endif
}

IN_PROC_BROWSER_TEST_F(AutofillUtilTest, AuthenticateUser_UnSuccessfulAuth) {
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  base::MockCallback<base::OnceCallback<void(bool)>> mock_result_callback;
  const std::u16string mock_prompt_message = u"This is a mock message";

  ON_CALL(*mock_device_authenticator_, AuthenticateWithMessage)
      .WillByDefault(
          testing::WithArg<1>([](base::OnceCallback<void(bool)> callback) {
            std::move(callback).Run(false);
          }));
  EXPECT_CALL(mock_result_callback, Run(false));
  EXPECT_CALL(*mock_device_authenticator_,
              AuthenticateWithMessage(mock_prompt_message, _));

  mock_device_authenticator_->AuthenticateWithMessage(
      mock_prompt_message, mock_result_callback.Get());
#endif
}

IN_PROC_BROWSER_TEST_F(AutofillUtilTestForBnpl,
                       GeneratePayOverTimeIssuerList_IssuerLinkedExternally) {
  autofill::TestPaymentsDataManager paydm;
  paydm.SetAutofillWalletImportEnabled(true);
  paydm.SetAutofillPaymentMethodsEnabled(true);
  paydm.SetIsAutofillBnplPrefEnabled(true);
  autofill::BnplIssuer issuer_affirm = autofill::test::GetTestLinkedBnplIssuer(
      autofill::BnplIssuer::IssuerId::kBnplAffirm);
  autofill::BnplIssuer issuer_klarna = autofill::test::GetTestLinkedBnplIssuer(
      autofill::BnplIssuer::IssuerId::kBnplKlarna,
      /*action_required=*/autofill::DenseSet(
          {autofill::PaymentInstrument::ActionRequired::kAcceptTos}));
  paydm.AddBnplIssuer(issuer_affirm);
  paydm.AddBnplIssuer(issuer_klarna);

  PayOverTimeIssuerEntryList bnpl_issuer_list =
      GeneratePayOverTimeIssuerList(paydm);
  EXPECT_THAT(bnpl_issuer_list,
              UnorderedElementsAre(MatchesBnplIssuer(issuer_affirm)));
}

IN_PROC_BROWSER_TEST_F(AutofillUtilTestForBnpl,
                       GeneratePayOverTimeIssuerList_IssuerTosAccepted) {
  autofill::TestPaymentsDataManager paydm;
  paydm.SetAutofillWalletImportEnabled(true);
  paydm.SetAutofillPaymentMethodsEnabled(true);
  paydm.SetIsAutofillBnplPrefEnabled(true);
  autofill::BnplIssuer issuer_affirm = autofill::test::GetTestLinkedBnplIssuer(
      autofill::BnplIssuer::IssuerId::kBnplAffirm);
  autofill::BnplIssuer issuer_klarna = autofill::test::GetTestLinkedBnplIssuer(
      autofill::BnplIssuer::IssuerId::kBnplKlarna);
  paydm.AddBnplIssuer(issuer_affirm);
  paydm.AddBnplIssuer(issuer_klarna);

  PayOverTimeIssuerEntryList bnpl_issuer_list =
      GeneratePayOverTimeIssuerList(paydm);
  EXPECT_THAT(bnpl_issuer_list,
              UnorderedElementsAre(MatchesBnplIssuer(issuer_affirm),
                                   MatchesBnplIssuer(issuer_klarna)));
}

}  // namespace

}  // namespace extensions::autofill_util