File: price_tracking_bubble_dialog_view_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (279 lines) | stat: -rw-r--r-- 10,207 bytes parent folder | download | duplicates (3)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2022 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/commerce/price_tracking_bubble_dialog_view.h"

#include "base/memory/raw_ptr.h"
#include "base/test/mock_callback.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/sync/local_or_syncable_bookmark_sync_service_factory.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_editor_view.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/views/chrome_test_widget.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/strings/grit/components_strings.h"
#include "components/sync_bookmarks/bookmark_sync_service.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/any_widget_observer.h"
#include "ui/views/widget/unique_widget_ptr.h"

namespace {
const char kTestURL[] = "http://www.google.com";
const char16_t kTestBookmarkTitle[] = u"Bookmark title";
}  // namespace

class PriceTrackingBubbleDialogViewUnitTest : public BrowserWithTestWindowTest {
 public:
  void SetUp() override {
    BrowserWithTestWindowTest::SetUp();

    anchor_widget_ =
        views::UniqueWidgetPtr(std::make_unique<ChromeTestWidget>());
    views::Widget::InitParams widget_params(
        views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET);
    widget_params.context = GetContext();
    anchor_widget_->Init(std::move(widget_params));

    bubble_coordinator_ = std::make_unique<PriceTrackingBubbleCoordinator>(
        anchor_widget_->GetContentsView());

    SetUpDependencies();
  }

  void TearDown() override {
    // Make sure the bubble is destroyed before the profile to avoid a crash.
    if (bubble_coordinator_->GetBubble()) {
      views::test::WidgetDestroyedWaiter destroyed_waiter(
          bubble_coordinator_->GetBubble()->GetWidget());
      bubble_coordinator_->GetBubble()->GetWidget()->Close();
      destroyed_waiter.Wait();
    }

    anchor_widget_.reset();

    BrowserWithTestWindowTest::TearDown();
  }

  TestingProfile::TestingFactories GetTestingFactories() override {
    return IdentityTestEnvironmentProfileAdaptor::
        GetIdentityTestEnvironmentFactoriesWithAppendedFactories(
            {TestingProfile::TestingFactory{
                BookmarkModelFactory::GetInstance(),
                BookmarkModelFactory::GetDefaultFactory()}});
  }

  void CreateBubbleViewAndShow(
      PriceTrackingBubbleDialogView::Type type,
      std::optional<std::u16string> bookmark_folder_name = std::nullopt) {
    SkBitmap bitmap;
    bitmap.allocN32Pixels(1, 1);
    bubble_coordinator_->Show(browser()->tab_strip_model()->GetWebContentsAt(0),
                              profile(), GURL(kTestURL),
                              ui::ImageModel::FromImage(gfx::Image(
                                  gfx::ImageSkia::CreateFrom1xBitmap(bitmap))),
                              Callback().Get(), OnDialogClosingCallback().Get(),
                              type, bookmark_folder_name);
  }

  base::MockCallback<PriceTrackingBubbleDialogView::OnTrackPriceCallback>&
  Callback() {
    return callback_;
  }

  base::MockCallback<base::OnceClosure>& OnDialogClosingCallback() {
    return on_dialog_closing_callback_;
  }

  PriceTrackingBubbleCoordinator* BubbleCoordinator() {
    return bubble_coordinator_.get();
  }

 protected:
  virtual void SetUpDependencies() {
    bookmark_model_ = BookmarkModelFactory::GetForBrowserContext(profile());
    bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model_);

    // Pretend sync is on for bookmarks, required for price tracking.
    LocalOrSyncableBookmarkSyncServiceFactory::GetForProfile(profile())
        ->SetIsTrackingMetadataForTesting();
  }

  raw_ptr<bookmarks::BookmarkModel, DanglingUntriaged> bookmark_model_;

 private:
  views::UniqueWidgetPtr anchor_widget_;
  base::MockCallback<PriceTrackingBubbleDialogView::OnTrackPriceCallback>
      callback_;
  std::unique_ptr<PriceTrackingBubbleCoordinator> bubble_coordinator_;
  base::MockCallback<base::OnceClosure> on_dialog_closing_callback_;
};

