keyword_cluster_finalizer_unittest.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2022 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/keyword_cluster_finalizer.h"
  5. #include "base/test/scoped_feature_list.h"
  6. #include "base/test/task_environment.h"
  7. #include "components/history_clusters/core/clustering_test_utils.h"
  8. #include "components/history_clusters/core/config.h"
  9. #include "components/history_clusters/core/on_device_clustering_features.h"
  10. #include "components/optimization_guide/core/entity_metadata.h"
  11. #include "testing/gmock/include/gmock/gmock.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace history_clusters {
  14. namespace {
  15. using ::testing::UnorderedElementsAre;
  16. class KeywordClusterFinalizerTest : public ::testing::Test {
  17. public:
  18. void SetUp() override {
  19. optimization_guide::EntityMetadata github_md;
  20. github_md.human_readable_aliases = {"git hub", "github llc"};
  21. github_md.collections = {"/collection/computer", "/collection/programming"};
  22. base::flat_map<std::string, optimization_guide::EntityMetadata>
  23. entity_metadata_map;
  24. entity_metadata_map["github"] = github_md;
  25. cluster_finalizer_ =
  26. std::make_unique<KeywordClusterFinalizer>(entity_metadata_map);
  27. config_.keyword_filter_on_noisy_visits = false;
  28. config_.keyword_filter_on_categories = false;
  29. config_.keyword_filter_on_entity_aliases = false;
  30. config_.keyword_filter_on_search_terms = false;
  31. config_.keyword_filter_on_visit_hosts =
  32. false; // Drop keywords match host names.
  33. SetConfigForTesting(config_);
  34. }
  35. void TearDown() override { cluster_finalizer_.reset(); }
  36. void FinalizeCluster(history::Cluster& cluster) {
  37. cluster_finalizer_->FinalizeCluster(cluster);
  38. }
  39. private:
  40. Config config_;
  41. std::unique_ptr<KeywordClusterFinalizer> cluster_finalizer_;
  42. base::test::TaskEnvironment task_environment_;
  43. };
  44. TEST_F(KeywordClusterFinalizerTest, IncludesKeywordsBasedOnFeatureParameters) {
  45. history::ClusterVisit visit = testing::CreateClusterVisit(
  46. testing::CreateDefaultAnnotatedVisit(1, GURL("https://foo.com/")));
  47. visit.engagement_score = 1.0;
  48. visit.annotated_visit.content_annotations.model_annotations.entities = {
  49. {"github", 1}};
  50. visit.annotated_visit.content_annotations.model_annotations.categories = {
  51. {"category", 1}};
  52. visit.annotated_visit.content_annotations.search_terms = u"search";
  53. history::ClusterVisit visit2 =
  54. testing::CreateClusterVisit(testing::CreateDefaultAnnotatedVisit(
  55. 2, GURL("https://engagementtoohigh.com/")));
  56. visit2.engagement_score = 25.0;
  57. visit2.annotated_visit.content_annotations.model_annotations.entities = {
  58. {"github", 1}, {"onlyinnoisyvisit", 1}};
  59. visit2.annotated_visit.content_annotations.model_annotations.categories = {
  60. {"category", 1}};
  61. history::ClusterVisit visit3 = testing::CreateClusterVisit(
  62. testing::CreateDefaultAnnotatedVisit(3, GURL("https://baz.com/")));
  63. visit3.duplicate_visits.push_back(visit);
  64. visit3.engagement_score = 1.0;
  65. visit3.annotated_visit.content_annotations.model_annotations.entities = {
  66. {"github", 1},
  67. {"otherentity", 1},
  68. {"baz", 1} /*should be filtered due to host*/};
  69. visit3.annotated_visit.content_annotations.model_annotations.categories = {
  70. {"category", 1}};
  71. visit3.annotated_visit.content_annotations.search_terms = u"search";
  72. history::Cluster cluster;
  73. cluster.visits = {visit2, visit3};
  74. FinalizeCluster(cluster);
  75. EXPECT_THAT(cluster.GetKeywords(),
  76. UnorderedElementsAre(u"github", u"otherentity"));
  77. ASSERT_TRUE(cluster.keyword_to_data_map.contains(u"github"));
  78. EXPECT_EQ(
  79. cluster.keyword_to_data_map.at(u"github"),
  80. history::ClusterKeywordData(
  81. history::ClusterKeywordData::kEntity, 1,
  82. std::vector<std::string>{
  83. "/collection/computer"} /*keep only top one entity collection*/));
  84. ASSERT_TRUE(cluster.keyword_to_data_map.contains(u"otherentity"));
  85. EXPECT_EQ(
  86. cluster.keyword_to_data_map.at(u"otherentity"),
  87. history::ClusterKeywordData(history::ClusterKeywordData::kEntity, 1, {}));
  88. }
  89. class KeywordClusterFinalizerIncludeAllTest
  90. : public KeywordClusterFinalizerTest {
  91. public:
  92. void SetUp() override {
  93. KeywordClusterFinalizerTest::SetUp();
  94. config_.keyword_filter_on_noisy_visits = true;
  95. config_.keyword_filter_on_categories = true;
  96. config_.keyword_filter_on_entity_aliases = true;
  97. config_.max_entity_aliases_in_keywords = 1;
  98. config_.keyword_filter_on_search_terms = true;
  99. config_.keyword_filter_on_visit_hosts = true;
  100. config_.category_keyword_score_weight = 0.1;
  101. config_.max_num_keywords_per_cluster = 7;
  102. SetConfigForTesting(config_);
  103. }
  104. private:
  105. Config config_;
  106. };
  107. TEST_F(KeywordClusterFinalizerIncludeAllTest,
  108. IncludesKeywordsBasedOnFeatureParameters) {
  109. history::ClusterVisit visit = testing::CreateClusterVisit(
  110. testing::CreateDefaultAnnotatedVisit(1, GURL("https://foo.com/")));
  111. visit.engagement_score = 1.0;
  112. visit.annotated_visit.content_annotations.model_annotations.entities = {
  113. {"github", 1}};
  114. visit.annotated_visit.content_annotations.model_annotations.categories = {
  115. {"category", 1}};
  116. visit.annotated_visit.content_annotations.search_terms = u"search";
  117. history::ClusterVisit visit2 =
  118. testing::CreateClusterVisit(testing::CreateDefaultAnnotatedVisit(
  119. 2, GURL("https://engagementtoohigh.com/")));
  120. visit2.engagement_score = 25.0;
  121. visit2.annotated_visit.content_annotations.model_annotations.entities = {
  122. {"github", 1}, {"onlyinnoisyvisit", 1}};
  123. visit2.annotated_visit.content_annotations.model_annotations.categories = {
  124. {"category", 1}};
  125. history::ClusterVisit visit3 = testing::CreateClusterVisit(
  126. testing::CreateDefaultAnnotatedVisit(2, GURL("https://baz.com/")));
  127. visit3.duplicate_visits.push_back(visit);
  128. visit3.engagement_score = 1.0;
  129. visit3.annotated_visit.content_annotations.model_annotations.entities = {
  130. {"github", 1}, {"otherentity", 1}, {"baz", 1}, {"search", 1}};
  131. visit3.annotated_visit.content_annotations.model_annotations.categories = {
  132. {"category2", 0}, // `category2` is dropped due to keywords capping.
  133. {"category", 1}};
  134. visit3.annotated_visit.content_annotations.search_terms =
  135. u"search"; // Keyword type should be `kSearchTerms`.
  136. history::Cluster cluster;
  137. cluster.visits = {visit2, visit3};
  138. FinalizeCluster(cluster);
  139. EXPECT_THAT(
  140. cluster.GetKeywords(),
  141. UnorderedElementsAre(u"github", u"git hub", u"otherentity", u"baz",
  142. u"category", u"onlyinnoisyvisit", u"search"));
  143. ASSERT_TRUE(cluster.keyword_to_data_map.contains(u"github"));
  144. EXPECT_EQ(cluster.keyword_to_data_map.at(u"github"),
  145. history::ClusterKeywordData(history::ClusterKeywordData::kEntity, 2,
  146. {"/collection/computer"}));
  147. ASSERT_TRUE(cluster.keyword_to_data_map.contains(u"git hub"));
  148. EXPECT_EQ(
  149. cluster.keyword_to_data_map.at(u"git hub"),
  150. history::ClusterKeywordData(history::ClusterKeywordData::kEntityAlias, 2,
  151. {"/collection/computer"}));
  152. ASSERT_TRUE(cluster.keyword_to_data_map.contains(u"category"));
  153. EXPECT_EQ(cluster.keyword_to_data_map.at(u"category"),
  154. history::ClusterKeywordData(
  155. history::ClusterKeywordData::kEntityCategory, 0.2, {}));
  156. ASSERT_TRUE(cluster.keyword_to_data_map.contains(u"onlyinnoisyvisit"));
  157. EXPECT_EQ(
  158. cluster.keyword_to_data_map.at(u"onlyinnoisyvisit"),
  159. history::ClusterKeywordData(history::ClusterKeywordData::kEntity, 1, {}));
  160. ASSERT_TRUE(cluster.keyword_to_data_map.contains(u"otherentity"));
  161. EXPECT_EQ(
  162. cluster.keyword_to_data_map.at(u"otherentity"),
  163. history::ClusterKeywordData(history::ClusterKeywordData::kEntity, 1, {}));
  164. ASSERT_TRUE(cluster.keyword_to_data_map.contains(u"search"));
  165. EXPECT_EQ(cluster.keyword_to_data_map.at(u"search"),
  166. history::ClusterKeywordData(
  167. history::ClusterKeywordData::kSearchTerms, 101, {}));
  168. ASSERT_TRUE(cluster.keyword_to_data_map.contains(u"baz"));
  169. EXPECT_EQ(
  170. cluster.keyword_to_data_map.at(u"baz"),
  171. history::ClusterKeywordData(history::ClusterKeywordData::kEntity, 1, {}));
  172. }
  173. } // namespace
  174. } // namespace history_clusters