File: most_visited_sites_provider_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 (1034 lines) | stat: -rw-r--r-- 43,069 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
// Copyright 2020 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/omnibox/browser/most_visited_sites_provider.h"

#include <list>
#include <memory>
#include <string>

#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/history/core/browser/features.h"
#include "components/history/core/browser/top_sites.h"
#include "components/history/core/browser/top_sites_impl.h"
#include "components/omnibox/browser/autocomplete_enums.h"
#include "components/omnibox/browser/autocomplete_provider_listener.h"
#include "components/omnibox/browser/fake_autocomplete_provider_client.h"
#include "components/omnibox/browser/suggestion_group_util.h"
#include "components/omnibox/browser/tab_matcher.h"
#include "components/omnibox/browser/test_scheme_classifier.h"
#include "components/omnibox/common/omnibox_feature_configs.h"
#include "components/omnibox/common/omnibox_features.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/omnibox_event.pb.h"
#include "third_party/metrics_proto/omnibox_focus_type.pb.h"
#include "ui/base/device_form_factor.h"

namespace {
struct TestData {
  bool is_search;
  history::MostVisitedURL entry;
};

using OEP = metrics::OmniboxEventProto;
using OFT = metrics::OmniboxFocusType;
using testing::_;

class FakeTopSites : public history::TopSites {
 public:
  FakeTopSites() = default;

  // history::TopSites:
  void GetMostVisitedURLs(GetMostVisitedURLsCallback callback) override {
    callbacks_.push_back(std::move(callback));
  }
  void SyncWithHistory() override {}

  bool HasBlockedUrls() const override { return !blocked_urls_.empty(); }
  void AddBlockedUrl(const GURL& url) override {
    blocked_urls_.insert(url.spec());
  }
  void RemoveBlockedUrl(const GURL& url) override {
    blocked_urls_.erase(url.spec());
  }
  bool IsBlocked(const GURL& url) override {
    return blocked_urls_.count(url.spec()) > 0;
  }
  void ClearBlockedUrls() override { blocked_urls_.clear(); }
  bool IsFull() override { return false; }
  bool loaded() const override { return false; }
  history::PrepopulatedPageList GetPrepopulatedPages() override {
    return history::PrepopulatedPageList();
  }
  void OnNavigationCommitted(const GURL& url) override {}
  int NumBlockedSites() const override { return blocked_urls_.size(); }

  // RefcountedKeyedService:
  void ShutdownOnUIThread() override {}

  // Only runs a single callback, so that the test can specify a different
  // set per call.
  // Returns true if there was a recipient to receive the URLs and the list was
  // emitted, otherwise returns false.
  bool EmitURLs(const std::vector<TestData>& data) {
    if (callbacks_.empty())
      return false;

    history::MostVisitedURLList urls;
    for (const auto& test_element : data) {
      urls.push_back(test_element.entry);
    }

    std::move(callbacks_.front()).Run(std::move(urls));
    callbacks_.pop_front();
    return true;
  }

  const std::set<std::string>& blocked_urls() const { return blocked_urls_; }

 protected:
  // A test-specific field for controlling when most visited callback is run
  // after top sites have been requested.
  std::list<GetMostVisitedURLsCallback> callbacks_;
  std::set<std::string> blocked_urls_;

  ~FakeTopSites() override = default;
};

constexpr const auto* WEB_URL = u"https://example.com/";

enum class ExpectedUiType { kAggregateMatch, kIndividualTiles };

const std::vector<TestData> DefaultTestData() {
  return {{false, {GURL("http://www.a.art/"), u"A art"}},
          {false, {GURL("http://www.b.biz/"), u"B biz"}},
          {false, {GURL("http://www.c.com/"), u"C com"}},
          {false, {GURL("http://www.d.de/"), u"D de"}},
          {true, {GURL("http://www.google.com/search?q=abc"), u"abc"}}};
}

class MockHistoryService : public history::HistoryService {
 public:
  MockHistoryService() = default;
  ~MockHistoryService() override = default;

  MOCK_METHOD(base::CancelableTaskTracker::TaskId,
              QueryMostVisitedURLs,
              (int result_count,
               history::HistoryService::QueryMostVisitedURLsCallback callback,
               base::CancelableTaskTracker* tracker,
               const std::optional<std::string>& recency_factor_name,
               std::optional<size_t> recency_window_days,
               bool check_visual_deduplication_flag),
              (override));
  MOCK_METHOD(void, DeleteURLs, (const std::vector<GURL>& urls), (override));
};

}  // namespace

