content_suggestions_metrics_unittest.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2017 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/ntp_snippets/content_suggestions_metrics.h"
  5. #include "base/test/metrics/histogram_tester.h"
  6. #include "base/time/time.h"
  7. #include "components/ntp_snippets/category.h"
  8. #include "testing/gmock/include/gmock/gmock.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace ntp_snippets {
  11. namespace metrics {
  12. namespace {
  13. using testing::ElementsAre;
  14. using testing::IsEmpty;
  15. TEST(ContentSuggestionsMetricsTest, ShouldLogOnSuggestionsShown) {
  16. base::HistogramTester histogram_tester;
  17. OnSuggestionShown(/*global_position=*/1,
  18. Category::FromKnownCategory(KnownCategories::ARTICLES),
  19. /*position_in_category=*/3, base::Time::Now(),
  20. /*score=*/0.01f, base::Time::Now() - base::Hours(2));
  21. // Test corner cases for score.
  22. OnSuggestionShown(/*global_position=*/1,
  23. Category::FromKnownCategory(KnownCategories::ARTICLES),
  24. /*position_in_category=*/3, base::Time::Now(),
  25. /*score=*/0.0f, base::Time::Now() - base::Hours(2));
  26. OnSuggestionShown(/*global_position=*/1,
  27. Category::FromKnownCategory(KnownCategories::ARTICLES),
  28. /*position_in_category=*/3, base::Time::Now(),
  29. /*score=*/1.0f, base::Time::Now() - base::Hours(2));
  30. OnSuggestionShown(/*global_position=*/1,
  31. Category::FromKnownCategory(KnownCategories::ARTICLES),
  32. /*position_in_category=*/3, base::Time::Now(),
  33. /*score=*/8.0f, base::Time::Now() - base::Hours(2));
  34. EXPECT_THAT(
  35. histogram_tester.GetAllSamples(
  36. "NewTabPage.ContentSuggestions.ShownScoreNormalized.Articles"),
  37. ElementsAre(base::Bucket(/*min=*/0, /*count=*/1),
  38. base::Bucket(/*min=*/1, /*count=*/1),
  39. base::Bucket(/*min=*/10, /*count=*/1),
  40. base::Bucket(/*min=*/11, /*count=*/1)));
  41. }
  42. TEST(ContentSuggestionsMetricsTest,
  43. ShouldNotLogNotShownCategoriesWhenPageShown) {
  44. base::HistogramTester histogram_tester;
  45. OnPageShown(std::vector<Category>(
  46. {Category::FromKnownCategory(KnownCategories::ARTICLES)}),
  47. /*suggestions_per_category=*/{0},
  48. /*is_category_visible=*/{false});
  49. EXPECT_THAT(
  50. histogram_tester.GetAllSamples(
  51. "NewTabPage.ContentSuggestions.CountOnNtpOpenedIfVisible.Articles"),
  52. IsEmpty());
  53. EXPECT_THAT(histogram_tester.GetAllSamples(
  54. "NewTabPage.ContentSuggestions.CountOnNtpOpenedIfVisible"),
  55. ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
  56. EXPECT_THAT(histogram_tester.GetAllSamples(
  57. "NewTabPage.ContentSuggestions.SectionCountOnNtpOpened"),
  58. ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
  59. }
  60. TEST(ContentSuggestionsMetricsTest,
  61. ShouldLogEmptyShownCategoriesWhenPageShown) {
  62. base::HistogramTester histogram_tester;
  63. OnPageShown(std::vector<Category>(
  64. {Category::FromKnownCategory(KnownCategories::ARTICLES)}),
  65. /*suggestions_per_category=*/{0},
  66. /*is_category_visible=*/{true});
  67. EXPECT_THAT(
  68. histogram_tester.GetAllSamples(
  69. "NewTabPage.ContentSuggestions.CountOnNtpOpenedIfVisible.Articles"),
  70. ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
  71. EXPECT_THAT(histogram_tester.GetAllSamples(
  72. "NewTabPage.ContentSuggestions.CountOnNtpOpenedIfVisible"),
  73. ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
  74. EXPECT_THAT(histogram_tester.GetAllSamples(
  75. "NewTabPage.ContentSuggestions.SectionCountOnNtpOpened"),
  76. ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
  77. }
  78. TEST(ContentSuggestionsMetricsTest,
  79. ShouldLogNonEmptyShownCategoryWhenPageShown) {
  80. base::HistogramTester histogram_tester;
  81. OnPageShown(std::vector<Category>(
  82. {Category::FromKnownCategory(KnownCategories::ARTICLES)}),
  83. /*suggestions_per_category=*/{10},
  84. /*is_category_visible=*/{true});
  85. EXPECT_THAT(
  86. histogram_tester.GetAllSamples(
  87. "NewTabPage.ContentSuggestions.CountOnNtpOpenedIfVisible.Articles"),
  88. ElementsAre(base::Bucket(/*min=*/10, /*count=*/1)));
  89. EXPECT_THAT(histogram_tester.GetAllSamples(
  90. "NewTabPage.ContentSuggestions.CountOnNtpOpenedIfVisible"),
  91. ElementsAre(base::Bucket(/*min=*/10, /*count=*/1)));
  92. EXPECT_THAT(histogram_tester.GetAllSamples(
  93. "NewTabPage.ContentSuggestions.SectionCountOnNtpOpened"),
  94. ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
  95. }
  96. TEST(ContentSuggestionsMetricsTest,
  97. ShouldLogMultipleNonEmptyShownCategoriesWhenPageShown) {
  98. base::HistogramTester histogram_tester;
  99. OnPageShown(std::vector<Category>(
  100. {Category::FromKnownCategory(KnownCategories::ARTICLES),
  101. Category::FromKnownCategory(KnownCategories::READING_LIST)}),
  102. /*suggestions_per_category=*/{10, 5},
  103. /*is_category_visible=*/{true, true});
  104. EXPECT_THAT(
  105. histogram_tester.GetAllSamples(
  106. "NewTabPage.ContentSuggestions.CountOnNtpOpenedIfVisible.Articles"),
  107. ElementsAre(base::Bucket(/*min=*/10, /*count=*/1)));
  108. EXPECT_THAT(histogram_tester.GetAllSamples(
  109. "NewTabPage.ContentSuggestions.CountOnNtpOpenedIfVisible"),
  110. ElementsAre(base::Bucket(/*min=*/15, /*count=*/1)));
  111. EXPECT_THAT(histogram_tester.GetAllSamples(
  112. "NewTabPage.ContentSuggestions.SectionCountOnNtpOpened"),
  113. ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
  114. }
  115. TEST(ContentSuggestionsMetricsTest, ShouldLogPrefetchedSuggestionsWhenOpened) {
  116. base::HistogramTester histogram_tester;
  117. OnSuggestionOpened(/*global_position=*/11,
  118. Category::FromKnownCategory(KnownCategories::ARTICLES),
  119. /*category_index=*/2,
  120. /*position_in_category=*/1, base::Time::Now(),
  121. /*score=*/1.0f, WindowOpenDisposition::NEW_BACKGROUND_TAB,
  122. /*is_prefetched=*/false, /*is_offline=*/false);
  123. OnSuggestionOpened(/*global_position=*/13,
  124. Category::FromKnownCategory(KnownCategories::ARTICLES),
  125. /*category_index=*/2,
  126. /*position_in_category=*/3, base::Time::Now(),
  127. /*score=*/1.0f, WindowOpenDisposition::NEW_BACKGROUND_TAB,
  128. /*is_prefetched=*/true, /*is_offline=*/false);
  129. OnSuggestionOpened(/*global_position=*/15,
  130. Category::FromKnownCategory(KnownCategories::ARTICLES),
  131. /*category_index=*/2,
  132. /*position_in_category=*/5, base::Time::Now(),
  133. /*score=*/1.0f, WindowOpenDisposition::NEW_BACKGROUND_TAB,
  134. /*is_prefetched=*/false, /*is_offline=*/true);
  135. OnSuggestionOpened(/*global_position=*/23,
  136. Category::FromKnownCategory(KnownCategories::ARTICLES),
  137. /*category_index=*/2,
  138. /*position_in_category=*/13, base::Time::Now(),
  139. /*score=*/1.0f, WindowOpenDisposition::NEW_BACKGROUND_TAB,
  140. /*is_prefetched=*/true, /*is_offline=*/true);
  141. // Only the last call should be reported, because only there the user is
  142. // offline and the suggestion was prefetched at the same time.
  143. EXPECT_THAT(
  144. histogram_tester.GetAllSamples(
  145. "NewTabPage.ContentSuggestions.Opened.Articles.Prefetched.Offline"),
  146. ElementsAre(base::Bucket(/*min=*/13, /*count=*/1)));
  147. }
  148. } // namespace
  149. } // namespace metrics
  150. } // namespace ntp_snippets