File: bookmark_update_manager_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 (323 lines) | stat: -rw-r--r-- 12,034 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
// 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 <math.h>
#include <map>
#include <memory>
#include <vector>

#include "base/cancelable_callback.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/clock.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/test/test_bookmark_client.h"
#include "components/commerce/core/bookmark_update_manager.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/mock_shopping_service.h"
#include "components/commerce/core/pref_names.h"
#include "components/commerce/core/test_utils.h"
#include "components/power_bookmarks/core/power_bookmark_utils.h"
#include "components/power_bookmarks/core/proto/power_bookmark_meta.pb.h"
#include "components/power_bookmarks/core/proto/shopping_specifics.pb.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace commerce {

class BookmarkUpdateManagerTest : public testing::Test {
 public:
  BookmarkUpdateManagerTest()
      : shopping_service_(std::make_unique<MockShoppingService>()),
        bookmark_model_(bookmarks::TestBookmarkClient::CreateModel()),
        pref_service_(std::make_unique<TestingPrefServiceSimple>()) {
    update_manager_ = std::make_unique<BookmarkUpdateManager>(
        shopping_service_.get(), bookmark_model_.get(), pref_service_.get());
  }
  BookmarkUpdateManagerTest(const BookmarkUpdateManagerTest&) = delete;
  BookmarkUpdateManagerTest operator=(const BookmarkUpdateManagerTest&) =
      delete;
  ~BookmarkUpdateManagerTest() override = default;

  void TestBody() override {}

  void SetUp() override {
    // The update manager should not have an update scheduled by default.
    EXPECT_FALSE(IsUpdateScheduled());

    RegisterPrefs(pref_service_->registry());
    pref_service_->SetTime(kShoppingListBookmarkLastUpdateTime, base::Time());
  }

  void TearDown() override { update_manager_->CancelUpdates(); }

  // Generates a large number of product bookmarks and returns them in a list.
  std::vector<const bookmarks::BookmarkNode*> AddProductBookmarks(
      size_t count) {
    std::vector<const bookmarks::BookmarkNode*> bookmarks;
    for (size_t i = 0; i < count; i++) {
      bookmarks.push_back(AddProductBookmark(
          bookmark_model_.get(), u"Title",
          GURL("http://example.com/" + base::NumberToString(i)), i));
    }
    return bookmarks;
  }

  // Get a list of IDs from the provided list of bookmarks (in the same order).
  std::vector<int64_t> GetIdsFromBookmarks(
      const std::vector<const bookmarks::BookmarkNode*>& bookmarks) {
    std::vector<int64_t> ids;
    for (size_t i = 0; i < bookmarks.size(); i++) {
      ids.push_back(bookmarks[i]->id());
    }
    return ids;
  }

  // Creates and returns a map of bookmark ID to fake product info that can be
  // used with the mock shopping service.
  std::map<int64_t, ProductInfo> BuildOnDemandMapForIds(
      const std::vector<int64_t>& ids) {
    std::map<int64_t, ProductInfo> update_map;

    for (int64_t id : ids) {
      ProductInfo info;
      info.title = "Updated title";
      info.product_cluster_id = id;
      update_map.emplace(id, std::move(info));
    }

    return update_map;
  }

  bool IsUpdateScheduled() {
    return update_manager_->scheduled_task_ != nullptr;
  }

  const base::CancelableOnceClosure* GetScheduledTask() {
    return update_manager_->scheduled_task_.get();
  }

 protected:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  base::test::ScopedFeatureList test_features_;
  std::unique_ptr<MockShoppingService> shopping_service_;
  std::unique_ptr<bookmarks::BookmarkModel> bookmark_model_;
  std::unique_ptr<TestingPrefServiceSimple> pref_service_;

  std::unique_ptr<BookmarkUpdateManager> update_manager_;
};

// Test that an update is scheduled
TEST_F(BookmarkUpdateManagerTest, UpdateScheduled) {
  test_features_.InitWithFeatures(
      {kShoppingList, kCommerceAllowOnDemandBookmarkUpdates}, {});

  pref_service_->SetTime(kShoppingListBookmarkLastUpdateTime,
                         base::Time::Now());

  update_manager_->ScheduleUpdate();

  EXPECT_TRUE(IsUpdateScheduled());
}

