File: bookmarks_helper.h

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 (481 lines) | stat: -rw-r--r-- 19,052 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_TEST_INTEGRATION_BOOKMARKS_HELPER_H_
#define CHROME_BROWSER_SYNC_TEST_INTEGRATION_BOOKMARKS_HELPER_H_

#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/strings/utf_string_conversions.h"
#include "base/uuid.h"
#include "chrome/browser/sync/test/integration/await_match_status_change_checker.h"
#include "chrome/browser/sync/test/integration/fake_server_match_status_checker.h"
#include "chrome/browser/sync/test/integration/multi_client_status_change_checker.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "components/bookmarks/browser/base_bookmark_model_observer.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/browser/bookmark_test_util.h"
#include "components/sync/engine/loopback_server/loopback_server_entity.h"
#include "components/sync/engine/nigori/cryptographer.h"
#include "components/sync/test/fake_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/skia/include/core/SkColor.h"
#include "url/gurl.h"

class BookmarkUndoService;
class GURL;

namespace bookmarks {
class BookmarkModel;
}  // namespace bookmarks

namespace gfx {
class Image;
}  // namespace gfx

namespace bookmarks_helper {

// Used to access the bookmark undo service within a particular sync profile.
[[nodiscard]] BookmarkUndoService* GetBookmarkUndoService(int index);

// Used to access the bookmark model within a particular sync profile.
[[nodiscard]] bookmarks::BookmarkModel* GetBookmarkModel(int index);

// Used to access the bookmark bar within a particular sync profile.
[[nodiscard]] const bookmarks::BookmarkNode* GetBookmarkBarNode(int index);

// Used to access the "other bookmarks" node within a particular sync profile.
[[nodiscard]] const bookmarks::BookmarkNode* GetOtherNode(int index);

// Used to access the "Synced Bookmarks" node within a particular sync profile.
[[nodiscard]] const bookmarks::BookmarkNode* GetSyncedBookmarksNode(int index);

// Used to access the "Managed Bookmarks" node for the given profile.
[[nodiscard]] const bookmarks::BookmarkNode* GetManagedNode(int index);

// Adds a URL with address |url| and title |title| to the bookmark bar of
// profile |profile|. Returns a pointer to the node that was added.
const bookmarks::BookmarkNode* AddURL(int profile,
                                      const std::u16string& title,
                                      const GURL& url);

// Adds a URL with address |url| and title |title| to the bookmark bar of
// profile |profile| at position |index|. Returns a pointer to the node that
// was added.
const bookmarks::BookmarkNode* AddURL(int profile,
                                      size_t index,
                                      const std::u16string& title,
                                      const GURL& url);

// Adds a URL with address |url| and title |title| under the node |parent| of
// profile |profile| at position |index|. Returns a pointer to the node that
// was added.
const bookmarks::BookmarkNode* AddURL(int profile,
                                      const bookmarks::BookmarkNode* parent,
                                      size_t index,
                                      const std::u16string& title,
                                      const GURL& url);

// Adds a folder named |title| to the bookmark bar of profile |profile|.
// Returns a pointer to the folder that was added.
const bookmarks::BookmarkNode* AddFolder(int profile,
                                         const std::u16string& title);

// Adds a folder named |title| to the bookmark bar of profile |profile| at
// position |index|. Returns a pointer to the folder that was added.
const bookmarks::BookmarkNode* AddFolder(int profile,
                                         size_t index,
                                         const std::u16string& title);

// Adds a folder named |title| to the node |parent| in the bookmark model of
// profile |profile| at position |index|. Returns a pointer to the node that
// was added.
const bookmarks::BookmarkNode* AddFolder(int profile,
                                         const bookmarks::BookmarkNode* parent,
                                         size_t index,
                                         const std::u16string& title);

// Changes the title of the node |node| in the bookmark model of profile
// |profile| to |new_title|.
void SetTitle(int profile,
              const bookmarks::BookmarkNode* node,
              const std::u16string& new_title);

// The source of the favicon.
enum FaviconSource { FROM_UI, FROM_SYNC };

// Sets the |icon_url| and |image| data for the favicon for |node| in the
// bookmark model for |profile|. Waits until the favicon is loaded, but does so
// comparing the icon URL and hence is unreliable if the same icon URL has been
// used before.
void SetFavicon(int profile,
                const bookmarks::BookmarkNode* node,
                const GURL& icon_url,
                const gfx::Image& image,
                FaviconSource source);

// Expires the favicon for |node| in the bookmark model for |profile|.
void ExpireFavicon(int profile, const bookmarks::BookmarkNode* node);

// Checks whether the favicon at |icon_url| for |profile| is expired;
void CheckFaviconExpired(int profile, const GURL& icon_url);

// Deletes the favicon mappings for |node| in the bookmark model for |profile|.
void DeleteFaviconMappings(int profile,
                           const bookmarks::BookmarkNode* node,
                           FaviconSource favicon_source);

// Checks whether |page_url| for |profile| has no favicon mappings.
void CheckHasNoFavicon(int profile, const GURL& page_url);

// Changes the url of the node |node| in the bookmark model of profile
// |profile| to |new_url|. Returns a pointer to the node with the changed url.
const bookmarks::BookmarkNode* SetURL(int profile,
                                      const bookmarks::BookmarkNode* node,
                                      const GURL& new_url);

// Moves the node |node| in the bookmark model of profile |profile| so it ends
// up under the node |new_parent| at position |index|.
void Move(int profile,
          const bookmarks::BookmarkNode* node,
          const bookmarks::BookmarkNode* new_parent,
          size_t index);

// Removes the node in the bookmark model of profile |profile| under the node
// |parent| at position |index|.
void Remove(int profile, const bookmarks::BookmarkNode* parent, size_t index);

// Sorts the children of the node |parent| in the bookmark model of profile
// |profile|.
void SortChildren(int profile, const bookmarks::BookmarkNode* parent);

// Reverses the order of the children of the node |parent| in the bookmark
// model of profile |profile|.
void ReverseChildOrder(int profile, const bookmarks::BookmarkNode* parent);

// Checks if the bookmark models of |profile_a| and |profile_b| match each
// other. Returns true if they match.
[[nodiscard]] bool ModelsMatch(int profile_a, int profile_b);

// Checks if the bookmark models of all sync profiles match each other. Does
// not compare them with the verifier bookmark model. Returns true if they
// match.
[[nodiscard]] bool AllModelsMatch();

// Checks if the bookmark model of profile |profile| contains any instances of
// two bookmarks with the same URL under the same parent folder. Returns true
// if even one instance is found.
bool ContainsDuplicateBookmarks(int profile);

// Returns whether a node exists with the specified url.
bool HasNodeWithURL(int profile, const GURL& url);

// Gets the node in the bookmark model of profile |profile| that has the url
// |url|. Note: Only one instance of |url| is assumed to be present.
[[nodiscard]] const bookmarks::BookmarkNode* GetUniqueNodeByURL(
    int profile,
    const GURL& url);

// Returns the number of bookmarks in bookmark model of profile |profile|.
[[nodiscard]] size_t CountAllBookmarks(int profile);

// Returns the number of bookmarks in bookmark model of profile |profile|
// whose titles match the string |title|.
[[nodiscard]] size_t CountBookmarksWithTitlesMatching(
    int profile,
    const std::u16string& title);

// Returns the number of bookmarks in bookmark model of profile |profile|
// whose URLs match the |url|.
[[nodiscard]] size_t CountBookmarksWithUrlsMatching(int profile,
                                                    const GURL& url);

// Returns the number of bookmark folders in the bookmark model of profile
// |profile| whose titles contain the query string |title|.
[[nodiscard]] size_t CountFoldersWithTitlesMatching(
    int profile,
    const std::u16string& title);

// Returns whether there exists a BookmarkNode in the bookmark model of
// profile |profile| whose UUID matches `uuid`.
bool ContainsBookmarkNodeWithUuid(int profile, const base::Uuid& uuid);

// Creates a favicon of |color| with image reps of the platform's supported
// scale factors (eg MacOS) in addition to 1x.
gfx::Image CreateFavicon(SkColor color);

// Creates a 1x only favicon from the PNG file at |path|.
gfx::Image Create1xFaviconFromPNGFile(const std::string& path);

// Returns a URL identifiable by |i|.
std::string IndexedURL(size_t i);

// Returns a URL title identifiable by |i|.
std::u16string IndexedURLTitle(size_t i);

// Returns a folder name identifiable by |i|.
std::u16string IndexedFolderName(size_t i);

// Returns a subfolder name identifiable by |i|.
std::u16string IndexedSubfolderName(size_t i);

// Returns a subsubfolder name identifiable by |i|.
std::u16string IndexedSubsubfolderName(size_t i);

// Creates a server-side entity representing a bookmark with the given title and
// URL.
std::unique_ptr<syncer::LoopbackServerEntity> CreateBookmarkServerEntity(
    const std::u16string& title,
    const GURL& url);

// Helper class that reacts to any BookmarkModelObserver event by running a
// callback provided in the constructor.
class AnyBookmarkChangeObserver : public bookmarks::BaseBookmarkModelObserver {
 public:
  explicit AnyBookmarkChangeObserver(const base::RepeatingClosure& cb);
  ~AnyBookmarkChangeObserver() override;

