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"
15 using ::testing::ElementsAre
;
17 const std::string kHistogram1
= "Test1";
18 const std::string kHistogram2
= "Test2";
19 const std::string kHistogram3
= "Test3";
20 const std::string kHistogram4
= "Test4";
21 const std::string kHistogram5
= "Test5";
23 typedef testing::Test HistogramTesterTest
;
25 TEST_F(HistogramTesterTest
, Scope
) {
26 // Record a histogram before the creation of the recorder.
27 UMA_HISTOGRAM_BOOLEAN(kHistogram1
, true);
29 HistogramTester tester
;
31 // Verify that no histogram is recorded.
32 scoped_ptr
<HistogramSamples
> samples(
33 tester
.GetHistogramSamplesSinceCreation(kHistogram1
));
34 EXPECT_FALSE(samples
);
36 // Record a histogram after the creation of the recorder.
37 UMA_HISTOGRAM_BOOLEAN(kHistogram1
, true);
39 // Verify that one histogram is recorded.
40 samples
= tester
.GetHistogramSamplesSinceCreation(kHistogram1
);
42 EXPECT_EQ(1, samples
->TotalCount());
45 TEST_F(HistogramTesterTest
, TestUniqueSample
) {
46 HistogramTester tester
;
48 // Record into a sample thrice
49 UMA_HISTOGRAM_COUNTS_100(kHistogram2
, 2);
50 UMA_HISTOGRAM_COUNTS_100(kHistogram2
, 2);
51 UMA_HISTOGRAM_COUNTS_100(kHistogram2
, 2);
53 tester
.ExpectUniqueSample(kHistogram2
, 2, 3);
56 TEST_F(HistogramTesterTest
, TestBucketsSample
) {
57 HistogramTester tester
;
59 // Record into a sample twice
60 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
61 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
62 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
63 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
64 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 3);
66 tester
.ExpectBucketCount(kHistogram3
, 2, 4);
67 tester
.ExpectBucketCount(kHistogram3
, 3, 1);
69 tester
.ExpectTotalCount(kHistogram3
, 5);
72 TEST_F(HistogramTesterTest
, TestBucketsSampleWithScope
) {
73 // Record into a sample twice, once before the tester creation and once after.
74 UMA_HISTOGRAM_COUNTS_100(kHistogram4
, 2);
76 HistogramTester tester
;
77 UMA_HISTOGRAM_COUNTS_100(kHistogram4
, 3);
79 tester
.ExpectBucketCount(kHistogram4
, 2, 0);
80 tester
.ExpectBucketCount(kHistogram4
, 3, 1);
82 tester
.ExpectTotalCount(kHistogram4
, 1);
85 TEST_F(HistogramTesterTest
, TestGetAllSamples
) {
86 HistogramTester tester
;
87 UMA_HISTOGRAM_ENUMERATION(kHistogram5
, 2, 5);
88 UMA_HISTOGRAM_ENUMERATION(kHistogram5
, 3, 5);
89 UMA_HISTOGRAM_ENUMERATION(kHistogram5
, 3, 5);
90 UMA_HISTOGRAM_ENUMERATION(kHistogram5
, 5, 5);
92 EXPECT_THAT(tester
.GetAllSamples(kHistogram5
),
93 ElementsAre(Bucket(2, 1), Bucket(3, 2), Bucket(5, 1)));