Use UintToString() for unsigned values.
[chromium-blink-merge.git] / base / test / histogram_tester_unittest.cc
blob21b81703c648c28d317dde656f83d7c191bd6081
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;
14 using ::testing::IsEmpty;
16 namespace {
18 const char kHistogram1[] = "Test1";
19 const char kHistogram2[] = "Test2";
20 const char kHistogram3[] = "Test3";
21 const char kHistogram4[] = "Test4";
22 const char kHistogram5[] = "Test5";
24 } // namespace
26 namespace base {
28 typedef testing::Test HistogramTesterTest;
30 TEST_F(HistogramTesterTest, Scope) {
31 // Record a histogram before the creation of the recorder.
32 UMA_HISTOGRAM_BOOLEAN(kHistogram1, true);
34 HistogramTester tester;
36 // Verify that no histogram is recorded.
37 tester.ExpectTotalCount(kHistogram1, 0);
39 // Record a histogram after the creation of the recorder.
40 UMA_HISTOGRAM_BOOLEAN(kHistogram1, true);
42 // Verify that one histogram is recorded.
43 scoped_ptr<HistogramSamples> samples(
44 tester.GetHistogramSamplesSinceCreation(kHistogram1));
45 EXPECT_TRUE(samples);
46 EXPECT_EQ(1, samples->TotalCount());
49 TEST_F(HistogramTesterTest, GetHistogramSamplesSinceCreationNotNull) {
50 // Chose the histogram name uniquely, to ensure nothing was recorded for it so
51 // far.
52 static const char kHistogram[] =
53 "GetHistogramSamplesSinceCreationNotNullHistogram";
54 HistogramTester tester;
56 // Verify that the returned samples are empty but not null.
57 scoped_ptr<HistogramSamples> samples(
58 tester.GetHistogramSamplesSinceCreation(kHistogram1));
59 EXPECT_TRUE(samples);
60 tester.ExpectTotalCount(kHistogram, 0);
63 TEST_F(HistogramTesterTest, TestUniqueSample) {
64 HistogramTester tester;
66 // Record into a sample thrice
67 UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2);
68 UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2);
69 UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2);
71 tester.ExpectUniqueSample(kHistogram2, 2, 3);
74 TEST_F(HistogramTesterTest, TestBucketsSample) {
75 HistogramTester tester;
77 // Record into a sample twice
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, 2);
82 UMA_HISTOGRAM_COUNTS_100(kHistogram3, 3);
84 tester.ExpectBucketCount(kHistogram3, 2, 4);
85 tester.ExpectBucketCount(kHistogram3, 3, 1);
87 tester.ExpectTotalCount(kHistogram3, 5);
90 TEST_F(HistogramTesterTest, TestBucketsSampleWithScope) {
91 // Record into a sample twice, once before the tester creation and once after.
92 UMA_HISTOGRAM_COUNTS_100(kHistogram4, 2);
94 HistogramTester tester;
95 UMA_HISTOGRAM_COUNTS_100(kHistogram4, 3);
97 tester.ExpectBucketCount(kHistogram4, 2, 0);
98 tester.ExpectBucketCount(kHistogram4, 3, 1);
100 tester.ExpectTotalCount(kHistogram4, 1);
103 TEST_F(HistogramTesterTest, TestGetAllSamples) {
104 HistogramTester tester;
105 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 2, 5);
106 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 3, 5);
107 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 3, 5);
108 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 5, 5);
110 EXPECT_THAT(tester.GetAllSamples(kHistogram5),
111 ElementsAre(Bucket(2, 1), Bucket(3, 2), Bucket(5, 1)));
114 TEST_F(HistogramTesterTest, TestGetAllSamples_NoSamples) {
115 HistogramTester tester;
116 EXPECT_THAT(tester.GetAllSamples(kHistogram5), IsEmpty());
119 } // namespace base