1 // Copyright 2017 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.
4 
5 #include "base/metrics/single_sample_metrics.h"
6 
7 #include "base/metrics/dummy_histogram.h"
8 #include "base/test/gtest_util.h"
9 #include "base/test/metrics/histogram_tester.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace base {
13 
14 namespace {
15 
16 const HistogramBase::Sample kMin = 1;
17 const HistogramBase::Sample kMax = 10;
18 const uint32_t kBucketCount = 10;
19 const char kMetricName[] = "Single.Sample.Metric";
20 
21 class SingleSampleMetricsTest : public testing::Test {
22  public:
23   SingleSampleMetricsTest() = default;
24 
~SingleSampleMetricsTest()25   ~SingleSampleMetricsTest() override {
26     // Ensure we cleanup after ourselves.
27     SingleSampleMetricsFactory::DeleteFactoryForTesting();
28   }
29 
30  private:
31   DISALLOW_COPY_AND_ASSIGN(SingleSampleMetricsTest);
32 };
33 
34 }  // namespace
35 
TEST_F(SingleSampleMetricsTest,DefaultFactoryGetSet)36 TEST_F(SingleSampleMetricsTest, DefaultFactoryGetSet) {
37   SingleSampleMetricsFactory* factory = SingleSampleMetricsFactory::Get();
38   ASSERT_TRUE(factory);
39 
40   // Same factory should be returned evermore.
41   EXPECT_EQ(factory, SingleSampleMetricsFactory::Get());
42 
43   // Setting a factory after the default has been instantiated should fail.
44   EXPECT_DCHECK_DEATH(SingleSampleMetricsFactory::SetFactory(nullptr));
45 }
46 
TEST_F(SingleSampleMetricsTest,CustomFactoryGetSet)47 TEST_F(SingleSampleMetricsTest, CustomFactoryGetSet) {
48   auto factory = std::make_unique<DefaultSingleSampleMetricsFactory>();
49   SingleSampleMetricsFactory* factory_raw = factory.get();
50   SingleSampleMetricsFactory::SetFactory(std::move(factory));
51   EXPECT_EQ(factory_raw, SingleSampleMetricsFactory::Get());
52 }
53 
TEST_F(SingleSampleMetricsTest,DefaultSingleSampleMetricNoValue)54 TEST_F(SingleSampleMetricsTest, DefaultSingleSampleMetricNoValue) {
55   SingleSampleMetricsFactory* factory = SingleSampleMetricsFactory::Get();
56 
57   HistogramTester tester;
58   std::unique_ptr<SingleSampleMetric> metric =
59       factory->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount);
60   metric.reset();
61 
62   // Verify that no sample is recorded if SetSample() is never called.
63   tester.ExpectTotalCount(kMetricName, 0);
64 }
65 
TEST_F(SingleSampleMetricsTest,DefaultSingleSampleMetricWithValue)66 TEST_F(SingleSampleMetricsTest, DefaultSingleSampleMetricWithValue) {
67   SingleSampleMetricsFactory* factory = SingleSampleMetricsFactory::Get();
68 
69   HistogramTester tester;
70   std::unique_ptr<SingleSampleMetric> metric =
71       factory->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount);
72 
73   const HistogramBase::Sample kLastSample = 9;
74   metric->SetSample(1);
75   metric->SetSample(3);
76   metric->SetSample(5);
77   metric->SetSample(kLastSample);
78   metric.reset();
79 
80   // Verify only the last sample sent to SetSample() is recorded.
81   tester.ExpectUniqueSample(kMetricName, kLastSample, 1);
82 
83   // Verify construction implicitly by requesting a histogram with the same
84   // parameters; this test relies on the fact that histogram objects are unique
85   // per name. Different parameters will result in a Dummy histogram returned.
86   EXPECT_EQ(
87       DummyHistogram::GetInstance(),
88       Histogram::FactoryGet(kMetricName, 1, 3, 3, HistogramBase::kNoFlags));
89   EXPECT_NE(DummyHistogram::GetInstance(),
90             Histogram::FactoryGet(kMetricName, kMin, kMax, kBucketCount,
91                                   HistogramBase::kUmaTargetedHistogramFlag));
92 }
93 
TEST_F(SingleSampleMetricsTest,MultipleMetricsAreDistinct)94 TEST_F(SingleSampleMetricsTest, MultipleMetricsAreDistinct) {
95   SingleSampleMetricsFactory* factory = SingleSampleMetricsFactory::Get();
96 
97   HistogramTester tester;
98   std::unique_ptr<SingleSampleMetric> metric =
99       factory->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount);
100   std::unique_ptr<SingleSampleMetric> metric2 =
101       factory->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount);
102   const char kMetricName2[] = "Single.Sample.Metric.2";
103   std::unique_ptr<SingleSampleMetric> metric3 =
104       factory->CreateCustomCountsMetric(kMetricName2, kMin, kMax, kBucketCount);
105 
106   const HistogramBase::Sample kSample1 = 5;
107   metric->SetSample(kSample1);
108   metric2->SetSample(kSample1);
109 
110   const HistogramBase::Sample kSample2 = 7;
111   metric3->SetSample(kSample2);
112 
113   metric.reset();
114   tester.ExpectUniqueSample(kMetricName, kSample1, 1);
115 
116   metric2.reset();
117   tester.ExpectUniqueSample(kMetricName, kSample1, 2);
118 
119   metric3.reset();
120   tester.ExpectUniqueSample(kMetricName2, kSample2, 1);
121 }
122 
123 }  // namespace base
124