class PriceTrackingBubbleDialogViewLayoutUnitTest
    : public PriceTrackingBubbleDialogViewUnitTest,
      public ::testing::WithParamInterface<bool> {
 public:
  bool BookmarkWasCreated() { return GetParam(); }

  static std::string DescribeParams(
      const ::testing::TestParamInfo<ParamType>& info) {
    if (info.param) {
      return "TrackBookmarkedPage";
    } else {
      return "TrackNonBookmarkedPage";
    }
  }

  std::u16string GetFolderName() {
    if (BookmarkWasCreated()) {
      return bookmark_folder_name_;
    } else {
      return u"Shopping list";
    }
  }

 protected:
  void SetUpDependencies() override {
    PriceTrackingBubbleDialogViewUnitTest::SetUpDependencies();

    EXPECT_FALSE(
        bookmarks::IsBookmarkedByUser(bookmark_model_, GURL(kTestURL)));

    if (BookmarkWasCreated()) {
      auto* node = bookmarks::AddIfNotBookmarked(
          bookmark_model_, GURL(kTestURL), kTestBookmarkTitle);
      EXPECT_TRUE(
          bookmarks::IsBookmarkedByUser(bookmark_model_, GURL(kTestURL)));
      bookmark_folder_name_ = node->parent()->GetTitle();
    }
  }

 private:
  std::u16string bookmark_folder_name_;
};

TEST_P(PriceTrackingBubbleDialogViewLayoutUnitTest, FUEBubble) {
  CreateBubbleViewAndShow(
      PriceTrackingBubbleDialogView::Type::TYPE_FIRST_USE_EXPERIENCE,
      GetFolderName());

  auto* bubble = BubbleCoordinator()->GetBubble();
  EXPECT_TRUE(bubble);

  EXPECT_EQ(bubble->GetWindowTitle(),
            l10n_util::GetStringUTF16(
                IDS_OMNIBOX_TRACK_PRICE_DIALOG_TITLE_FIRST_RUN));

  EXPECT_TRUE(bubble->GetBodyLabelForTesting());
  EXPECT_EQ(bubble->GetBodyLabelForTesting()->GetText(),
            l10n_util::GetStringFUTF16(
                IDS_OMNIBOX_TRACK_PRICE_DIALOG_DESCRIPTION_FIRST_RUN,
                GetFolderName()));

  EXPECT_EQ(
      bubble->GetDialogButtonLabel(ui::mojom::DialogButton::kOk),
      l10n_util::GetStringUTF16(IDS_OMNIBOX_TRACK_PRICE_DIALOG_ACTION_BUTTON));
  EXPECT_EQ(
      bubble->GetDialogButtonLabel(ui::mojom::DialogButton::kCancel),
      l10n_util::GetStringUTF16(IDS_OMNIBOX_TRACK_PRICE_DIALOG_CANCEL_BUTTON));
}

