File: bookmark_utils_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 (1109 lines) | stat: -rw-r--r-- 49,592 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/bookmarks/browser/bookmark_utils.h"

#include <stddef.h>

#include <array>
#include <memory>
#include <utility>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "components/bookmarks/browser/base_bookmark_model_observer.h"
#include "components/bookmarks/browser/bookmark_client.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_model_observer.h"
#include "components/bookmarks/browser/bookmark_node_data.h"
#include "components/bookmarks/common/bookmark_metrics.h"
#include "components/bookmarks/test/test_bookmark_client.h"
#include "components/signin/public/base/signin_switches.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"

namespace bookmarks {
namespace {

using base::ASCIIToUTF16;
using std::string;
using testing::ElementsAre;
using testing::UnorderedElementsAre;

class BookmarkUtilsTest : public testing::Test,
                          public BaseBookmarkModelObserver {
 public:
  BookmarkUtilsTest()
      : task_environment_(base::test::TaskEnvironment::MainThreadType::UI) {}

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

  ~BookmarkUtilsTest() override {}

// Copy and paste is not yet supported on iOS. http://crbug.com/228147
#if !BUILDFLAG(IS_IOS)
  void TearDown() override {
    ui::Clipboard::DestroyClipboardForCurrentThread();
  }
#endif  // !BUILDFLAG(IS_IOS)

  // Certain user actions require multiple changes to the bookmark model,
  // however these modifications need to be atomic for the undo framework. The
  // BaseBookmarkModelObserver is used to inform the boundaries of the user
  // action. For example, when multiple bookmarks are cut to the clipboard we
  // expect one call each to GroupedBookmarkChangesBeginning/Ended.
  void ExpectGroupedChangeCount(int expected_beginning_count,
                                int expected_ended_count) {
    // The undo framework is not used under Android.  Thus the group change
    // events will not be fired and so should not be tested for Android.
#if !BUILDFLAG(IS_ANDROID)
    EXPECT_EQ(grouped_changes_beginning_count_, expected_beginning_count);
    EXPECT_EQ(grouped_changes_ended_count_, expected_ended_count);
#endif
  }

  base::HistogramTester* histogram() { return &histogram_; }

 private:
  // BaseBookmarkModelObserver:
  void BookmarkModelChanged() override {}

  void GroupedBookmarkChangesBeginning() override {
    ++grouped_changes_beginning_count_;
  }

  void GroupedBookmarkChangesEnded() override {
    ++grouped_changes_ended_count_;
  }

  // Some of these tests exercise account bookmarks.
  base::test::ScopedFeatureList features_override_{
      switches::kSyncEnableBookmarksInTransportMode};

  // Clipboard requires a full TaskEnvironment.
  base::test::TaskEnvironment task_environment_;

  int grouped_changes_beginning_count_{0};
  int grouped_changes_ended_count_{0};
  base::HistogramTester histogram_;
};

// A bookmark client that suggests a save location for new nodes.
class SuggestFolderClient : public TestBookmarkClient {
 public:
  SuggestFolderClient() = default;
  SuggestFolderClient(const SuggestFolderClient&) = delete;
  SuggestFolderClient& operator=(const SuggestFolderClient&) = delete;
  ~SuggestFolderClient() override = default;

  const BookmarkNode* GetSuggestedSaveLocation(const GURL& url) override {
    return suggested_save_location_.get();
  }

  void SetSuggestedSaveLocation(const BookmarkNode* node) {
    suggested_save_location_ = node;
  }

 private:
  raw_ptr<const BookmarkNode> suggested_save_location_;
};

TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesWordPhraseQuery) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  const BookmarkNode* node1 = model->AddURL(model->other_node(), 0, u"foo bar",
                                            GURL("http://www.google.com"));
  const BookmarkNode* node2 = model->AddURL(model->other_node(), 0, u"baz buz",
                                            GURL("http://www.cnn.com"));
  const BookmarkNode* folder1 =
      model->AddFolder(model->other_node(), 0, u"foo");
  QueryFields query;
  query.word_phrase_query = std::make_unique<std::u16string>();
  // No nodes are returned for empty string.
  *query.word_phrase_query = u"";
  EXPECT_TRUE(GetBookmarksMatchingProperties(model.get(), query, 100).empty());

  // No nodes are returned for space-only string.
  *query.word_phrase_query = u"   ";
  EXPECT_TRUE(GetBookmarksMatchingProperties(model.get(), query, 100).empty());

  // Node "foo bar" and folder "foo" are returned in search results.
  *query.word_phrase_query = u"foo";
  EXPECT_THAT(GetBookmarksMatchingProperties(model.get(), query, 100),
              UnorderedElementsAre(folder1, node1));

  // Ensure url matches return in search results.
  *query.word_phrase_query = u"cnn";
  EXPECT_THAT(GetBookmarksMatchingProperties(model.get(), query, 100),
              UnorderedElementsAre(node2));

  // Ensure folder "foo" is not returned in more specific search.
  *query.word_phrase_query = u"foo bar";
  EXPECT_THAT(GetBookmarksMatchingProperties(model.get(), query, 100),
              UnorderedElementsAre(node1));

