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 #include "base/test/histogram_tester.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/metrics/histogram_samples.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using ::testing::ElementsAre
;
17 const char kHistogram1
[] = "Test1";
18 const char kHistogram2
[] = "Test2";
19 const char kHistogram3
[] = "Test3";
20 const char kHistogram4
[] = "Test4";
21 const char kHistogram5
[] = "Test5";
27 typedef testing::Test HistogramTesterTest
;
29 TEST_F(HistogramTesterTest
, Scope
) {
30 // Record a histogram before the creation of the recorder.
31 UMA_HISTOGRAM_BOOLEAN(kHistogram1
, true);
33 HistogramTester tester
;
35 // Verify that no histogram is recorded.
36 tester
.ExpectTotalCount(kHistogram1
, 0);
38 // Record a histogram after the creation of the recorder.
39 UMA_HISTOGRAM_BOOLEAN(kHistogram1
, true);
41 // Verify that one histogram is recorded.
42 scoped_ptr
<HistogramSamples
> samples(
43 tester
.GetHistogramSamplesSinceCreation(kHistogram1
));
45 EXPECT_EQ(1, samples
->TotalCount());
48 TEST_F(HistogramTesterTest
, GetHistogramSamplesSinceCreationNotNull
) {
49 // Chose the histogram name uniquely, to ensure nothing was recorded for it so
51 static const char kHistogram
[] =
52 "GetHistogramSamplesSinceCreationNotNullHistogram";
53 HistogramTester tester
;
55 // Verify that the returned samples are empty but not null.
56 scoped_ptr
<HistogramSamples
> samples(
57 tester
.GetHistogramSamplesSinceCreation(kHistogram1
));
59 tester
.ExpectTotalCount(kHistogram
, 0);
62 TEST_F(HistogramTesterTest
, TestUniqueSample
) {
63 HistogramTester tester
;
65 // Record into a sample thrice
66 UMA_HISTOGRAM_COUNTS_100(kHistogram2
, 2);
67 UMA_HISTOGRAM_COUNTS_100(kHistogram2
, 2);
68 UMA_HISTOGRAM_COUNTS_100(kHistogram2
, 2);
70 tester
.ExpectUniqueSample(kHistogram2
, 2, 3);
73 TEST_F(HistogramTesterTest
, TestBucketsSample
) {
74 HistogramTester tester
;
76 // Record into a sample twice
77 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
78 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
79 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
80 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
81 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 3);
83 tester
.ExpectBucketCount(kHistogram3
, 2, 4);
84 tester
.ExpectBucketCount(kHistogram3
, 3, 1);
86 tester
.ExpectTotalCount(kHistogram3
, 5);
89 TEST_F(HistogramTesterTest
, TestBucketsSampleWithScope
) {
90 // Record into a sample twice, once before the tester creation and once after.
91 UMA_HISTOGRAM_COUNTS_100(kHistogram4
, 2);
93 HistogramTester tester
;
94 UMA_HISTOGRAM_COUNTS_100(kHistogram4
, 3);
96 tester
.ExpectBucketCount(kHistogram4
, 2, 0);
97 tester
.ExpectBucketCount(kHistogram4
, 3, 1);
99 tester
.ExpectTotalCount(kHistogram4
, 1);
102 TEST_F(HistogramTesterTest
, TestGetAllSamples
) {
103 HistogramTester tester
;
104 UMA_HISTOGRAM_ENUMERATION(kHistogram5
, 2, 5);
105 UMA_HISTOGRAM_ENUMERATION(kHistogram5
, 3, 5);
106 UMA_HISTOGRAM_ENUMERATION(kHistogram5
, 3, 5);
107 UMA_HISTOGRAM_ENUMERATION(kHistogram5
, 5, 5);
109 EXPECT_THAT(tester
.GetAllSamples(kHistogram5
),
110 ElementsAre(Bucket(2, 1), Bucket(3, 2), Bucket(5, 1)));