File: autofill_save_iban_delegate_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (161 lines) | stat: -rw-r--r-- 6,594 bytes parent folder | download | duplicates (6)
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
// 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/ui/android/autofill/autofill_save_iban_delegate.h"

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

#include "base/test/mock_callback.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/autofill/core/browser/foundations/autofill_client.h"
#include "components/browser_ui/device_lock/android/test_device_lock_bridge.h"
#include "content/public/browser/web_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/android/window_android.h"

namespace autofill {
namespace {

const std::u16string kUserProvidedNickname = u"My doctor's IBAN";

using SaveIbanOfferUserDecision =
    payments::PaymentsAutofillClient::SaveIbanOfferUserDecision;
using LocalCallbackArgs = std::pair<SaveIbanOfferUserDecision, std::u16string>;

class AutofillSaveIbanDelegateTest : public ChromeRenderViewHostTestHarness {
 protected:
  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();

    delegate_ = std::make_unique<AutofillSaveIbanDelegate>(MakeLocalCallback(),
                                                           web_contents());
    std::unique_ptr<TestDeviceLockBridge> bridge =
        std::make_unique<TestDeviceLockBridge>();
    test_bridge_ = bridge.get();
    delegate_->SetDeviceLockBridgeForTesting(std::move(bridge));

    // Create a scoped window so that
    // WebContents::GetNativeView()->GetWindowAndroid() does not return null.
    window_ = ui::WindowAndroid::CreateForTesting();
    window_.get()->get()->AddChild(web_contents()->GetNativeView());
  }

  void LocalCallback(SaveIbanOfferUserDecision decision,
                     std::u16string_view nickname);

  payments::PaymentsAutofillClient::SaveIbanPromptCallback MakeLocalCallback();

  std::optional<LocalCallbackArgs> local_offer_decision_;

  std::unique_ptr<AutofillSaveIbanDelegate> delegate_;
  raw_ptr<TestDeviceLockBridge> test_bridge_;
  std::unique_ptr<ui::WindowAndroid::ScopedWindowAndroidForTesting> window_;
};

void AutofillSaveIbanDelegateTest::LocalCallback(
    SaveIbanOfferUserDecision decision,
    std::u16string_view nickname) {
  local_offer_decision_.emplace(decision, nickname);
}

payments::PaymentsAutofillClient::SaveIbanPromptCallback
AutofillSaveIbanDelegateTest::MakeLocalCallback() {
  return base::BindOnce(
      &AutofillSaveIbanDelegateTest::LocalCallback,
      base::Unretained(this));  // Test function does not outlive test fixture.
}

// Matches the SaveIbanPromptCallback arguments to a LocalCallbackArgs.
testing::Matcher<LocalCallbackArgs> EqualToLocalCallbackArgs(
    SaveIbanOfferUserDecision decision,
    std::u16string user_provided_nickname) {
  return testing::AllOf(
      testing::Field(&LocalCallbackArgs::first, decision),
      testing::Field(&LocalCallbackArgs::second, user_provided_nickname));
}

TEST_F(AutofillSaveIbanDelegateTest,
       OnUiAcceptedWithCallbackArgumentRunsCallback) {
  auto delegate = AutofillSaveIbanDelegate(MakeLocalCallback(), web_contents());

  base::MockOnceClosure mock_finish_gathering_consent_callback;
  EXPECT_CALL(mock_finish_gathering_consent_callback, Run).Times(1);
  delegate.OnUiAccepted(mock_finish_gathering_consent_callback.Get());
}

TEST_F(AutofillSaveIbanDelegateTest, OnUiAcceptedRunsLocalCallback) {
  auto delegate = AutofillSaveIbanDelegate(MakeLocalCallback(), web_contents());

  base::MockOnceClosure mock_finish_gathering_consent_callback;
  delegate.OnUiAccepted(mock_finish_gathering_consent_callback.Get(),
                        kUserProvidedNickname);

  EXPECT_THAT(local_offer_decision_,
              testing::Optional(EqualToLocalCallbackArgs(
                  SaveIbanOfferUserDecision::kAccepted,
                  /*user_provided_nickname=*/kUserProvidedNickname)));
}

TEST_F(AutofillSaveIbanDelegateTest, OnUiCanceledRunsLocalCallback) {
  auto delegate = AutofillSaveIbanDelegate(MakeLocalCallback(), web_contents());

  delegate.OnUiCanceled();

  EXPECT_THAT(local_offer_decision_, testing::Optional(EqualToLocalCallbackArgs(
                                         SaveIbanOfferUserDecision::kDeclined,
                                         /*user_provided_nickname=*/u"")));
}

TEST_F(AutofillSaveIbanDelegateTest, OnUiIgnoredRunsLocalCallback) {
  auto delegate = AutofillSaveIbanDelegate(MakeLocalCallback(), web_contents());

  delegate.OnUiIgnored();

  EXPECT_THAT(local_offer_decision_, testing::Optional(EqualToLocalCallbackArgs(
                                         SaveIbanOfferUserDecision::kIgnored,
                                         /*user_provided_nickname=*/u"")));
}

// Tests that IBAN is saved if device lock requirements are met (example: device
// lock is set).
TEST_F(AutofillSaveIbanDelegateTest, DeviceLockRequirementsMet) {
  base::MockOnceClosure mock_finish_gathering_consent_callback;
  delegate_->OnUiAccepted(mock_finish_gathering_consent_callback.Get(),
                          kUserProvidedNickname);

  // Verify that device lock requirements check was started but that IBAN is not
  // saved yet.
  EXPECT_TRUE(test_bridge_->did_start_checking_device_lock_requirements());
  EXPECT_FALSE(local_offer_decision_.has_value());

  test_bridge_->SimulateFinishedCheckingDeviceLockRequirements(
      /*are_device_lock_requirements_met=*/true);
  EXPECT_THAT(local_offer_decision_,
              testing::Optional(EqualToLocalCallbackArgs(
                  SaveIbanOfferUserDecision::kAccepted,
                  /*user_provided_nickname=*/kUserProvidedNickname)));
}

// Tests that IBAN is not saved if device lock UI is shown but device lock is
// not set.
TEST_F(AutofillSaveIbanDelegateTest, DeviceLockRequirementsNotMet) {
  base::MockOnceClosure mock_finish_gathering_consent_callback;
  delegate_->OnUiAccepted(mock_finish_gathering_consent_callback.Get(), u"");

  // Verify that device lock requirements check was started but that IBAN is not
  // saved yet.
  EXPECT_TRUE(test_bridge_->did_start_checking_device_lock_requirements());
  EXPECT_FALSE(local_offer_decision_.has_value());

  test_bridge_->SimulateFinishedCheckingDeviceLockRequirements(
      /*are_device_lock_requirements_met=*/false);
  EXPECT_THAT(local_offer_decision_, testing::Optional(EqualToLocalCallbackArgs(
                                         SaveIbanOfferUserDecision::kDeclined,
                                         /*user_provided_nickname=*/u"")));
}

}  // namespace
}  // namespace autofill