File: price_tracking_page_action_controller_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; 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 (412 lines) | stat: -rw-r--r-- 17,189 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// 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/commerce/price_tracking_page_action_controller.h"

#include <memory>
#include <optional>

#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "chrome/test/base/testing_profile.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/mock_shopping_service.h"
#include "components/commerce/core/shopping_service.h"
#include "components/commerce/core/subscriptions/commerce_subscription.h"
#include "components/commerce/core/test_utils.h"
#include "components/feature_engagement/test/mock_tracker.h"
#include "components/image_fetcher/core/mock_image_fetcher.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"

namespace commerce {

namespace {

// Build a basic ProductInfo object.
std::optional<ProductInfo> CreateProductInfo(uint64_t cluster_id,
                                             const GURL& image_url = GURL()) {
  std::optional<ProductInfo> info;
  info.emplace();
  info->product_cluster_id = cluster_id;
  if (!image_url.is_empty()) {
    info->image_url = image_url;
  }
  return info;
}

}  // namespace

class PriceTrackingPageActionControllerUnittest : public testing::Test {
 public:
  PriceTrackingPageActionControllerUnittest()
      : shopping_service_(std::make_unique<MockShoppingService>()),
        image_fetcher_(std::make_unique<image_fetcher::MockImageFetcher>()),
        tracker_(std::make_unique<feature_engagement::test::MockTracker>()) {}

  PriceTrackingPageActionControllerUnittest(
      const PriceTrackingPageActionControllerUnittest&) = delete;
  PriceTrackingPageActionControllerUnittest operator=(
      const PriceTrackingPageActionControllerUnittest&) = delete;
  ~PriceTrackingPageActionControllerUnittest() override = default;

  void TestBody() override {}

  void SetupImageFetcherForSimpleImage(bool valid_image) {
    ON_CALL(*image_fetcher_, FetchImageAndData_)
        .WillByDefault(
            [valid_image = valid_image](
                const GURL& image_url,
                image_fetcher::ImageDataFetcherCallback* image_data_callback,
                image_fetcher::ImageFetcherCallback* image_callback,
                image_fetcher::ImageFetcherParams params) {
              if (valid_image) {
                SkBitmap bitmap;
                bitmap.allocN32Pixels(1, 1);
                gfx::Image image =
                    gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(bitmap));
                std::move(*image_callback)
                    .Run(std::move(image), image_fetcher::RequestMetadata());
              } else {
                std::move(*image_callback)
                    .Run(gfx::Image(), image_fetcher::RequestMetadata());
              }
            });
  }

 protected:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  base::MockRepeatingCallback<void()> notify_host_callback_;
  std::unique_ptr<MockShoppingService> shopping_service_;
  std::unique_ptr<image_fetcher::MockImageFetcher> image_fetcher_;
  std::unique_ptr<feature_engagement::test::MockTracker> tracker_;
};

TEST_F(PriceTrackingPageActionControllerUnittest, IconShown) {
  shopping_service_->SetIsShoppingListEligible(true);
  shopping_service_->SetIsSubscribedCallbackValue(false);
  SetupImageFetcherForSimpleImage(/*valid_image=*/true);
  base::RepeatingCallback<void()> callback = notify_host_callback_.Get();
  PriceTrackingPageActionController controller(
      std::move(callback), shopping_service_.get(), image_fetcher_.get(),
      tracker_.get());

  // Before a navigation, the controller should be in an "undecided" state.
  ASSERT_FALSE(controller.ShouldShowForNavigation().has_value());
  ASSERT_FALSE(controller.WantsExpandedUi());

  uint64_t cluster_id = 12345L;
  EXPECT_CALL(notify_host_callback_, Run()).Times(testing::AtLeast(1));
  EXPECT_CALL(*shopping_service_,
              IsSubscribed(SubscriptionWithId(base::NumberToString(cluster_id)),
                           testing::_))
      .Times(1);
  shopping_service_->SetResponseForGetProductInfoForUrl(
      CreateProductInfo(cluster_id, GURL("http://example.com/image.png")));
  controller.ResetForNewNavigation(GURL("http://example.com"));
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(controller.ShouldShowForNavigation().has_value());
  ASSERT_TRUE(controller.ShouldShowForNavigation().value());
}

