File: bookmark_browsertest.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 (906 lines) | stat: -rw-r--r-- 38,266 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
// 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.

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/interstitials/security_interstitial_page_test_utils.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/bookmarks/bookmark_drag_drop.h"
#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
#include "chrome/browser/ui/bookmarks/bookmark_utils_desktop.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "components/bookmarks/browser/url_and_title.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "components/signin/public/base/signin_switches.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/prerender_test_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/image/image_skia.h"

using bookmarks::BookmarkModel;
using bookmarks::BookmarkNode;
using bookmarks::UrlAndTitle;

namespace {

const char kPersistBookmarkURL[] = "http://www.cnn.com/";
const char16_t kPersistBookmarkTitle[] = u"CNN";
const base::Time kPersistLastUsedTime =
    base::Time() + base::Days(7) + base::Hours(2) + base::Minutes(55) +
    base::Seconds(24) + base::Milliseconds(133);

}  // namespace

class TestBookmarkTabHelperObserver : public BookmarkTabHelperObserver {
 public:
  explicit TestBookmarkTabHelperObserver(BookmarkTabHelper* helper) {
    observation_.Observe(helper);
  }

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

  ~TestBookmarkTabHelperObserver() override = default;

  void URLStarredChanged(content::WebContents*, bool starred) override {
    starred_ = starred;
  }
  bool is_starred() const { return starred_; }

 private:
  base::ScopedObservation<BookmarkTabHelper, BookmarkTabHelperObserver>
      observation_{this};

  bool starred_ = false;
};

class BookmarkBrowsertest : public InProcessBrowserTest {
 public:
  BookmarkBrowsertest() {
    feature_list_.InitWithFeatures(
        /*enabled_features=*/{switches::kSyncEnableBookmarksInTransportMode},
        /*disabled_features=*/{
#if BUILDFLAG(IS_WIN)
            // This needs to be disabled so that animations are guaranteed to
            // work.
            features::kApplyNativeOcclusionToCompositor
#endif
        });
  }

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

  bool IsVisible() {
    return browser()->bookmark_bar_state() == BookmarkBar::SHOW;
  }

  static void CheckAnimation(Browser* browser, base::RunLoop* loop) {
    if (!browser->window()->IsBookmarkBarAnimating()) {
      loop->Quit();
    }
  }

  base::TimeDelta WaitForBookmarkBarAnimationToFinish() {
    base::Time start(base::Time::Now());
    {
      base::RunLoop loop;
      base::RepeatingTimer timer;
      timer.Start(FROM_HERE, base::Milliseconds(15),
                  base::BindRepeating(&CheckAnimation, browser(), &loop));
      loop.Run();
    }
    return base::Time::Now() - start;
  }

  BookmarkModel* WaitForBookmarkModel(Profile* profile) {
    BookmarkModel* bookmark_model =
        BookmarkModelFactory::GetForBrowserContext(profile);
    bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model);
    return bookmark_model;
  }

  base::HistogramTester* histogram_tester() { return &histogram_tester_; }

 private:
  base::test::ScopedFeatureList feature_list_;

  // We make the histogram tester a member field to make sure it starts
  // recording as early as possible.
  base::HistogramTester histogram_tester_;
};

// Test of bookmark bar toggling, visibility, and animation.
IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, BookmarkBarVisibleWait) {
  ASSERT_FALSE(IsVisible());
  chrome::ExecuteCommand(browser(), IDC_SHOW_BOOKMARK_BAR);
  base::TimeDelta delay = WaitForBookmarkBarAnimationToFinish();
  LOG(INFO) << "Took " << delay.InMilliseconds() << " ms to show bookmark bar";
  ASSERT_TRUE(IsVisible());
  chrome::ExecuteCommand(browser(), IDC_SHOW_BOOKMARK_BAR);
  delay = WaitForBookmarkBarAnimationToFinish();
  LOG(INFO) << "Took " << delay.InMilliseconds() << " ms to hide bookmark bar";
  ASSERT_FALSE(IsVisible());
}

// Verify that bookmarks persist browser restart.
IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, PRE_Persist) {
  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());

  const BookmarkNode* node = bookmarks::AddIfNotBookmarked(
      bookmark_model, GURL(kPersistBookmarkURL), kPersistBookmarkTitle);
  bookmark_model->UpdateLastUsedTime(node, kPersistLastUsedTime,
                                     /*just_opened=*/true);
}

