similar_visit_deduper_cluster_finalizer_unittest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/history_clusters/core/similar_visit_deduper_cluster_finalizer.h"
  5. #include "base/test/task_environment.h"
  6. #include "components/history_clusters/core/clustering_test_utils.h"
  7. #include "testing/gmock/include/gmock/gmock.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace history_clusters {
  10. namespace {
  11. using ::testing::ElementsAre;
  12. using ::testing::UnorderedElementsAre;
  13. class SimilarVisitDeduperClusterFinalizerTest : public ::testing::Test {
  14. public:
  15. void SetUp() override {
  16. cluster_finalizer_ =
  17. std::make_unique<SimilarVisitDeduperClusterFinalizer>();
  18. }
  19. void TearDown() override { cluster_finalizer_.reset(); }
  20. void FinalizeCluster(history::Cluster& cluster) {
  21. cluster_finalizer_->FinalizeCluster(cluster);
  22. }
  23. private:
  24. std::unique_ptr<SimilarVisitDeduperClusterFinalizer> cluster_finalizer_;
  25. base::test::TaskEnvironment task_environment_;
  26. };
  27. TEST_F(SimilarVisitDeduperClusterFinalizerTest, DedupeExactSimilarVisit) {
  28. // canonical_visit has the same URL as Visit1.
  29. history::ClusterVisit visit = testing::CreateClusterVisit(
  30. testing::CreateDefaultAnnotatedVisit(1, GURL("https://google.com/")));
  31. visit.annotated_visit.url_row.set_title(u"sametitle");
  32. visit.annotated_visit.context_annotations.total_foreground_duration =
  33. base::Seconds(20);
  34. visit.url_for_display = u"someurl";
  35. history::ClusterVisit canonical_visit = testing::CreateClusterVisit(
  36. testing::CreateDefaultAnnotatedVisit(2, GURL("https://google.com/#abc")));
  37. canonical_visit.annotated_visit.url_row.set_title(u"sametitle");
  38. canonical_visit.url_for_display = u"someurl";
  39. history::Cluster cluster;
  40. cluster.visits = {visit, canonical_visit};
  41. FinalizeCluster(cluster);
  42. EXPECT_THAT(testing::ToVisitResults({cluster}),
  43. ElementsAre(ElementsAre(testing::VisitResult(
  44. 2, 1.0, {testing::VisitResult(1, 1.0)}))));
  45. const auto& actual_canonical_visit = cluster.visits.at(0);
  46. // Make sure total foreground duration is updated correctly even if some don't
  47. // have the field populated.
  48. EXPECT_EQ(actual_canonical_visit.annotated_visit.context_annotations
  49. .total_foreground_duration,
  50. base::Seconds(20));
  51. }
  52. TEST_F(SimilarVisitDeduperClusterFinalizerTest,
  53. DedupeRespectsDifferentSimilarVisits) {
  54. history::ClusterVisit visit = testing::CreateClusterVisit(
  55. testing::CreateDefaultAnnotatedVisit(1, GURL("https://google.com/")));
  56. history::ClusterVisit canonical_visit = testing::CreateClusterVisit(
  57. testing::CreateDefaultAnnotatedVisit(2, GURL("https://google.com/")));
  58. canonical_visit.annotated_visit.url_row.set_title(u"someothertitle");
  59. history::Cluster cluster;
  60. cluster.visits = {visit, canonical_visit};
  61. FinalizeCluster(cluster);
  62. EXPECT_THAT(testing::ToVisitResults({cluster}),
  63. ElementsAre(ElementsAre(testing::VisitResult(1, 1.0),
  64. testing::VisitResult(2, 1.0))));
  65. }
  66. TEST_F(SimilarVisitDeduperClusterFinalizerTest, MergesAnnotations) {
  67. // canonical_visit has the same title and host as duplicated_visit.
  68. history::ClusterVisit duplicate_visit = testing::CreateClusterVisit(
  69. testing::CreateDefaultAnnotatedVisit(
  70. 1, GURL("https://example.com/normalized?q=whatever")),
  71. GURL("https://example.com/normalized"));
  72. duplicate_visit.annotated_visit.url_row.set_title(u"title");
  73. duplicate_visit.annotated_visit.content_annotations.related_searches = {
  74. "xyz"};
  75. duplicate_visit.annotated_visit.context_annotations.omnibox_url_copied = true;
  76. duplicate_visit.annotated_visit.context_annotations.is_existing_bookmark =
  77. true;
  78. duplicate_visit.annotated_visit.context_annotations
  79. .is_existing_part_of_tab_group = true;
  80. duplicate_visit.annotated_visit.context_annotations.is_new_bookmark = true;
  81. duplicate_visit.annotated_visit.context_annotations.is_placed_in_tab_group =
  82. true;
  83. duplicate_visit.annotated_visit.context_annotations.is_ntp_custom_link = true;
  84. duplicate_visit.annotated_visit.context_annotations
  85. .total_foreground_duration = base::Seconds(20);
  86. history::ClusterVisit canonical_visit =
  87. testing::CreateClusterVisit(testing::CreateDefaultAnnotatedVisit(
  88. 2, GURL("https://example.com/normalized")));
  89. canonical_visit.annotated_visit.url_row.set_title(u"title");
  90. canonical_visit.annotated_visit.content_annotations.related_searches = {
  91. "abc", "xyz"};
  92. canonical_visit.annotated_visit.context_annotations.omnibox_url_copied =
  93. false;
  94. canonical_visit.annotated_visit.context_annotations.is_existing_bookmark =
  95. false;
  96. canonical_visit.annotated_visit.context_annotations
  97. .is_existing_part_of_tab_group = false;
  98. canonical_visit.annotated_visit.context_annotations.is_new_bookmark = false;
  99. canonical_visit.annotated_visit.context_annotations.is_placed_in_tab_group =
  100. false;
  101. canonical_visit.annotated_visit.context_annotations.is_ntp_custom_link =
  102. false;
  103. canonical_visit.annotated_visit.context_annotations
  104. .total_foreground_duration = base::Seconds(20);
  105. history::Cluster cluster;
  106. cluster.visits = {duplicate_visit, canonical_visit};
  107. FinalizeCluster(cluster);
  108. EXPECT_THAT(testing::ToVisitResults({cluster}),
  109. ElementsAre(ElementsAre(testing::VisitResult(
  110. 2, 1.0, {testing::VisitResult(1, 1.0)}))));
  111. const auto& actual_canonical_visit = cluster.visits.at(0);
  112. EXPECT_TRUE(actual_canonical_visit.annotated_visit.context_annotations
  113. .omnibox_url_copied);
  114. EXPECT_TRUE(actual_canonical_visit.annotated_visit.context_annotations
  115. .is_existing_bookmark);
  116. EXPECT_TRUE(actual_canonical_visit.annotated_visit.context_annotations
  117. .is_existing_part_of_tab_group);
  118. EXPECT_TRUE(actual_canonical_visit.annotated_visit.context_annotations
  119. .is_new_bookmark);
  120. EXPECT_TRUE(actual_canonical_visit.annotated_visit.context_annotations
  121. .is_placed_in_tab_group);
  122. EXPECT_TRUE(actual_canonical_visit.annotated_visit.context_annotations
  123. .is_ntp_custom_link);
  124. EXPECT_THAT(actual_canonical_visit.annotated_visit.content_annotations
  125. .related_searches,
  126. UnorderedElementsAre("abc", "xyz"));
  127. EXPECT_EQ(actual_canonical_visit.annotated_visit.visit_row.visit_duration,
  128. base::Seconds(10 * 2));
  129. EXPECT_EQ(actual_canonical_visit.annotated_visit.context_annotations
  130. .total_foreground_duration,
  131. base::Seconds(20 * 2));
  132. }
  133. } // namespace
  134. } // namespace history_clusters