TEST_F(PriceTrackingPageActionControllerUnittest, IconNotShown_NoImage) {
  shopping_service_->SetIsShoppingListEligible(true);
  shopping_service_->SetIsSubscribedCallbackValue(false);
  SetupImageFetcherForSimpleImage(/*valid_image=*/false);
  base::RepeatingCallback<void()> callback = notify_host_callback_.Get();
  PriceTrackingPageActionController controller(
      std::move(callback), shopping_service_.get(), image_fetcher_.get(),
      tracker_.get());

  // Before a navigation, the controller should be in an "undecided" state.
  ASSERT_FALSE(controller.ShouldShowForNavigation().has_value());
  ASSERT_FALSE(controller.WantsExpandedUi());

  uint64_t cluster_id = 12345L;
  EXPECT_CALL(notify_host_callback_, Run()).Times(testing::AtLeast(1));
  EXPECT_CALL(*shopping_service_,
              IsSubscribed(SubscriptionWithId(base::NumberToString(cluster_id)),
                           testing::_))
      .Times(1);
  shopping_service_->SetResponseForGetProductInfoForUrl(
      CreateProductInfo(cluster_id, GURL("http://example.com/image.png")));
  controller.ResetForNewNavigation(GURL("http://example.com"));
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(controller.ShouldShowForNavigation().has_value());
  ASSERT_FALSE(controller.ShouldShowForNavigation().value());
}

TEST_F(PriceTrackingPageActionControllerUnittest, IconNotShown_NoProductInfo) {
  shopping_service_->SetIsShoppingListEligible(true);
  base::RepeatingCallback<void()> callback = notify_host_callback_.Get();
  PriceTrackingPageActionController controller(
      std::move(callback), shopping_service_.get(), image_fetcher_.get(),
      tracker_.get());

  // Before a navigation, the controller should be in an "undecided" state.
  ASSERT_FALSE(controller.ShouldShowForNavigation().has_value());
  ASSERT_FALSE(controller.WantsExpandedUi());

  EXPECT_CALL(notify_host_callback_, Run()).Times(testing::AtLeast(1));
  EXPECT_CALL(*shopping_service_, IsSubscribed(testing::_, testing::_))
      .Times(0);
  shopping_service_->SetResponseForGetProductInfoForUrl(std::nullopt);
  controller.ResetForNewNavigation(GURL("http://example.com"));
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(controller.ShouldShowForNavigation().has_value());
  ASSERT_FALSE(controller.ShouldShowForNavigation().value());
}

TEST_F(PriceTrackingPageActionControllerUnittest, IconNotShown_NotEligible) {
  shopping_service_->SetIsShoppingListEligible(false);
  base::RepeatingCallback<void()> callback = notify_host_callback_.Get();
  PriceTrackingPageActionController controller(
      std::move(callback), shopping_service_.get(), image_fetcher_.get(),
      tracker_.get());

  controller.ResetForNewNavigation(GURL("http://example.com"));
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(controller.ShouldShowForNavigation().has_value());
  ASSERT_FALSE(controller.ShouldShowForNavigation().value());
}

TEST_F(PriceTrackingPageActionControllerUnittest,
       SubscriptionEventsUpdateState) {
  shopping_service_->SetIsShoppingListEligible(true);
  shopping_service_->SetIsSubscribedCallbackValue(false);
  SetupImageFetcherForSimpleImage(/*valid_image=*/true);
  base::RepeatingCallback<void()> callback = notify_host_callback_.Get();
  PriceTrackingPageActionController controller(
      std::move(callback), shopping_service_.get(), image_fetcher_.get(),
      tracker_.get());

  // Before a navigation, the controller should be in an "undecided" state.
  ASSERT_FALSE(controller.ShouldShowForNavigation().has_value());
  ASSERT_FALSE(controller.WantsExpandedUi());

  shopping_service_->SetResponseForGetProductInfoForUrl(
      CreateProductInfo(12345L, GURL("http://example.com/image.png")));
  controller.ResetForNewNavigation(GURL("http://example.com"));
  base::RunLoop().RunUntilIdle();

  CommerceSubscription sub(SubscriptionType::kPriceTrack,
                           IdentifierType::kProductClusterId, "12345",
                           ManagementType::kUserManaged);

  EXPECT_CALL(notify_host_callback_, Run()).Times(testing::AtLeast(1));
  shopping_service_->SetIsSubscribedCallbackValue(true);
  controller.OnSubscribe(sub, true);
  base::RunLoop().RunUntilIdle();

  EXPECT_CALL(notify_host_callback_, Run()).Times(testing::AtLeast(1));
  shopping_service_->SetIsSubscribedCallbackValue(false);
  controller.OnUnsubscribe(sub, true);
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(controller.ShouldShowForNavigation().has_value());
  ASSERT_TRUE(controller.ShouldShowForNavigation().value());
}

