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 "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 class TestNTPUserDataLogger
: public NTPUserDataLogger
{
19 TestNTPUserDataLogger() : NTPUserDataLogger(NULL
) {}
20 ~TestNTPUserDataLogger() override
{}
23 base::HistogramBase::Count
GetTotalCount(const std::string
& histogram_name
) {
24 base::HistogramBase
* histogram
= base::StatisticsRecorder::FindHistogram(
26 // Return 0 if history is uninitialized.
27 return histogram
? histogram
->SnapshotSamples()->TotalCount() : 0;
30 base::HistogramBase::Count
GetBinCount(const std::string
& histogram_name
,
31 base::HistogramBase::Sample value
) {
32 base::HistogramBase
* histogram
= base::StatisticsRecorder::FindHistogram(
34 // Return 0 if history is uninitialized.
35 return histogram
? histogram
->SnapshotSamples()->GetCount(value
) : 0;
40 class NTPUserDataLoggerTest
: public testing::Test
{
41 content::TestBrowserThreadBundle thread_bundle_
;
44 TEST_F(NTPUserDataLoggerTest
, TestLogging
) {
45 base::StatisticsRecorder::Initialize();
47 // Ensure empty statistics.
48 EXPECT_EQ(0, GetTotalCount("NewTabPage.NumberOfMouseOvers"));
49 EXPECT_EQ(0, GetBinCount("NewTabPage.NumberOfMouseOvers", 0));
51 // Enusure non-zero statistics.
52 TestNTPUserDataLogger logger
;
54 base::TimeDelta delta
= base::TimeDelta::FromMilliseconds(0);
56 for (int i
= 0; i
< 20; ++i
)
57 logger
.LogEvent(NTP_MOUSEOVER
, delta
);
58 for (int i
= 0; i
< 8; ++i
)
59 logger
.LogEvent(NTP_TILE
, delta
);
60 for (int i
= 0; i
< 4; ++i
)
61 logger
.LogEvent(NTP_THUMBNAIL_TILE
, delta
);
62 for (int i
= 0; i
< 2; ++i
)
63 logger
.LogEvent(NTP_THUMBNAIL_ERROR
, delta
);
64 logger
.LogEvent(NTP_GRAY_TILE_FALLBACK
, delta
);
65 logger
.LogEvent(NTP_EXTERNAL_TILE_FALLBACK
, delta
);
66 for (int i
= 0; i
< 2; ++i
)
67 logger
.LogEvent(NTP_EXTERNAL_TILE
, delta
);
68 for (int i
= 0; i
< 2; ++i
)
69 logger
.LogEvent(NTP_GRAY_TILE
, delta
);
70 logger
.LogEvent(NTP_SERVER_SIDE_SUGGESTION
, delta
);
72 logger
.EmitNtpStatistics();
74 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfMouseOvers"));
75 EXPECT_EQ(0, GetBinCount("NewTabPage.NumberOfMouseOvers", 0));
76 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfMouseOvers", 20));
77 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfTiles"));
78 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfTiles", 8));
79 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailTiles"));
80 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfThumbnailTiles", 4));
81 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailErrors"));
82 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfThumbnailErrors", 2));
83 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTileFallbacks"));
84 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfGrayTileFallbacks", 1));
85 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTileFallbacks"));
86 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfExternalTileFallbacks", 1));
87 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTiles"));
88 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfExternalTiles", 2));
89 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTiles"));
90 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfGrayTiles", 2));
91 EXPECT_EQ(1, GetTotalCount("NewTabPage.SuggestionsType"));
92 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsType", 1));
94 // Statistics should be reset to 0, so we should not log anything else.
95 logger
.EmitNtpStatistics();
96 EXPECT_EQ(2, GetTotalCount("NewTabPage.NumberOfMouseOvers"));
97 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfMouseOvers", 0));
98 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfMouseOvers", 20));
99 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfTiles"));
100 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailTiles"));
101 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailErrors"));
102 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTileFallbacks"));
103 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTileFallbacks"));
104 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTiles"));
105 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTiles"));
106 EXPECT_EQ(1, GetTotalCount("NewTabPage.SuggestionsType"));
109 TEST_F(NTPUserDataLoggerTest
, TestLogMostVisitedImpression
) {
110 base::StatisticsRecorder::Initialize();
112 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1));
113 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5));
115 TestNTPUserDataLogger logger
;
117 logger
.LogMostVisitedImpression(1, base::ASCIIToUTF16("foobar"));
118 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1));
119 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5));
121 logger
.LogMostVisitedImpression(5, base::ASCIIToUTF16("foobar"));
122 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1));
123 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5));
125 // Try without provider. Only total increases.
126 logger
.LogMostVisitedImpression(5, base::ASCIIToUTF16(""));
127 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1));
128 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5));
130 logger
.LogMostVisitedImpression(1, base::ASCIIToUTF16("foobar"));
131 EXPECT_EQ(2, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1));
132 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5));
135 TEST_F(NTPUserDataLoggerTest
, TestLogMostVisitedNavigation
) {
136 base::StatisticsRecorder::Initialize();
138 EXPECT_EQ(0, GetTotalCount("NewTabPage.MostVisited"));
139 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 1));
140 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 5));
142 TestNTPUserDataLogger logger
;
144 logger
.LogMostVisitedNavigation(1, base::ASCIIToUTF16("foobar"));
145 EXPECT_EQ(1, GetTotalCount("NewTabPage.MostVisited"));
146 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1));
147 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 5));
149 logger
.LogMostVisitedNavigation(5, base::ASCIIToUTF16("foobar"));
150 EXPECT_EQ(2, GetTotalCount("NewTabPage.MostVisited"));
151 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1));
152 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5));
154 // Try without provider. Only total increases.
155 logger
.LogMostVisitedNavigation(5, base::ASCIIToUTF16(""));
156 EXPECT_EQ(3, GetTotalCount("NewTabPage.MostVisited"));
157 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1));
158 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5));
160 logger
.LogMostVisitedNavigation(1, base::ASCIIToUTF16("foobar"));
161 EXPECT_EQ(4, GetTotalCount("NewTabPage.MostVisited"));
162 EXPECT_EQ(2, GetBinCount("NewTabPage.MostVisited.foobar", 1));
163 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5));