File: history_clusters_tab_helper_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 (757 lines) | stat: -rw-r--r-- 34,496 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/history_clusters/history_clusters_tab_helper.h"

#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/history_clusters/history_clusters_service_factory.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_types.h"
#include "components/history/core/test/history_service_test_util.h"
#include "components/history_clusters/core/features.h"
#include "components/history_clusters/core/history_clusters_service_test_api.h"
#include "components/keyed_service/core/service_access_type.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Used to invoke a callback after `WebContentsDestroyed()` is invoked, but
// before the `WebContents` has been destroyed.
class OnDestroyWebContentsObserver : content::WebContentsObserver {
 public:
  OnDestroyWebContentsObserver(content::WebContents* web_contents,
                               base::OnceCallback<void()> callback)
      : callback_(std::move(callback)) {
    Observe(web_contents);
  }

 private:
  void WebContentsDestroyed() override { std::move(callback_).Run(); }

  base::OnceCallback<void()> callback_;
};

class HistoryClustersTabHelperTest : public ChromeRenderViewHostTestHarness {
 public:
  HistoryClustersTabHelperTest(const HistoryClustersTabHelperTest&) = delete;
  HistoryClustersTabHelperTest& operator=(const HistoryClustersTabHelperTest&) =
      delete;

 protected:
  HistoryClustersTabHelperTest() {
    // This needs to be initialized as early as possible to avoid data races
    // with tasks on other threads checking if features are enabled.
    feature_list_.InitAndEnableFeature(history_clusters::internal::kJourneys);
  }

  // ChromeRenderViewHostTestHarness:
  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();

    HistoryClustersTabHelper::CreateForWebContents(web_contents());
    helper_ = HistoryClustersTabHelper::FromWebContents(web_contents());
    ASSERT_TRUE(helper_);

    ASSERT_TRUE(history_service_ = HistoryServiceFactory::GetForProfile(
                    profile(), ServiceAccessType::IMPLICIT_ACCESS));

    history_clusters_service_test_api_ =
        std::make_unique<history_clusters::HistoryClustersServiceTestApi>(
            HistoryClustersServiceFactory::GetForBrowserContext(
                web_contents()->GetBrowserContext()),
            history_service_);
    ASSERT_TRUE(history_clusters_service_test_api_);

    BookmarkModelFactory::GetInstance()->SetTestingFactory(
        web_contents()->GetBrowserContext(),
        BookmarkModelFactory::GetDefaultFactory());
    ASSERT_TRUE(bookmark_model_ = BookmarkModelFactory::GetForBrowserContext(
                    web_contents()->GetBrowserContext()));
    ASSERT_TRUE(bookmark_model_);
    bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model_);

    run_loop_quit_ = run_loop_.QuitClosure();
  }

  TestingProfile::TestingFactories GetTestingFactories() const override {
    return {TestingProfile::TestingFactory{
        HistoryServiceFactory::GetInstance(),
        HistoryServiceFactory::GetDefaultFactory()}};
  }

  std::vector<history::AnnotatedVisit> GetVisits() const {
    return history_clusters_service_test_api_->GetVisits();
  }

  base::Time MakeTime(int time_seconds) {
    return base::Time::FromDeltaSinceWindowsEpoch(base::Seconds(time_seconds));
  }

  void AddToHistory(const GURL& url, base::Time timestamp) {
    history::HistoryAddPageArgs add_page_args;
    add_page_args.url = url;
    add_page_args.title = u"Fake Title";
    add_page_args.time = timestamp;
    history_service_->AddPage(add_page_args);
  }

  void AddBookmark(const GURL& url) {
    ASSERT_TRUE(bookmark_model_->AddURL(bookmark_model_->bookmark_bar_node(), 0,
                                        std::u16string(), url));
  }

  base::test::ScopedFeatureList feature_list_;

  raw_ptr<HistoryClustersTabHelper, DanglingUntriaged> helper_;

  std::unique_ptr<history_clusters::HistoryClustersServiceTestApi>
      history_clusters_service_test_api_;

  base::CancelableTaskTracker tracker_;
  raw_ptr<history::HistoryService, DanglingUntriaged> history_service_;

  raw_ptr<bookmarks::BookmarkModel, DanglingUntriaged> bookmark_model_;

  // Used to verify the async callback is invoked.
  base::RunLoop run_loop_;
  base::RepeatingClosure run_loop_quit_;
};