// Test that the kill switch blocks updates.
TEST_F(BookmarkUpdateManagerTest, NoUpdateScheduled_KillSwitch) {
  test_features_.InitWithFeatures({kShoppingList},
                                  {kCommerceAllowOnDemandBookmarkUpdates});

  pref_service_->SetTime(kShoppingListBookmarkLastUpdateTime,
                         base::Time::Now());

  update_manager_->ScheduleUpdate();

  EXPECT_FALSE(IsUpdateScheduled());
}

// Ensure that calling ScheduleUpdate multiple times does not affect the
// previously scheduled update.
TEST_F(BookmarkUpdateManagerTest, UpdateNotDoubleScheduled) {
  test_features_.InitWithFeatures(
      {kShoppingList, kCommerceAllowOnDemandBookmarkUpdates}, {});

  pref_service_->SetTime(kShoppingListBookmarkLastUpdateTime,
                         base::Time::Now());

  update_manager_->ScheduleUpdate();

  const base::CancelableOnceClosure* original = GetScheduledTask();

  update_manager_->ScheduleUpdate();

  const base::CancelableOnceClosure* task = GetScheduledTask();

  EXPECT_TRUE(IsUpdateScheduled());
  EXPECT_EQ(original, task);
}

TEST_F(BookmarkUpdateManagerTest, RunScheduledTask) {
  test_features_.InitWithFeatures(
      {kShoppingList, kCommerceAllowOnDemandBookmarkUpdates}, {});

  shopping_service_->SetIsShoppingListEligible(true);

  const int64_t cluster_id = 123L;
  const bookmarks::BookmarkNode* bookmark = AddProductBookmark(
      bookmark_model_.get(), u"Title", GURL("http://example.com"), cluster_id);

  ProductInfo new_info;
  const std::string updated_title = "Updated Title";
  new_info.title = updated_title;
  new_info.product_cluster_id = cluster_id;

  std::map<int64_t, ProductInfo> info_map;
  info_map[bookmark->id()] = new_info;
  shopping_service_->SetResponsesForGetUpdatedProductInfoForBookmarks(
      std::move(info_map));

  shopping_service_->SetGetAllShoppingBookmarksValue({bookmark});

  update_manager_->ScheduleUpdate();
  task_environment_.FastForwardBy(base::Days(1));
  base::RunLoop().RunUntilIdle();

  auto meta = power_bookmarks::GetNodePowerBookmarkMeta(bookmark_model_.get(),
                                                        bookmark);

  EXPECT_EQ(meta->shopping_specifics().title(), updated_title);

  // Ensure the preference for last updated time was also set.
  base::TimeDelta time_since_last =
      base::Time::Now() -
      pref_service_->GetTime(kShoppingListBookmarkLastUpdateTime);
  EXPECT_TRUE(time_since_last < base::Minutes(1));
}

TEST_F(BookmarkUpdateManagerTest, RunScheduledTask_BlockedByFeatureCheck) {
  test_features_.InitWithFeatures(
      {kShoppingList, kCommerceAllowOnDemandBookmarkUpdates}, {});
  shopping_service_->SetIsShoppingListEligible(false);

  const std::string title = "Title";
  const int64_t cluster_id = 123L;
  const bookmarks::BookmarkNode* bookmark = AddProductBookmark(
      bookmark_model_.get(), u"Title", GURL("http://example.com"), cluster_id);

  ProductInfo new_info;
  new_info.title = "Updated Title";
  new_info.product_cluster_id = cluster_id;

  std::map<int64_t, ProductInfo> info_map;
  info_map[bookmark->id()] = new_info;
  shopping_service_->SetResponsesForGetUpdatedProductInfoForBookmarks(
      std::move(info_map));

  shopping_service_->SetGetAllShoppingBookmarksValue({bookmark});

  update_manager_->ScheduleUpdate();
  task_environment_.FastForwardBy(base::Hours(6));
  base::RunLoop().RunUntilIdle();

  auto meta = power_bookmarks::GetNodePowerBookmarkMeta(bookmark_model_.get(),
                                                        bookmark);

  // The bookmark should not have been updated.
  EXPECT_EQ(meta->shopping_specifics().title(), title);

  // Even though the update was blocked, we're still scheduling the noop task.
  // Make sure the previous time was recorded (recorded time is not default).
  EXPECT_TRUE(pref_service_->GetTime(kShoppingListBookmarkLastUpdateTime) !=
              base::Time());
}

