noisy_cluster_finalizer_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/noisy_cluster_finalizer.h"
  5. #include "base/test/metrics/histogram_tester.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 "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace history_clusters {
  13. namespace {
  14. using ::testing::ElementsAre;
  15. class NoisyClusterFinalizerTest : public ::testing::Test {
  16. public:
  17. void SetUp() override {
  18. cluster_finalizer_ = std::make_unique<NoisyClusterFinalizer>();
  19. config_.number_interesting_visits_filter_threshold = 2;
  20. SetConfigForTesting(config_);
  21. }
  22. void TearDown() override { cluster_finalizer_.reset(); }
  23. void FinalizeCluster(history::Cluster& cluster) {
  24. cluster_finalizer_->FinalizeCluster(cluster);
  25. }
  26. private:
  27. Config config_;
  28. std::unique_ptr<NoisyClusterFinalizer> cluster_finalizer_;
  29. base::test::TaskEnvironment task_environment_;
  30. };
  31. TEST_F(NoisyClusterFinalizerTest, FilterHighEngagementClusters) {
  32. history::ClusterVisit visit = testing::CreateClusterVisit(
  33. testing::CreateDefaultAnnotatedVisit(1, GURL("https://bar.com/")));
  34. visit.engagement_score = 25.0;
  35. history::ClusterVisit visit2 = testing::CreateClusterVisit(
  36. testing::CreateDefaultAnnotatedVisit(2, GURL("https://bar.com/")));
  37. visit2.duplicate_visits.push_back(visit);
  38. visit2.engagement_score = 25.0;
  39. history::Cluster cluster;
  40. cluster.visits = {visit2};
  41. FinalizeCluster(cluster);
  42. EXPECT_FALSE(cluster.should_show_on_prominent_ui_surfaces);
  43. }
  44. TEST_F(NoisyClusterFinalizerTest, HideClusterWithOnlyOneInterestingVisit) {
  45. base::HistogramTester histogram_tester;
  46. history::ClusterVisit visit = testing::CreateClusterVisit(
  47. testing::CreateDefaultAnnotatedVisit(1, GURL("https://bar.com/")));
  48. visit.engagement_score = 5.0;
  49. history::ClusterVisit visit2 = testing::CreateClusterVisit(
  50. testing::CreateDefaultAnnotatedVisit(2, GURL("https://bar.com/")));
  51. visit2.duplicate_visits.push_back(visit);
  52. visit2.engagement_score = 25.0;
  53. history::Cluster cluster;
  54. cluster.visits = {visit2};
  55. FinalizeCluster(cluster);
  56. EXPECT_FALSE(cluster.should_show_on_prominent_ui_surfaces);
  57. histogram_tester.ExpectUniqueSample(
  58. "History.Clusters.Backend.WasClusterFiltered.NoisyCluster", true, 1);
  59. }
  60. TEST_F(NoisyClusterFinalizerTest, KeepClusterWithAtLeastTwoInterestingVisits) {
  61. base::HistogramTester histogram_tester;
  62. history::ClusterVisit visit = testing::CreateClusterVisit(
  63. testing::CreateDefaultAnnotatedVisit(1, GURL("https://bar.com/")));
  64. visit.engagement_score = 5.0;
  65. history::ClusterVisit visit2 = testing::CreateClusterVisit(
  66. testing::CreateDefaultAnnotatedVisit(2, GURL("https://bar.com/")));
  67. visit2.engagement_score = 25.0;
  68. history::ClusterVisit visit3 = testing::CreateClusterVisit(
  69. testing::CreateDefaultAnnotatedVisit(3, GURL("https://foo.com/")));
  70. visit3.duplicate_visits.push_back(visit);
  71. visit3.engagement_score = 5.0;
  72. history::Cluster cluster;
  73. cluster.visits = {visit2, visit3};
  74. FinalizeCluster(cluster);
  75. EXPECT_TRUE(cluster.should_show_on_prominent_ui_surfaces);
  76. histogram_tester.ExpectUniqueSample(
  77. "History.Clusters.Backend.WasClusterFiltered.NoisyCluster", false, 1);
  78. }
  79. } // namespace
  80. } // namespace history_clusters