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. 4 5 #include <vector> 6 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" 13 14 namespace base { 15 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(); 23 } 24 25 virtual ~HistogramBaseTest() { 26 delete statistics_recorder_; 27 } 28 29 void ResetStatisticsRecorder() { 30 delete statistics_recorder_; 31 statistics_recorder_ = new StatisticsRecorder(); 32 } 33 34 private: 35 StatisticsRecorder* statistics_recorder_; 36 }; 37 38 TEST_F(HistogramBaseTest, DeserializeHistogram) { 39 HistogramBase* histogram = Histogram::FactoryGet( 40 "TestHistogram", 1, 1000, 10, 41 (HistogramBase::kUmaTargetedHistogramFlag | 42 HistogramBase::kIPCSerializationSourceFlag)); 43 44 Pickle pickle; 45 ASSERT_TRUE(histogram->SerializeInfo(&pickle)); 46 47 PickleIterator iter(pickle); 48 HistogramBase* deserialized = DeserializeHistogramInfo(&iter); 49 EXPECT_EQ(histogram, deserialized); 50 51 ResetStatisticsRecorder(); 52 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)); 59 60 // kIPCSerializationSourceFlag will be cleared. 61 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, deserialized->flags()); 62 } 63 64 TEST_F(HistogramBaseTest, DeserializeLinearHistogram) { 65 HistogramBase* histogram = LinearHistogram::FactoryGet( 66 "TestHistogram", 1, 1000, 10, 67 HistogramBase::kIPCSerializationSourceFlag); 68 69 Pickle pickle; 70 ASSERT_TRUE(histogram->SerializeInfo(&pickle)); 71 72 PickleIterator iter(pickle); 73 HistogramBase* deserialized = DeserializeHistogramInfo(&iter); 74 EXPECT_EQ(histogram, deserialized); 75 76 ResetStatisticsRecorder(); 77 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()); 85 } 86 87 TEST_F(HistogramBaseTest, DeserializeBooleanHistogram) { 88 HistogramBase* histogram = BooleanHistogram::FactoryGet( 89 "TestHistogram", HistogramBase::kIPCSerializationSourceFlag); 90 91 Pickle pickle; 92 ASSERT_TRUE(histogram->SerializeInfo(&pickle)); 93 94 PickleIterator iter(pickle); 95 HistogramBase* deserialized = DeserializeHistogramInfo(&iter); 96 EXPECT_EQ(histogram, deserialized); 97 98 ResetStatisticsRecorder(); 99 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()); 107 } 108 109 TEST_F(HistogramBaseTest, DeserializeCustomHistogram) { 110 std::vector<HistogramBase::Sample> ranges; 111 ranges.push_back(13); 112 ranges.push_back(5); 113 ranges.push_back(9); 114 115 HistogramBase* histogram = CustomHistogram::FactoryGet( 116 "TestHistogram", ranges, HistogramBase::kIPCSerializationSourceFlag); 117 118 Pickle pickle; 119 ASSERT_TRUE(histogram->SerializeInfo(&pickle)); 120 121 PickleIterator iter(pickle); 122 HistogramBase* deserialized = DeserializeHistogramInfo(&iter); 123 EXPECT_EQ(histogram, deserialized); 124 125 ResetStatisticsRecorder(); 126 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()); 134 } 135 136 TEST_F(HistogramBaseTest, DeserializeSparseHistogram) { 137 HistogramBase* histogram = SparseHistogram::FactoryGet( 138 "TestHistogram", HistogramBase::kIPCSerializationSourceFlag); 139 140 Pickle pickle; 141 ASSERT_TRUE(histogram->SerializeInfo(&pickle)); 142 143 PickleIterator iter(pickle); 144 HistogramBase* deserialized = DeserializeHistogramInfo(&iter); 145 EXPECT_EQ(histogram, deserialized); 146 147 ResetStatisticsRecorder(); 148 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()); 155 } 156 157 } // namespace base 158