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 "chrome/common/ntp_logging_events.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class TestNTPUserDataLogger
: public NTPUserDataLogger
{
17 TestNTPUserDataLogger() : NTPUserDataLogger(NULL
) {}
18 virtual ~TestNTPUserDataLogger() {}
21 base::HistogramBase::Count
GetTotalCount(const std::string
& histogram_name
) {
22 base::HistogramBase
* histogram
= base::StatisticsRecorder::FindHistogram(
24 return histogram
->SnapshotSamples()->TotalCount();
27 base::HistogramBase::Count
GetBinCount(const std::string
& histogram_name
,
28 base::HistogramBase::Sample value
) {
29 base::HistogramBase
* histogram
= base::StatisticsRecorder::FindHistogram(
31 return histogram
->SnapshotSamples()->GetCount(value
);
36 TEST(NTPUserDataLoggerTest
, TestLogging
) {
37 base::StatisticsRecorder::Initialize();
38 TestNTPUserDataLogger logger
;
40 // Ensure it works when the statistics are all empty. Only the mouseover
41 // should be logged in this case. The other histograms are not created yet so
42 // we can't query them.
43 logger
.EmitNtpStatistics();
45 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfMouseOvers"));
46 EXPECT_EQ(1, GetBinCount("NewTabPage.NumberOfMouseOvers", 0));
48 // Ensure it works with some non-zero statistics. All statistics should now
50 for (int i
= 0; i
< 20; ++i
)
51 logger
.LogEvent(NTP_MOUSEOVER
);
52 for (int i
= 0; i
< 8; ++i
)
53 logger
.LogEvent(NTP_TILE
);
54 for (int i
= 0; i
< 4; ++i
)
55 logger
.LogEvent(NTP_THUMBNAIL_TILE
);
56 for (int i
= 0; i
< 2; ++i
)
57 logger
.LogEvent(NTP_THUMBNAIL_ERROR
);
58 logger
.LogEvent(NTP_GRAY_TILE_FALLBACK
);
59 logger
.LogEvent(NTP_EXTERNAL_TILE_FALLBACK
);
60 for (int i
= 0; i
< 2; ++i
)
61 logger
.LogEvent(NTP_EXTERNAL_TILE
);
62 for (int i
= 0; i
< 2; ++i
)
63 logger
.LogEvent(NTP_GRAY_TILE
);
64 logger
.LogEvent(NTP_SERVER_SIDE_SUGGESTION
);
65 logger
.EmitNtpStatistics();
67 EXPECT_EQ(2, GetTotalCount("NewTabPage.NumberOfMouseOvers"));
68 EXPECT_EQ(1, 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));
87 // Statistics should be reset to 0, so we should not log anything else.
88 logger
.EmitNtpStatistics();
89 EXPECT_EQ(3, GetTotalCount("NewTabPage.NumberOfMouseOvers"));
90 EXPECT_EQ(2, 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"));