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.h"
9 #include "base/metrics/histogram_samples.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 const std::string kHistogram1
= "Test1";
15 const std::string kHistogram2
= "Test2";
16 const std::string kHistogram3
= "Test3";
17 const std::string kHistogram4
= "Test4";
19 typedef testing::Test HistogramTesterTest
;
21 TEST_F(HistogramTesterTest
, Scope
) {
22 // Record a histogram before the creation of the recorder.
23 UMA_HISTOGRAM_BOOLEAN(kHistogram1
, true);
25 HistogramTester tester
;
27 // Verify that no histogram is recorded.
28 scoped_ptr
<HistogramSamples
> samples(
29 tester
.GetHistogramSamplesSinceCreation(kHistogram1
));
30 EXPECT_FALSE(samples
);
32 // Record a histogram after the creation of the recorder.
33 UMA_HISTOGRAM_BOOLEAN(kHistogram1
, true);
35 // Verify that one histogram is recorded.
36 samples
= tester
.GetHistogramSamplesSinceCreation(kHistogram1
);
38 EXPECT_EQ(1, samples
->TotalCount());
41 TEST_F(HistogramTesterTest
, TestUniqueSample
) {
42 HistogramTester tester
;
44 // Record into a sample thrice
45 UMA_HISTOGRAM_COUNTS_100(kHistogram2
, 2);
46 UMA_HISTOGRAM_COUNTS_100(kHistogram2
, 2);
47 UMA_HISTOGRAM_COUNTS_100(kHistogram2
, 2);
49 tester
.ExpectUniqueSample(kHistogram2
, 2, 3);
52 TEST_F(HistogramTesterTest
, TestBucketsSample
) {
53 HistogramTester tester
;
55 // Record into a sample twice
56 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
57 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
58 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
59 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 2);
60 UMA_HISTOGRAM_COUNTS_100(kHistogram3
, 3);
62 tester
.ExpectBucketCount(kHistogram3
, 2, 4);
63 tester
.ExpectBucketCount(kHistogram3
, 3, 1);
65 tester
.ExpectTotalCount(kHistogram3
, 5);
68 TEST_F(HistogramTesterTest
, TestBucketsSampleWithScope
) {
69 // Record into a sample twice, once before the tester creation and once after.
70 UMA_HISTOGRAM_COUNTS_100(kHistogram4
, 2);
72 HistogramTester tester
;
73 UMA_HISTOGRAM_COUNTS_100(kHistogram4
, 3);
75 tester
.ExpectBucketCount(kHistogram4
, 2, 0);
76 tester
.ExpectBucketCount(kHistogram4
, 3, 1);
78 tester
.ExpectTotalCount(kHistogram4
, 1);