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 virtual ~HistogramBaseTest() {
26 delete statistics_recorder_
;
29 void ResetStatisticsRecorder() {
30 delete statistics_recorder_
;
31 statistics_recorder_
= new StatisticsRecorder();
35 StatisticsRecorder
* statistics_recorder_
;
38 TEST_F(HistogramBaseTest
, DeserializeHistogram
) {
39 HistogramBase
* histogram
= Histogram::FactoryGet(
40 "TestHistogram", 1, 1000, 10,
41 (HistogramBase::kUmaTargetedHistogramFlag
|
42 HistogramBase::kIPCSerializationSourceFlag
));
45 ASSERT_TRUE(histogram
->SerializeInfo(&pickle
));
47 PickleIterator
iter(pickle
);
48 HistogramBase
* deserialized
= DeserializeHistogramInfo(&iter
);
49 EXPECT_EQ(histogram
, deserialized
);
51 ResetStatisticsRecorder();
53 PickleIterator
iter2(pickle
);
54 deserialized
= DeserializeHistogramInfo(&iter2
);
55 EXPECT_TRUE(deserialized
);
56 EXPECT_NE(histogram
, deserialized
);
57 EXPECT_EQ("TestHistogram", deserialized
->histogram_name());
58 EXPECT_TRUE(deserialized
->HasConstructionArguments(1, 1000, 10));
60 // kIPCSerializationSourceFlag will be cleared.
61 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag
, deserialized
->flags());
64 TEST_F(HistogramBaseTest
, DeserializeLinearHistogram
) {
65 HistogramBase
* histogram
= LinearHistogram::FactoryGet(
66 "TestHistogram", 1, 1000, 10,
67 HistogramBase::kIPCSerializationSourceFlag
);
70 ASSERT_TRUE(histogram
->SerializeInfo(&pickle
));
72 PickleIterator
iter(pickle
);
73 HistogramBase
* deserialized
= DeserializeHistogramInfo(&iter
);
74 EXPECT_EQ(histogram
, deserialized
);
76 ResetStatisticsRecorder();
78 PickleIterator
iter2(pickle
);
79 deserialized
= DeserializeHistogramInfo(&iter2
);
80 EXPECT_TRUE(deserialized
);
81 EXPECT_NE(histogram
, deserialized
);
82 EXPECT_EQ("TestHistogram", deserialized
->histogram_name());
83 EXPECT_TRUE(deserialized
->HasConstructionArguments(1, 1000, 10));
84 EXPECT_EQ(0, deserialized
->flags());
87 TEST_F(HistogramBaseTest
, DeserializeBooleanHistogram
) {
88 HistogramBase
* histogram
= BooleanHistogram::FactoryGet(
89 "TestHistogram", HistogramBase::kIPCSerializationSourceFlag
);
92 ASSERT_TRUE(histogram
->SerializeInfo(&pickle
));
94 PickleIterator
iter(pickle
);
95 HistogramBase
* deserialized
= DeserializeHistogramInfo(&iter
);
96 EXPECT_EQ(histogram
, deserialized
);
98 ResetStatisticsRecorder();
100 PickleIterator
iter2(pickle
);
101 deserialized
= DeserializeHistogramInfo(&iter2
);
102 EXPECT_TRUE(deserialized
);
103 EXPECT_NE(histogram
, deserialized
);
104 EXPECT_EQ("TestHistogram", deserialized
->histogram_name());
105 EXPECT_TRUE(deserialized
->HasConstructionArguments(1, 2, 3));
106 EXPECT_EQ(0, deserialized
->flags());
109 TEST_F(HistogramBaseTest
, DeserializeCustomHistogram
) {
110 std::vector
<HistogramBase::Sample
> ranges
;
111 ranges
.push_back(13);
115 HistogramBase
* histogram
= CustomHistogram::FactoryGet(
116 "TestHistogram", ranges
, HistogramBase::kIPCSerializationSourceFlag
);
119 ASSERT_TRUE(histogram
->SerializeInfo(&pickle
));
121 PickleIterator
iter(pickle
);
122 HistogramBase
* deserialized
= DeserializeHistogramInfo(&iter
);
123 EXPECT_EQ(histogram
, deserialized
);
125 ResetStatisticsRecorder();
127 PickleIterator
iter2(pickle
);
128 deserialized
= DeserializeHistogramInfo(&iter2
);
129 EXPECT_TRUE(deserialized
);
130 EXPECT_NE(histogram
, deserialized
);
131 EXPECT_EQ("TestHistogram", deserialized
->histogram_name());
132 EXPECT_TRUE(deserialized
->HasConstructionArguments(5, 13, 4));
133 EXPECT_EQ(0, deserialized
->flags());
136 TEST_F(HistogramBaseTest
, DeserializeSparseHistogram
) {
137 HistogramBase
* histogram
= SparseHistogram::FactoryGet(
138 "TestHistogram", HistogramBase::kIPCSerializationSourceFlag
);
141 ASSERT_TRUE(histogram
->SerializeInfo(&pickle
));
143 PickleIterator
iter(pickle
);
144 HistogramBase
* deserialized
= DeserializeHistogramInfo(&iter
);
145 EXPECT_EQ(histogram
, deserialized
);
147 ResetStatisticsRecorder();
149 PickleIterator
iter2(pickle
);
150 deserialized
= DeserializeHistogramInfo(&iter2
);
151 EXPECT_TRUE(deserialized
);
152 EXPECT_NE(histogram
, deserialized
);
153 EXPECT_EQ("TestHistogram", deserialized
->histogram_name());
154 EXPECT_EQ(0, deserialized
->flags());