  // Bookmark Bar and Other Bookmarks are not returned in search results.
  *query.word_phrase_query = u"Bookmark";
  EXPECT_TRUE(GetBookmarksMatchingProperties(model.get(), query, 100).empty());
}

// Check exact matching against a URL query.
TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesUrl) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  const BookmarkNode* node1 = model->AddURL(model->other_node(), 0, u"Google",
                                            GURL("https://www.google.com/"));
  model->AddURL(model->other_node(), 0, u"Google Calendar",
                GURL("https://www.google.com/calendar"));

  model->AddFolder(model->other_node(), 0, u"Folder");

  QueryFields query;
  query.url = std::make_unique<std::u16string>();
  *query.url = u"https://www.google.com/";
  EXPECT_THAT(GetBookmarksMatchingProperties(model.get(), query, 100),
              UnorderedElementsAre(node1));

  *query.url = u"calendar";
  EXPECT_TRUE(GetBookmarksMatchingProperties(model.get(), query, 100).empty());

  // Empty URL should not match folders.
  *query.url = u"";
  EXPECT_TRUE(GetBookmarksMatchingProperties(model.get(), query, 100).empty());
}

// Check exact matching against a title query.
TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesTitle) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  const BookmarkNode* node1 = model->AddURL(model->other_node(), 0, u"Google",
                                            GURL("https://www.google.com/"));
  model->AddURL(model->other_node(), 0, u"Google Calendar",
                GURL("https://www.google.com/calendar"));

  const BookmarkNode* folder1 =
      model->AddFolder(model->other_node(), 0, u"Folder");

  QueryFields query;
  query.title = std::make_unique<std::u16string>();
  *query.title = u"Google";
  EXPECT_THAT(GetBookmarksMatchingProperties(model.get(), query, 100),
              UnorderedElementsAre(node1));

  *query.title = u"Calendar";
  EXPECT_TRUE(GetBookmarksMatchingProperties(model.get(), query, 100).empty());

  // Title should match folders.
  *query.title = u"Folder";
  EXPECT_THAT(GetBookmarksMatchingProperties(model.get(), query, 100),
              UnorderedElementsAre(folder1));
}

// Check matching against a query with multiple predicates.
TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesConjunction) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  const BookmarkNode* node1 = model->AddURL(model->other_node(), 0, u"Google",
                                            GURL("https://www.google.com/"));
  model->AddURL(model->other_node(), 0, u"Google Calendar",
                GURL("https://www.google.com/calendar"));

  model->AddFolder(model->other_node(), 0, u"Folder");

  QueryFields query;

  // Test all fields matching.
  query.word_phrase_query = std::make_unique<std::u16string>(u"www");
  query.url = std::make_unique<std::u16string>(u"https://www.google.com/");
  query.title = std::make_unique<std::u16string>(u"Google");
  EXPECT_THAT(GetBookmarksMatchingProperties(model.get(), query, 100),
              UnorderedElementsAre(node1));

  auto fields = std::to_array<std::unique_ptr<std::u16string>*>({
      &query.word_phrase_query,
      &query.url,
      &query.title,
  });

  // Test two fields matching.
  for (size_t i = 0; i < std::size(fields); i++) {
    std::unique_ptr<std::u16string> original_value(fields[i]->release());
    EXPECT_THAT(GetBookmarksMatchingProperties(model.get(), query, 100),
                UnorderedElementsAre(node1));
    *fields[i] = std::move(original_value);
  }

  // Test two fields matching with one non-matching field.
  for (size_t i = 0; i < std::size(fields); i++) {
    std::unique_ptr<std::u16string> original_value(fields[i]->release());
    *fields[i] = std::make_unique<std::u16string>(u"fjdkslafjkldsa");
    EXPECT_TRUE(
        GetBookmarksMatchingProperties(model.get(), query, 100).empty());
    *fields[i] = std::move(original_value);
  }
}

// Ensures the BookmarkClient has the power to suggest the parent for new nodes.
TEST_F(BookmarkUtilsTest, GetParentForNewNodes_ClientOverride) {
  std::unique_ptr<SuggestFolderClient> client =
      std::make_unique<SuggestFolderClient>();
  SuggestFolderClient* client_ptr = client.get();
  std::unique_ptr<BookmarkModel> model(
      TestBookmarkClient::CreateModelWithClient(std::move(client)));

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_EQ(model->mobile_node(), GetParentForNewNodes(model.get(), GURL()));
#else
  EXPECT_EQ(model->other_node(), GetParentForNewNodes(model.get(), GURL()));
  model->CreateAccountPermanentFolders();
  EXPECT_EQ(model->account_other_node(),
            GetParentForNewNodes(model.get(), GURL()));
#endif

  const BookmarkNode* folder_to_suggest =
      model->AddFolder(model->bookmark_bar_node(), 0, u"Suggested");
  const BookmarkNode* folder1 =
      model->AddFolder(model->bookmark_bar_node(), 1, u"Folder 1");
  EXPECT_EQ(folder1, GetParentForNewNodes(model.get(), GURL()));

  client_ptr->SetSuggestedSaveLocation(folder_to_suggest);
  EXPECT_EQ(folder_to_suggest, GetParentForNewNodes(model.get(), GURL()));
  client_ptr = nullptr;
}