// There are multiple events that occur with nondeterministic order:
// - History (w/ N visits): a history navigation occurs,
//   `OnUpdatedHistoryForNavigation()` is invoked, and a history query is made
//   which will (possibly after other events in the timeline) resolve with N
//   visits.
// - History resolve: the history query made above resolves.
// - Copy: the omnibox URL is copied and `OnOmniboxUrlCopied()` is invoked.
// - Expect UKM: UKM begins tracking a navigation and
//   `TagNavigationAsExpectingUkmNavigationComplete()` is invoked.
// - UKM: UKM ends tracking a navigation and `OnUkmNavigationComplete()` is
//   invoked.
// - Destroy: The `WebContents` is destroyed (i.e. the tab is closed) and
//   `WebContentsDestroyed()` is invoked.
// The below tests test different permutations of these events.

// History (w/ 0 visits) -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked but its history request isn't
//     resolved (because either the tab is closed too soon or there are no
//     matching visits).
// 2) `WebContentsDestroyed()` is invoked.
// Then: 0 context annotations should be committed.
TEST_F(HistoryClustersTabHelperTest, NavigationWith0HistoryVisits) {
  AddToHistory(GURL{"https://other.com"}, MakeTime(2));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(1),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  helper_->OnOmniboxUrlCopied();
  DeleteContents();

  auto visits = GetVisits();
  EXPECT_EQ(visits.size(), 1U);
  // Context signals should be unavailable because there's no matching visit.
  EXPECT_FALSE(visits[0].context_annotations.omnibox_url_copied);
}

// History (w/ 1 visit) -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked and 1 history visit are
//    fetched.
// 2) `WebContentsDestroyed()` is invoked.
// Then: 1 context annotation should be committed.
TEST_F(HistoryClustersTabHelperTest, NavigationWith1HistoryVisits) {
  AddToHistory(GURL{"https://github.com"}, MakeTime(1));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(1),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  helper_->OnOmniboxUrlCopied();
  EXPECT_EQ(GetVisits().size(), 1u);

  DeleteContents();
  // Adding the bookmark after the WebContents is destroyed should do nothing.
  AddBookmark(GURL{"https://github.com"});

  auto visits = GetVisits();
  ASSERT_EQ(visits.size(), 1u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[0].context_annotations.duration_since_last_visit.InSeconds(),
            -1);
  EXPECT_FALSE(visits[0].context_annotations.is_new_bookmark);
  EXPECT_TRUE(visits[0].context_annotations.omnibox_url_copied);
}

// History (w/ 1 *mismatched* visit) -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked and 1 history visit is
//    fetched, but it doesn't match the navigation timestamp.
// 2) `WebContentsDestroyed()` is invoked.
// Then: 0 context annotations should be committed.
TEST_F(HistoryClustersTabHelperTest, NavigationWith1MismatchedHistoryVisit) {
  // One pre-existing visit to the URL exists in the DB.
  AddToHistory(GURL{"https://github.com"}, MakeTime(1));
  // A new visit to the same URL happens, but this one is *not* in the DB.
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(100),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  helper_->OnOmniboxUrlCopied();
  ASSERT_EQ(GetVisits().size(), 1u);

  DeleteContents();

  auto visits = GetVisits();
  ASSERT_EQ(visits.size(), 1u);
  ASSERT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_NE(visits[0].visit_row.visit_time, MakeTime(100));
  // Since the timestamps didn't match, the visit should *not* have gotten any
  // annotations.
  EXPECT_FALSE(visits[0].context_annotations.omnibox_url_copied);
}

// History (w/ 2 visits) -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked and 2 history visits are
//    fetched.
// 2) `WebContentsDestroyed()` is invoked.
// Then: 1 context annotation should be committed.
TEST_F(HistoryClustersTabHelperTest, NavigationWith2HistoryVisits) {
  AddToHistory(GURL{"https://github.com"}, MakeTime(19));
  AddToHistory(GURL{"https://github.com"}, MakeTime(23));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(23),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  auto visits = GetVisits();
  // Two visits are there but context annotations are initially unavailable.
  EXPECT_EQ(visits.size(), 2u);
  EXPECT_EQ(visits[0].context_annotations.duration_since_last_visit.InSeconds(),
            -1);

  DeleteContents();
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 2u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[0].context_annotations.duration_since_last_visit.InSeconds(),
            4);
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[1].context_annotations.duration_since_last_visit.InSeconds(),
            -1);
}

