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.
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"
16 class HistogramBaseTest
: public testing::Test
{
19 // Each test will have a clean state (no Histogram / BucketRanges
21 statistics_recorder_
= NULL
;
22 ResetStatisticsRecorder();
25 ~HistogramBaseTest() override
{ delete statistics_recorder_
; }
27 void ResetStatisticsRecorder() {
28 delete statistics_recorder_
;
29 statistics_recorder_
= new StatisticsRecorder();
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
));
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
);
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
);
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);
113 HistogramBase
* histogram
= CustomHistogram::FactoryGet(
114 "TestHistogram", ranges
, HistogramBase::kIPCSerializationSourceFlag
);
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
);
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());