Roll src/third_party/WebKit 6d85854:7e30d51 (svn 202247:202248)
[chromium-blink-merge.git] / base / metrics / histogram_base_unittest.cc
blob2d6b6df0288850cab31362762957070201ca72e3
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/metrics/histogram.h"
8 #include "base/metrics/histogram_base.h"
9 #include "base/metrics/sparse_histogram.h"
10 #include "base/metrics/statistics_recorder.h"
11 #include "base/pickle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace base {
16 class HistogramBaseTest : public testing::Test {
17 protected:
18 HistogramBaseTest() {
19 // Each test will have a clean state (no Histogram / BucketRanges
20 // registered).
21 statistics_recorder_ = NULL;
22 ResetStatisticsRecorder();
25 ~HistogramBaseTest() override { delete statistics_recorder_; }
27 void ResetStatisticsRecorder() {
28 delete statistics_recorder_;
29 statistics_recorder_ = new StatisticsRecorder();
32 private:
33 StatisticsRecorder* statistics_recorder_;
36 TEST_F(HistogramBaseTest, DeserializeHistogram) {
37 HistogramBase* histogram = Histogram::FactoryGet(
38 "TestHistogram", 1, 1000, 10,
39 (HistogramBase::kUmaTargetedHistogramFlag |
40 HistogramBase::kIPCSerializationSourceFlag));
42 Pickle pickle;
43 ASSERT_TRUE(histogram->SerializeInfo(&pickle));
45 PickleIterator iter(pickle);
46 HistogramBase* deserialized = DeserializeHistogramInfo(&iter);
47 EXPECT_EQ(histogram, deserialized);
49 ResetStatisticsRecorder();
51 PickleIterator iter2(pickle);
52 deserialized = DeserializeHistogramInfo(&iter2);
53 EXPECT_TRUE(deserialized);
54 EXPECT_NE(histogram, deserialized);
55 EXPECT_EQ("TestHistogram", deserialized->histogram_name());
56 EXPECT_TRUE(deserialized->HasConstructionArguments(1, 1000, 10));
58 // kIPCSerializationSourceFlag will be cleared.
59 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, deserialized->flags());
62 TEST_F(HistogramBaseTest, DeserializeLinearHistogram) {
63 HistogramBase* histogram = LinearHistogram::FactoryGet(
64 "TestHistogram", 1, 1000, 10,
65 HistogramBase::kIPCSerializationSourceFlag);
67 Pickle pickle;
68 ASSERT_TRUE(histogram->SerializeInfo(&pickle));
70 PickleIterator iter(pickle);
71 HistogramBase* deserialized = DeserializeHistogramInfo(&iter);
72 EXPECT_EQ(histogram, deserialized);
74 ResetStatisticsRecorder();
76 PickleIterator iter2(pickle);
77 deserialized = DeserializeHistogramInfo(&iter2);
78 EXPECT_TRUE(deserialized);
79 EXPECT_NE(histogram, deserialized);
80 EXPECT_EQ("TestHistogram", deserialized->histogram_name());
81 EXPECT_TRUE(deserialized->HasConstructionArguments(1, 1000, 10));
82 EXPECT_EQ(0, deserialized->flags());
85 TEST_F(HistogramBaseTest, DeserializeBooleanHistogram) {
86 HistogramBase* histogram = BooleanHistogram::FactoryGet(
87 "TestHistogram", HistogramBase::kIPCSerializationSourceFlag);
89 Pickle pickle;
90 ASSERT_TRUE(histogram->SerializeInfo(&pickle));
92 PickleIterator iter(pickle);
93 HistogramBase* deserialized = DeserializeHistogramInfo(&iter);
94 EXPECT_EQ(histogram, deserialized);
96 ResetStatisticsRecorder();
98 PickleIterator iter2(pickle);
99 deserialized = DeserializeHistogramInfo(&iter2);
100 EXPECT_TRUE(deserialized);
101 EXPECT_NE(histogram, deserialized);
102 EXPECT_EQ("TestHistogram", deserialized->histogram_name());
103 EXPECT_TRUE(deserialized->HasConstructionArguments(1, 2, 3));
104 EXPECT_EQ(0, deserialized->flags());
107 TEST_F(HistogramBaseTest, DeserializeCustomHistogram) {
108 std::vector<HistogramBase::Sample> ranges;
109 ranges.push_back(13);
110 ranges.push_back(5);
111 ranges.push_back(9);
113 HistogramBase* histogram = CustomHistogram::FactoryGet(
114 "TestHistogram", ranges, HistogramBase::kIPCSerializationSourceFlag);
116 Pickle pickle;
117 ASSERT_TRUE(histogram->SerializeInfo(&pickle));
119 PickleIterator iter(pickle);
120 HistogramBase* deserialized = DeserializeHistogramInfo(&iter);
121 EXPECT_EQ(histogram, deserialized);
123 ResetStatisticsRecorder();
125 PickleIterator iter2(pickle);
126 deserialized = DeserializeHistogramInfo(&iter2);
127 EXPECT_TRUE(deserialized);
128 EXPECT_NE(histogram, deserialized);
129 EXPECT_EQ("TestHistogram", deserialized->histogram_name());
130 EXPECT_TRUE(deserialized->HasConstructionArguments(5, 13, 4));
131 EXPECT_EQ(0, deserialized->flags());
134 TEST_F(HistogramBaseTest, DeserializeSparseHistogram) {
135 HistogramBase* histogram = SparseHistogram::FactoryGet(
136 "TestHistogram", HistogramBase::kIPCSerializationSourceFlag);
138 Pickle pickle;
139 ASSERT_TRUE(histogram->SerializeInfo(&pickle));
141 PickleIterator iter(pickle);
142 HistogramBase* deserialized = DeserializeHistogramInfo(&iter);
143 EXPECT_EQ(histogram, deserialized);
145 ResetStatisticsRecorder();
147 PickleIterator iter2(pickle);
148 deserialized = DeserializeHistogramInfo(&iter2);
149 EXPECT_TRUE(deserialized);
150 EXPECT_NE(histogram, deserialized);
151 EXPECT_EQ("TestHistogram", deserialized->histogram_name());
152 EXPECT_EQ(0, deserialized->flags());
155 } // namespace base