  AnyBookmarkChangeObserver(const AnyBookmarkChangeObserver&) = delete;
  AnyBookmarkChangeObserver& operator=(const AnyBookmarkChangeObserver&) =
      delete;

  // BaseBookmarkModelObserver overrides.
  void BookmarkModelChanged() override;
  void BookmarkNodeFaviconChanged(const bookmarks::BookmarkNode* node) override;

 private:
  const base::RepeatingClosure cb_;
};

// Base class used for checkers that verify the state of an arbitrary number
// of BookmarkModel instances.
class BookmarkModelStatusChangeChecker : public StatusChangeChecker {
 public:
  BookmarkModelStatusChangeChecker();
  ~BookmarkModelStatusChangeChecker() override;

  BookmarkModelStatusChangeChecker(const BookmarkModelStatusChangeChecker&) =
      delete;
  BookmarkModelStatusChangeChecker& operator=(
      const BookmarkModelStatusChangeChecker&) = delete;

 protected:
  void Observe(bookmarks::BookmarkModel* model);

  // StatusChangeChecker override.
  void CheckExitCondition() override;

 private:
  // Equivalent of CheckExitCondition() that instead posts a task in the current
  // task runner.
  void PostCheckExitCondition();

  SEQUENCE_CHECKER(sequence_checker_);