class MostVisitedSitesProviderTest : public testing::Test,
                                     public AutocompleteProviderListener {
 public:
  void SetUp() override;

 protected:
  // Construct AutocompleteInput object a hypothetical Omnibox session context.
  // Does not run any validation on the supplied values, allowing any
  // combination (including invalid ones) to be used to create AutocompleteInput
  // context object.
  AutocompleteInput BuildAutocompleteInput(
      const std::u16string& input_url,
      const std::u16string& current_url,
      metrics::OmniboxEventProto::PageClassification page_class,
      metrics::OmniboxFocusType focus_type) {
    AutocompleteInput input(input_url, page_class, TestSchemeClassifier());
    input.set_focus_type(focus_type);
    input.set_current_url(GURL(current_url));
    return input;
  }

  // Helper method, constructing a valid AutocompleteInput object for a website
  // visit.
  AutocompleteInput BuildAutocompleteInputForWebOnFocus() {
    return BuildAutocompleteInput(WEB_URL, WEB_URL,
                                  metrics::OmniboxEventProto::OTHER,
                                  metrics::OmniboxFocusType::INTERACTION_FOCUS);
  }

  // A prefetch AutocompleteInput on Web.
  AutocompleteInput BuildAutocompletePrefetchInputForWeb() {
    return BuildAutocompleteInput(
        WEB_URL, WEB_URL, metrics::OmniboxEventProto::OTHER_ZPS_PREFETCH,
        metrics::OmniboxFocusType::INTERACTION_FOCUS);
  }

  // Iterate over all matches offered by the Provider and verify these against
  // the supplied list of History URLs.
  void CheckMatchesEquivalentTo(const std::vector<TestData>& data,
                                ExpectedUiType ui_type);

  // Iterate over all matches offered by the Provider and verify these against
  // the supplied list of History URLs for desktop.
  void CheckDesktopMatchesEquivalentTo(const std::vector<TestData>& data,
                                       size_t url_limit);

  // Returns total number of all NAVSUGGEST and TILE_NAVSUGGEST elements.
  size_t NumMostVisitedMatches();

  // Returns the N-th match of a particular type, skipping over all matches of
  // other types. If match of that type does not exist, or there are not enough
  // elements of that type, this call returns null.
  const AutocompleteMatch* GetMatch(AutocompleteMatchType::Type type,
                                    size_t index);

  // AutocompleteProviderListener:
  void OnProviderUpdate(bool updated_matches,
                        const AutocompleteProvider* provider) override;

  base::HistogramTester histogram_;
  std::unique_ptr<base::test::SingleThreadTaskEnvironment> task_environment_;
  FakeAutocompleteProviderClient client_;
  scoped_refptr<FakeTopSites> top_sites_;
  scoped_refptr<MostVisitedSitesProvider> provider_;
  int provider_update_count_{};
};

size_t MostVisitedSitesProviderTest::NumMostVisitedMatches() {
  const auto& result = provider_->matches();
  size_t count = 0;
  for (const auto& match : result) {
    if ((match.type == AutocompleteMatchType::TILE_NAVSUGGEST) ||
        (match.type == AutocompleteMatchType::NAVSUGGEST) ||
        (match.type == AutocompleteMatchType::TILE_MOST_VISITED_SITE) ||
        (match.type == AutocompleteMatchType::TILE_REPEATABLE_QUERY)) {
      ++count;
    }
  }
  return count;
}

const AutocompleteMatch* MostVisitedSitesProviderTest::GetMatch(
    AutocompleteMatchType::Type type,
    size_t index) {
  const auto& result = provider_->matches();
  for (const auto& match : result) {
    if (match.type == type) {
      if (!index)
        return &match;
      --index;
    }
  }
  return nullptr;
}

void MostVisitedSitesProviderTest::CheckMatchesEquivalentTo(
    const std::vector<TestData>& data,
    ExpectedUiType ui_type) {
  // Compare the AutocompleteResult against a set of URLs that we expect to see.
  // Note that additional matches may be offered if other providers are also
  // registered in the same category as MostVisitedSitesProvider.
  // We ignore all matches that are not ours.
  const auto& result = provider_->matches();

  size_t match_index = 0;

  if (ui_type == ExpectedUiType::kAggregateMatch) {
    ASSERT_EQ(1ul, NumMostVisitedMatches())
        << "Expected only one TILE_NAVSUGGEST match";
    for (const auto& match : result) {
      if (match.type != AutocompleteMatchType::TILE_NAVSUGGEST)
        continue;
      EXPECT_TRUE(match.subtypes.contains(
          omnibox::SUBTYPE_ZERO_PREFIX_LOCAL_FREQUENT_URLS));
      EXPECT_TRUE(match.subtypes.contains(omnibox::SUBTYPE_URL_BASED));
      const auto& tiles = match.suggest_tiles;
      ASSERT_EQ(data.size(), tiles.size()) << "Wrong number of tiles reported";
      for (size_t index = 0u; index < data.size(); index++) {
        EXPECT_EQ(data[index].entry.url, tiles[index].url)
            << "Invalid Tile URL at position " << index;
        EXPECT_EQ(data[index].entry.title, tiles[index].title)
            << "Invalid Tile Title at position " << index;
      }
      break;
    }
  } else if (ui_type == ExpectedUiType::kIndividualTiles) {
    ASSERT_EQ(data.size(), NumMostVisitedMatches())
        << "Unexpected number of TILE matches";
    int expected_relevance = omnibox::kMostVisitedTilesZeroSuggestHighRelevance;
    for (const auto& match : result) {
      if (data[match_index].is_search) {
        EXPECT_EQ(match.type, AutocompleteMatchType::TILE_REPEATABLE_QUERY);
        EXPECT_TRUE(match.subtypes.contains(
            omnibox::SUBTYPE_ZERO_PREFIX_LOCAL_FREQUENT_QUERIES));
      } else {
        EXPECT_EQ(match.type, AutocompleteMatchType::TILE_MOST_VISITED_SITE);
        EXPECT_TRUE(match.subtypes.contains(
            omnibox::SUBTYPE_ZERO_PREFIX_LOCAL_FREQUENT_URLS));
        EXPECT_TRUE(match.subtypes.contains(omnibox::SUBTYPE_URL_BASED));
      }

      EXPECT_EQ(data[match_index].entry.url, match.destination_url)
          << "Invalid Match URL at position " << match_index;
      EXPECT_EQ(data[match_index].entry.title, match.description)
          << "Invalid Match Title at position " << match_index;
      EXPECT_EQ(expected_relevance, match.relevance)
          << "Invalid Match Relevance at position " << match_index;
      ++match_index;
      --expected_relevance;
    }
  }
}