// Verifies that meta info is copied when nodes are cloned.
TEST_F(BookmarkUtilsTest, CloneMetaInfo) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  // Add a node containing meta info.
  const BookmarkNode* node = model->AddURL(model->other_node(), 0, u"foo bar",
                                           GURL("http://www.google.com"));
  model->SetNodeMetaInfo(node, "somekey", "somevalue");
  model->SetNodeMetaInfo(node, "someotherkey", "someothervalue");

  // Clone node to a different folder.
  const BookmarkNode* folder =
      model->AddFolder(model->bookmark_bar_node(), 0, u"Folder");
  std::vector<BookmarkNodeData::Element> elements;
  BookmarkNodeData::Element node_data(node);
  elements.push_back(node_data);
  ASSERT_EQ(0u, folder->children().size());
  CloneBookmarkNode(model.get(), elements, folder, 0, false);
  ASSERT_EQ(1u, folder->children().size());

  // Verify that the cloned node contains the same meta info.
  const BookmarkNode* clone = folder->children().front().get();
  ASSERT_TRUE(clone->GetMetaInfoMap());
  EXPECT_EQ(2u, clone->GetMetaInfoMap()->size());
  std::string value;
  EXPECT_TRUE(clone->GetMetaInfo("somekey", &value));
  EXPECT_EQ("somevalue", value);
  EXPECT_TRUE(clone->GetMetaInfo("someotherkey", &value));
  EXPECT_EQ("someothervalue", value);
  histogram()->ExpectTotalCount("Bookmarks.Clone.NumCloned", 1);
  histogram()->ExpectBucketCount("Bookmarks.Clone.NumCloned", 1, 1);
}

TEST_F(BookmarkUtilsTest, RemoveAllBookmarks) {
  // Load a model with an managed node that is not editable.
  auto client = std::make_unique<TestBookmarkClient>();
  BookmarkNode* managed_node = client->EnableManagedNode();

  std::unique_ptr<BookmarkModel> model(
      TestBookmarkClient::CreateModelWithClient(std::move(client)));
  ASSERT_TRUE(model->bookmark_bar_node()->children().empty());
  ASSERT_TRUE(model->other_node()->children().empty());
  ASSERT_TRUE(model->mobile_node()->children().empty());
  ASSERT_TRUE(managed_node->children().empty());

  const std::u16string title = u"Title";
  const GURL url("http://google.com");
  model->AddURL(model->bookmark_bar_node(), 0, title, url);
  model->AddURL(model->other_node(), 0, title, url);
  model->AddURL(model->mobile_node(), 0, title, url);
  model->AddURL(managed_node, 0, title, url);

  std::vector<raw_ptr<const BookmarkNode, VectorExperimental>> nodes =
      model->GetNodesByURL(url);
  ASSERT_EQ(4u, nodes.size());

  RemoveAllBookmarks(model.get(), url, FROM_HERE);

  nodes = model->GetNodesByURL(url);
  ASSERT_EQ(1u, nodes.size());
  EXPECT_TRUE(model->bookmark_bar_node()->children().empty());
  EXPECT_TRUE(model->other_node()->children().empty());
  EXPECT_TRUE(model->mobile_node()->children().empty());
  EXPECT_EQ(1u, managed_node->children().size());
}

