Make certificate viewer a tab-modal dialog.
[chromium-blink-merge.git] / base / metrics / statistics_recorder_unittest.cc
blobea91e8e8e9b601e90b8658d429e789b26ffccefb
1 // Copyright (c) 2012 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 <vector>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace base {
14 class StatisticsRecorderTest : public testing::Test {
15 protected:
16 virtual void SetUp() {
17 // Each test will have a clean state (no Histogram / BucketRanges
18 // registered).
19 InitializeStatisticsRecorder();
22 virtual void TearDown() {
23 UninitializeStatisticsRecorder();
26 void InitializeStatisticsRecorder() {
27 statistics_recorder_ = new StatisticsRecorder();
30 void UninitializeStatisticsRecorder() {
31 delete statistics_recorder_;
32 statistics_recorder_ = NULL;
35 Histogram* CreateHistogram(const std::string& name,
36 HistogramBase::Sample min,
37 HistogramBase::Sample max,
38 size_t bucket_count) {
39 return new Histogram(name, min, max, bucket_count, NULL);
42 void DeleteHistogram(HistogramBase* histogram) {
43 delete histogram;
46 StatisticsRecorder* statistics_recorder_;
49 TEST_F(StatisticsRecorderTest, NotInitialized) {
50 UninitializeStatisticsRecorder();
52 ASSERT_FALSE(StatisticsRecorder::IsActive());
54 StatisticsRecorder::Histograms registered_histograms;
55 std::vector<const BucketRanges*> registered_ranges;
57 StatisticsRecorder::GetHistograms(&registered_histograms);
58 EXPECT_EQ(0u, registered_histograms.size());
60 Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10);
62 // When StatisticsRecorder is not initialized, register is a noop.
63 EXPECT_EQ(histogram,
64 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
65 // Manually delete histogram that was not registered.
66 DeleteHistogram(histogram);
68 // RegisterOrDeleteDuplicateRanges is a no-op.
69 BucketRanges* ranges = new BucketRanges(3);;
70 ranges->ResetChecksum();
71 EXPECT_EQ(ranges,
72 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges));
73 StatisticsRecorder::GetBucketRanges(&registered_ranges);
74 EXPECT_EQ(0u, registered_ranges.size());
77 TEST_F(StatisticsRecorderTest, RegisterBucketRanges) {
78 std::vector<const BucketRanges*> registered_ranges;
80 BucketRanges* ranges1 = new BucketRanges(3);;
81 ranges1->ResetChecksum();
82 BucketRanges* ranges2 = new BucketRanges(4);;
83 ranges2->ResetChecksum();
85 // Register new ranges.
86 EXPECT_EQ(ranges1,
87 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1));
88 EXPECT_EQ(ranges2,
89 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges2));
90 StatisticsRecorder::GetBucketRanges(&registered_ranges);
91 ASSERT_EQ(2u, registered_ranges.size());
93 // Register some ranges again.
94 EXPECT_EQ(ranges1,
95 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1));
96 registered_ranges.clear();
97 StatisticsRecorder::GetBucketRanges(&registered_ranges);
98 ASSERT_EQ(2u, registered_ranges.size());
99 // Make sure the ranges is still the one we know.
100 ASSERT_EQ(3u, ranges1->size());
101 EXPECT_EQ(0, ranges1->range(0));
102 EXPECT_EQ(0, ranges1->range(1));
103 EXPECT_EQ(0, ranges1->range(2));
105 // Register ranges with same values.
106 BucketRanges* ranges3 = new BucketRanges(3);;
107 ranges3->ResetChecksum();
108 EXPECT_EQ(ranges1, // returning ranges1
109 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges3));
110 registered_ranges.clear();
111 StatisticsRecorder::GetBucketRanges(&registered_ranges);
112 ASSERT_EQ(2u, registered_ranges.size());
115 TEST_F(StatisticsRecorderTest, RegisterHistogram) {
116 // Create a Histogram that was not registered.
117 Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10);
119 StatisticsRecorder::Histograms registered_histograms;
120 StatisticsRecorder::GetHistograms(&registered_histograms);
121 EXPECT_EQ(0u, registered_histograms.size());
123 // Register the Histogram.
124 EXPECT_EQ(histogram,
125 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
126 StatisticsRecorder::GetHistograms(&registered_histograms);
127 EXPECT_EQ(1u, registered_histograms.size());
129 // Register the same Histogram again.
130 EXPECT_EQ(histogram,
131 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
132 registered_histograms.clear();
133 StatisticsRecorder::GetHistograms(&registered_histograms);
134 EXPECT_EQ(1u, registered_histograms.size());
137 TEST_F(StatisticsRecorderTest, FindHistogram) {
138 HistogramBase* histogram1 = Histogram::FactoryGet(
139 "TestHistogram1", 1, 1000, 10, HistogramBase::kNoFlags);
140 HistogramBase* histogram2 = Histogram::FactoryGet(
141 "TestHistogram2", 1, 1000, 10, HistogramBase::kNoFlags);
143 EXPECT_EQ(histogram1, StatisticsRecorder::FindHistogram("TestHistogram1"));
144 EXPECT_EQ(histogram2, StatisticsRecorder::FindHistogram("TestHistogram2"));
145 EXPECT_TRUE(StatisticsRecorder::FindHistogram("TestHistogram") == NULL);
148 TEST_F(StatisticsRecorderTest, GetSnapshot) {
149 Histogram::FactoryGet("TestHistogram1", 1, 1000, 10, Histogram::kNoFlags);
150 Histogram::FactoryGet("TestHistogram2", 1, 1000, 10, Histogram::kNoFlags);
151 Histogram::FactoryGet("TestHistogram3", 1, 1000, 10, Histogram::kNoFlags);
153 StatisticsRecorder::Histograms snapshot;
154 StatisticsRecorder::GetSnapshot("Test", &snapshot);
155 EXPECT_EQ(3u, snapshot.size());
157 snapshot.clear();
158 StatisticsRecorder::GetSnapshot("1", &snapshot);
159 EXPECT_EQ(1u, snapshot.size());
161 snapshot.clear();
162 StatisticsRecorder::GetSnapshot("hello", &snapshot);
163 EXPECT_EQ(0u, snapshot.size());
166 TEST_F(StatisticsRecorderTest, RegisterHistogramWithFactoryGet) {
167 StatisticsRecorder::Histograms registered_histograms;
169 StatisticsRecorder::GetHistograms(&registered_histograms);
170 ASSERT_EQ(0u, registered_histograms.size());
172 // Create a histogram.
173 HistogramBase* histogram = Histogram::FactoryGet(
174 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
175 registered_histograms.clear();
176 StatisticsRecorder::GetHistograms(&registered_histograms);
177 EXPECT_EQ(1u, registered_histograms.size());
179 // Get an existing histogram.
180 HistogramBase* histogram2 = Histogram::FactoryGet(
181 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
182 registered_histograms.clear();
183 StatisticsRecorder::GetHistograms(&registered_histograms);
184 EXPECT_EQ(1u, registered_histograms.size());
185 EXPECT_EQ(histogram, histogram2);
187 // Create a LinearHistogram.
188 histogram = LinearHistogram::FactoryGet(
189 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
190 registered_histograms.clear();
191 StatisticsRecorder::GetHistograms(&registered_histograms);
192 EXPECT_EQ(2u, registered_histograms.size());
194 // Create a BooleanHistogram.
195 histogram = BooleanHistogram::FactoryGet(
196 "TestBooleanHistogram", HistogramBase::kNoFlags);
197 registered_histograms.clear();
198 StatisticsRecorder::GetHistograms(&registered_histograms);
199 EXPECT_EQ(3u, registered_histograms.size());
201 // Create a CustomHistogram.
202 std::vector<int> custom_ranges;
203 custom_ranges.push_back(1);
204 custom_ranges.push_back(5);
205 histogram = CustomHistogram::FactoryGet(
206 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags);
207 registered_histograms.clear();
208 StatisticsRecorder::GetHistograms(&registered_histograms);
209 EXPECT_EQ(4u, registered_histograms.size());
212 TEST_F(StatisticsRecorderTest, RegisterHistogramWithMacros) {
213 StatisticsRecorder::Histograms registered_histograms;
215 HistogramBase* histogram = Histogram::FactoryGet(
216 "TestHistogramCounts", 1, 1000000, 50, HistogramBase::kNoFlags);
218 // The histogram we got from macro is the same as from FactoryGet.
219 HISTOGRAM_COUNTS("TestHistogramCounts", 30);
220 registered_histograms.clear();
221 StatisticsRecorder::GetHistograms(&registered_histograms);
222 ASSERT_EQ(1u, registered_histograms.size());
223 EXPECT_EQ(histogram, registered_histograms[0]);
225 HISTOGRAM_TIMES("TestHistogramTimes", TimeDelta::FromDays(1));
226 HISTOGRAM_ENUMERATION("TestHistogramEnumeration", 20, 200);
228 registered_histograms.clear();
229 StatisticsRecorder::GetHistograms(&registered_histograms);
230 EXPECT_EQ(3u, registered_histograms.size());
232 // Debugging only macros.
233 DHISTOGRAM_TIMES("TestHistogramDebugTimes", TimeDelta::FromDays(1));
234 DHISTOGRAM_COUNTS("TestHistogramDebugCounts", 30);
235 registered_histograms.clear();
236 StatisticsRecorder::GetHistograms(&registered_histograms);
237 #ifndef NDEBUG
238 EXPECT_EQ(5u, registered_histograms.size());
239 #else
240 EXPECT_EQ(3u, registered_histograms.size());
241 #endif
244 TEST_F(StatisticsRecorderTest, BucketRangesSharing) {
245 std::vector<const BucketRanges*> ranges;
246 StatisticsRecorder::GetBucketRanges(&ranges);
247 EXPECT_EQ(0u, ranges.size());
249 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags);
250 Histogram::FactoryGet("Histogram2", 1, 64, 8, HistogramBase::kNoFlags);
252 StatisticsRecorder::GetBucketRanges(&ranges);
253 EXPECT_EQ(1u, ranges.size());
255 Histogram::FactoryGet("Histogram3", 1, 64, 16, HistogramBase::kNoFlags);
257 ranges.clear();
258 StatisticsRecorder::GetBucketRanges(&ranges);
259 EXPECT_EQ(2u, ranges.size());
262 } // namespace base