TEST_F(PriceTrackingPageActionControllerUnittest, WantsExpandedUi_HighPrice) {
  shopping_service_->SetIsShoppingListEligible(true);
  SetupImageFetcherForSimpleImage(/*valid_image=*/true);
  base::RepeatingCallback<void()> callback = notify_host_callback_.Get();
  PriceTrackingPageActionController controller(
      std::move(callback), shopping_service_.get(), image_fetcher_.get(),
      tracker_.get());

  std::optional<ProductInfo> info =
      CreateProductInfo(12345L, GURL("http://example.com/image.png"));
  info->amount_micros = 150000000L;  // $150 in micros
  shopping_service_->SetResponseForGetProductInfoForUrl(info);

  // First run the case where the user isn't subscribed.
  shopping_service_->SetIsSubscribedCallbackValue(false);
  controller.ResetForNewNavigation(GURL("http://example.com"));
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(controller.ShouldShowForNavigation().has_value());
  ASSERT_TRUE(controller.ShouldShowForNavigation().value());
  ASSERT_TRUE(controller.WantsExpandedUi());

  // If the user is already subscribed, we shouldn't expand regardless of the
  // price.
  shopping_service_->SetIsSubscribedCallbackValue(true);
  controller.ResetForNewNavigation(GURL("http://example.com"));
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(controller.ShouldShowForNavigation().has_value());
  ASSERT_TRUE(controller.ShouldShowForNavigation().value());
  ASSERT_FALSE(controller.WantsExpandedUi());
}

TEST_F(PriceTrackingPageActionControllerUnittest, WantsExpandedUi_Tracker) {
  shopping_service_->SetIsShoppingListEligible(true);
  SetupImageFetcherForSimpleImage(/*valid_image=*/true);
  base::RepeatingCallback<void()> callback = notify_host_callback_.Get();
  PriceTrackingPageActionController controller(
      std::move(callback), shopping_service_.get(), image_fetcher_.get(),
      tracker_.get());

  shopping_service_->SetResponseForGetProductInfoForUrl(
      CreateProductInfo(12345L, GURL("http://example.com/image.png")));

  ON_CALL(*tracker_,
          ShouldTriggerHelpUI(testing::Ref(
              feature_engagement::kIPHPriceTrackingPageActionIconLabelFeature)))
      .WillByDefault(testing::Return(true));

  // First run the case where the user isn't subscribed.
  shopping_service_->SetIsSubscribedCallbackValue(false);
  controller.ResetForNewNavigation(GURL("http://example.com"));
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(controller.ShouldShowForNavigation().has_value());
  ASSERT_TRUE(controller.ShouldShowForNavigation().value());
  ASSERT_TRUE(controller.WantsExpandedUi());

  // If the user is already subscribed, we shouldn't access the tracker or
  // expand.
  shopping_service_->SetIsSubscribedCallbackValue(true);
  controller.ResetForNewNavigation(GURL("http://example.com"));
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(controller.ShouldShowForNavigation().has_value());
  ASSERT_TRUE(controller.ShouldShowForNavigation().value());
  ASSERT_FALSE(controller.WantsExpandedUi());
}