#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithOnlyLocalBookmarks_PermanentNodesOrderUnaffectedByDisplay) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());

  // Note that because `local_other_bookmark` is a child of a permanent node its
  // use in `GetMostRecentlyUsedFoldersForDisplay()` will not cause
  // `other_node()` to be displayed before more recently modified folders.
  const BookmarkNode* const local_other_bookmark = model->AddURL(
      model->other_node(), 0, u"Title", GURL("http://google.com"));

  model->SetDateFolderModified(model->other_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(1));
  model->SetDateFolderModified(model->bookmark_bar_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(2));

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(model.get(),
                                                      local_other_bookmark);

  // Permanent nodes display in a fixed order even if a bookmark in
  // `other_node()` is currently displayed.
  EXPECT_TRUE(mru_bookmarks.account_nodes.empty());
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(model->bookmark_bar_node(), model->other_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithOnlyLocalBookmarks_NonPermanentNodesOrderAffectedByDisplay) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());

  // Add two folders.
  const BookmarkNode* const folder1 =
      model->AddFolder(model->other_node(), 0, u"Folder1");
  const BookmarkNode* const folder2 =
      model->AddFolder(model->other_node(), 0, u"Folder2");

  // Add a new bookmark to `folder1`.
  const BookmarkNode* const bookmark =
      model->AddURL(folder1, 0, u"Title", GURL("http://google.com"));

  // Set `folder2` to most recent.
  model->SetDateFolderModified(folder1,
                               base::Time::FromMillisecondsSinceUnixEpoch(1));
  model->SetDateFolderModified(folder2,
                               base::Time::FromMillisecondsSinceUnixEpoch(2));

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(model.get(), bookmark);

  // `folder1` as a parent to `bookmark` displays first even though not most
  // recent.
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(folder1, folder2, model->bookmark_bar_node(),
                          model->other_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithOnlyLocalBookmarks_DateFolderModifiedChangesOrder) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  const BookmarkNode* const local_other_bookmark = model->AddURL(
      model->other_node(), 0, u"Title", GURL("http://google.com"));

  // Add two folders.
  const BookmarkNode* const folder1 =
      model->AddFolder(model->other_node(), 0, u"Folder1");
  const BookmarkNode* const folder2 =
      model->AddFolder(model->other_node(), 0, u"Folder2");

  // `folder2` is set to most recent.
  model->SetDateFolderModified(folder1,
                               base::Time::FromMillisecondsSinceUnixEpoch(1));
  model->SetDateFolderModified(folder2,
                               base::Time::FromMillisecondsSinceUnixEpoch(2));

  bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(model.get(),
                                                      local_other_bookmark);
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(folder2, folder1, model->bookmark_bar_node(),
                          model->other_node()));

  // `folder1` is set to most recent.
  model->SetDateFolderModified(folder1,
                               base::Time::FromMillisecondsSinceUnixEpoch(3));

  mru_bookmarks = bookmarks::GetMostRecentlyUsedFoldersForDisplay(
      model.get(), local_other_bookmark);
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(folder1, folder2, model->bookmark_bar_node(),
                          model->other_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithOnlyLocalBookmarks_PermanentFoldersAlwaysShownWithCustomFolders) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  const BookmarkNode* const local_other_bookmark = model->AddURL(
      model->other_node(), 0, u"Title", GURL("http://google.com"));

  // Add more than 5 custom folders. The first one will not be displayed, as
  // only the 5 most recent ones are chosen for display.
  std::vector<const BookmarkNode*> custom_nodes;
  for (int i = 0; i < 6; i++) {
    custom_nodes.push_back(
        model->AddFolder(model->other_node(), 0, u"CustomFolder"));
  }
  CHECK_EQ(6u, custom_nodes.size());

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(model.get(),
                                                      local_other_bookmark);

  // Only 5 custom nodes should be displayed. The permanent ones come last.
  EXPECT_TRUE(mru_bookmarks.account_nodes.empty());
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(custom_nodes[5], custom_nodes[4], custom_nodes[3],
                          custom_nodes[2], custom_nodes[1],
                          model->bookmark_bar_node(), model->other_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithOnlyLocalBookmarks_CustomFolderShownWhenChildNodeDisplayed) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  std::vector<const BookmarkNode*> custom_nodes;

  // Add a first custom folder node and a new bookmark to it.
  custom_nodes.push_back(
      model->AddFolder(model->other_node(), 0, u"CustomFolder"));
  const BookmarkNode* const bookmark =
      model->AddURL(custom_nodes[0], 0, u"Title", GURL("http://google.com"));

  // Add 5 more custom folders now to make sure the first custom folder will
  // not be the most recently modified one.
  for (int i = 0; i < 5; i++) {
    custom_nodes.push_back(
        model->AddFolder(model->other_node(), 0, u"CustomFolder"));
  }
  CHECK_EQ(6u, custom_nodes.size());

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(model.get(), bookmark);

  // Only 5 custom nodes should be displayed. The parent node of the currently
  // displayed bookmark comes first. The permanent nodes come last.
  EXPECT_TRUE(mru_bookmarks.account_nodes.empty());
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(custom_nodes[0], custom_nodes[5], custom_nodes[4],
                          custom_nodes[3], custom_nodes[2],
                          model->bookmark_bar_node(), model->other_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithOnlyLocalBookmarks_CustomFolderShownWhenRecentlyModified) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  const BookmarkNode* const local_other_bookmark = model->AddURL(
      model->other_node(), 0, u"Title", GURL("http://google.com"));

  // Add more than 5 custom folders.
  std::vector<const BookmarkNode*> custom_nodes;
  for (int i = 0; i < 6; i++) {
    custom_nodes.push_back(
        model->AddFolder(model->other_node(), 0, u"CustomFolder"));
  }
  CHECK_EQ(6u, custom_nodes.size());

  // The first folder is set to most recently modified.
  model->SetDateFolderModified(custom_nodes[0], base::Time::Now());
  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(model.get(),
                                                      local_other_bookmark);

  // Only 5 custom nodes should be displayed. The recently modified node comes
  // first. The permanent nodes come last.
  EXPECT_TRUE(mru_bookmarks.account_nodes.empty());
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(custom_nodes[0], custom_nodes[5], custom_nodes[4],
                          custom_nodes[3], custom_nodes[2],
                          model->bookmark_bar_node(), model->other_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithAccountBookmarks_PermanentNodesOrderUnaffectedByDisplay) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();

  // Note that because `bookmark_in_account_other_node` is a child of a
  // permanent node its use in `GetMostRecentlyUsedFoldersForDisplay()` will not
  // cause `account_other_node()` to be displayed before more recently modified
  // folders.
  const BookmarkNode* const bookmark_in_account_other_node = model->AddURL(
      model->account_other_node(), 0, u"Title", GURL("http://google.com"));

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(
          model.get(), bookmark_in_account_other_node);

  // No local nodes display by default.
  EXPECT_TRUE(mru_bookmarks.local_nodes.empty());

  // Permanent nodes are only added to account nodes by default. They display in
  // a fixed order even if a bookmark in `account_other_node()` is currently
  // displayed.
  EXPECT_THAT(mru_bookmarks.account_nodes,
              ElementsAre(model->account_bookmark_bar_node(),
                          model->account_other_node()));
}