#if BUILDFLAG(IS_WIN)
// TODO(crbug.com/41443454): The test fails on Windows.
#define MAYBE_Persist DISABLED_Persist
#else
#define MAYBE_Persist Persist
#endif

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, MAYBE_Persist) {
  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());

  GURL url(kPersistBookmarkURL);
  std::vector<raw_ptr<const BookmarkNode, VectorExperimental>> nodes =
      bookmark_model->GetNodesByURL(url);

  ASSERT_EQ(1u, nodes.size());
  ASSERT_EQ(url, nodes[0]->url());
  ASSERT_EQ(kPersistBookmarkTitle, nodes[0]->GetTitledUrlNodeTitle());
  EXPECT_EQ(kPersistLastUsedTime, nodes[0]->date_last_used());
}

#if !BUILDFLAG(IS_CHROMEOS)  // No multi-profile on ChromeOS.

// Sanity check that bookmarks from different profiles are separate.
IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, MultiProfile) {
  BookmarkModel* bookmark_model1 = WaitForBookmarkModel(browser()->profile());

  base::RunLoop run_loop;
  Profile* profile2 = nullptr;
  g_browser_process->profile_manager()->CreateMultiProfileAsync(
      u"New Profile", 0, false,
      base::BindLambdaForTesting([&](Profile* profile) {
        if (profile) {
          profile2 = profile;
          run_loop.Quit();
        }
      }));
  run_loop.Run();
  BookmarkModel* bookmark_model2 = WaitForBookmarkModel(profile2);

  bookmarks::AddIfNotBookmarked(bookmark_model1, GURL(kPersistBookmarkURL),
                                kPersistBookmarkTitle);

  ASSERT_EQ(1u, bookmark_model1->GetUniqueUrls().size());
  ASSERT_TRUE(bookmark_model2->GetUniqueUrls().empty());
}

#endif

// Sanity check that bookmarks from Incognito mode persist Incognito restart.
IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, IncognitoPersistence) {
  Browser* incognito_browser = CreateIncognitoBrowser();
  BookmarkModel* bookmark_model =
      WaitForBookmarkModel(incognito_browser->profile());

  // Add bookmark for Incognito and ensure it is added.
  bookmarks::AddIfNotBookmarked(bookmark_model, GURL(kPersistBookmarkURL),
                                kPersistBookmarkTitle);

  ASSERT_EQ(1u, bookmark_model->GetUniqueUrls().size());

  // Restart Incognito, and check again.
  CloseBrowserSynchronously(incognito_browser);
  incognito_browser = CreateIncognitoBrowser();
  bookmark_model = WaitForBookmarkModel(incognito_browser->profile());
  ASSERT_EQ(1u, bookmark_model->GetUniqueUrls().size());

  // Ensure it is also available in regular mode.
  bookmark_model = WaitForBookmarkModel(browser()->profile());
  ASSERT_EQ(1u, bookmark_model->GetUniqueUrls().size());
}

