Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: chrome/browser/ui/webui/ntp/ntp_user_data_logger_unittest.cc

Issue 1086733002: Ensure tests have an active task runner (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" 5 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
9 #include "base/metrics/statistics_recorder.h" 9 #include "base/metrics/statistics_recorder.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/common/ntp_logging_events.h" 11 #include "chrome/common/ntp_logging_events.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace { 15 namespace {
15 16
16 class TestNTPUserDataLogger : public NTPUserDataLogger { 17 class TestNTPUserDataLogger : public NTPUserDataLogger {
17 public: 18 public:
18 TestNTPUserDataLogger() : NTPUserDataLogger(NULL) {} 19 TestNTPUserDataLogger() : NTPUserDataLogger(NULL) {}
19 ~TestNTPUserDataLogger() override {} 20 ~TestNTPUserDataLogger() override {}
20 }; 21 };
21 22
22 base::HistogramBase::Count GetTotalCount(const std::string& histogram_name) { 23 base::HistogramBase::Count GetTotalCount(const std::string& histogram_name) {
23 base::HistogramBase* histogram = base::StatisticsRecorder::FindHistogram( 24 base::HistogramBase* histogram = base::StatisticsRecorder::FindHistogram(
24 histogram_name); 25 histogram_name);
25 // Return 0 if history is uninitialized. 26 // Return 0 if history is uninitialized.
26 return histogram ? histogram->SnapshotSamples()->TotalCount() : 0; 27 return histogram ? histogram->SnapshotSamples()->TotalCount() : 0;
27 } 28 }
28 29
29 base::HistogramBase::Count GetBinCount(const std::string& histogram_name, 30 base::HistogramBase::Count GetBinCount(const std::string& histogram_name,
30 base::HistogramBase::Sample value) { 31 base::HistogramBase::Sample value) {
31 base::HistogramBase* histogram = base::StatisticsRecorder::FindHistogram( 32 base::HistogramBase* histogram = base::StatisticsRecorder::FindHistogram(
32 histogram_name); 33 histogram_name);
33 // Return 0 if history is uninitialized. 34 // Return 0 if history is uninitialized.
34 return histogram ? histogram->SnapshotSamples()->GetCount(value) : 0; 35 return histogram ? histogram->SnapshotSamples()->GetCount(value) : 0;
35 } 36 }
36 37
37 } // namespace 38 } // namespace
38 39
39 TEST(NTPUserDataLoggerTest, TestLogging) { 40 class NTPUserDataLoggerTest : public testing::Test {
41 content::TestBrowserThreadBundle thread_bundle_;
42 };
43
44 TEST_F(NTPUserDataLoggerTest, TestLogging) {
40 base::StatisticsRecorder::Initialize(); 45 base::StatisticsRecorder::Initialize();
41 46
42 // Ensure empty statistics. 47 // Ensure empty statistics.
43 EXPECT_EQ(0, GetTotalCount("NewTabPage.NumberOfMouseOvers")); 48 EXPECT_EQ(0, GetTotalCount("NewTabPage.NumberOfMouseOvers"));
44 EXPECT_EQ(0, GetBinCount("NewTabPage.NumberOfMouseOvers", 0)); 49 EXPECT_EQ(0, GetBinCount("NewTabPage.NumberOfMouseOvers", 0));
45 50
46 // Enusure non-zero statistics. 51 // Enusure non-zero statistics.
47 TestNTPUserDataLogger logger; 52 TestNTPUserDataLogger logger;
48 53
49 base::TimeDelta delta = base::TimeDelta::FromMilliseconds(0); 54 base::TimeDelta delta = base::TimeDelta::FromMilliseconds(0);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfTiles")); 99 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfTiles"));
95 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailTiles")); 100 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailTiles"));
96 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailErrors")); 101 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfThumbnailErrors"));
97 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTileFallbacks")); 102 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTileFallbacks"));
98 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTileFallbacks")); 103 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTileFallbacks"));
99 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTiles")); 104 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfExternalTiles"));
100 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTiles")); 105 EXPECT_EQ(1, GetTotalCount("NewTabPage.NumberOfGrayTiles"));
101 EXPECT_EQ(1, GetTotalCount("NewTabPage.SuggestionsType")); 106 EXPECT_EQ(1, GetTotalCount("NewTabPage.SuggestionsType"));
102 } 107 }
103 108
104 TEST(NTPUserDataLoggerTest, TestLogMostVisitedImpression) { 109 TEST_F(NTPUserDataLoggerTest, TestLogMostVisitedImpression) {
105 base::StatisticsRecorder::Initialize(); 110 base::StatisticsRecorder::Initialize();
106 111
107 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1)); 112 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1));
108 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5)); 113 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5));
109 114
110 TestNTPUserDataLogger logger; 115 TestNTPUserDataLogger logger;
111 116
112 logger.LogMostVisitedImpression(1, base::ASCIIToUTF16("foobar")); 117 logger.LogMostVisitedImpression(1, base::ASCIIToUTF16("foobar"));
113 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1)); 118 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1));
114 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5)); 119 EXPECT_EQ(0, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5));
115 120
116 logger.LogMostVisitedImpression(5, base::ASCIIToUTF16("foobar")); 121 logger.LogMostVisitedImpression(5, base::ASCIIToUTF16("foobar"));
117 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1)); 122 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1));
118 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5)); 123 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5));
119 124
120 // Try without provider. Only total increases. 125 // Try without provider. Only total increases.
121 logger.LogMostVisitedImpression(5, base::ASCIIToUTF16("")); 126 logger.LogMostVisitedImpression(5, base::ASCIIToUTF16(""));
122 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1)); 127 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1));
123 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5)); 128 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5));
124 129
125 logger.LogMostVisitedImpression(1, base::ASCIIToUTF16("foobar")); 130 logger.LogMostVisitedImpression(1, base::ASCIIToUTF16("foobar"));
126 EXPECT_EQ(2, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1)); 131 EXPECT_EQ(2, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 1));
127 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5)); 132 EXPECT_EQ(1, GetBinCount("NewTabPage.SuggestionsImpression.foobar", 5));
128 } 133 }
129 134
130 TEST(NTPUserDataLoggerTest, TestLogMostVisitedNavigation) { 135 TEST_F(NTPUserDataLoggerTest, TestLogMostVisitedNavigation) {
131 base::StatisticsRecorder::Initialize(); 136 base::StatisticsRecorder::Initialize();
132 137
133 EXPECT_EQ(0, GetTotalCount("NewTabPage.MostVisited")); 138 EXPECT_EQ(0, GetTotalCount("NewTabPage.MostVisited"));
134 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 1)); 139 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 1));
135 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 5)); 140 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 5));
136 141
137 TestNTPUserDataLogger logger; 142 TestNTPUserDataLogger logger;
138 143
139 logger.LogMostVisitedNavigation(1, base::ASCIIToUTF16("foobar")); 144 logger.LogMostVisitedNavigation(1, base::ASCIIToUTF16("foobar"));
140 EXPECT_EQ(1, GetTotalCount("NewTabPage.MostVisited")); 145 EXPECT_EQ(1, GetTotalCount("NewTabPage.MostVisited"));
141 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1)); 146 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1));
142 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 5)); 147 EXPECT_EQ(0, GetBinCount("NewTabPage.MostVisited.foobar", 5));
143 148
144 logger.LogMostVisitedNavigation(5, base::ASCIIToUTF16("foobar")); 149 logger.LogMostVisitedNavigation(5, base::ASCIIToUTF16("foobar"));
145 EXPECT_EQ(2, GetTotalCount("NewTabPage.MostVisited")); 150 EXPECT_EQ(2, GetTotalCount("NewTabPage.MostVisited"));
146 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1)); 151 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1));
147 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5)); 152 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5));
148 153
149 // Try without provider. Only total increases. 154 // Try without provider. Only total increases.
150 logger.LogMostVisitedNavigation(5, base::ASCIIToUTF16("")); 155 logger.LogMostVisitedNavigation(5, base::ASCIIToUTF16(""));
151 EXPECT_EQ(3, GetTotalCount("NewTabPage.MostVisited")); 156 EXPECT_EQ(3, GetTotalCount("NewTabPage.MostVisited"));
152 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1)); 157 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 1));
153 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5)); 158 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5));
154 159
155 logger.LogMostVisitedNavigation(1, base::ASCIIToUTF16("foobar")); 160 logger.LogMostVisitedNavigation(1, base::ASCIIToUTF16("foobar"));
156 EXPECT_EQ(4, GetTotalCount("NewTabPage.MostVisited")); 161 EXPECT_EQ(4, GetTotalCount("NewTabPage.MostVisited"));
157 EXPECT_EQ(2, GetBinCount("NewTabPage.MostVisited.foobar", 1)); 162 EXPECT_EQ(2, GetBinCount("NewTabPage.MostVisited.foobar", 1));
158 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5)); 163 EXPECT_EQ(1, GetBinCount("NewTabPage.MostVisited.foobar", 5));
159 } 164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698