// Ensure that updates are handled in batches that the backend can handle. For
// example: if the backend can only handle 30 updates per call, make sure two
// batches are sent.
TEST_F(BookmarkUpdateManagerTest, RunBatchedUpdate) {
  test_features_.InitWithFeatures(
      {kShoppingList, kCommerceAllowOnDemandBookmarkUpdates},
      {});

  shopping_service_->SetIsShoppingListEligible(true);
  ON_CALL(*shopping_service_, GetMaxProductBookmarkUpdatesPerBatch)
      .WillByDefault(testing::Return(30));

  const size_t bookmark_count = 50;
  ASSERT_LT(shopping_service_->GetMaxProductBookmarkUpdatesPerBatch(),
            bookmark_count);

  const size_t expected_update_calls =
      ceil(static_cast<float>(bookmark_count) /
           shopping_service_->GetMaxProductBookmarkUpdatesPerBatch());
  std::vector<const bookmarks::BookmarkNode*> bookmarks =
      AddProductBookmarks(bookmark_count);
  std::vector<int64_t> ids = GetIdsFromBookmarks(bookmarks);
  shopping_service_->SetGetAllShoppingBookmarksValue(bookmarks);
  std::map<int64_t, ProductInfo> info_map = BuildOnDemandMapForIds(ids);

  shopping_service_->SetResponsesForGetUpdatedProductInfoForBookmarks(info_map);

  // With 50 items and a max batch size of 30, we expect two calls to the
  // shopping service api.
  EXPECT_CALL(*shopping_service_,
              GetUpdatedProductInfoForBookmarks(testing::_, testing::_))
      .Times(expected_update_calls);

  update_manager_->ScheduleUpdate();
  base::RunLoop().RunUntilIdle();

  // Ensure the preference for last updated time was also set.
  base::TimeDelta time_since_last =
      base::Time::Now() -
      pref_service_->GetTime(kShoppingListBookmarkLastUpdateTime);
  EXPECT_TRUE(time_since_last < base::Minutes(1));
}

// Same as the above test, but ensures that if a user has more than the max
// allowed updatable bookmarks, we stop updating.
TEST_F(BookmarkUpdateManagerTest, RunBatchedUpdate_OverMaxAllowed) {
  test_features_.InitWithFeatures(
      {kShoppingList, kCommerceAllowOnDemandBookmarkUpdates},
      {});

  shopping_service_->SetIsShoppingListEligible(true);
  ON_CALL(*shopping_service_, GetMaxProductBookmarkUpdatesPerBatch)
      .WillByDefault(testing::Return(10));

  const size_t bookmark_count = kShoppingListBookmarkUpdateBatchMaxParam + 10;
  const size_t expected_update_calls =
      ceil(static_cast<float>(kShoppingListBookmarkUpdateBatchMaxParam) /
           shopping_service_->GetMaxProductBookmarkUpdatesPerBatch());
  const size_t ungated_update_calls =
      ceil(static_cast<float>(bookmark_count) /
           shopping_service_->GetMaxProductBookmarkUpdatesPerBatch());
  ASSERT_GT(ungated_update_calls, expected_update_calls);

  std::vector<const bookmarks::BookmarkNode*> bookmarks =
      AddProductBookmarks(bookmark_count);
  std::vector<int64_t> ids = GetIdsFromBookmarks(bookmarks);
  shopping_service_->SetGetAllShoppingBookmarksValue(bookmarks);
  std::map<int64_t, ProductInfo> info_map = BuildOnDemandMapForIds(ids);

  shopping_service_->SetResponsesForGetUpdatedProductInfoForBookmarks(info_map);

  EXPECT_CALL(*shopping_service_,
              GetUpdatedProductInfoForBookmarks(testing::_, testing::_))
      .Times(expected_update_calls);

  update_manager_->ScheduleUpdate();
  base::RunLoop().RunUntilIdle();

  // Ensure the preference for last updated time was also set.
  base::TimeDelta time_since_last =
      base::Time::Now() -
      pref_service_->GetTime(kShoppingListBookmarkLastUpdateTime);
  EXPECT_TRUE(time_since_last < base::Minutes(1));
}

}  // namespace commerce