// Regression for crash caused by opening folder as a group in an incognito
// window when the folder contains URLs that cannot be displayed in incognito.
// See discussion starting at crbug.com/1242351#c15
IN_PROC_BROWSER_TEST_F(
    BookmarkBrowsertest,
    OpenFolderAsGroupInIncognitoWhenBookmarksCantOpenInIncognito) {
  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());
  const BookmarkNode* const folder = bookmark_model->AddFolder(
      bookmark_model->bookmark_bar_node(), 0, u"Folder");
  const BookmarkNode* const page1 = bookmark_model->AddURL(
      folder, 0, u"Extensions", GURL(chrome::kChromeUIExtensionsURL));
  const BookmarkNode* const page2 = bookmark_model->AddURL(
      folder, 1, u"Settings", GURL(chrome::kChromeUISettingsURL));

  Browser* incognito_browser = CreateIncognitoBrowser();
  BookmarkModel* incognito_model =
      WaitForBookmarkModel(incognito_browser->profile());
  ASSERT_FALSE(incognito_model->root_node()->children().empty());
  ASSERT_TRUE(incognito_model->root_node()->children()[0]->is_folder());
  BookmarkNode* const incognito_folder =
      incognito_model->bookmark_bar_node()->children()[0].get();
  ASSERT_EQ(2U, incognito_folder->children().size());
  EXPECT_EQ(page1->url(), incognito_folder->children()[0]->url());
  EXPECT_EQ(page2->url(), incognito_folder->children()[1]->url());

  const int browser_tabs = browser()->tab_strip_model()->GetTabCount();
  const int incognito_tabs =
      incognito_browser->tab_strip_model()->GetTabCount();

  bookmarks::OpenAllIfAllowed(incognito_browser, {incognito_folder},
                              WindowOpenDisposition::NEW_BACKGROUND_TAB,
                              bookmarks::OpenAllBookmarksContext::kInGroup);

  EXPECT_EQ(incognito_tabs,
            incognito_browser->tab_strip_model()->GetTabCount());
  EXPECT_EQ(browser_tabs + 2, browser()->tab_strip_model()->GetTabCount());
}

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, OpenAllBookmarks) {
  Browser* regular_browser = browser();
  BookmarkModel* bookmark_model =
      WaitForBookmarkModel(regular_browser->profile());
  const BookmarkNode* const bbar = bookmark_model->bookmark_bar_node();
  ASSERT_TRUE(bbar->children().empty());

  Browser* incognito_browser = CreateIncognitoBrowser();
  BookmarkModel* incognito_model =
      WaitForBookmarkModel(incognito_browser->profile());
  const BookmarkNode* const incognito_bbar =
      incognito_model->bookmark_bar_node();

  auto close_all_tabs_except_first = [](Browser* browser) {
    int num_tabs = browser->tab_strip_model()->GetTabCount();
    for (int i = 0; i < num_tabs - 1; ++i) {
      browser->tab_strip_model()->CloseWebContentsAt(num_tabs - 1 - i, 0);
    }
    EXPECT_EQ(1, browser->tab_strip_model()->count());
  };

  auto open_urls_and_test = [&regular_browser, &incognito_browser, &bbar,
                             &close_all_tabs_except_first, this]() {
    // open all in new tab from regular browser
    {
      close_all_tabs_except_first(regular_browser);
      close_all_tabs_except_first(incognito_browser);
      bookmarks::OpenAllIfAllowed(regular_browser, {bbar},
                                  WindowOpenDisposition::NEW_BACKGROUND_TAB);
      int num_tabs_regular = regular_browser->tab_strip_model()->GetTabCount();
      int num_tabs_incognito =
          incognito_browser->tab_strip_model()->GetTabCount();
      EXPECT_EQ(num_tabs_regular, 5);
      EXPECT_EQ(num_tabs_incognito, 1);
    }

    // open all in a new window from regular browser
    {
      close_all_tabs_except_first(regular_browser);
      close_all_tabs_except_first(incognito_browser);
      bookmarks::OpenAllIfAllowed(regular_browser, {bbar},
                                  WindowOpenDisposition::NEW_WINDOW);
      Browser* regular_browser2 = nullptr;
      for (Browser* browser_instance : *BrowserList::GetInstance()) {
        if (browser_instance != incognito_browser &&
            browser_instance != regular_browser) {
          regular_browser2 = browser_instance;
        }
      }
      // new browser needs to be opened
      EXPECT_NE(regular_browser2, nullptr);
      int num_tabs_regular = regular_browser->tab_strip_model()->GetTabCount();
      int num_tabs_regular2 =
          regular_browser2->tab_strip_model()->GetTabCount();
      EXPECT_EQ(num_tabs_regular, 1);
      EXPECT_EQ(num_tabs_regular2, 4);
      CloseBrowserSynchronously(regular_browser2);
    }

    // open all in a new incognito window from regular browser
    {
      close_all_tabs_except_first(regular_browser);
      close_all_tabs_except_first(incognito_browser);
      bookmarks::OpenAllIfAllowed(regular_browser, {bbar},
                                  WindowOpenDisposition::OFF_THE_RECORD);
      int num_tabs_incognito =
          incognito_browser->tab_strip_model()->GetTabCount();
      EXPECT_EQ(num_tabs_incognito, 3);
    }
  };

  auto open_urls_from_incognito_and_test = [&regular_browser,
                                            &incognito_browser, &incognito_bbar,
                                            &close_all_tabs_except_first,
                                            this]() {
    // open all in new tab from incognito
    {
      close_all_tabs_except_first(regular_browser);
      close_all_tabs_except_first(incognito_browser);
      bookmarks::OpenAllIfAllowed(incognito_browser, {incognito_bbar},
                                  WindowOpenDisposition::NEW_BACKGROUND_TAB);
      int num_tabs_regular = regular_browser->tab_strip_model()->GetTabCount();
      int num_tabs_incognito =
          incognito_browser->tab_strip_model()->GetTabCount();
      EXPECT_EQ(num_tabs_regular, 3);
      EXPECT_EQ(num_tabs_incognito, 3);
    }

    // open all in new window from incognito
    {
      close_all_tabs_except_first(regular_browser);
      close_all_tabs_except_first(incognito_browser);
      bookmarks::OpenAllIfAllowed(incognito_browser, {incognito_bbar},
                                  WindowOpenDisposition::NEW_WINDOW);
      Browser* incognito_browser2 = nullptr;
      for (Browser* browser_instance : *BrowserList::GetInstance()) {
        if (browser_instance != incognito_browser &&
            browser_instance != regular_browser) {
          incognito_browser2 = browser_instance;
        }
      }
      // new browser needs to be opened
      EXPECT_NE(incognito_browser2, nullptr);
      int num_tabs_regular = regular_browser->tab_strip_model()->GetTabCount();
      int num_tabs_incognito =
          incognito_browser->tab_strip_model()->GetTabCount();
      int num_tabs_incognito2 =
          incognito_browser2->tab_strip_model()->GetTabCount();
      EXPECT_EQ(num_tabs_regular, 3);
      EXPECT_EQ(num_tabs_incognito, 1);
      EXPECT_EQ(num_tabs_incognito2, 2);
      CloseBrowserSynchronously(incognito_browser2);
    }
  };

  {
    // Bookmark 4 pages, with the first and third one not being able to be
    // opened in incognito mode
    bookmark_model->AddURL(bbar, 0, u"Settings",
                           GURL(chrome::kChromeUISettingsURL));
    bookmark_model->AddURL(bbar, 1, u"Google", GURL("http://www.google.com"));
    bookmark_model->AddURL(bbar, 2, u"Extensions",
                           GURL(chrome::kChromeUIExtensionsURL));
    bookmark_model->AddURL(bbar, 3, u"Gmail", GURL("http://mail.google.com"));
    open_urls_and_test();
    open_urls_from_incognito_and_test();
    bookmark_model->RemoveAllUserBookmarks(FROM_HERE);
  }
  {
    // Bookmark 4 pages, with the second and fourth one not being able to be
    // opened in incognito mode
    bookmark_model->AddURL(bbar, 0, u"Google", GURL("http://www.google.com"));
    bookmark_model->AddURL(bbar, 1, u"Settings",
                           GURL(chrome::kChromeUISettingsURL));
    bookmark_model->AddURL(bbar, 2, u"Gmail", GURL("http://mail.google.com"));
    bookmark_model->AddURL(bbar, 3, u"Extensions",
                           GURL(chrome::kChromeUIExtensionsURL));
    open_urls_and_test();
    open_urls_from_incognito_and_test();
    bookmark_model->RemoveAllUserBookmarks(FROM_HERE);
  }
}

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest,
                       HideStarOnNonbookmarkedInterstitial) {
  // Start an HTTPS server with a certificate error.
  net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS);
  https_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME);
  https_server.ServeFilesFromSourceDirectory(GetChromeTestDataDir());
  ASSERT_TRUE(https_server.Start());
  ASSERT_TRUE(embedded_test_server()->Start());

  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());
  GURL bookmark_url = embedded_test_server()->GetURL("example.test", "/");
  bookmarks::AddIfNotBookmarked(bookmark_model, bookmark_url, u"Bookmark");

  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  BookmarkTabHelper* tab_helper =
      BookmarkTabHelper::FromWebContents(web_contents);
  TestBookmarkTabHelperObserver bookmark_observer(tab_helper);

  // Go to a bookmarked url. Bookmark star should show.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), bookmark_url));
  EXPECT_FALSE(
      chrome_browser_interstitials::IsShowingInterstitial(web_contents));
  EXPECT_TRUE(bookmark_observer.is_starred());
  // Now go to a non-bookmarked url which triggers an SSL warning. Bookmark
  // star should disappear.
  GURL error_url = https_server.GetURL("/");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), error_url));
  web_contents = browser()->tab_strip_model()->GetActiveWebContents();
  EXPECT_TRUE(
      chrome_browser_interstitials::IsShowingInterstitial(web_contents));
  EXPECT_FALSE(bookmark_observer.is_starred());
}