void MostVisitedSitesProviderTest::CheckDesktopMatchesEquivalentTo(
    const std::vector<TestData>& data,
    size_t url_limit) {
  // Compare the AutocompleteResult against a set of URLs that we expect to see.
  // Note that additional matches may be offered if other providers are also
  // registered in the same category as MostVisitedSitesProvider.
  // We ignore all matches that are not ours.
  const auto& result = provider_->matches();

  size_t match_index = 0;
  ASSERT_EQ(url_limit, NumMostVisitedMatches())
      << "Unexpected number of TILE matches";
  int expected_relevance = omnibox::kMostVisitedTilesZeroSuggestHighRelevance;
  for (const auto& match : result) {
    EXPECT_EQ(match.type, AutocompleteMatchType::TILE_MOST_VISITED_SITE);
    EXPECT_TRUE(match.subtypes.contains(
        omnibox::SUBTYPE_ZERO_PREFIX_LOCAL_FREQUENT_URLS));
    EXPECT_TRUE(match.subtypes.contains(omnibox::SUBTYPE_URL_BASED));

    EXPECT_EQ(data[match_index].entry.url, match.destination_url)
        << "Invalid Match URL at position " << match_index;
    EXPECT_EQ(data[match_index].entry.title, match.description)
        << "Invalid Match Title at position " << match_index;
    EXPECT_EQ(expected_relevance, match.relevance)
        << "Invalid Match Relevance at position " << match_index;
    EXPECT_EQ(omnibox::GROUP_MOST_VISITED, match.suggestion_group_id);
    ++match_index;
    --expected_relevance;
  }
}

void MostVisitedSitesProviderTest::SetUp() {
  task_environment_ =
      std::make_unique<base::test::SingleThreadTaskEnvironment>();
  top_sites_ = new FakeTopSites();

  client_.set_top_sites(top_sites_);

  // For tests requiring direct interaction with the Provider.
  provider_ = new MostVisitedSitesProvider(&client_, this);
}

void MostVisitedSitesProviderTest::OnProviderUpdate(
    bool updated_matches,
    const AutocompleteProvider* provider) {
  provider_update_count_++;
}

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)

constexpr const auto* SRP_URL = u"https://www.google.com/?q=flowers";
constexpr const auto* FTP_URL = u"ftp://just.for.filtering.com";

TEST_F(MostVisitedSitesProviderTest, TestMostVisitedCallback) {
  base::test::ScopedFeatureList features;
  features.InitAndDisableFeature(
      omnibox::kMostVisitedTilesHorizontalRenderGroup);
  auto input = BuildAutocompleteInputForWebOnFocus();
  provider_->Start(input, true);
  EXPECT_EQ(0u, NumMostVisitedMatches());
  auto test_data = DefaultTestData();
  EXPECT_TRUE(top_sites_->EmitURLs(test_data));
  CheckMatchesEquivalentTo(test_data, ExpectedUiType::kAggregateMatch);
  EXPECT_EQ(1, provider_update_count_);
  provider_->Stop(AutocompleteStopReason::kClobbered);

  // Observe that subsequent request does not return stale data.
  provider_->Start(input, true);
  provider_->Stop(AutocompleteStopReason::kClobbered);
  // Since this provider's async logic is still in-flight (`EmitURLs()` has not
  // been called yet), we should not be reporting anything from past runs.
  EXPECT_EQ(0ul, NumMostVisitedMatches());
  EXPECT_EQ(1, provider_update_count_);

  // Most visited results arriving after Stop() has been called, ensure they
  // are not displayed.
  std::vector<TestData> new_urls{{
      false,
      {GURL("http://www.g.gov/"), u"G gov"},
  }};
  EXPECT_TRUE(top_sites_->EmitURLs(new_urls));
  EXPECT_EQ(0ul, NumMostVisitedMatches());
  EXPECT_EQ(1, provider_update_count_);

  provider_->Start(input, true);
  provider_->Stop(AutocompleteStopReason::kClobbered);
  provider_->Start(input, true);

  // Stale results (reported for the first of the two Start() requests) should
  // be rejected.
  EXPECT_TRUE(top_sites_->EmitURLs(DefaultTestData()));
  EXPECT_EQ(0ul, NumMostVisitedMatches());
  EXPECT_EQ(1, provider_update_count_);

  // Results for the second Start() action should be recorded.
  EXPECT_TRUE(top_sites_->EmitURLs(test_data));
  CheckMatchesEquivalentTo(test_data, ExpectedUiType::kAggregateMatch);
  EXPECT_EQ(2, provider_update_count_);
  provider_->Stop(AutocompleteStopReason::kClobbered);
}

TEST_F(MostVisitedSitesProviderTest, TestMostVisitedNavigateToSearchPage) {
  provider_->Start(BuildAutocompleteInputForWebOnFocus(), true);
  EXPECT_EQ(0u, NumMostVisitedMatches());
  // Stop() doesn't always get called.

  auto srp_input = BuildAutocompleteInput(
      SRP_URL, SRP_URL,
      metrics::OmniboxEventProto::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT,
      metrics::OmniboxFocusType::INTERACTION_FOCUS);

  provider_->Start(srp_input, true);
  EXPECT_EQ(0u, NumMostVisitedMatches());

  // Most visited results arriving after a new request has been started.
  EXPECT_TRUE(top_sites_->EmitURLs(DefaultTestData()));
  EXPECT_EQ(0u, NumMostVisitedMatches());
}