TEST_F(PriceTrackingPageActionControllerUnittest,
       IconInteractionStateMetrics_Expanded) {
  shopping_service_->SetIsShoppingListEligible(true);
  shopping_service_->SetIsSubscribedCallbackValue(false);
  SetupImageFetcherForSimpleImage(/*valid_image=*/true);
  base::RepeatingCallback<void()> callback = notify_host_callback_.Get();
  PriceTrackingPageActionController controller(
      std::move(callback), shopping_service_.get(), image_fetcher_.get(),
      tracker_.get());

  std::optional<ProductInfo> info =
      CreateProductInfo(12345L, GURL("http://example.com/image.png"));
  info->amount_micros = 150000000L;  // $150 in micros
  shopping_service_->SetResponseForGetProductInfoForUrl(info);

  std::string histogram_name = "Commerce.PriceTracking.IconInteractionState";
  base::HistogramTester histogram_tester;

  // First, simulate a click on the expanded chip.
  controller.ResetForNewNavigation(GURL("http://example.com/1"));
  base::RunLoop().RunUntilIdle();
  ASSERT_TRUE(controller.WantsExpandedUi());
  controller.OnIconClicked();

  histogram_tester.ExpectTotalCount(histogram_name, 1);
  histogram_tester.ExpectBucketCount(
      histogram_name, PageActionIconInteractionState::kClickedExpanded, 1);

  // Simulate no click on an expanded chip.
  controller.ResetForNewNavigation(GURL("http://example.com/2"));
  base::RunLoop().RunUntilIdle();
  ASSERT_TRUE(controller.WantsExpandedUi());
  // Leave the page before a click happens.
  controller.ResetForNewNavigation(GURL("http://example.com/3"));
  base::RunLoop().RunUntilIdle();

  histogram_tester.ExpectTotalCount(histogram_name, 2);
  histogram_tester.ExpectBucketCount(
      histogram_name, PageActionIconInteractionState::kNotClickedExpanded, 1);
}

TEST_F(PriceTrackingPageActionControllerUnittest,
       IconInteractionStateMetrics_NotExpanded) {
  shopping_service_->SetIsShoppingListEligible(true);

  // Simulate a product not being tracked.
  shopping_service_->SetIsSubscribedCallbackValue(false);

  SetupImageFetcherForSimpleImage(/*valid_image=*/true);
  base::RepeatingCallback<void()> callback = notify_host_callback_.Get();
  PriceTrackingPageActionController controller(
      std::move(callback), shopping_service_.get(), image_fetcher_.get(),
      tracker_.get());

  shopping_service_->SetResponseForGetProductInfoForUrl(
      CreateProductInfo(12345L, GURL("http://example.com/image.png")));

  std::string histogram_name = "Commerce.PriceTracking.IconInteractionState";
  base::HistogramTester histogram_tester;

  // Click on an unexpanded chip.
  controller.ResetForNewNavigation(GURL("http://example.com/1"));
  base::RunLoop().RunUntilIdle();
  ASSERT_FALSE(controller.WantsExpandedUi());
  controller.OnIconClicked();

  histogram_tester.ExpectTotalCount(histogram_name, 1);
  histogram_tester.ExpectBucketCount(
      histogram_name, PageActionIconInteractionState::kClicked, 1);

  // No click on an unexpanded chip.
  controller.ResetForNewNavigation(GURL("http://example.com/2"));
  base::RunLoop().RunUntilIdle();
  ASSERT_FALSE(controller.WantsExpandedUi());
  // Leave the page before a click happens.
  controller.ResetForNewNavigation(GURL("http://example.com/3"));
  base::RunLoop().RunUntilIdle();

  histogram_tester.ExpectTotalCount(histogram_name, 2);
  histogram_tester.ExpectBucketCount(
      histogram_name, PageActionIconInteractionState::kNotClicked, 1);
}

TEST_F(PriceTrackingPageActionControllerUnittest,
       IconInteractionStateMetrics_AlreadyTracked) {
  shopping_service_->SetIsShoppingListEligible(true);

  // Simulate a product being tracked.
  shopping_service_->SetIsSubscribedCallbackValue(true);

  SetupImageFetcherForSimpleImage(/*valid_image=*/true);
  base::RepeatingCallback<void()> callback = notify_host_callback_.Get();
  PriceTrackingPageActionController controller(
      std::move(callback), shopping_service_.get(), image_fetcher_.get(),
      tracker_.get());

  shopping_service_->SetResponseForGetProductInfoForUrl(
      CreateProductInfo(12345L, GURL("http://example.com/image.png")));

  std::string histogram_name = "Commerce.PriceTracking.IconInteractionState";
  base::HistogramTester histogram_tester;

  // First, simulate a click on the expanded chip.
  controller.ResetForNewNavigation(GURL("http://example.com/1"));
  base::RunLoop().RunUntilIdle();
  controller.OnIconClicked();

  // Simulate no click on the chip.
  controller.ResetForNewNavigation(GURL("http://example.com/2"));
  base::RunLoop().RunUntilIdle();
  // Leave the page before a click happens.
  controller.ResetForNewNavigation(GURL("http://example.com/3"));
  base::RunLoop().RunUntilIdle();

  // Since the product is already tracked, we shouldn't have collected any
  // samples.
  histogram_tester.ExpectTotalCount(histogram_name, 0);
}

}  // namespace commerce