// Provides coverage for the Bookmark Manager bookmark drag and drag image
// generation for dragging a single bookmark.
IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, DragSingleBookmark) {
  BookmarkModel* model = WaitForBookmarkModel(browser()->profile());
  const std::u16string page_title(u"foo");
  const GURL page_url("http://www.google.com");
  const BookmarkNode* root = model->bookmark_bar_node();
  const BookmarkNode* node = model->AddURL(root, 0, page_title, page_url);
  const gfx::Point expected_point(100, 100);

  auto run_loop = std::make_unique<base::RunLoop>();

  chrome::DoBookmarkDragCallback cb = base::BindLambdaForTesting(
      [&run_loop, page_title, page_url, expected_point](
          std::unique_ptr<ui::OSExchangeData> drag_data,
          gfx::NativeView native_view, ui::mojom::DragEventSource source,
          gfx::Point point, int operation) {
        std::optional<ui::OSExchangeData::UrlInfo> url_info =
            drag_data->GetURLAndTitle(
                ui::FilenameToURLPolicy::DO_NOT_CONVERT_FILENAMES);
        ASSERT_TRUE(url_info.has_value());
        EXPECT_EQ(page_url, url_info->url);
        EXPECT_EQ(page_title, url_info->title);
#if !BUILDFLAG(IS_WIN)
        // On Windows, GetDragImage() is a NOTREACHED() as the Windows
        // implementation of OSExchangeData just sets the drag image on the OS
        // API. https://crbug.com/893388
        EXPECT_FALSE(drag_data->provider().GetDragImage().isNull());
#endif
        EXPECT_EQ(expected_point, point);
        run_loop->Quit();
      });

  constexpr int kDragNodeIndex = 0;
  chrome::DragBookmarksForTest(
      browser()->profile(),
      {{node},
       kDragNodeIndex,
       browser()->tab_strip_model()->GetActiveWebContents(),
       ui::mojom::DragEventSource::kMouse,
       expected_point},
      std::move(cb));

  run_loop->Run();
}