TEST_F(MostVisitedSitesProviderTest, AllowMostVisitedSitesSuggestions) {
  // MostVisited should never deal with prefix suggestions.
  EXPECT_FALSE(provider_->AllowMostVisitedSitesSuggestions(
      &client_, BuildAutocompleteInput(WEB_URL, WEB_URL, OEP::OTHER,
                                       OFT::INTERACTION_DEFAULT)));

  // This should always be true, as otherwise we will break MostVisited.
  EXPECT_TRUE(provider_->AllowMostVisitedSitesSuggestions(
      &client_, BuildAutocompleteInput(WEB_URL, WEB_URL, OEP::OTHER,
                                       OFT::INTERACTION_FOCUS)));

  // Verifies that non-permitted schemes are rejected.
  EXPECT_FALSE(provider_->AllowMostVisitedSitesSuggestions(
      &client_, BuildAutocompleteInput(FTP_URL, FTP_URL, OEP::OTHER,
                                       OFT::INTERACTION_FOCUS)));

  // Offer MV sites when the User is visiting a website and deletes text.
  EXPECT_TRUE(provider_->AllowMostVisitedSitesSuggestions(
      &client_, BuildAutocompleteInput(WEB_URL, WEB_URL, OEP::OTHER,
                                       OFT::INTERACTION_FOCUS)));
}

TEST_F(MostVisitedSitesProviderTest, NoSRPCoverage) {
  EXPECT_FALSE(provider_->AllowMostVisitedSitesSuggestions(
      &client_,
      BuildAutocompleteInput(WEB_URL, WEB_URL,
                             OEP::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT,
                             OFT::INTERACTION_FOCUS)));
}

TEST_F(MostVisitedSitesProviderTest, TestCreateMostVisitedMatch) {
  base::test::ScopedFeatureList features;
  features.InitAndDisableFeature(
      omnibox::kMostVisitedTilesHorizontalRenderGroup);
  provider_->Start(BuildAutocompleteInputForWebOnFocus(), true);
  EXPECT_EQ(0u, NumMostVisitedMatches());
  // Accept only direct TopSites data.
  auto test_data = DefaultTestData();
  EXPECT_TRUE(top_sites_->EmitURLs(test_data));
  CheckMatchesEquivalentTo(test_data, ExpectedUiType::kAggregateMatch);
}

TEST_F(MostVisitedSitesProviderTest, NoMatchesWhenNoMostVisitedSites) {
  // Start with no URLs.
  provider_->Start(BuildAutocompleteInputForWebOnFocus(), true);
  EXPECT_EQ(0u, NumMostVisitedMatches());
  // Accept only direct TopSites data, confirm no matches are built.
  EXPECT_TRUE(top_sites_->EmitURLs({}));
  EXPECT_EQ(0u, NumMostVisitedMatches());
}

TEST_F(MostVisitedSitesProviderTest,
       NoMatchesWhenTopSitesNotLoadedAndWantAsyncMatchesFalse) {
  // Assume that top sites list has not been loaded yet from the DB.
  ASSERT_FALSE(top_sites_->loaded());
  auto input = BuildAutocompleteInputForWebOnFocus();
  input.set_focus_type(metrics::OmniboxFocusType::INTERACTION_DEFAULT);
  input.set_omit_asynchronous_matches(true);
  provider_->Start(input, true);
  EXPECT_TRUE(provider_->done());
  EXPECT_EQ(0u, NumMostVisitedMatches());
  // No callbacks should have been added due to early return.
  EXPECT_FALSE(top_sites_->EmitURLs(DefaultTestData()));
  EXPECT_EQ(0u, NumMostVisitedMatches());
}

TEST_F(MostVisitedSitesProviderTest, TestDeleteMostVisitedElement) {
  base::test::ScopedFeatureList features;
  features.InitAndDisableFeature(
      omnibox::kMostVisitedTilesHorizontalRenderGroup);
  // Make a copy (intentional - we'll modify this later)
  provider_->Start(BuildAutocompleteInputForWebOnFocus(), true);
  // Accept only direct TopSites data.
  auto test_data = DefaultTestData();
  EXPECT_TRUE(top_sites_->EmitURLs(test_data));
  CheckMatchesEquivalentTo(test_data, ExpectedUiType::kAggregateMatch);

  // Commence delete.
  histogram_.ExpectTotalCount("Omnibox.SuggestTiles.TileTypeCount.Search", 1);
  histogram_.ExpectBucketCount("Omnibox.SuggestTiles.TileTypeCount.Search", 1,
                               1);
  histogram_.ExpectTotalCount("Omnibox.SuggestTiles.TileTypeCount.URL", 1);
  histogram_.ExpectBucketCount("Omnibox.SuggestTiles.TileTypeCount.URL", 4, 1);
  histogram_.ExpectTotalCount("Omnibox.SuggestTiles.DeletedTileIndex", 0);
  auto* match = GetMatch(AutocompleteMatchType::TILE_NAVSUGGEST, 0);
  ASSERT_NE(nullptr, match) << "No TILE_NAVSUGGEST Match found";
  provider_->DeleteMatchElement(*match, 1);
  histogram_.ExpectTotalCount("Omnibox.SuggestTiles.DeletedTileIndex", 1);
  histogram_.ExpectBucketCount("Omnibox.SuggestTiles.DeletedTileIndex", 1, 1);
  // Note: TileTypeCounts are not emitted after deletion.

  // Observe that the URL is now blocked and removed from suggestion.
  auto deleted_url = test_data[1].entry.url;
  test_data.erase(test_data.begin() + 1);
  CheckMatchesEquivalentTo(test_data, ExpectedUiType::kAggregateMatch);
  EXPECT_TRUE(top_sites_->IsBlocked(deleted_url));
}