  std::vector<std::pair<bookmarks::BookmarkModel*,
                        std::unique_ptr<AnyBookmarkChangeObserver>>>
      observers_;

  bool pending_check_exit_condition_ = false;
  base::WeakPtrFactory<BookmarkModelStatusChangeChecker> weak_ptr_factory_{
      this};
};

// Checker used to block until bookmarks match on all clients.
class BookmarksMatchChecker : public BookmarkModelStatusChangeChecker {
 public:
  BookmarksMatchChecker();

  // StatusChangeChecker implementation.
  bool IsExitConditionSatisfied(std::ostream* os) override;
  bool Wait() override;
};

// Base class used for checkers that verify the state of a single BookmarkModel
// instance.
class SingleBookmarkModelStatusChangeChecker
    : public BookmarkModelStatusChangeChecker {
 public:
  explicit SingleBookmarkModelStatusChangeChecker(int profile_index);
  ~SingleBookmarkModelStatusChangeChecker() override;

  SingleBookmarkModelStatusChangeChecker(
      const SingleBookmarkModelStatusChangeChecker&) = delete;
  SingleBookmarkModelStatusChangeChecker& operator=(
      const SingleBookmarkModelStatusChangeChecker&) = delete;

 protected:
  int profile_index() const;
  bookmarks::BookmarkModel* bookmark_model() const;

 private:
  const int profile_index_;
  const raw_ptr<bookmarks::BookmarkModel> bookmark_model_;
};

// Generic status change checker that waits until a predicate as defined by
// a gMock matches becomes true.
class SingleBookmarksModelMatcherChecker
    : public SingleBookmarkModelStatusChangeChecker {
 public:
  using Matcher = testing::Matcher<std::vector<const bookmarks::BookmarkNode*>>;

  SingleBookmarksModelMatcherChecker(int profile_index, const Matcher& matcher);
  ~SingleBookmarksModelMatcherChecker() override;

  // StatusChangeChecker implementation.
  bool IsExitConditionSatisfied(std::ostream* os) final;

 private:
  const Matcher matcher_;
};

// Checker used to block until the actual number of bookmarks with the given
// title match the expected count.
class BookmarksTitleChecker : public SingleBookmarkModelStatusChangeChecker {
 public:
  BookmarksTitleChecker(int profile_index,
                        const std::u16string& title,
                        int expected_count);

  // StatusChangeChecker implementation.
  bool IsExitConditionSatisfied(std::ostream* os) override;

