1 // Copyright 2014 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 "base/metrics/histogram_snapshot_manager.h" 6 7 #include <string> 8 #include <vector> 9 10 #include "base/macros.h" 11 #include "base/metrics/histogram_delta_serialization.h" 12 #include "base/metrics/histogram_macros.h" 13 #include "base/metrics/statistics_recorder.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace base { 17 18 class HistogramFlattenerDeltaRecorder : public HistogramFlattener { 19 public: 20 HistogramFlattenerDeltaRecorder() {} 21 22 void RecordDelta(const HistogramBase& histogram, 23 const HistogramSamples& snapshot) override { 24 recorded_delta_histogram_names_.push_back(histogram.histogram_name()); 25 } 26 27 void InconsistencyDetected(HistogramBase::Inconsistency problem) override { 28 ASSERT_TRUE(false); 29 } 30 31 void UniqueInconsistencyDetected( 32 HistogramBase::Inconsistency problem) override { 33 ASSERT_TRUE(false); 34 } 35 36 void InconsistencyDetectedInLoggedCount(int amount) override { 37 ASSERT_TRUE(false); 38 } 39 40 std::vector<std::string> GetRecordedDeltaHistogramNames() { 41 return recorded_delta_histogram_names_; 42 } 43 44 private: 45 std::vector<std::string> recorded_delta_histogram_names_; 46 47 DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder); 48 }; 49 50 class HistogramSnapshotManagerTest : public testing::Test { 51 protected: 52 HistogramSnapshotManagerTest() 53 : histogram_snapshot_manager_(&histogram_flattener_delta_recorder_) {} 54 55 ~HistogramSnapshotManagerTest() override {} 56 57 StatisticsRecorder statistics_recorder_; 58 HistogramFlattenerDeltaRecorder histogram_flattener_delta_recorder_; 59 HistogramSnapshotManager histogram_snapshot_manager_; 60 }; 61 62 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasNoFlagsFilter) { 63 // kNoFlags filter should record all histograms. 64 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 2); 65 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); 66 67 histogram_snapshot_manager_.PrepareDeltas(HistogramBase::kNoFlags, 68 HistogramBase::kNoFlags); 69 70 const std::vector<std::string>& histograms = 71 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); 72 EXPECT_EQ(2U, histograms.size()); 73 EXPECT_EQ("UmaHistogram", histograms[0]); 74 EXPECT_EQ("UmaStabilityHistogram", histograms[1]); 75 } 76 77 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasUmaHistogramFlagFilter) { 78 // Note that kUmaStabilityHistogramFlag includes kUmaTargetedHistogramFlag. 79 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 2); 80 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); 81 82 histogram_snapshot_manager_.PrepareDeltas( 83 HistogramBase::kNoFlags, HistogramBase::kUmaTargetedHistogramFlag); 84 85 const std::vector<std::string>& histograms = 86 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); 87 EXPECT_EQ(2U, histograms.size()); 88 EXPECT_EQ("UmaHistogram", histograms[0]); 89 EXPECT_EQ("UmaStabilityHistogram", histograms[1]); 90 } 91 92 TEST_F(HistogramSnapshotManagerTest, 93 PrepareDeltasUmaStabilityHistogramFlagFilter) { 94 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 2); 95 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); 96 97 histogram_snapshot_manager_.PrepareDeltas( 98 HistogramBase::kNoFlags, HistogramBase::kUmaStabilityHistogramFlag); 99 100 const std::vector<std::string>& histograms = 101 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); 102 EXPECT_EQ(1U, histograms.size()); 103 EXPECT_EQ("UmaStabilityHistogram", histograms[0]); 104 } 105 106 } // namespace base 107