TEST_F(MostVisitedSitesProviderTest, NoMatchesWhenLastURLIsDeleted) {
  base::test::ScopedFeatureList features;
  features.InitAndDisableFeature(
      omnibox::kMostVisitedTilesHorizontalRenderGroup);

  // Start with just one URL.
  std::vector<TestData> urls{{
      {false, {GURL("http://www.a.art/"), u"A art"}},
  }};

  provider_->Start(BuildAutocompleteInputForWebOnFocus(), true);
  EXPECT_TRUE(top_sites_->EmitURLs(urls));
  CheckMatchesEquivalentTo(urls, ExpectedUiType::kAggregateMatch);

  // Commence delete of the only item that we have.
  histogram_.ExpectTotalCount("Omnibox.SuggestTiles.TileTypeCount.Search", 1);
  histogram_.ExpectBucketCount("Omnibox.SuggestTiles.TileTypeCount.Search", 0,
                               1);
  histogram_.ExpectTotalCount("Omnibox.SuggestTiles.TileTypeCount.URL", 1);
  histogram_.ExpectBucketCount("Omnibox.SuggestTiles.TileTypeCount.URL", 1, 1);
  histogram_.ExpectTotalCount("Omnibox.SuggestTiles.DeletedTileIndex", 0);
  auto* match = GetMatch(AutocompleteMatchType::TILE_NAVSUGGEST, 0);
  ASSERT_NE(nullptr, match) << "No TILE_NAVSUGGEST Match found";
  provider_->DeleteMatchElement(*match, 0);
  histogram_.ExpectTotalCount("Omnibox.SuggestTiles.DeletedTileIndex", 1);
  histogram_.ExpectBucketCount("Omnibox.SuggestTiles.DeletedTileIndex", 0, 1);
  // Note: TileTypeCounts are not emitted after deletion.

  // Confirm no more NAVSUGGEST matches are offered.
  EXPECT_EQ(0u, NumMostVisitedMatches());
}

TEST_F(MostVisitedSitesProviderTest,
       TestCreateMostVisitedHorizontalGroupTiles) {
  base::test::ScopedFeatureList features;
  features.InitWithFeatures({omnibox::kMostVisitedTilesHorizontalRenderGroup},
                            {});

  provider_->Start(BuildAutocompleteInputForWebOnFocus(), true);
  EXPECT_EQ(0u, NumMostVisitedMatches());
  // Accept only direct TopSites data.
  auto test_data = DefaultTestData();
  EXPECT_TRUE(top_sites_->EmitURLs(test_data));
  CheckMatchesEquivalentTo(test_data, ExpectedUiType::kIndividualTiles);
}

#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)

#if !(BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS))
TEST_F(MostVisitedSitesProviderTest, TestCreateMostVisitedTopSitesMatches) {
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;
  scoped_config.Get().directly_query_history_service = false;
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxZpsSuggestionLimit>
      suggestion_limit_scoped_config;
  suggestion_limit_scoped_config.Get().enabled = true;
  suggestion_limit_scoped_config.Get().max_suggestions = 8U;
  suggestion_limit_scoped_config.Get().max_url_suggestions = 4U;
  suggestion_limit_scoped_config.Get().max_search_suggestions = 4U;

  provider_->Start(BuildAutocompleteInputForWebOnFocus(), true);

  EXPECT_EQ(0u, NumMostVisitedMatches());
  // Accept only direct TopSites data.
  auto test_data = DefaultTestData();
  EXPECT_TRUE(top_sites_->EmitURLs(test_data));
  CheckDesktopMatchesEquivalentTo(
      test_data, suggestion_limit_scoped_config.Get().max_url_suggestions);
}

TEST_F(MostVisitedSitesProviderTest, DesktopProviderDoesNotAllowChromeSites) {
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;

  const auto* chrome_url = u"chrome://omnibox";

  // Verifies that non-permitted schemes are rejected.
  EXPECT_TRUE(provider_->AllowMostVisitedSitesSuggestions(
      &client_, BuildAutocompleteInputForWebOnFocus()));

  // Verifies that non-permitted schemes are rejected.
  EXPECT_FALSE(provider_->AllowMostVisitedSitesSuggestions(
      &client_, BuildAutocompleteInput(chrome_url, chrome_url, OEP::OTHER,
                                       OFT::INTERACTION_FOCUS)));
}

TEST_F(MostVisitedSitesProviderTest, TestDesktopQueryingHistoryService) {
  // Set a MockHistoryService.
  auto history_service = std::make_unique<MockHistoryService>();
  auto& history_service_ref = *history_service;
  client_.set_history_service(std::move(history_service));
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;
  scoped_config.Get().most_visited_recency_window = 4;
  scoped_config.Get().most_visited_recency_factor =
      history::kMvtScoringParamRecencyFactor_Classic;
  scoped_config.Get().prefetch_most_visited_sites = false;
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxZpsSuggestionLimit>
      suggestion_limit_scoped_config;
  suggestion_limit_scoped_config.Get().enabled = true;
  suggestion_limit_scoped_config.Get().max_suggestions = 8U;

  AutocompleteInput input(BuildAutocompleteInputForWebOnFocus());

  history::HistoryService::QueryMostVisitedURLsCallback callback;
  EXPECT_CALL(history_service_ref, QueryMostVisitedURLs(_, _, _, _, _, _))
      .WillOnce([&](int result_count,
                    history::HistoryService::QueryMostVisitedURLsCallback cb,
                    base::CancelableTaskTracker* tracker,
                    std::optional<std::string> recency_factor_name,
                    std::optional<size_t> recency_window_days,
                    bool check_visual_deduplication_flag)
                    -> base::CancelableTaskTracker::TaskId {
        // Add 1 to simulate 1 site being open.
        EXPECT_EQ(static_cast<int>(provider_->GetRequestedResultSize(input)),
                  result_count);
        EXPECT_TRUE(recency_factor_name.has_value());
        EXPECT_EQ(history::kMvtScoringParamRecencyFactor_Classic,
                  recency_factor_name.value());
        EXPECT_TRUE(recency_window_days.has_value());
        EXPECT_EQ(4u, recency_window_days.value());
        callback = std::move(cb);
        return {};
      });
  provider_->Start(input, false);
  EXPECT_FALSE(provider_->done());
  history::MostVisitedURLList result;
  for (const auto& test_element : DefaultTestData()) {
    result.push_back(test_element.entry);
  }
  std::move(callback).Run(std::move(result));
  // Shouldn't include the match from the SRP.
  EXPECT_TRUE(provider_->done());
  ASSERT_EQ(4u, provider_->matches().size());
}

