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.
5 #ifndef BASE_TEST_HISTOGRAM_TESTER_H_
6 #define BASE_TEST_HISTOGRAM_TESTER_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/histogram.h"
14 #include "base/metrics/histogram_base.h"
18 class HistogramSamples
;
20 // HistogramTester provides a simple interface for examining histograms, UMA
21 // or otherwise. Tests can use this interface to verify that histogram data is
22 // getting logged as intended.
23 class HistogramTester
{
25 // The constructor will call StatisticsRecorder::Initialize() for you. Also,
26 // this takes a snapshot of all current histograms counts.
30 // We know the exact number of samples in a bucket, and that no other bucket
31 // should have samples. Measures the diff from the snapshot taken when this
32 // object was constructed.
33 void ExpectUniqueSample(const std::string
& name
,
34 base::HistogramBase::Sample sample
,
35 base::HistogramBase::Count expected_count
) const;
37 // We know the exact number of samples in a bucket, but other buckets may
38 // have samples as well. Measures the diff from the snapshot taken when this
39 // object was constructed.
40 void ExpectBucketCount(const std::string
& name
,
41 base::HistogramBase::Sample sample
,
42 base::HistogramBase::Count expected_count
) const;
44 // We don't know the values of the samples, but we know how many there are.
45 // This measures the diff from the snapshot taken when this object was
47 void ExpectTotalCount(const std::string
& name
,
48 base::HistogramBase::Count count
) const;
50 // Access a modified HistogramSamples containing only what has been logged
51 // to the histogram since the creation of this object.
52 scoped_ptr
<HistogramSamples
> GetHistogramSamplesSinceCreation(
53 const std::string
& histogram_name
);
56 // Verifies and asserts that value in the |sample| bucket matches the
57 // |expected_count|. The bucket's current value is determined from |samples|
58 // and is modified based on the snapshot stored for histogram |name|.
59 void CheckBucketCount(const std::string
& name
,
60 base::HistogramBase::Sample sample
,
61 base::Histogram::Count expected_count
,
62 base::HistogramSamples
& samples
) const;
64 // Verifies that the total number of values recorded for the histogram |name|
65 // is |expected_count|. This is checked against |samples| minus the snapshot
66 // that was taken for |name|.
67 void CheckTotalCount(const std::string
& name
,
68 base::Histogram::Count expected_count
,
69 base::HistogramSamples
& samples
) const;
71 // Used to determine the histogram changes made during this instance's
72 // lifecycle. This instance takes ownership of the samples, which are deleted
73 // when the instance is destroyed.
74 std::map
<std::string
, HistogramSamples
*> histograms_snapshot_
;
76 DISALLOW_COPY_AND_ASSIGN(HistogramTester
);
81 #endif // BASE_TEST_HISTOGRAM_TESTER_H_