TEST_F(BookmarkUtilsTest,
       GetRecentlyUsedFoldersWithAccountBookmarks_LocalPermanentNodesNotShown) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  const BookmarkNode* const bookmark_in_account_other_node = model->AddURL(
      model->account_other_node(), 0, u"Title", GURL("http://google.com"));

  // The most recent folders under account and local are split up as the
  // topmost entries.
  const BookmarkNode* const account_folder =
      model->AddFolder(model->account_other_node(), 0, u"Folder");
  const BookmarkNode* const local_folder =
      model->AddFolder(model->other_node(), 0, u"Folder2");

  model->SetDateFolderModified(account_folder,
                               base::Time::FromMillisecondsSinceUnixEpoch(20));
  // Older than `account_folder` but not filtered out (not permanent node).
  model->SetDateFolderModified(local_folder,
                               base::Time::FromMillisecondsSinceUnixEpoch(10));

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(
          model.get(), bookmark_in_account_other_node);

  // Permanent account folders are included, permanent local folders are not.
  EXPECT_THAT(mru_bookmarks.account_nodes,
              ElementsAre(account_folder, model->account_bookmark_bar_node(),
                          model->account_other_node()));
  EXPECT_THAT(mru_bookmarks.local_nodes, ElementsAre(local_folder));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithAccountBookmarks_LocalPermanentNodeShownWhenChildDisplayed) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  const BookmarkNode* const local_other_bookmark = model->AddURL(
      model->other_node(), 0, u"Title", GURL("http://google.com"));

  // Make sure local permanent nodes are not the most recently modified ones.
  model->SetDateFolderModified(model->other_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(1));
  model->SetDateFolderModified(model->account_other_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(2));

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(model.get(),
                                                      local_other_bookmark);

  // Local permanent node included when its child is being displayed.
  EXPECT_THAT(mru_bookmarks.local_nodes, ElementsAre(model->other_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithAccountBookmarks_LocalPermanentNodeShownWhenRecentlyModified) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  const base::Time account_folders_created_time = base::Time::Now();

  // Add a local node as well so that local permanent nodes are not filtered
  // out.
  model->AddURL(model->bookmark_bar_node(), 0, u"Title",
                GURL("http://google.com"));

  const BookmarkNode* const bookmark_in_account_other_node = model->AddURL(
      model->account_other_node(), 0, u"Title", GURL("http://google.com"));

  // Make sure a local permanent node is the most recently modified one.
  model->SetDateFolderModified(model->account_other_node(),
                               account_folders_created_time + base::Seconds(1));
  model->SetDateFolderModified(model->bookmark_bar_node(),
                               account_folders_created_time + base::Seconds(2));

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(
          model.get(), bookmark_in_account_other_node);

  // Local permanent node included when most recently modified.
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(model->bookmark_bar_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithAccountBookmarks_LocalPermanentNodeDisplayedOnlyOnce) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  const BookmarkNode* const local_bookmark = model->AddURL(
      model->bookmark_bar_node(), 0, u"Title", GURL("http://google.com"));

  // Make sure a local permanent node is the most recently modified one.
  model->SetDateFolderModified(model->account_other_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(1));
  model->SetDateFolderModified(model->bookmark_bar_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(2));

  // Display the bookmark saved to the local permanent node.
  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(model.get(),
                                                      local_bookmark);

  // The local permanent node is only included once, even though there are two
  // conditions fulfilled which would add it to the most recently used folders.
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(model->bookmark_bar_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithAccountBookmarks_LocalPermanentNodesDisplayedLast) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  const base::Time account_folders_created_time = base::Time::Now();

  const BookmarkNode* const bookmark_in_account_other_node = model->AddURL(
      model->account_other_node(), 0, u"Title", GURL("http://google.com"));
  const BookmarkNode* const local_bookmark = model->AddURL(
      model->bookmark_bar_node(), 0, u"Title", GURL("http://google.com"));

  // Add two local folders.
  const BookmarkNode* const folder1 =
      model->AddFolder(model->bookmark_bar_node(), 0, u"Folder");
  const BookmarkNode* const folder2 =
      model->AddFolder(model->bookmark_bar_node(), 0, u"Folder2");

  // Set the account other node as most recently modified.
  model->SetDateFolderModified(folder1, account_folders_created_time);
  model->SetDateFolderModified(model->bookmark_bar_node(),
                               account_folders_created_time + base::Seconds(1));
  model->SetDateFolderModified(folder2,
                               account_folders_created_time + base::Seconds(2));
  model->SetDateFolderModified(model->account_other_node(),
                               account_folders_created_time + base::Seconds(3));

  bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(
          model.get(), bookmark_in_account_other_node);

  // When the permanent node is not the one most recently modified or the parent
  // of a currently displayed bookmark, it is not included in the list at all.
  EXPECT_THAT(mru_bookmarks.local_nodes, ElementsAre(folder2, folder1));

  mru_bookmarks = bookmarks::GetMostRecentlyUsedFoldersForDisplay(
      model.get(), local_bookmark);

  // When the permanent node is the parent of the displayed node, it is added at
  // the end.
  mru_bookmarks = bookmarks::GetMostRecentlyUsedFoldersForDisplay(
      model.get(), local_bookmark);
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(folder2, folder1, model->bookmark_bar_node()));

  // When the permanent node is the most recently modified node, it is added at
  // the end.
  model->SetDateFolderModified(
      model->bookmark_bar_node(),
      account_folders_created_time + base::Seconds(10));
  mru_bookmarks = bookmarks::GetMostRecentlyUsedFoldersForDisplay(
      model.get(), bookmark_in_account_other_node);

  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(folder2, folder1, model->bookmark_bar_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithAccountBookmarks_MultipleLocalPermanentNodesDisplayed) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  const base::Time account_folders_created_time = base::Time::Now();

  const BookmarkNode* const local_bookmark = model->AddURL(
      model->bookmark_bar_node(), 0, u"Title", GURL("http://google.com"));

  // Set the local other node as most recently modified.
  model->SetDateFolderModified(model->bookmark_bar_node(),
                               account_folders_created_time + base::Seconds(1));
  model->SetDateFolderModified(model->account_bookmark_bar_node(),
                               account_folders_created_time + base::Seconds(1));
  model->SetDateFolderModified(model->account_other_node(),
                               account_folders_created_time + base::Seconds(1));
  model->SetDateFolderModified(model->other_node(),
                               account_folders_created_time + base::Seconds(2));

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(model.get(),
                                                      local_bookmark);

  // The local permanent nodes are both shown as one of them is the parent of
  // the displayed node, and one is the most recently modified.
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(model->other_node(), model->bookmark_bar_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithAccountBookmarks_AccountPermanentFoldersAlwaysShownWithCustomFolders) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  const BookmarkNode* const bookmark_in_account_other_node = model->AddURL(
      model->account_other_node(), 0, u"Title", GURL("http://google.com"));

  // Add more than 5 custom folders. The first one will not be displayed, as
  // only the 5 most recent ones are chosen for display.
  std::vector<const BookmarkNode*> custom_nodes;
  for (int i = 0; i < 6; i++) {
    custom_nodes.push_back(model->AddFolder(model->account_other_node(), 0,
                                            u"CustomAccountFolder"));
  }
  CHECK_EQ(6u, custom_nodes.size());

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(
          model.get(), bookmark_in_account_other_node);

  // Only 5 custom nodes should be displayed. The account permanent ones come
  // last. Local permanent nodes should not be included.
  EXPECT_TRUE(mru_bookmarks.local_nodes.empty());
  EXPECT_THAT(mru_bookmarks.account_nodes,
              ElementsAre(custom_nodes[5], custom_nodes[4], custom_nodes[3],
                          custom_nodes[2], custom_nodes[1],
                          model->account_bookmark_bar_node(),
                          model->account_other_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithAccountBookmarks_CustomFolderShownWhenChildNodeDisplayed) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  std::vector<const BookmarkNode*> custom_nodes;

  // Add a first custom folder node and a new bookmark to it.
  custom_nodes.push_back(
      model->AddFolder(model->account_other_node(), 0, u"CustomAccountFolder"));
  const BookmarkNode* const bookmark =
      model->AddURL(custom_nodes[0], 0, u"Title", GURL("http://google.com"));

  // Add 5 more custom folders now to make sure the first custom folder will
  // not be the most recently modified one.
  for (int i = 0; i < 5; i++) {
    custom_nodes.push_back(model->AddFolder(model->account_other_node(), 0,
                                            u"CustomAccountFolder"));
  }
  CHECK_EQ(6u, custom_nodes.size());

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(model.get(), bookmark);

  // Only 5 custom nodes should be displayed. The parent node of the currently
  // displayed bookmark comes first. The account permanent nodes come last.
  // Local permanent nodes should not be included.
  EXPECT_TRUE(mru_bookmarks.local_nodes.empty());
  EXPECT_THAT(mru_bookmarks.account_nodes,
              ElementsAre(custom_nodes[0], custom_nodes[5], custom_nodes[4],
                          custom_nodes[3], custom_nodes[2],
                          model->account_bookmark_bar_node(),
                          model->account_other_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithAccountBookmarks_CustomFolderShownWhenRecentlyModified) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  const BookmarkNode* const bookmark_in_account_other_node = model->AddURL(
      model->account_other_node(), 0, u"Title", GURL("http://google.com"));

  // Add more than 5 custom folders.
  std::vector<const BookmarkNode*> custom_nodes;
  for (int i = 0; i < 6; i++) {
    custom_nodes.push_back(model->AddFolder(model->account_other_node(), 0,
                                            u"CustomAccountFolder"));
  }
  CHECK_EQ(6u, custom_nodes.size());

  // The first folder is set to most recently modified.
  model->SetDateFolderModified(custom_nodes[0], base::Time::Now());
  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(
          model.get(), bookmark_in_account_other_node);

  // Only 5 custom nodes should be displayed. The recently modified node comes
  // first. The account permanent nodes come last. Local permanent nodes should
  // not be included.
  EXPECT_TRUE(mru_bookmarks.local_nodes.empty());
  EXPECT_THAT(mru_bookmarks.account_nodes,
              ElementsAre(custom_nodes[0], custom_nodes[5], custom_nodes[4],
                          custom_nodes[3], custom_nodes[2],
                          model->account_bookmark_bar_node(),
                          model->account_other_node()));
}

TEST_F(
    BookmarkUtilsTest,
    GetRecentlyUsedFoldersWithAccountBookmarks_CustomFolderMaximumDisplayedIndependentOfStorage) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  const BookmarkNode* const bookmark_in_account_other_node = model->AddURL(
      model->account_other_node(), 0, u"Title", GURL("http://google.com"));

  // Add 3 custom folders to each account and local permanent nodes.
  std::vector<const BookmarkNode*> custom_nodes;
  for (int i = 0; i < 3; i++) {
    custom_nodes.push_back(
        model->AddFolder(model->other_node(), 0, u"CustomLocalFolder"));
    custom_nodes.push_back(model->AddFolder(model->account_other_node(), 0,
                                            u"CustomAccountFolder"));
  }
  CHECK_EQ(6u, custom_nodes.size());

  const bookmarks::BookmarkNodesSplitByAccountAndLocal mru_bookmarks =
      bookmarks::GetMostRecentlyUsedFoldersForDisplay(
          model.get(), bookmark_in_account_other_node);

  // Only 5 custom nodes should be displayed. These are the most recently added
  // ones. The account permanent nodes come last. Local permanent nodes should
  // not be included.
  EXPECT_THAT(mru_bookmarks.account_nodes,
              ElementsAre(custom_nodes[5], custom_nodes[3], custom_nodes[1],
                          model->account_bookmark_bar_node(),
                          model->account_other_node()));
  EXPECT_THAT(mru_bookmarks.local_nodes,
              ElementsAre(custom_nodes[4], custom_nodes[2]));
}