TEST_F(MostVisitedSitesProviderTest, TestDeleteMatch) {
  // Set a MockHistoryService.
  auto history_service = std::make_unique<MockHistoryService>();
  auto& history_service_ref = *history_service;
  client_.set_history_service(std::move(history_service));
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;
  scoped_config.Get().prefetch_most_visited_sites = false;
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxZpsSuggestionLimit>
      suggestion_limit_scoped_config;
  suggestion_limit_scoped_config.Get().enabled = true;
  suggestion_limit_scoped_config.Get().max_suggestions = 8U;

  AutocompleteInput input(BuildAutocompleteInputForWebOnFocus());

  history::HistoryService::QueryMostVisitedURLsCallback callback;
  EXPECT_CALL(history_service_ref, QueryMostVisitedURLs(_, _, _, _, _, _))
      .WillOnce([&](int result_count,
                    history::HistoryService::QueryMostVisitedURLsCallback cb,
                    base::CancelableTaskTracker* tracker,
                    std::optional<std::string> recency_factor_name,
                    std::optional<size_t> recency_window_days,
                    bool check_visual_deduplication_flag)
                    -> base::CancelableTaskTracker::TaskId {
        // Add 1 to simulate 1 site being open.
        EXPECT_EQ(static_cast<int>(provider_->GetRequestedResultSize(input)),
                  result_count);
        callback = std::move(cb);
        return {};
      });

  provider_->Start(input, false);
  histogram_.ExpectTotalCount(
      "Omnibox.MostVisitedProvider.QueryMostVisitedURLs.NumRequested", 1);
  EXPECT_FALSE(provider_->done());
  history::MostVisitedURLList result;
  for (const auto& test_element : DefaultTestData()) {
    result.push_back(test_element.entry);
  }

  std::move(callback).Run(std::move(result));
  histogram_.ExpectTotalCount(
      "Omnibox.MostVisitedProvider.QueryMostVisitedURLs.NumReceived", 1);
  histogram_.ExpectTotalCount(
      "Omnibox.MostVisitedProvider.QueryMostVisitedURLs.Duration", 1);

  // Shouldn't include the match from the SRP.
  EXPECT_TRUE(provider_->done());
  ASSERT_EQ(4u, provider_->matches().size());

  EXPECT_CALL(history_service_ref, DeleteURLs(_))
      .WillOnce([&](const std::vector<GURL>& urls) -> void {
        EXPECT_FALSE(urls.empty());
        EXPECT_EQ(provider_->matches().at(0).destination_url, urls.at(0));
        return;
      });

  provider_->DeleteMatch(provider_->matches().at(0));

  ASSERT_EQ(3u, provider_->matches().size());
}

TEST_F(MostVisitedSitesProviderTest, PrefetchingUpdatesCachedSites) {
  // Set a MockHistoryService.
  auto history_service = std::make_unique<MockHistoryService>();
  auto& history_service_ref = *history_service;
  client_.set_history_service(std::move(history_service));

  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;
  scoped_config.Get().prefetch_most_visited_sites = true;
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxZpsSuggestionLimit>
      suggestion_limit_scoped_config;
  suggestion_limit_scoped_config.Get().enabled = true;
  suggestion_limit_scoped_config.Get().max_suggestions = 8U;

  AutocompleteInput input(BuildAutocompletePrefetchInputForWeb());

  history::HistoryService::QueryMostVisitedURLsCallback callback;
  EXPECT_CALL(history_service_ref, QueryMostVisitedURLs(_, _, _, _, _, _))
      .Times(2)
      .WillRepeatedly(
          [&](int result_count,
              history::HistoryService::QueryMostVisitedURLsCallback cb,
              base::CancelableTaskTracker* tracker,
              std::optional<std::string> recency_factor_name,
              std::optional<size_t> recency_window_days,
              bool check_visual_deduplication_flag)
              -> base::CancelableTaskTracker::TaskId {
            // Add 1 to simulate 1 site being open.
            EXPECT_EQ(
                static_cast<int>(provider_->GetRequestedResultSize(input)),
                result_count);
            callback = std::move(cb);
            return {};
          });

  // `StartPrefetch()` shouldn't affect provider state.
  EXPECT_TRUE(provider_->done());
  provider_->StartPrefetch(input);
  // Number of sites requested should be logged at the beginnign of the history
  // query.
  histogram_.ExpectTotalCount(
      "Omnibox.MostVisitedProvider.QueryMostVisitedURLs.NumRequested", 1);
  EXPECT_TRUE(provider_->done());
  history::MostVisitedURLList result;
  for (const auto& test_element : DefaultTestData()) {
    result.push_back(test_element.entry);
  }

  // Updates cache.
  std::move(callback).Run(std::move(result));
  // Duration should be logged when cache is updated.
  histogram_.ExpectTotalCount(
      "Omnibox.MostVisitedProvider.QueryMostVisitedURLs.Duration", 1);
  histogram_.ExpectTotalCount(
      "Omnibox.MostVisitedProvider.QueryMostVisitedURLs.NumReceived", 1);

  // `StartPrefetch()` should update the list of cached sites, but not
  // update the actual provider's matches.
  ASSERT_EQ(5u, provider_->GetCachedSitesForTesting().size());
  ASSERT_EQ(0u, provider_->matches().size());

  // Calling Start() should use the cached sites to create matches. Shouldn't
  // include the SRP result.
  provider_->Start(input, false);
  ASSERT_EQ(4u, provider_->matches().size());
  // Duration shouldn't be recorded again when results are returned
  // synchronously.
  histogram_.ExpectTotalCount(
      "Omnibox.MostVisitedProvider.QueryMostVisitedURLs.Duration", 1);
  histogram_.ExpectTotalCount(
      "Omnibox.MostVisitedProvider.QueryMostVisitedURLs.NumReceived", 1);
}