// History (w/ 0 visits) -> history (w/ 0 visits) -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked but its history request isn't
//     resolved (because either the tab is closed too soon or there are no
//     matching visits).
// 2) `OnUpdatedHistoryForNavigation()` is invoked but its history request isn't
//     resolved (because either the tab is closed too soon or there are no
//     matching visits).
// 3) `WebContentsDestroyed()` is invoked.
// Then: 0 visits should be committed.
TEST_F(HistoryClustersTabHelperTest, TwoNavigationsWith0HistoryVisits) {
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(1),
                                         GURL{"https://github.com"});
  helper_->OnUpdatedHistoryForNavigation(1, MakeTime(2),
                                         GURL{"https://google.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  EXPECT_TRUE(GetVisits().empty());
}

// History (w/ 2 visits) -> history (w/ 2 visits) -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked and 2 history visits are
//    fetched.
// 2) `OnUpdatedHistoryForNavigation()` is invoked and 2 history visits are
//    fetched.
// 3) `WebContentsDestroyed()` is invoked.
// Then: 2 context annotations should be committed.
TEST_F(HistoryClustersTabHelperTest, TwoNavigationsWith2HistoryVisits) {
  AddToHistory(GURL{"https://github.com"}, MakeTime(1));
  AddToHistory(GURL{"https://github.com"}, MakeTime(5));
  AddToHistory(GURL{"https://google.com"}, MakeTime(10));
  AddToHistory(GURL{"https://google.com"}, MakeTime(18));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(5),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  // 4 visits are in History, but they don't have context annotations.
  auto visits = GetVisits();
  ASSERT_EQ(visits.size(), 4u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://google.com"});
  EXPECT_EQ(visits[0].context_annotations.duration_since_last_visit.InSeconds(),
            -1);
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_EQ(visits[1].context_annotations.duration_since_last_visit.InSeconds(),
            -1);
  EXPECT_EQ(visits[2].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[2].context_annotations.duration_since_last_visit.InSeconds(),
            -1);
  EXPECT_EQ(visits[3].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[3].context_annotations.duration_since_last_visit.InSeconds(),
            -1);

  helper_->OnUpdatedHistoryForNavigation(1, MakeTime(18),
                                         GURL{"https://google.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 4u);
  EXPECT_EQ(visits[0].context_annotations.duration_since_last_visit.InSeconds(),
            -1);
  EXPECT_EQ(visits[1].context_annotations.duration_since_last_visit.InSeconds(),
            -1);
  EXPECT_EQ(visits[2].context_annotations.duration_since_last_visit.InSeconds(),
            4);
  EXPECT_EQ(visits[3].context_annotations.duration_since_last_visit.InSeconds(),
            -1);

  DeleteContents();
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 4u);
  EXPECT_EQ(visits[0].context_annotations.duration_since_last_visit.InSeconds(),
            8);
  EXPECT_EQ(visits[1].context_annotations.duration_since_last_visit.InSeconds(),
            -1);
  EXPECT_EQ(visits[2].context_annotations.duration_since_last_visit.InSeconds(),
            4);
  EXPECT_EQ(visits[3].context_annotations.duration_since_last_visit.InSeconds(),
            -1);
}

// For the remaining tests, all navigations will have at least 1 history visit.

// History -> destroy -> history resolve
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked.
// 2) `WebContentsDestroyed()` is invoked before the previous history request is
//    resolved.
// Then: 0 context annotations should be committed.
TEST_F(HistoryClustersTabHelperTest, HistoryResolvedAfterDestroy) {
  AddToHistory(GURL{"https://github.com"}, MakeTime(1));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(1),
                                         GURL{"https://github.com"});
  helper_->OnOmniboxUrlCopied();
  DeleteContents();
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);

  // Validate that the omnibox-url-copied signal was lost.
  auto visits = GetVisits();
  ASSERT_EQ(visits.size(), 1u);
  EXPECT_FALSE(visits[0].context_annotations.omnibox_url_copied);
}

// History -> history -> history resolve -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked.
// 2) `OnUpdatedHistoryForNavigation()` is invoked before the previous history
//    request is resolved.
// 3) `WebContentsDestroyed()` is invoked.
// Then: 2 context annotations should be committed.
TEST_F(HistoryClustersTabHelperTest, HistoryResolvedAfter2ndNavigation) {
  AddToHistory(GURL{"https://google.com"}, MakeTime(1));
  AddToHistory(GURL{"https://github.com"}, MakeTime(2));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(1),
                                         GURL{"https://google.com"});
  helper_->OnUpdatedHistoryForNavigation(1, MakeTime(2),
                                         GURL{"https://github.com"});

  // Bookmarked after navigation ends, but before its history request resolved.
  AddBookmark(GURL{"https://google.com"});

  auto visits = GetVisits();
  ASSERT_EQ(visits.size(), 2u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_FALSE(visits[0].context_annotations.is_new_bookmark);
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_FALSE(visits[1].context_annotations.is_new_bookmark);

  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  DeleteContents();
  // Bookmarked after navigation ends and its history request resolved.
  AddBookmark(GURL{"https://github.com"});
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 2u);
  EXPECT_FALSE(visits[0].context_annotations.is_new_bookmark);
  EXPECT_TRUE(visits[1].context_annotations.is_new_bookmark);
}