 private:
  const int profile_index_;
  const std::u16string title_;
  const int expected_count_;
};

// Checker used to wait until the favicon of a bookmark has been loaded. It
// doesn't itself trigger the load of the favicon.
class BookmarkFaviconLoadedChecker
    : public SingleBookmarkModelStatusChangeChecker {
 public:
  // There must be exactly one bookmark for |page_url| in the BookmarkModel in
  // |profile_index|.
  BookmarkFaviconLoadedChecker(int profile_index, const GURL& page_url);

  // StatusChangeChecker implementation.
  bool IsExitConditionSatisfied(std::ostream* os) override;

 private:
  const raw_ptr<const bookmarks::BookmarkNode> bookmark_node_;
};

// Checker used to block until the bookmarks on the server match a given set of
// expected bookmarks. The |title| is comapred to both legacy and full titles.
class ServerBookmarksEqualityChecker
    : public fake_server::FakeServerMatchStatusChecker {
 public:
  struct ExpectedBookmark {
    // Used to check both legacy and full titles in specifics.
    std::u16string title;
    GURL url;
  };

  // If a |cryptographer| is provided (i.e. is not nullptr), it is assumed that
  // the server-side data should be encrypted, and the provided cryptographer
  // will be used to decrypt the data prior to checking for equality.
  // |fake_server| must not be nullptr and must outlive this object.
  ServerBookmarksEqualityChecker(
      std::vector<ExpectedBookmark> expected_bookmarks,
      syncer::Cryptographer* cryptographer);

  bool IsExitConditionSatisfied(std::ostream* os) override;

  ServerBookmarksEqualityChecker(const ServerBookmarksEqualityChecker&) =
      delete;
  ServerBookmarksEqualityChecker& operator=(
      const ServerBookmarksEqualityChecker&) = delete;

  ~ServerBookmarksEqualityChecker() override;

 private:
  raw_ptr<syncer::Cryptographer> cryptographer_;
  const std::vector<ExpectedBookmark> expected_bookmarks_;
};

// Checker used to block until the actual number of bookmarks with the given url
// match the expected count.
class BookmarksUrlChecker : public SingleBookmarkModelStatusChangeChecker {
 public:
  BookmarksUrlChecker(int profile, const GURL& url, int expected_count);

  // StatusChangeChecker implementation.
  bool IsExitConditionSatisfied(std::ostream* os) override;

 private:
  const GURL url_;
  const int expected_count_;
};

// Checker used to block until there exists a bookmark with the given UUID.
class BookmarksUuidChecker : public SingleBookmarksModelMatcherChecker {
 public:
  BookmarksUuidChecker(int profile, const base::Uuid& uuid);
  ~BookmarksUuidChecker() override;
};

// Waits until the fake server has the similar structure of bookmarks like the
// bookmark model. The checker verifies that all nodes have the same UUID,
// title, URL, parent and order. It doesn't check favicons and any other fields.
// Note that this class is not enough to verify test's result as it only waits
// for the state when the bookmark model has the same structure on the server.
// It doesn't check their content and the expected number of bookmarks. The fake
// server must have entities with unique UUIDs.
class BookmarkModelMatchesFakeServerChecker
    : public SingleClientStatusChangeChecker {
 public:
  BookmarkModelMatchesFakeServerChecker(int profile,
                                        syncer::SyncServiceImpl* service,
                                        fake_server::FakeServer* fake_server);

  bool IsExitConditionSatisfied(std::ostream* os) override;

 private:
  std::map<std::string, sync_pb::SyncEntity>
  GetServerPermanentBookmarksGroupedBySyncId() const;

  // Fills in |server_bookmarks_by_uuid| with all non-permanent entities stored
  // on the server. All entities must have unique UUID in specifics. Returns
  // false if there are duplicate entities on the server.
  bool GetServerBookmarksByUniqueUuid(std::map<base::Uuid, sync_pb::SyncEntity>*
                                          server_bookmarks_by_uuid) const;

  // Check that a permanent parent node of given |node| is the same as for the
  // matching |server_entity|.
  bool CheckPermanentParentNode(const bookmarks::BookmarkNode* node,
                                const sync_pb::SyncEntity& server_entity,
                                std::ostream* os) const;

  // Check that a regular parent node of given |node| matches to the parent of
  // matching server entity.
  bool CheckParentNode(
      const bookmarks::BookmarkNode* node,
      const std::map<base::Uuid, sync_pb::SyncEntity>& server_bookmarks_by_uuid,
      std::ostream* os) const;

  // Return ordered UUIDs of server entities grouped by their parents.
  std::map<std::string, std::vector<base::Uuid>>
  GetServerUuidsGroupedByParentSyncId(
      const std::map<base::Uuid, sync_pb::SyncEntity>& server_bookmarks_by_uuid)
      const;

  const raw_ptr<fake_server::FakeServer> fake_server_;
  const int profile_index_;
};

}  // namespace bookmarks_helper

#endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_BOOKMARKS_HELPER_H_