TEST_F(MostVisitedSitesProviderTest,
       StartDoesNotUpdateMatchesWhenPrefetchEnabled) {
  // Set a MockHistoryService.
  auto history_service = std::make_unique<MockHistoryService>();
  auto& history_service_ref = *history_service;
  client_.set_history_service(std::move(history_service));
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;
  scoped_config.Get().prefetch_most_visited_sites = true;
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxZpsSuggestionLimit>
      suggestion_limit_scoped_config;
  suggestion_limit_scoped_config.Get().enabled = true;
  suggestion_limit_scoped_config.Get().max_suggestions = 8U;

  AutocompleteInput input(BuildAutocompleteInputForWebOnFocus());

  history::HistoryService::QueryMostVisitedURLsCallback callback;
  EXPECT_CALL(history_service_ref, QueryMostVisitedURLs(_, _, _, _, _, _))
      .WillOnce([&](int result_count,
                    history::HistoryService::QueryMostVisitedURLsCallback cb,
                    base::CancelableTaskTracker* tracker,
                    std::optional<std::string> recency_factor_name,
                    std::optional<size_t> recency_window_days,
                    bool check_visual_deduplication_flag)
                    -> base::CancelableTaskTracker::TaskId {
        // Add 1 to simulate 1 site being open.
        EXPECT_EQ(static_cast<int>(provider_->GetRequestedResultSize(input)),
                  result_count);
        callback = std::move(cb);
        return {};
      });

  provider_->Start(input, false);
  history::MostVisitedURLList result;
  for (const auto& test_element : DefaultTestData()) {
    result.push_back(test_element.entry);
  }

  std::move(callback).Run(std::move(result));

  EXPECT_TRUE(provider_->done());
  // `Start()` when prefetch is enabled should update the list of cached sites,
  // but not update the actual provider's matches if the cache is empty.
  ASSERT_EQ(5u, provider_->GetCachedSitesForTesting().size());
  ASSERT_EQ(0u, provider_->matches().size());
}

TEST_F(MostVisitedSitesProviderTest, BlocklistedURLs) {
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;
  AutocompleteInput input(BuildAutocompleteInputForWebOnFocus());
  history::MostVisitedURLList result;
  std::vector<TestData> test_data = {
      {false, {GURL("http://www.nonblocked.com"), u"Non blocked"}},
      {false, {GURL("http://accounts.google.com/signin"), u"Blocked"}}};
  result.push_back(test_data[0].entry);
  result.push_back(test_data[1].entry);
  provider_->OnMostVisitedUrlsAvailable(input, result);
  // Shouldn't include the "accounts.google.com" URL since it is blocklisted.
  ASSERT_EQ(1u, provider_->matches().size());
  ASSERT_EQ("http://www.nonblocked.com/",
            provider_->matches().at(0).destination_url.spec());
}

TEST_F(MostVisitedSitesProviderTest, TestDeleteWithPrefetching) {
  // Set a MockHistoryService.
  auto history_service = std::make_unique<MockHistoryService>();
  auto& history_service_ref = *history_service;
  client_.set_history_service(std::move(history_service));
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;
  scoped_config.Get().prefetch_most_visited_sites = true;
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxZpsSuggestionLimit>
      suggestion_limit_scoped_config;
  suggestion_limit_scoped_config.Get().enabled = true;
  suggestion_limit_scoped_config.Get().max_suggestions = 8U;

  history::HistoryService::QueryMostVisitedURLsCallback callback;
  EXPECT_CALL(history_service_ref, QueryMostVisitedURLs(_, _, _, _, _, _))
      .WillRepeatedly(
          [&](int result_count,
              history::HistoryService::QueryMostVisitedURLsCallback cb,
              base::CancelableTaskTracker* tracker,
              std::optional<std::string> recency_factor_name,
              std::optional<size_t> recency_window_days,
              bool check_visual_deduplication_flag)
              -> base::CancelableTaskTracker::TaskId {
            callback = std::move(cb);
            return {};
          });

  AutocompleteInput input(BuildAutocompleteInputForWebOnFocus());
  provider_->StartPrefetch(input);
  history::MostVisitedURLList result;

  // Populates cached sites.
  for (const auto& test_element : DefaultTestData()) {
    result.push_back(test_element.entry);
  }
  std::move(callback).Run(std::move(result));
  ASSERT_EQ(5u, provider_->GetCachedSitesForTesting().size());

  // Shouldn't include the match from the SRP.
  EXPECT_TRUE(provider_->done());
  ASSERT_EQ(0u, provider_->matches().size());

  EXPECT_CALL(history_service_ref, DeleteURLs(_))
      .WillOnce([&](const std::vector<GURL>& urls) -> void {
        EXPECT_FALSE(urls.empty());
        EXPECT_EQ(provider_->matches().at(0).destination_url, urls.at(0));
        return;
      });

  // Calling `Start()` will use the list of cached sites, but won't include the
  // SRP site..
  provider_->Start(input, false);
  ASSERT_EQ(4u, provider_->matches().size());

  provider_->DeleteMatch(provider_->matches().at(0));

  // `DeleteMatch()` should remove from both the cached sites list and from
  // `matches_`.
  ASSERT_EQ(3u, provider_->matches().size());
  ASSERT_EQ(4u, provider_->GetCachedSitesForTesting().size());

  // Calling `Start()` uses the `cached_sites_`, which should now only return
  // 3 valid sites.
  provider_->Start(input, false);
  ASSERT_EQ(3u, provider_->matches().size());
}