// History -> copy -> history resolve -> history -> history -> copy -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked.
// 2) `OnOmniboxUrlCopied()` is invoked before the previous history request is
//    resolved.
// 3) `OnUpdatedHistoryForNavigation()` is invoked.
// 4) `OnUpdatedHistoryForNavigation()` is invoked.
// 5) `OnOmniboxUrlCopied()` is invoked after the previous history request is
//    resolved
// 6) `WebContentsDestroyed()` is invoked.
// Then: 3 context annotations should be committed; the 1st and 3rd should have
//       `omnibox_url_copied` true.
TEST_F(HistoryClustersTabHelperTest, UrlsCopied) {
  AddToHistory(GURL{"https://github.com"}, MakeTime(1));
  AddToHistory(GURL{"https://google.com"}, MakeTime(2));
  AddToHistory(GURL{"https://gmail.com"}, MakeTime(3));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(1),
                                         GURL{"https://github.com"});
  helper_->OnOmniboxUrlCopied();
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  auto visits = GetVisits();
  ASSERT_EQ(visits.size(), 3u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://gmail.com"});
  EXPECT_FALSE(visits[0].context_annotations.omnibox_url_copied);
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_FALSE(visits[1].context_annotations.omnibox_url_copied);
  EXPECT_EQ(visits[2].url_row.url(), GURL{"https://github.com"});
  EXPECT_FALSE(visits[2].context_annotations.omnibox_url_copied);

  helper_->OnUpdatedHistoryForNavigation(1, MakeTime(2),
                                         GURL{"https://google.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 3u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://gmail.com"});
  EXPECT_FALSE(visits[0].context_annotations.omnibox_url_copied);
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_FALSE(visits[1].context_annotations.omnibox_url_copied);
  EXPECT_EQ(visits[2].url_row.url(), GURL{"https://github.com"});
  EXPECT_TRUE(visits[2].context_annotations.omnibox_url_copied);

  helper_->OnUpdatedHistoryForNavigation(2, MakeTime(3),
                                         GURL{"https://gmail.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  helper_->OnOmniboxUrlCopied();
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 3u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://gmail.com"});
  EXPECT_FALSE(visits[0].context_annotations.omnibox_url_copied);
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_FALSE(visits[1].context_annotations.omnibox_url_copied);
  EXPECT_EQ(visits[2].url_row.url(), GURL{"https://github.com"});
  EXPECT_TRUE(visits[2].context_annotations.omnibox_url_copied);

  DeleteContents();
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 3u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://gmail.com"});
  EXPECT_TRUE(visits[0].context_annotations.omnibox_url_copied);
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_FALSE(visits[1].context_annotations.omnibox_url_copied);
  EXPECT_EQ(visits[2].url_row.url(), GURL{"https://github.com"});
  EXPECT_TRUE(visits[2].context_annotations.omnibox_url_copied);
}

// History -> expect UKM -> UKM -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked.
// 2) `TagNavigationAsExpectingUkmNavigationComplete()` is invoked.
// 3) `OnUkmNavigationComplete()` is invoked.
// 4) `WebContentsDestroyed()` is invoked.
// Then: 1 context annotations committed after step 3 w/ a `page_end_reason`.
TEST_F(HistoryClustersTabHelperTest, NavigationWithUkmBeforeDestroy) {
  AddToHistory(GURL{"https://github.com"}, MakeTime(1));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(1),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  helper_->TagNavigationAsExpectingUkmNavigationComplete(0);
  ASSERT_EQ(GetVisits().size(), 1u);
  EXPECT_EQ(GetVisits()[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(GetVisits()[0].context_annotations.page_end_reason, 0);
  helper_->OnUkmNavigationComplete(
      0,
      /*total_foreground_duration=*/base::Seconds(20),
      page_load_metrics::PageEndReason::END_OTHER);
  ASSERT_EQ(GetVisits().size(), 1u);
  EXPECT_EQ(GetVisits()[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(GetVisits()[0].context_annotations.page_end_reason,
            page_load_metrics::PageEndReason::END_OTHER);
  EXPECT_EQ(GetVisits()[0].context_annotations.total_foreground_duration,
            base::Seconds(20));
  DeleteContents();
}

// History -> expect UKM -> destroy -> UKM
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked.
// 2) `TagNavigationAsExpectingUkmNavigationComplete()` is invoked.
// 3) `WebContentsDestroyed()` is invoked.
// 4) `OnUkmNavigationComplete()` is invoked.
// Then: 1 context annotation should be committed after step 4 w/ a
//       `page_end_reason`.
TEST_F(HistoryClustersTabHelperTest, NavigationWithUkmAfterDestroy) {
  AddToHistory(GURL{"https://github.com"}, MakeTime(1));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(1),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  helper_->TagNavigationAsExpectingUkmNavigationComplete(0);

  // Invoke `OnUkmNavigationComplete()` after `WebContentsDestroyed()` is
  // invoked, but before the `WebContents` has been destroyed.
  ASSERT_EQ(GetVisits().size(), 1u);
  EXPECT_EQ(GetVisits()[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(GetVisits()[0].context_annotations.page_end_reason, 0);
  OnDestroyWebContentsObserver test_web_contents_observer(
      web_contents(), base::BindLambdaForTesting([&]() {
        EXPECT_EQ(GetVisits()[0].context_annotations.page_end_reason, 0);
        helper_->OnUkmNavigationComplete(
            0, base::Seconds(20), page_load_metrics::PageEndReason::END_OTHER);
        run_loop_quit_.Run();
      }));

  DeleteContents();
  run_loop_.Run();
  ASSERT_EQ(GetVisits().size(), 1u);
  EXPECT_EQ(GetVisits()[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(GetVisits()[0].context_annotations.page_end_reason,
            page_load_metrics::PageEndReason::END_OTHER);
  EXPECT_EQ(GetVisits()[0].context_annotations.total_foreground_duration,
            base::Seconds(20));
}

// Expect UKM -> history -> UKM -> destroy
// When:
// 1) `TagNavigationAsExpectingUkmNavigationComplete()` is invoked.
// 2) `OnUpdatedHistoryForNavigation()` is invoked.
// 3) `OnUkmNavigationComplete()` is invoked.
// 4) `WebContentsDestroyed()` is invoked.
// Then: 1 context annotation should be committed after step 3 w/ a
//       `page_end_reason`.
TEST_F(HistoryClustersTabHelperTest,
       NavigationAfterUkmExpectAndWithUkmBeforeDestroy) {
  AddToHistory(GURL{"https://github.com"}, MakeTime(1));
  helper_->TagNavigationAsExpectingUkmNavigationComplete(0);
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(1),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  ASSERT_EQ(GetVisits().size(), 1u);
  EXPECT_EQ(GetVisits()[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(GetVisits()[0].context_annotations.page_end_reason, 0);
  helper_->OnUkmNavigationComplete(0, base::Seconds(20),
                                   page_load_metrics::PageEndReason::END_OTHER);
  ASSERT_EQ(GetVisits().size(), 1u);
  EXPECT_EQ(GetVisits()[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(GetVisits()[0].context_annotations.page_end_reason,
            page_load_metrics::PageEndReason::END_OTHER);
  EXPECT_EQ(GetVisits()[0].context_annotations.total_foreground_duration,
            base::Seconds(20));
  DeleteContents();
}

// History -> expect UKM -> UKM -> destroy -> history resolve
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked.
// 2) `TagNavigationAsExpectingUkmNavigationComplete()` is invoked.
// 3) `OnUkmNavigationComplete()` is invoked.
// 4) `WebContentsDestroyed()` is invoked before the previous history request is
//    resolved.
// Then: 1 context annotation should be committed after step 4 w/ a
// `page_end_reason`.
TEST_F(HistoryClustersTabHelperTest,
       NavigationWithUkmBeforeDestroyAndHistoryResolvedAfterDestroy) {
  AddToHistory(GURL{"https://github.com"}, MakeTime(1));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(1),
                                         GURL{"https://github.com"});
  helper_->TagNavigationAsExpectingUkmNavigationComplete(0);
  AddBookmark(GURL{"https://github.com"});
  helper_->OnUkmNavigationComplete(0, base::Seconds(20),
                                   page_load_metrics::PageEndReason::END_OTHER);
  auto visits = GetVisits();
  ASSERT_EQ(visits.size(), 1u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_FALSE(visits[0].context_annotations.is_new_bookmark);
  EXPECT_EQ(visits[0].context_annotations.page_end_reason, 0);
  EXPECT_EQ(visits[0].context_annotations.total_foreground_duration,
            base::Seconds(-1));

  // Resolve the history request after `WebContentsDestroyed()` is invoked, but
  // before the `WebContents` has been destroyed.
  OnDestroyWebContentsObserver test_web_contents_observer(
      web_contents(), base::BindLambdaForTesting([&]() {
        history::BlockUntilHistoryProcessesPendingRequests(history_service_);
        run_loop_quit_.Run();
      }));

  DeleteContents();
  run_loop_.Run();
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 1u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_TRUE(visits[0].context_annotations.is_new_bookmark);
  EXPECT_EQ(visits[0].context_annotations.page_end_reason,
            page_load_metrics::PageEndReason::END_OTHER);
  EXPECT_EQ(visits[0].context_annotations.total_foreground_duration,
            base::Seconds(20));
}

// Expect History -> expect UKM 1 -> UKM 1 -> history -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked.
// 2) `TagNavigationAsExpectingUkmNavigationComplete()` is invoked for the above
//    navigation.
// 3) `OnUkmNavigationComplete()` is invoked for the above navigation.
// 4) `OnUpdatedHistoryForNavigation()` is invoked.
// 5) `WebContentsDestroyed()` is invoked.
// Then: 2 context annotations should be committed after steps 3 and 5; the 1st
//.      should have a `page_end_reason`.
TEST_F(HistoryClustersTabHelperTest,
       TwoNavigationsWith1stUkmBefore2ndNavigation) {
  AddToHistory(GURL{"https://google.com"}, MakeTime(1));
  AddToHistory(GURL{"https://github.com"}, MakeTime(2));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(2),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  helper_->TagNavigationAsExpectingUkmNavigationComplete(0);

  auto visits = GetVisits();
  ASSERT_EQ(visits.size(), 2u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[0].context_annotations.page_end_reason, 0);
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_EQ(visits[1].context_annotations.page_end_reason, 0);
  helper_->OnUkmNavigationComplete(0, base::Seconds(20),
                                   page_load_metrics::PageEndReason::END_OTHER);

  helper_->OnUpdatedHistoryForNavigation(1, MakeTime(1),
                                         GURL{"https://google.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);

  DeleteContents();
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 2u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[0].context_annotations.page_end_reason,
            page_load_metrics::PageEndReason::END_OTHER);
  EXPECT_EQ(visits[0].context_annotations.total_foreground_duration,
            base::Seconds(20));
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_EQ(visits[1].context_annotations.page_end_reason, 0);
}

// Expect History -> Expect UKM 1 -> history -> UKM 1 -> destroy
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked.
// 2) `TagNavigationAsExpectingUkmNavigationComplete()` is invoked for the above
//    navigation.
// 3) `OnUpdatedHistoryForNavigation()` is invoked.
// 4) `OnUkmNavigationComplete()` is invoked for the 1st navigation.
// 5) `WebContentsDestroyed()` is invoked.
// Then: 2 context annotations should be committed after steps 4 and 5; the 1st
//.      should have a `page_end_reason`.
TEST_F(HistoryClustersTabHelperTest,
       TwoNavigationsWith1stUkmAfter2ndNavigation) {
  AddToHistory(GURL{"https://google.com"}, MakeTime(1));
  AddToHistory(GURL{"https://github.com"}, MakeTime(2));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(2),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  helper_->TagNavigationAsExpectingUkmNavigationComplete(0);
  helper_->OnUpdatedHistoryForNavigation(1, MakeTime(1),
                                         GURL{"https://google.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  auto visits = GetVisits();
  ASSERT_EQ(visits.size(), 2u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[0].context_annotations.page_end_reason, 0);
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_EQ(visits[1].context_annotations.page_end_reason, 0);

  helper_->OnUkmNavigationComplete(0, base::Seconds(20),
                                   page_load_metrics::PageEndReason::END_OTHER);
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 2u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[0].context_annotations.page_end_reason,
            page_load_metrics::PageEndReason::END_OTHER);
  EXPECT_EQ(visits[0].context_annotations.total_foreground_duration,
            base::Seconds(20));
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_EQ(visits[1].context_annotations.page_end_reason, 0);
  EXPECT_EQ(visits[1].context_annotations.total_foreground_duration,
            base::Seconds(-1));

  DeleteContents();
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 2u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[0].context_annotations.page_end_reason,
            page_load_metrics::PageEndReason::END_OTHER);
  EXPECT_EQ(visits[0].context_annotations.total_foreground_duration,
            base::Seconds(20));
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_EQ(visits[1].context_annotations.page_end_reason, 0);
  EXPECT_EQ(visits[1].context_annotations.total_foreground_duration,
            base::Seconds(-1));
}

// Expect History -> Expect UKM 2 -> history -> destroy -> UKM 2
// When:
// 1) `OnUpdatedHistoryForNavigation()` is invoked.
// 2) `TagNavigationAsExpectingUkmNavigationComplete()` is invoked for the below
//    navigation.
// 3) `OnUpdatedHistoryForNavigation()` is invoked.
// 4) `WebContentsDestroyed()` is invoked.
// 5) `OnUkmNavigationComplete()` is invoked for the 2nd navigation.
// Then: 2 context annotations should be committed after steps 2 and 5; the 2nd
//.      should have a `page_end_reason`.
TEST_F(HistoryClustersTabHelperTest, TwoNavigations2ndUkmBefore2ndNavigation) {
  AddToHistory(GURL{"https://google.com"}, MakeTime(1));
  AddToHistory(GURL{"https://github.com"}, MakeTime(2));
  helper_->OnUpdatedHistoryForNavigation(0, MakeTime(2),
                                         GURL{"https://github.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);
  auto visits = GetVisits();
  ASSERT_EQ(visits.size(), 2u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[0].context_annotations.page_end_reason, 0);
  EXPECT_EQ(visits[0].context_annotations.total_foreground_duration,
            base::Seconds(-1));
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_EQ(visits[1].context_annotations.page_end_reason, 0);
  EXPECT_EQ(visits[1].context_annotations.total_foreground_duration,
            base::Seconds(-1));

  helper_->TagNavigationAsExpectingUkmNavigationComplete(1);
  EXPECT_EQ(GetVisits().size(), 2u);

  helper_->OnUpdatedHistoryForNavigation(1, MakeTime(1),
                                         GURL{"https://google.com"});
  history::BlockUntilHistoryProcessesPendingRequests(history_service_);

  // Invoke `OnUkmNavigationComplete()` after `WebContentsDestroyed()` is
  // invoked, but before the `WebContents` has been destroyed.
  EXPECT_EQ(GetVisits().size(), 2u);
  OnDestroyWebContentsObserver test_web_contents_observer(
      web_contents(), base::BindLambdaForTesting([&]() {
        EXPECT_EQ(GetVisits().size(), 2u);
        helper_->OnUkmNavigationComplete(
            1, base::Seconds(20), page_load_metrics::PageEndReason::END_OTHER);
        run_loop_quit_.Run();
      }));

  DeleteContents();
  visits = GetVisits();
  ASSERT_EQ(visits.size(), 2u);
  EXPECT_EQ(visits[0].url_row.url(), GURL{"https://github.com"});
  EXPECT_EQ(visits[0].context_annotations.page_end_reason, 0);
  EXPECT_EQ(visits[0].context_annotations.total_foreground_duration,
            base::Seconds(-1));
  EXPECT_EQ(visits[1].url_row.url(), GURL{"https://google.com"});
  EXPECT_EQ(visits[1].context_annotations.page_end_reason,
            page_load_metrics::PageEndReason::END_OTHER);
  EXPECT_EQ(visits[1].context_annotations.total_foreground_duration,
            base::Seconds(20));
}

}  // namespace