OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/test/histogram_tester.h" | 5 #include "base/test/histogram_tester.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
9 #include "base/metrics/histogram_samples.h" | 9 #include "base/metrics/histogram_samples.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 using ::testing::ElementsAre; | 15 using ::testing::ElementsAre; |
16 | 16 |
17 const std::string kHistogram1 = "Test1"; | 17 static const char kHistogram1[] = "Test1"; |
vabr (Chromium)
2015/08/03 07:33:37
@thestig -- Here I followed you request literally,
Lei Zhang
2015/08/03 18:00:30
An anonymous namespace would have been fine. I sho
| |
18 const std::string kHistogram2 = "Test2"; | 18 static const char kHistogram2[] = "Test2"; |
19 const std::string kHistogram3 = "Test3"; | 19 static const char kHistogram3[] = "Test3"; |
20 const std::string kHistogram4 = "Test4"; | 20 static const char kHistogram4[] = "Test4"; |
21 const std::string kHistogram5 = "Test5"; | 21 static const char kHistogram5[] = "Test5"; |
22 | 22 |
23 typedef testing::Test HistogramTesterTest; | 23 typedef testing::Test HistogramTesterTest; |
24 | 24 |
25 TEST_F(HistogramTesterTest, Scope) { | 25 TEST_F(HistogramTesterTest, Scope) { |
26 // Record a histogram before the creation of the recorder. | 26 // Record a histogram before the creation of the recorder. |
27 UMA_HISTOGRAM_BOOLEAN(kHistogram1, true); | 27 UMA_HISTOGRAM_BOOLEAN(kHistogram1, true); |
28 | 28 |
29 HistogramTester tester; | 29 HistogramTester tester; |
30 | 30 |
31 // Verify that no histogram is recorded. | 31 // Verify that no histogram is recorded. |
32 scoped_ptr<HistogramSamples> samples( | 32 tester.ExpectTotalCount(kHistogram1, 0); |
33 tester.GetHistogramSamplesSinceCreation(kHistogram1)); | |
34 EXPECT_FALSE(samples); | |
35 | 33 |
36 // Record a histogram after the creation of the recorder. | 34 // Record a histogram after the creation of the recorder. |
37 UMA_HISTOGRAM_BOOLEAN(kHistogram1, true); | 35 UMA_HISTOGRAM_BOOLEAN(kHistogram1, true); |
38 | 36 |
39 // Verify that one histogram is recorded. | 37 // Verify that one histogram is recorded. |
40 samples = tester.GetHistogramSamplesSinceCreation(kHistogram1); | 38 scoped_ptr<HistogramSamples> samples( |
39 tester.GetHistogramSamplesSinceCreation(kHistogram1)); | |
41 EXPECT_TRUE(samples); | 40 EXPECT_TRUE(samples); |
42 EXPECT_EQ(1, samples->TotalCount()); | 41 EXPECT_EQ(1, samples->TotalCount()); |
43 } | 42 } |
44 | 43 |
44 TEST_F(HistogramTesterTest, GetHistogramSamplesSinceCreationNotNull) { | |
45 // Chose the histogram name uniquely, to ensure nothing was recorded for it so | |
46 // far. | |
47 static const char kHistogram[] = | |
48 "GetHistogramSamplesSinceCreationNotNullHistogram"; | |
49 HistogramTester tester; | |
50 | |
51 // Verify that the returned samples are empty but not null. | |
52 scoped_ptr<HistogramSamples> samples( | |
53 tester.GetHistogramSamplesSinceCreation(kHistogram1)); | |
54 EXPECT_TRUE(samples); | |
55 tester.ExpectTotalCount(kHistogram, 0); | |
56 } | |
57 | |
45 TEST_F(HistogramTesterTest, TestUniqueSample) { | 58 TEST_F(HistogramTesterTest, TestUniqueSample) { |
46 HistogramTester tester; | 59 HistogramTester tester; |
47 | 60 |
48 // Record into a sample thrice | 61 // Record into a sample thrice |
49 UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2); | 62 UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2); |
50 UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2); | 63 UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2); |
51 UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2); | 64 UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2); |
52 | 65 |
53 tester.ExpectUniqueSample(kHistogram2, 2, 3); | 66 tester.ExpectUniqueSample(kHistogram2, 2, 3); |
54 } | 67 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 2, 5); | 100 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 2, 5); |
88 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 3, 5); | 101 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 3, 5); |
89 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 3, 5); | 102 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 3, 5); |
90 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 5, 5); | 103 UMA_HISTOGRAM_ENUMERATION(kHistogram5, 5, 5); |
91 | 104 |
92 EXPECT_THAT(tester.GetAllSamples(kHistogram5), | 105 EXPECT_THAT(tester.GetAllSamples(kHistogram5), |
93 ElementsAre(Bucket(2, 1), Bucket(3, 2), Bucket(5, 1))); | 106 ElementsAre(Bucket(2, 1), Bucket(3, 2), Bucket(5, 1))); |
94 } | 107 } |
95 | 108 |
96 } // namespace base | 109 } // namespace base |
OLD | NEW |