TEST_P(PriceTrackingBubbleDialogViewLayoutUnitTest, NormalBubble) {
  // Price tracking can't happen if the bookmark wasn't created.
  if (!BookmarkWasCreated()) {
    return;
  }

  CreateBubbleViewAndShow(PriceTrackingBubbleDialogView::Type::TYPE_NORMAL,
                          GetFolderName());

  auto* bubble = BubbleCoordinator()->GetBubble();
  EXPECT_TRUE(bubble);

  EXPECT_EQ(bubble->GetWindowTitle(),
            l10n_util::GetStringUTF16(IDS_OMNIBOX_TRACKING_PRICE_DIALOG_TITLE));

  EXPECT_TRUE(bubble->GetBodyLabelForTesting());
  std::u16string expected_label =
      l10n_util::GetStringUTF16(IDS_PRICE_TRACKING_SAVE_DESCRIPTION);
  std::u16string expected_save_label = l10n_util::GetStringFUTF16(
      IDS_PRICE_TRACKING_SAVE_LOCATION, GetFolderName());
  EXPECT_TRUE(bubble->GetBodyLabelForTesting()->GetText().find(
                  expected_label) != std::u16string::npos);
  EXPECT_TRUE(bubble->GetBodyLabelForTesting()->GetText().find(
                  expected_save_label) != std::u16string::npos);
  EXPECT_TRUE(bubble->GetBodyLabelForTesting()->GetFirstLinkForTesting());

  EXPECT_EQ(bubble->GetDialogButtonLabel(ui::mojom::DialogButton::kOk),
            l10n_util::GetStringUTF16(
                IDS_OMNIBOX_TRACKING_PRICE_DIALOG_ACTION_BUTTON));
  EXPECT_EQ(bubble->GetDialogButtonLabel(ui::mojom::DialogButton::kCancel),
            l10n_util::GetStringUTF16(
                IDS_OMNIBOX_TRACKING_PRICE_DIALOG_UNTRACK_BUTTON));
}

INSTANTIATE_TEST_SUITE_P(
    All,
    PriceTrackingBubbleDialogViewLayoutUnitTest,
    ::testing::Values(true, false),
    &PriceTrackingBubbleDialogViewLayoutUnitTest::DescribeParams);

class PriceTrackingBubbleDialogViewActionUnitTest
    : public PriceTrackingBubbleDialogViewUnitTest {
 protected:
  void SetUpDependencies() override {
    PriceTrackingBubbleDialogViewUnitTest::SetUpDependencies();
    bookmarks::AddIfNotBookmarked(bookmark_model_, GURL(kTestURL),
                                  kTestBookmarkTitle);
  }
};

TEST_F(PriceTrackingBubbleDialogViewActionUnitTest, AcceptFUEBubble) {
  CreateBubbleViewAndShow(
      PriceTrackingBubbleDialogView::Type::TYPE_FIRST_USE_EXPERIENCE);

  auto* bubble = BubbleCoordinator()->GetBubble();
  EXPECT_TRUE(bubble);
  EXPECT_CALL(Callback(), Run(true));
  EXPECT_CALL(OnDialogClosingCallback(), Run());
  bubble->Accept();
}

TEST_F(PriceTrackingBubbleDialogViewActionUnitTest, CancelNormalBubble) {
  CreateBubbleViewAndShow(PriceTrackingBubbleDialogView::Type::TYPE_NORMAL);

  auto* bubble = BubbleCoordinator()->GetBubble();
  EXPECT_TRUE(bubble);
  EXPECT_CALL(Callback(), Run(false));
  EXPECT_CALL(OnDialogClosingCallback(), Run());
  bubble->Cancel();
}

TEST_F(PriceTrackingBubbleDialogViewActionUnitTest,
       ClickLinkInTheNormalBubble) {
  CreateBubbleViewAndShow(PriceTrackingBubbleDialogView::Type::TYPE_NORMAL,
                          /*bookmark_folder_name=*/u"Shopping list");

  auto* bubble = BubbleCoordinator()->GetBubble();
  EXPECT_TRUE(bubble);

  auto bookmark_editor_waiter = views::NamedWidgetShownWaiter(
      views::test::AnyWidgetTestPasskey{}, BookmarkEditorView::kViewClassName);

  EXPECT_CALL(OnDialogClosingCallback(), Run());
  bubble->GetBodyLabelForTesting()->ClickFirstLinkForTesting();
  EXPECT_TRUE(bookmark_editor_waiter.WaitIfNeededAndGet());

  task_environment()->RunUntilIdle();
  EXPECT_FALSE(BubbleCoordinator()->GetBubble());
}