TEST_F(MostVisitedSitesProviderTest, DuplicateSuggestions) {
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;
  std::vector<TestData> test_data = {
      {false, {GURL("http://www.mail.com"), u"Mail"}},
      {false, {GURL("http://different.mail.com/signin"), u"Mail"}},
      {false, {GURL("http://www.samesites.com/c/#ref"), u"Same URL"}},
      {false,
       {GURL("http://www.samesites.com/c/#differentref"), u"Same URL 2"}},
      {false,
       {GURL("http://www.samesites.com/differentpath/#ref"),
        u"Different URL"}}};
  history::MostVisitedURLList result;
  for (auto& data : test_data) {
    result.push_back(data.entry);
  }
  AutocompleteInput input(BuildAutocompleteInputForWebOnFocus());
  provider_->OnMostVisitedUrlsAvailable(input, result);
  // Filter by same titles and stripped urls when deduping suggestions within
  // the suggestion list.
  ASSERT_EQ(3u, provider_->matches().size());
  ASSERT_EQ("http://www.mail.com/",
            provider_->matches().at(0).destination_url.spec());
  ASSERT_EQ("http://www.samesites.com/c/#ref",
            provider_->matches().at(1).destination_url.spec());
  ASSERT_EQ("http://www.samesites.com/differentpath/#ref",
            provider_->matches().at(2).destination_url.spec());
}

TEST_F(MostVisitedSitesProviderTest, TestProviderDoneWithEmptyCachedSites) {
  // Set a MockHistoryService.
  auto history_service = std::make_unique<MockHistoryService>();
  auto& history_service_ref = *history_service;
  client_.set_history_service(std::move(history_service));
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;
  scoped_config.Get().prefetch_most_visited_sites = true;

  history::HistoryService::QueryMostVisitedURLsCallback callback;
  EXPECT_CALL(history_service_ref, QueryMostVisitedURLs(_, _, _, _, _, _))
      .WillRepeatedly(
          [&](int result_count,
              history::HistoryService::QueryMostVisitedURLsCallback cb,
              base::CancelableTaskTracker* tracker,
              std::optional<std::string> recency_factor_name,
              std::optional<size_t> recency_window_days,
              bool check_visual_deduplication_flag)
              -> base::CancelableTaskTracker::TaskId {
            callback = std::move(cb);
            return {};
          });

  AutocompleteInput input(BuildAutocompleteInputForWebOnFocus());
  // Simulate a prefetch that returns no results.
  provider_->StartPrefetch(input);
  history::MostVisitedURLList result;
  std::move(callback).Run(std::move(result));
  ASSERT_EQ(0u, provider_->GetCachedSitesForTesting().size());

  // Provider should still be set to done immediately after calling `Start()`
  // even if cached sites is empty.
  provider_->Start(input, false);
  EXPECT_TRUE(provider_->done());
}

TEST_F(MostVisitedSitesProviderTest, DedupingOpenTabs) {
  omnibox_feature_configs::ScopedConfigForTesting<
      omnibox_feature_configs::OmniboxUrlSuggestionsOnFocus>
      scoped_config;
  scoped_config.Get().enabled = true;

  AutocompleteInput input(BuildAutocompleteInputForWebOnFocus());
  history::MostVisitedURLList result;

  FakeTabMatcher& tab_matcher = static_cast<FakeTabMatcher&>(
      const_cast<TabMatcher&>(client_.GetTabMatcher()));

  tab_matcher.AddOpenTab(FakeTabMatcher::TabWrapper(
      u"Test", GURL("http://foo.org/path/#ref"), base::Time::Now()));
  tab_matcher.AddOpenTab(FakeTabMatcher::TabWrapper(
      u"Testing", GURL("http://bar.org"), base::Time::Now()));

  std::vector<TestData> test_data = {
      // Tabs with same title and url should be considered matches.
      {false, {GURL("http://bar.org"), u"Testing"}},
      // Tabs with same title should be considered matches even if urls are
      // different.
      {false, {GURL("http://different.org"), u"Testing"}},
      // Sites with same path and different refs should be considered matches.
      {false, {GURL("http://foo.org/path/#differentref"), u"Different Name"}},
      // Sites with different path should not be considered matches.
      {false, {GURL("http://foo.org/differentpath/#ref"), u"Different Name"}},
      // Sites with same match and different refs and query params should be
      // considered matches.
      {false, {GURL("http://foo.org/path/#ref?param=123"), u"Different Name"}}};
  for (auto& data : test_data) {
    result.push_back(data.entry);
  }
  provider_->OnMostVisitedUrlsAvailable(input, result);
  ASSERT_EQ(1u, provider_->matches().size());
  ASSERT_EQ("http://foo.org/differentpath/#ref",
            provider_->matches().at(0).destination_url.spec());
}

#endif  // !(BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS))