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/metrics/histogram.h" 11#include "base/metrics/histogram_delta_serialization.h" 12#include "base/metrics/statistics_recorder.h" 13#include "testing/gtest/include/gtest/gtest.h" 14 15namespace base { 16 17class HistogramFlattenerDeltaRecorder : public HistogramFlattener { 18 public: 19 HistogramFlattenerDeltaRecorder() {} 20 21 virtual void RecordDelta(const HistogramBase& histogram, 22 const HistogramSamples& snapshot) OVERRIDE { 23 recorded_delta_histogram_names_.push_back(histogram.histogram_name()); 24 } 25 26 virtual void InconsistencyDetected( 27 HistogramBase::Inconsistency problem) OVERRIDE { 28 ASSERT_TRUE(false); 29 } 30 31 virtual void UniqueInconsistencyDetected( 32 HistogramBase::Inconsistency problem) OVERRIDE { 33 ASSERT_TRUE(false); 34 } 35 36 virtual 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 50class HistogramSnapshotManagerTest : public testing::Test { 51 protected: 52 HistogramSnapshotManagerTest() 53 : histogram_snapshot_manager_(&histogram_flattener_delta_recorder_) {} 54 55 virtual ~HistogramSnapshotManagerTest() {} 56 57 StatisticsRecorder statistics_recorder_; 58 HistogramFlattenerDeltaRecorder histogram_flattener_delta_recorder_; 59 HistogramSnapshotManager histogram_snapshot_manager_; 60}; 61 62TEST_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 77TEST_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 92TEST_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