label_cluster_finalizer_unittest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/label_cluster_finalizer.h"
  5. #include "base/test/task_environment.h"
  6. #include "components/history_clusters/core/clustering_test_utils.h"
  7. #include "components/history_clusters/core/config.h"
  8. #include "components/history_clusters/core/on_device_clustering_features.h"
  9. #include "testing/gmock/include/gmock/gmock.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace history_clusters {
  12. namespace {
  13. using ::testing::UnorderedElementsAre;
  14. class LabelClusterFinalizerTest : public ::testing::Test {
  15. public:
  16. void SetUp() override {
  17. cluster_finalizer_ = std::make_unique<LabelClusterFinalizer>();
  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<LabelClusterFinalizer> cluster_finalizer_;
  25. base::test::TaskEnvironment task_environment_;
  26. };
  27. TEST_F(LabelClusterFinalizerTest, ClusterWithNoSearchTerms) {
  28. history::ClusterVisit visit = testing::CreateClusterVisit(
  29. testing::CreateDefaultAnnotatedVisit(1, GURL("https://foo.com/")));
  30. visit.score = 0.8;
  31. visit.annotated_visit.content_annotations.model_annotations.entities = {
  32. {"chosenlabel", 50}};
  33. history::ClusterVisit visit2 = testing::CreateClusterVisit(
  34. testing::CreateDefaultAnnotatedVisit(2, GURL("https://bar.com/")));
  35. visit2.score = 0.25;
  36. visit2.annotated_visit.content_annotations.model_annotations.entities = {
  37. {"chosenlabel", 50}, {"highscoringentitybutlowvisitscore", 100}};
  38. history::ClusterVisit visit3 = testing::CreateClusterVisit(
  39. testing::CreateDefaultAnnotatedVisit(3, GURL("https://baz.com/")));
  40. visit3.duplicate_visits.push_back(visit);
  41. visit3.score = 0.8;
  42. visit3.annotated_visit.content_annotations.model_annotations.entities = {
  43. {"chosenlabel", 25}, {"someotherentity", 10}};
  44. {
  45. // With only search term labelling active, there should be no label.
  46. Config config;
  47. config.should_label_clusters = true;
  48. config.labels_from_hostnames = false;
  49. config.labels_from_entities = false;
  50. SetConfigForTesting(config);
  51. history::Cluster cluster;
  52. cluster.visits = {visit2, visit3};
  53. FinalizeCluster(cluster);
  54. EXPECT_EQ(cluster.raw_label, absl::nullopt);
  55. EXPECT_EQ(cluster.label, absl::nullopt);
  56. }
  57. {
  58. // With hostname labelling and entity labelling both enabled, we should
  59. // prefer the entity because if we prefer hostnames, every cluster will have
  60. // a hostname label, and no entity labels will ever get surfaced.
  61. Config config;
  62. config.should_label_clusters = true;
  63. config.labels_from_hostnames = true;
  64. config.labels_from_entities = true;
  65. SetConfigForTesting(config);
  66. history::Cluster cluster;
  67. cluster.visits = {visit2, visit3};
  68. FinalizeCluster(cluster);
  69. EXPECT_EQ(cluster.raw_label, u"chosenlabel");
  70. EXPECT_EQ(cluster.label, u"chosenlabel");
  71. }
  72. {
  73. // With hostname labelling active only, we should use the hostname.
  74. Config config;
  75. config.should_label_clusters = true;
  76. config.labels_from_hostnames = true;
  77. config.labels_from_entities = false;
  78. SetConfigForTesting(config);
  79. history::Cluster cluster;
  80. cluster.visits = {visit2, visit3};
  81. FinalizeCluster(cluster);
  82. EXPECT_EQ(cluster.raw_label, u"baz.com");
  83. EXPECT_EQ(cluster.label, u"baz.com and more");
  84. }
  85. {
  86. // With entity labelling active only, we should use the entity name.
  87. Config config;
  88. config.should_label_clusters = true;
  89. config.labels_from_hostnames = false;
  90. config.labels_from_entities = true;
  91. SetConfigForTesting(config);
  92. history::Cluster cluster;
  93. cluster.visits = {visit2, visit3};
  94. FinalizeCluster(cluster);
  95. EXPECT_EQ(cluster.raw_label, u"chosenlabel");
  96. EXPECT_EQ(cluster.label, u"chosenlabel");
  97. }
  98. }
  99. TEST_F(LabelClusterFinalizerTest, TakesHighestScoringSearchTermIfAvailable) {
  100. // Verify that search terms take precedence even if labels from entities are
  101. // enabled.
  102. Config config;
  103. config.should_label_clusters = true;
  104. config.labels_from_hostnames = true;
  105. config.labels_from_entities = true;
  106. SetConfigForTesting(config);
  107. history::ClusterVisit visit =
  108. testing::CreateClusterVisit(testing::CreateDefaultAnnotatedVisit(
  109. 2, GURL("https://nosearchtermsbuthighscorevisit.com/")));
  110. visit.engagement_score = 0.9;
  111. visit.annotated_visit.content_annotations.model_annotations.entities = {
  112. {"github", 100}, {"onlyinnoisyvisit", 99}};
  113. history::ClusterVisit visit2 =
  114. testing::CreateClusterVisit(testing::CreateDefaultAnnotatedVisit(
  115. 1, GURL("https://lowerscoringsearchterm.com/")));
  116. visit2.score = 0.6;
  117. visit2.annotated_visit.content_annotations.search_terms = u"lowscore";
  118. history::ClusterVisit visit3 = testing::CreateClusterVisit(
  119. testing::CreateDefaultAnnotatedVisit(2, GURL("https://baz.com/")));
  120. visit3.score = 0.8;
  121. visit3.annotated_visit.content_annotations.model_annotations.entities = {
  122. {"github", 100}, {"otherentity", 100}};
  123. visit3.annotated_visit.content_annotations.search_terms = u"searchtermlabel";
  124. history::Cluster cluster;
  125. cluster.visits = {visit, visit2, visit3};
  126. FinalizeCluster(cluster);
  127. EXPECT_THAT(cluster.raw_label, u"searchtermlabel");
  128. EXPECT_THAT(cluster.label, u"“searchtermlabel”");
  129. }
  130. } // namespace
  131. } // namespace history_clusters