TEST_F(BookmarkUtilsTest, GetPermanentNodesForDisplayWithOnlyLocalBookmarks) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  const BookmarkNodesSplitByAccountAndLocal permanent_display_nodes =
      GetPermanentNodesForDisplay(model.get());

  EXPECT_TRUE(permanent_display_nodes.account_nodes.empty());
  EXPECT_THAT(permanent_display_nodes.local_nodes,
              ElementsAre(model->bookmark_bar_node(), model->other_node()));
}

TEST_F(BookmarkUtilsTest, GetPermanentNodesForDisplayWithAccountBookmarks) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();
  BookmarkNodesSplitByAccountAndLocal permanent_display_nodes =
      GetPermanentNodesForDisplay(model.get());

  ASSERT_FALSE(HasLocalOrSyncableBookmarks(model.get()));
  ASSERT_TRUE(permanent_display_nodes.local_nodes.empty());
  ASSERT_THAT(permanent_display_nodes.account_nodes,
              ElementsAre(model->account_bookmark_bar_node(),
                          model->account_other_node()));

  // With visible local/syncable bookmarks we should display visible local
  // permanent nodes too.
  model->AddURL(model->other_node(), 0, u"Title", GURL("http://google.com"));
  ASSERT_TRUE(HasLocalOrSyncableBookmarks(model.get()));

  permanent_display_nodes = GetPermanentNodesForDisplay(model.get());
  // Account nodes still showing.
  EXPECT_THAT(permanent_display_nodes.account_nodes,
              ElementsAre(model->account_bookmark_bar_node(),
                          model->account_other_node()));
  // Local nodes too.
  EXPECT_THAT(permanent_display_nodes.local_nodes,
              ElementsAre(model->bookmark_bar_node(), model->other_node()));
}

TEST_F(BookmarkUtilsTest, GetPermanentNodesForDisplayWithSyncEnabled) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  static_cast<TestBookmarkClient*>(model->client())
      ->SetIsSyncFeatureEnabledIncludingBookmarks(true);

  const BookmarkNodesSplitByAccountAndLocal permanent_display_nodes =
      GetPermanentNodesForDisplay(model.get());

  EXPECT_TRUE(permanent_display_nodes.account_nodes.empty());
  EXPECT_THAT(permanent_display_nodes.local_nodes,
              ElementsAre(model->bookmark_bar_node(), model->other_node()));
}
#endif  // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)

TEST_F(BookmarkUtilsTest,
       GetMostRecentlyModifiedUserFolders_DefaultOrderWithoutAccountBookmarks) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  ASSERT_FALSE(model->account_other_node());

  // The local other node should come first in order.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(GetMostRecentlyModifiedUserFolders(model.get()),
              ElementsAre(model->mobile_node()));
#else   // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(GetMostRecentlyModifiedUserFolders(model.get()),
              ElementsAre(model->other_node(), model->bookmark_bar_node()));
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
}

TEST_F(BookmarkUtilsTest,
       GetMostRecentlyModifiedUserFolders_DefaultOrderWithAccountBookmarks) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();

  // The account other node should come first in order. Local nodes are not
  // included if empty.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(GetMostRecentlyModifiedUserFolders(model.get()),
              ElementsAre(model->account_mobile_node(), model->mobile_node()));
