1 // Copyright 2018 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 "components/scheduling_metrics/total_duration_metric_reporter.h"
6 #include "base/test/metrics/histogram_tester.h"
7 #include "testing/gmock/include/gmock/gmock.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace scheduling_metrics {
12 TEST(TotalDurationMetricReporterTest, Test) {
13 base::HistogramTester histogram_tester;
15 TotalDurationMetricReporter metric_reporter("TestHistogram.Positive",
16 "TestHistogram.Negative");
18 metric_reporter.RecordAdditionalDuration(base::TimeDelta::FromSeconds(1));
19 metric_reporter.RecordAdditionalDuration(base::TimeDelta::FromSeconds(2));
20 metric_reporter.RecordAdditionalDuration(base::TimeDelta::FromSeconds(5));
22 metric_reporter.Reset();
24 metric_reporter.RecordAdditionalDuration(base::TimeDelta::FromSeconds(10));
26 std::map<base::Histogram::Sample, base::HistogramBase::Count> result;
28 for (const base::Bucket& bucket :
29 histogram_tester.GetAllSamples("TestHistogram.Positive")) {
30 result[bucket.min] += bucket.count;
32 for (const base::Bucket& bucket :
33 histogram_tester.GetAllSamples("TestHistogram.Negative")) {
34 result[bucket.min] -= bucket.count;
37 // 1 and 3 correspond to "reverted" values.
38 EXPECT_THAT(result, testing::UnorderedElementsAre(
39 std::make_pair(1, 0), std::make_pair(3, 0),
40 std::make_pair(8, 1), std::make_pair(10, 1)));
43 } // namespace scheduling_metrics