1 // Copyright 2013 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 5 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" 6 7 #include "base/basictypes.h" 8 #include "base/metrics/histogram.h" 9 #include "base/metrics/statistics_recorder.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "chrome/common/ntp_logging_events.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace { 15 16 class TestNTPUserDataLogger : public NTPUserDataLogger { 17 public: 18 TestNTPUserDataLogger() : NTPUserDataLogger(NULL) {} 19 virtual ~TestNTPUserDataLogger() {} 20 }; 21 22 base::HistogramBase::Count GetTotalCount(const std::string& histogram_name) { 23 base::HistogramBase* histogram = base::StatisticsRecorder::FindHistogram( 24 histogram_name); 25 // Return 0 if history is uninitialized. 26 return histogram ? histogram->SnapshotSamples()->TotalCount() : 0; 27 } 28 29 base::HistogramBase::Count GetBinCount(const std::string& histogram_name, 30 base::HistogramBase::Sample value) { 31 base::HistogramBase* histogram = base::StatisticsRecorder::FindHistogram( 32 histogram_name); 33 // Return 0 if history is uninitialized. 34 return histogram ? histogram->SnapshotSamples()->GetCount(value) : 0; 35 } 36 37 } // namespace 38 39 TEST(NTPUserDataLoggerTest, TestLogging) { 40 base::StatisticsRecorder::Initialize(); 41 42 // Ensure empty statistics. 43 EXPECT_EQ(0, GetTotalCount("NewTabPage.NumberOfMouseOvers")); 44 EXPECT_EQ(0, GetBinCount("NewTabPage.NumberOfMouseOvers", 0)); 45 46 // Enusure non-zero statistics. 47 TestNTPUserDataLogger logger; 48 49 for (int i = 0; i < 20; ++i) 50 logger.LogEvent(NTP_MOUSEOVER); 51 for (int i = 0; i < 8; ++i) 52 logger.LogEvent(NTP_TILE); 53 for (int i = 0; i < 4; ++i) 54 logger.LogEvent(NTP_THUMBNAIL_TILE); 55 for (int i = 0; i < 2; ++i) 56 logger.LogEvent(NTP_THUMBNAIL_ERROR); 57 logger.LogEvent(NTP_GRAY_TILE_FALLBACK); 58 logger.LogEvent(NTP_EXTERNAL_TILE_FALLBACK); 59 for (int i = 0; i < 2; ++i) 60 logger.LogEvent(NTP_EXTERNAL_TILE); 61 for (int i = 0; i < 2; ++i) 62 logger.LogEvent(NTP_GRAY_TILE); 63 logger.LogEvent(NTP_SERVER_SIDE_SUGGESTION); 64 65 logger.EmitNtpStatistics(); 66 67 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfMouseOvers")); 68 EXPECT_EQ(0, GetBinCount("NewTabPage.NumberOfMouseOvers", 0)); 69 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfMouseOvers", 20)); 70 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfTiles")); 71 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfTiles", 8)); 72 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailTiles")); 73 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfThumbnailTiles", 4)); 74 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailErrors")); 75 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfThumbnailErrors", 2)); 76 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTileFallbacks")); 77 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfGrayTileFallbacks", 1)); 78 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTileFallbacks")); 79 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfExternalTileFallbacks", 1)); 80 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTiles")); 81 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfExternalTiles", 2)); 82 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTiles")); 83 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfGrayTiles", 2)); 84 EXPECT_EQ(1, GetTotalCount("NewTabPage.SuggestionsType")); 85 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsType", 1)); 86 87 // Statistics should be reset to 0, so we should not log anything else. 88 logger.EmitNtpStatistics(); 89 EXPECT_EQ(2, GetTotalCount("NewTabPage.NumberOfMouseOvers")); 90 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfMouseOvers", 0)); 91 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfMouseOvers", 20)); 92 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfTiles")); 93 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailTiles")); 94 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailErrors")); 95 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTileFallbacks")); 96 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTileFallbacks")); 97 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTiles")); 98 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTiles")); 99 EXPECT_EQ(1, GetTotalCount("NewTabPage.SuggestionsType")); 100 } 101 102 TEST(NTPUserDataLoggerTest, TestLogMostVisitedImpression) { 103 base::StatisticsRecorder::Initialize(); 104 105 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1)); 106 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5)); 107 108 TestNTPUserDataLogger logger; 109 110 logger.LogMostVisitedImpression(1, base::ASCIIToUTF16("foobar")); 111 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1)); 112 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5)); 113 114 logger.LogMostVisitedImpression(5, base::ASCIIToUTF16("foobar")); 115 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1)); 116 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5)); 117 118 // Try without provider. Only total increases. 119 logger.LogMostVisitedImpression(5, base::ASCIIToUTF16("")); 120 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1)); 121 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5)); 122 123 logger.LogMostVisitedImpression(1, base::ASCIIToUTF16("foobar")); 124 EXPECT_EQ(2, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1)); 125 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5)); 126 } 127 128 TEST(NTPUserDataLoggerTest, TestLogMostVisitedNavigation) { 129 base::StatisticsRecorder::Initialize(); 130 131 EXPECT_EQ(0, GetTotalCount("NewTabPage.MostVisited")); 132 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 1)); 133 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 5)); 134 135 TestNTPUserDataLogger logger; 136 137 logger.LogMostVisitedNavigation(1, base::ASCIIToUTF16("foobar")); 138 EXPECT_EQ(1, GetTotalCount("NewTabPage.MostVisited")); 139 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1)); 140 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 5)); 141 142 logger.LogMostVisitedNavigation(5, base::ASCIIToUTF16("foobar")); 143 EXPECT_EQ(2, GetTotalCount("NewTabPage.MostVisited")); 144 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1)); 145 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5)); 146 147 // Try without provider. Only total increases. 148 logger.LogMostVisitedNavigation(5, base::ASCIIToUTF16("")); 149 EXPECT_EQ(3, GetTotalCount("NewTabPage.MostVisited")); 150 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1)); 151 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5)); 152 153 logger.LogMostVisitedNavigation(1, base::ASCIIToUTF16("foobar")); 154 EXPECT_EQ(4, GetTotalCount("NewTabPage.MostVisited")); 155 EXPECT_EQ(2, GetBinCount("NewTabPage.MostVisited.foobar", 1)); 156 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5)); 157 } 158