File: update_address_profile_view_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 (149 lines) | stat: -rw-r--r-- 5,439 bytes parent folder | download | duplicates (5)
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
// Copyright 2021 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/views/autofill/update_address_profile_view.h"

#include <utility>

#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/common/autofill_features.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace autofill {

using profile_ref = base::optional_ref<const AutofillProfile>;
using ::testing::Property;

class MockUpdateAddressBubbleController : public UpdateAddressBubbleController {
 public:
  MockUpdateAddressBubbleController()
      : UpdateAddressBubbleController(
            /*delegate=*/nullptr,
            /*web_contents=*/nullptr,
            /*profile_to_save=*/test::GetFullProfile(),
            /*original_profile=*/test::GetFullProfile()) {}
  MOCK_METHOD(std::u16string, GetWindowTitle, (), (const, override));
  MOCK_METHOD(std::u16string, GetFooterMessage, (), (const, override));
  MOCK_METHOD(const AutofillProfile&, GetProfileToSave, (), (const, override));
  MOCK_METHOD(const AutofillProfile&,
              GetOriginalProfile,
              (),
              (const, override));
  MOCK_METHOD(void,
              OnUserDecision,
              (AutofillClient::AddressPromptUserDecision,
               base::optional_ref<const AutofillProfile>),
              (override));
  MOCK_METHOD(void, OnEditButtonClicked, (), (override));
  MOCK_METHOD(void, OnBubbleClosed, (), (override));
};

class UpdateAddressProfileViewTest : public ChromeViewsTestBase {
 public:
  UpdateAddressProfileViewTest() = default;
  ~UpdateAddressProfileViewTest() override = default;

  void CreateViewAndShow();

  void SetUp() override {
    ChromeViewsTestBase::SetUp();

    test_web_contents_ =
        content::WebContentsTester::CreateTestWebContents(&profile_, nullptr);
  }

  void TearDown() override {
    mock_controller_ = nullptr;
    std::exchange(view_, nullptr)
        ->GetWidget()
        ->CloseWithReason(views::Widget::ClosedReason::kCloseButtonClicked);
    anchor_widget_.reset();

    ChromeViewsTestBase::TearDown();
  }

  const AutofillProfile& address_profile_to_save() {
    return address_profile_to_save_;
  }
  const AutofillProfile& original_profile() { return original_profile_; }
  UpdateAddressProfileView* view() { return view_; }
  MockUpdateAddressBubbleController* mock_controller() {
    return mock_controller_;
  }

 private:
  base::test::ScopedFeatureList feature_list_;
  TestingProfile profile_;

  AutofillProfile address_profile_to_save_ = test::GetFullProfile();
  AutofillProfile original_profile_ = test::GetFullProfile2();

  // This enables uses of TestWebContents.
  content::RenderViewHostTestEnabler test_render_host_factories_;
  std::unique_ptr<content::WebContents> test_web_contents_;
  std::unique_ptr<views::Widget> anchor_widget_;
  raw_ptr<UpdateAddressProfileView> view_ = nullptr;
  raw_ptr<testing::NiceMock<MockUpdateAddressBubbleController>>
      mock_controller_ = nullptr;
};

void UpdateAddressProfileViewTest::CreateViewAndShow() {
  // The bubble needs the parent as an anchor.
  views::Widget::InitParams params =
      CreateParams(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
                   views::Widget::InitParams::TYPE_WINDOW);

  anchor_widget_ = std::make_unique<views::Widget>();
  anchor_widget_->Init(std::move(params));
  anchor_widget_->Show();

  auto mock_controller_unique =
      std::make_unique<testing::NiceMock<MockUpdateAddressBubbleController>>();
  mock_controller_ = mock_controller_unique.get();

  ON_CALL(*mock_controller(), GetWindowTitle())
      .WillByDefault(testing::Return(std::u16string()));
  ON_CALL(*mock_controller(), GetProfileToSave())
      .WillByDefault(testing::ReturnRef(address_profile_to_save()));
  ON_CALL(*mock_controller(), GetOriginalProfile())
      .WillByDefault(testing::ReturnRef(original_profile()));

  view_ = new UpdateAddressProfileView(anchor_widget_->GetContentsView(),
                                       std::move(mock_controller_unique),
                                       test_web_contents_.get());
  views::BubbleDialogDelegateView::CreateBubble(view_)->Show();
}

TEST_F(UpdateAddressProfileViewTest, HasCloseButton) {
  CreateViewAndShow();
  EXPECT_TRUE(view()->ShouldShowCloseButton());
}

TEST_F(UpdateAddressProfileViewTest, AcceptInvokesTheController) {
  CreateViewAndShow();
  EXPECT_CALL(
      *mock_controller(),
      OnUserDecision(AutofillClient::AddressPromptUserDecision::kAccepted,
                     Property(&profile_ref::has_value, true)));
  view()->AcceptDialog();
}

TEST_F(UpdateAddressProfileViewTest, CancelInvokesTheController) {
  CreateViewAndShow();
  EXPECT_CALL(
      *mock_controller(),
      OnUserDecision(AutofillClient::AddressPromptUserDecision::kDeclined,
                     Property(&profile_ref::has_value, false)));
  view()->CancelDialog();
}

}  // namespace autofill