// A favicon update during drag shouldn't trigger the drag flow again. The test
// passes if the favicon update does not cause a crash. (see
// https://crbug.com/1364056)
IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, FaviconChangeDuringBookmarkDrag) {
  BookmarkModel* model = WaitForBookmarkModel(browser()->profile());
  const std::u16string kPageTitle(u"foo");
  const GURL kPageUrl("http://www.google.com");
  const GURL kFaviconUrl("http://www.google.com/favicon.ico");
  const BookmarkNode* root = model->bookmark_bar_node();
  const BookmarkNode* node = model->AddURL(root, 0, kPageTitle, kPageUrl);
  constexpr gfx::Point kExpectedPoint(100, 100);

  auto run_loop = std::make_unique<base::RunLoop>();

  chrome::DoBookmarkDragCallback cb = base::BindLambdaForTesting(
      [&run_loop, model, kPageUrl, kFaviconUrl](
          std::unique_ptr<ui::OSExchangeData> drag_data,
          gfx::NativeView native_view, ui::mojom::DragEventSource source,
          gfx::Point point, int operation) {
        // Simulate a favicon change during the drag operation.
        model->OnFaviconsChanged({kPageUrl}, kFaviconUrl);
        run_loop->Quit();
      });

  constexpr int kDragNodeIndex = 0;
  chrome::DragBookmarksForTest(
      browser()->profile(),
      {{node},
       kDragNodeIndex,
       browser()->tab_strip_model()->GetActiveWebContents(),
       ui::mojom::DragEventSource::kMouse,
       kExpectedPoint},
      std::move(cb));

  run_loop->Run();
}

// Provides coverage for the Bookmark Manager bookmark drag and drag image
// generation for dragging multiple bookmarks.
IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, DragMultipleBookmarks) {
  BookmarkModel* model = WaitForBookmarkModel(browser()->profile());
  const std::u16string page_title = u"foo";
  const GURL page_url("http://www.google.com");
  const BookmarkNode* root = model->bookmark_bar_node();
  const BookmarkNode* node1 = model->AddURL(root, 0, page_title, page_url);
  const BookmarkNode* node2 = model->AddFolder(root, 0, page_title);
  const gfx::Point expected_point(100, 100);

  auto run_loop = std::make_unique<base::RunLoop>();

  chrome::DoBookmarkDragCallback cb = base::BindLambdaForTesting(
      [&run_loop, expected_point, page_title, page_url](
          std::unique_ptr<ui::OSExchangeData> drag_data,
          gfx::NativeView native_view, ui::mojom::DragEventSource source,
          gfx::Point point, int operation) {
#if BUILDFLAG(IS_MAC)
        // On the Mac, the clipboard can hold multiple items, each with
        // different representations. Therefore, when the "write multiple URLs"
        // call is made, a full-fledged array of objects and types are written
        // to the clipboard, providing rich interoperability with the rest of
        // the OS and other apps. Then, when `GetURLAndTitle` is called, it
        // looks at the clipboard, sees URL and title data, and returns true.
        std::optional<ui::OSExchangeData::UrlInfo> url_info =
            drag_data->GetURLAndTitle(
                ui::FilenameToURLPolicy::DO_NOT_CONVERT_FILENAMES);
        ASSERT_TRUE(url_info.has_value());

        // The bookmarks are added in order, and the first is retrieved, so
        // expect the values from the first bookmark.
        EXPECT_EQ(page_title, url_info->title);
        EXPECT_EQ(page_url, url_info->url);
#else
        // On other platforms, because they don't have the concept of multiple
        // items on the clipboard, single URLs are added as a URL, but multiple
        // URLs are added as a data blob opaque to the outside world. Then, when
        // `GetURLAndTitle` is called, it's unable to extract any single URL,
        // and returns false.
        EXPECT_FALSE(drag_data
                         ->GetURLAndTitle(
                             ui::FilenameToURLPolicy::DO_NOT_CONVERT_FILENAMES)
                         .has_value());
#endif
#if !BUILDFLAG(IS_WIN)
        // On Windows, GetDragImage() is a NOTREACHED() as the Windows
        // implementation of OSExchangeData just sets the drag image on the OS
        // API. https://crbug.com/893388
        EXPECT_FALSE(drag_data->provider().GetDragImage().isNull());
#endif
        EXPECT_EQ(expected_point, point);
        run_loop->Quit();
      });

  constexpr int kDragNodeIndex = 1;
  chrome::DragBookmarksForTest(
      browser()->profile(),
      {
          {node1, node2},
          kDragNodeIndex,
          browser()->tab_strip_model()->GetActiveWebContents(),
          ui::mojom::DragEventSource::kMouse,
          expected_point,
      },
      std::move(cb));

  run_loop->Run();
}

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, PRE_EmitUmaForTimeMetrics) {
  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());
  const BookmarkNode* parent = bookmarks::GetParentForNewNodes(bookmark_model);
  const BookmarkNode* other_parent =
      bookmark_model->AddFolder(parent, 0, u"Folder");

  // Add one bookmark with a unique URL, two other bookmarks with a shared URL,
  // and three more with another shared URL.
  bookmark_model->AddURL(parent, parent->children().size(), u"title1",
                         GURL("http://a.com"));
  bookmark_model->AddURL(parent, parent->children().size(), u"title2",
                         GURL("http://b.com"));
  bookmark_model->AddURL(parent, parent->children().size(), u"title3",
                         GURL("http://b.com"));
  bookmark_model->AddURL(parent, parent->children().size(), u"title4",
                         GURL("http://c.com"));
  bookmark_model->AddURL(parent, parent->children().size(), u"title5",
                         GURL("http://c.com"));
  bookmark_model->AddURL(parent, parent->children().size(), u"title5",
                         GURL("http://c.com"));
  bookmark_model->AddURL(other_parent, other_parent->children().size(),
                         u"title5", GURL("http://c.com"));
}

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, EmitUmaForTimeMetrics) {
  WaitForBookmarkModel(browser()->profile());

  // The total number of bookmarks is 7, but it gets rounded down due to
  // bucketing.
  ASSERT_THAT(
      histogram_tester()->GetAllSamples("Bookmarks.Count.OnProfileLoad3"),
      testing::ElementsAre(base::Bucket(/*min=*/6, /*count=*/1)));

  EXPECT_THAT(histogram_tester()->GetAllSamples(
                  "Bookmarks.Times.OnProfileLoad.TimeSinceAdded3"),
              testing::ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
}

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, PRE_EmitUmaForMostRecentlyUsed) {
  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());
  BookmarkNode* parent = const_cast<BookmarkNode*>(bookmark_model->AddFolder(
      bookmarks::GetParentForNewNodes(bookmark_model), 0, u"Test Folder"));
  parent->set_date_added(base::Time::Now() - base::Days(3));

  BookmarkNode* node = const_cast<BookmarkNode*>(bookmark_model->AddURL(
      parent, parent->children().size(), u"title1", GURL("http://a.com")));
  node->set_date_added(base::Time::Now() - base::Days(2));
  node->set_date_last_used(base::Time::Now() - base::Days(1));

  // This shouldn't count towards metrics because there's another node which
  // is more recently saved/used.
  node = const_cast<BookmarkNode*>(bookmark_model->AddURL(
      parent, parent->children().size(), u"title1", GURL("http://a.com")));
  node->set_date_added(base::Time::Now() - base::Days(3));
  node->set_date_last_used(base::Time::Now() - base::Days(3));
}

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, EmitUmaForMostRecentlyUsed) {
  WaitForBookmarkModel(browser()->profile());

  EXPECT_THAT(
      histogram_tester()->GetAllSamples(
          "Bookmarks.Times.OnProfileLoad.MostRecentlyUsedBookmarkInDays"),
      testing::ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));

  EXPECT_THAT(
      histogram_tester()->GetAllSamples(
          "Bookmarks.Times.OnProfileLoad.MostRecentlySavedBookmarkInDays"),
      testing::ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
  EXPECT_THAT(
      histogram_tester()->GetAllSamples(
          "Bookmarks.Times.OnProfileLoad.MostRecentlyAddedFolderInDays"),
      testing::ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
}

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest,
                       EmitUmaForMostRecentlyUsed_NoBookmarks) {
  WaitForBookmarkModel(browser()->profile());

  EXPECT_THAT(
      histogram_tester()->GetAllSamples(
          "Bookmarks.Times.OnProfileLoad.MostRecentlyUsedBookmarkInDays"),
      testing::ElementsAre());

  EXPECT_THAT(
      histogram_tester()->GetAllSamples(
          "Bookmarks.Times.OnProfileLoad.MostRecentlySavedBookmarkInDays"),
      testing::ElementsAre());
  EXPECT_THAT(
      histogram_tester()->GetAllSamples(
          "Bookmarks.Times.OnProfileLoad.MostRecentlyAddedFolderInDays"),
      testing::ElementsAre());
}

// Test that the bookmark star state updates in response to same document
// navigations that change the URL
IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, SameDocumentNavigation) {
  ASSERT_TRUE(embedded_test_server()->Start());

  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());
  GURL bookmark_url = embedded_test_server()->GetURL("/title1.html");
  bookmarks::AddIfNotBookmarked(bookmark_model, bookmark_url, u"Bookmark");

  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  BookmarkTabHelper* tab_helper =
      BookmarkTabHelper::FromWebContents(web_contents);
  TestBookmarkTabHelperObserver bookmark_observer(tab_helper);

  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), bookmark_url));
  EXPECT_TRUE(bookmark_observer.is_starred());

  GURL same_document_url = embedded_test_server()->GetURL("/title1.html#test");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), same_document_url));
  EXPECT_FALSE(bookmark_observer.is_starred());
}

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest,
                       DifferentDocumentNavigationWithoutFinishing) {
  ASSERT_TRUE(embedded_test_server()->Start());

  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());
  GURL bookmark_url = embedded_test_server()->GetURL("/title1.html");
  bookmarks::AddIfNotBookmarked(bookmark_model, bookmark_url, u"Bookmark");

  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  BookmarkTabHelper* tab_helper =
      BookmarkTabHelper::FromWebContents(web_contents);
  TestBookmarkTabHelperObserver bookmark_observer(tab_helper);

  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), bookmark_url));
  EXPECT_TRUE(bookmark_observer.is_starred());

  // Navigate to the second page and check that the bookmark is not starred even
  // when the navigation is not finished.
  GURL different_document_url = embedded_test_server()->GetURL("/title2.html");
  content::TestNavigationManager manager(web_contents, different_document_url);
  web_contents->GetController().LoadURL(
      different_document_url, content::Referrer(), ui::PAGE_TRANSITION_TYPED,
      std::string());
  EXPECT_TRUE(manager.WaitForRequestStart());
  EXPECT_FALSE(manager.was_committed());
  EXPECT_EQ(web_contents->GetVisibleURL(), different_document_url);
  EXPECT_EQ(web_contents->GetLastCommittedURL(), bookmark_url);
  EXPECT_FALSE(bookmark_observer.is_starred());
}

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, NonCommitURLNavigation) {
  ASSERT_TRUE(embedded_test_server()->Start());

  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());
  GURL bookmark_url = embedded_test_server()->GetURL("/title1.html");
  bookmarks::AddIfNotBookmarked(bookmark_model, bookmark_url, u"Bookmark");

  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  BookmarkTabHelper* tab_helper =
      BookmarkTabHelper::FromWebContents(web_contents);
  TestBookmarkTabHelperObserver bookmark_observer(tab_helper);

  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), bookmark_url));
  EXPECT_TRUE(bookmark_observer.is_starred());

  const GURL non_commit_url = embedded_test_server()->GetURL("/page204.html");
  content::TestNavigationManager manager(web_contents, non_commit_url);
  web_contents->GetController().LoadURL(non_commit_url, content::Referrer(),
                                        ui::PAGE_TRANSITION_TYPED,
                                        std::string());
  EXPECT_TRUE(manager.WaitForRequestStart());
  EXPECT_FALSE(manager.was_committed());
  EXPECT_EQ(web_contents->GetVisibleURL(), non_commit_url);
  EXPECT_EQ(web_contents->GetLastCommittedURL(), bookmark_url);
  EXPECT_FALSE(bookmark_observer.is_starred());

  // Since the navigation did not commit, the last committed URL becomes the
  // visible URL again, so the starred state should be restored.
  ASSERT_TRUE(manager.WaitForNavigationFinished());
  EXPECT_FALSE(manager.was_committed());
  EXPECT_TRUE(bookmark_observer.is_starred());
}

IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest,
                       BookmarkCurrentTab_WithoutAccountNodes) {
  ASSERT_TRUE(embedded_test_server()->Start());
  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());

  // If no folders have been modified, bookmarks are saved to other bookmarks by
  // default.
  ASSERT_EQ(bookmark_model->other_node(),
            bookmarks::GetParentForNewNodes(bookmark_model));

  ASSERT_TRUE(chrome::ExecuteCommand(browser(), IDC_BOOKMARK_THIS_TAB));
  EXPECT_THAT(1u, bookmark_model->other_node()->children().size());

  // Add a bookmark to the local bookmark bar node, so that it becomes the most
  // recently modified one.
  bookmark_model->AddURL(bookmark_model->bookmark_bar_node(), 0, u"Title",
                         GURL("http://google.com"));
  ASSERT_EQ(bookmark_model->bookmark_bar_node(),
            bookmarks::GetParentForNewNodes(bookmark_model));

  // After the bookmarks bar was modified, it should be the new default save
  // location.
  ASSERT_TRUE(chrome::ExecuteCommand(browser(), IDC_BOOKMARK_THIS_TAB));
  EXPECT_THAT(1u, bookmark_model->bookmark_bar_node()->children().size());
}

// Account nodes don't exist on ChromeOS, so this test does not apply.
#if !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest,
                       BookmarkCurrentTab_WithAccountNodes) {
  ASSERT_TRUE(embedded_test_server()->Start());
  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());
  bookmark_model->CreateAccountPermanentFolders();

  // If no folders have been modified, bookmarks are saved to account other
  // bookmarks by default.
  ASSERT_EQ(bookmark_model->account_other_node(),
            bookmarks::GetParentForNewNodes(bookmark_model));

  ASSERT_TRUE(chrome::ExecuteCommand(browser(), IDC_BOOKMARK_THIS_TAB));
  EXPECT_THAT(1u, bookmark_model->account_other_node()->children().size());

  // Add a bookmark to the local bookmark bar node, so that it becomes the most
  // recently modified one.
  bookmark_model->AddURL(bookmark_model->bookmark_bar_node(), 0, u"Title",
                         GURL("http://google.com"));
  ASSERT_EQ(bookmark_model->bookmark_bar_node(),
            bookmarks::GetParentForNewNodes(bookmark_model));

  // After the bookmarks bar was modified, it should be the new default save
  // location.
  ASSERT_TRUE(chrome::ExecuteCommand(browser(), IDC_BOOKMARK_THIS_TAB));
  EXPECT_THAT(1u, bookmark_model->bookmark_bar_node()->children().size());
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

class BookmarkPrerenderBrowsertest : public BookmarkBrowsertest {
 public:
  BookmarkPrerenderBrowsertest()
      : prerender_helper_(
            base::BindRepeating(&BookmarkPrerenderBrowsertest::GetWebContents,
                                base::Unretained(this))) {}
  ~BookmarkPrerenderBrowsertest() override = default;
  BookmarkPrerenderBrowsertest(const BookmarkPrerenderBrowsertest&) = delete;

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

  void SetUp() override {
    prerender_helper_.RegisterServerRequestMonitor(embedded_test_server());
    BookmarkBrowsertest::SetUp();
  }

  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(embedded_test_server()->Start());
    BookmarkBrowsertest::SetUpOnMainThread();
  }

  content::test::PrerenderTestHelper& prerender_test_helper() {
    return prerender_helper_;
  }

  content::WebContents* GetWebContents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

 private:
  content::test::PrerenderTestHelper prerender_helper_;
};

IN_PROC_BROWSER_TEST_F(BookmarkPrerenderBrowsertest,
                       PrerenderingShouldNotUpdateStarredState) {
  GURL initial_url = embedded_test_server()->GetURL("/empty.html");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), initial_url));

  BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());
  GURL bookmark_url = embedded_test_server()->GetURL("/title1.html");
  bookmarks::AddIfNotBookmarked(bookmark_model, bookmark_url, u"Bookmark");

  BookmarkTabHelper* tab_helper =
      BookmarkTabHelper::FromWebContents(GetWebContents());
  TestBookmarkTabHelperObserver bookmark_observer(tab_helper);

  // Load a prerender page and prerendering should not notify to
  // URLStarredChanged listener.
  const content::FrameTreeNodeId host_id =
      prerender_test_helper().AddPrerender(bookmark_url);
  content::test::PrerenderHostObserver host_observer(*GetWebContents(),
                                                     host_id);
  EXPECT_FALSE(host_observer.was_activated());
  EXPECT_FALSE(bookmark_observer.is_starred());

  // Activate the prerender page.
  prerender_test_helper().NavigatePrimaryPage(bookmark_url);
  EXPECT_TRUE(host_observer.was_activated());
  EXPECT_TRUE(bookmark_observer.is_starred());
}