#else   // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(GetMostRecentlyModifiedUserFolders(model.get()),
              ElementsAre(model->account_other_node(),
                          model->account_bookmark_bar_node()));
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
}
TEST_F(
    BookmarkUtilsTest,
    GetMostRecentlyModifiedUserFolders_LocalFolderModifiedBeforeAccountFoldersAdded) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());

  // Add a bookmark to the local bookmark bar node, so it should be visible and
  // first in the list.
  model->AddURL(model->bookmark_bar_node(), 0, u"Title",
                GURL("http://google.com"));

  std::vector<const BookmarkNode*> recently_modified =
      GetMostRecentlyModifiedUserFolders(model.get());
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  ASSERT_THAT(recently_modified,
              ElementsAre(model->bookmark_bar_node(), model->mobile_node()));
#else   // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  ASSERT_THAT(recently_modified,
              ElementsAre(model->bookmark_bar_node(), model->other_node()));
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)

  // Creating the account permanent folders should push the account other node
  // to the front of the list.
  model->CreateAccountPermanentFolders();
  recently_modified = GetMostRecentlyModifiedUserFolders(model.get());

  // The permanent account nodes should come first in order, then the local
  // ones. The account other node comes first.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(recently_modified,
              ElementsAre(model->account_mobile_node(),
                          model->bookmark_bar_node(), model->mobile_node()));
#else   // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(recently_modified,
              ElementsAre(model->account_other_node(),
                          model->account_bookmark_bar_node(),
                          model->bookmark_bar_node(), model->other_node()));
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
}

TEST_F(
    BookmarkUtilsTest,
    GetMostRecentlyModifiedUserFolders_LocalFolderModifiedAfterAccountFoldersAdded) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();

  // Add a bookmark to the local bookmark bar node after the account nodes were
  // added, so it should be visible and first in the list.
  model->AddURL(model->bookmark_bar_node(), 0, u"Title",
                GURL("http://google.com"));

  // The local bookmark bar should come first in order.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(GetMostRecentlyModifiedUserFolders(model.get()),
              ElementsAre(model->bookmark_bar_node(),
                          model->account_mobile_node(), model->mobile_node()));
#else   // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(
      GetMostRecentlyModifiedUserFolders(model.get()),
      ElementsAre(model->bookmark_bar_node(), model->account_other_node(),
                  model->account_bookmark_bar_node(), model->other_node()));
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
}

TEST_F(
    BookmarkUtilsTest,
    GetMostRecentlyModifiedUserFolders_AccountFolderModifiedAfterAccountFoldersAdded) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();

  // Add a bookmark to the account bookmark bar node after the account nodes
  // were added, so it should be first in the list.
  model->AddURL(model->account_bookmark_bar_node(), 0, u"Title",
                GURL("http://google.com"));

  // The account bookmark bar should come first in order.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(GetMostRecentlyModifiedUserFolders(model.get()),
              ElementsAre(model->account_bookmark_bar_node(),
                          model->account_mobile_node(), model->mobile_node()));
#else   // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(GetMostRecentlyModifiedUserFolders(model.get()),
              ElementsAre(model->account_bookmark_bar_node(),
                          model->account_other_node()));
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
}

TEST_F(
    BookmarkUtilsTest,
    GetMostRecentlyModifiedUserFolders_DefinedModifiedOrderWithCustomLocalFolders) {
  std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
  model->CreateAccountPermanentFolders();

  const BookmarkNode* const local_folder1 =
      model->AddFolder(model->other_node(), 0, u"Folder");
  const BookmarkNode* const local_folder2 =
      model->AddFolder(model->bookmark_bar_node(), 0, u"Folder2");

  model->SetDateFolderModified(model->account_other_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(0));
  model->SetDateFolderModified(model->account_bookmark_bar_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(1));

  model->SetDateFolderModified(model->bookmark_bar_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(2));
  model->SetDateFolderModified(local_folder2,
                               base::Time::FromMillisecondsSinceUnixEpoch(3));
  model->SetDateFolderModified(model->other_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(4));
  model->SetDateFolderModified(local_folder1,
                               base::Time::FromMillisecondsSinceUnixEpoch(5));

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  model->SetDateFolderModified(model->mobile_node(),
                               base::Time::FromMillisecondsSinceUnixEpoch(6));
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)

  // This simulates signing out and in again, or turning account storage for
  // bookmarks off and on through the settings. Doing this should row the
  // account permanent nodes first, and then follow the order of the last
  // recently modified folders.
  model->RemoveAccountPermanentFolders();
  model->CreateAccountPermanentFolders();

  // The permanent account nodes should come first in order, then the local
  // permanent folders and non-permanent nodes. The account other node comes
  // first.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(GetMostRecentlyModifiedUserFolders(model.get()),
              ElementsAre(model->account_mobile_node(), model->mobile_node(),
                          local_folder1, model->other_node(), local_folder2,
                          model->bookmark_bar_node()));
#else   // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  EXPECT_THAT(GetMostRecentlyModifiedUserFolders(model.get()),
              ElementsAre(model->account_other_node(),
                          model->account_bookmark_bar_node(), local_folder1,
                          model->other_node(), local_folder2,
                          model->bookmark_bar_node()));
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
}

}  // namespace
}  // namespace bookmarks