OLD | NEW |
(Empty) | |
| 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 "components/metrics/single_sample_metrics_provider.h" |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/test/gtest_util.h" |
| 10 #include "base/test/histogram_tester.h" |
| 11 #include "mojo/public/cpp/bindings/interface_request.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace metrics { |
| 15 |
| 16 namespace { |
| 17 |
| 18 const base::HistogramBase::Sample kMin = 1; |
| 19 const base::HistogramBase::Sample kMax = 10; |
| 20 const uint32_t kBucketCount = 10; |
| 21 const char kMetricName[] = "Single.Sample.Metric"; |
| 22 |
| 23 class SingleSampleMetricsProviderTest : public testing::Test { |
| 24 public: |
| 25 SingleSampleMetricsProviderTest() { |
| 26 SingleSampleMetricsProvider::Create(mojo::MakeRequest(&provider_)); |
| 27 } |
| 28 |
| 29 ~SingleSampleMetricsProviderTest() override {} |
| 30 |
| 31 void CreateMetric(mojom::SingleSampleMetricRequest request) { |
| 32 provider_->AcquireSingleSampleMetric( |
| 33 kMetricName, kMin, kMax, kBucketCount, |
| 34 base::HistogramBase::kUmaTargetedHistogramFlag, std::move(request)); |
| 35 } |
| 36 |
| 37 private: |
| 38 base::MessageLoop message_loop_; |
| 39 mojom::SingleSampleMetricsProviderPtr provider_; |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(SingleSampleMetricsProviderTest); |
| 42 }; |
| 43 |
| 44 } // namespace |
| 45 |
| 46 TEST_F(SingleSampleMetricsProviderTest, DefaultSingleSampleMetricNoValue) { |
| 47 base::HistogramTester tester; |
| 48 mojom::SingleSampleMetricPtr metric; |
| 49 CreateMetric(mojo::MakeRequest(&metric)); |
| 50 metric.reset(); |
| 51 |
| 52 // Verify that no sample is recorded if SetSample() is never called. |
| 53 base::RunLoop().RunUntilIdle(); |
| 54 tester.ExpectTotalCount(kMetricName, 0); |
| 55 } |
| 56 |
| 57 TEST_F(SingleSampleMetricsProviderTest, DefaultSingleSampleMetricWithValue) { |
| 58 base::HistogramTester tester; |
| 59 mojom::SingleSampleMetricPtr metric; |
| 60 CreateMetric(mojo::MakeRequest(&metric)); |
| 61 |
| 62 const base::HistogramBase::Sample kLastSample = 9; |
| 63 metric->SetSample(1); |
| 64 metric->SetSample(3); |
| 65 metric->SetSample(5); |
| 66 metric->SetSample(kLastSample); |
| 67 metric.reset(); |
| 68 |
| 69 // Verify only the last sample sent to SetSample() is recorded. |
| 70 base::RunLoop().RunUntilIdle(); |
| 71 tester.ExpectUniqueSample(kMetricName, kLastSample, 1); |
| 72 |
| 73 // Verify construction implicitly by requesting a histogram with the same |
| 74 // parameters; this test relies on the fact that histogram objects are unique |
| 75 // per name. Different parameters will result in a nullptr being returned. |
| 76 EXPECT_FALSE(base::Histogram::FactoryGet(kMetricName, 1, 3, 3, |
| 77 base::HistogramBase::kNoFlags)); |
| 78 EXPECT_TRUE(base::Histogram::FactoryGet( |
| 79 kMetricName, kMin, kMax, kBucketCount, |
| 80 base::HistogramBase::kUmaTargetedHistogramFlag)); |
| 81 } |
| 82 |
| 83 } // namespace metrics |
OLD | NEW |