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