Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ahmed Nasr | 9e477705 | 2022-11-02 20:38:39 | [diff] [blame] | 5 | #include "components/reporting/metrics/fakes/fake_sampler.h" |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 6 | |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 7 | #include <optional> |
Ahmed Nasr | 27cd9f4e | 2021-11-12 22:49:40 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
Ahmed Nasr | d9af70c | 2022-09-02 17:45:10 | [diff] [blame] | 10 | #include "base/check.h" |
Ahmed Nasr | b94c2d3 | 2022-11-07 22:18:16 | [diff] [blame] | 11 | #include "components/reporting/metrics/sampler.h" |
| 12 | #include "components/reporting/proto/synced/metric_data.pb.h" |
Ahmed Nasr | 27cd9f4e | 2021-11-12 22:49:40 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
Ahmed Nasr | b94c2d3 | 2022-11-07 22:18:16 | [diff] [blame] | 15 | namespace reporting::test { |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 16 | |
Ahmed Nasr | c4e332b | 2022-04-29 00:51:54 | [diff] [blame] | 17 | FakeSampler::FakeSampler() = default; |
| 18 | |
| 19 | FakeSampler::~FakeSampler() = default; |
| 20 | |
Ahmed Nasr | c4e332b | 2022-04-29 00:51:54 | [diff] [blame] | 21 | void FakeSampler::MaybeCollect(OptionalMetricCallback cb) { |
Ahmed Nasr | d9af70c | 2022-09-02 17:45:10 | [diff] [blame] | 22 | ++num_calls_; |
Ahmed Nasr | a055e52 | 2021-12-20 21:09:41 | [diff] [blame] | 23 | std::move(cb).Run(metric_data_); |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 24 | } |
| 25 | |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 26 | void FakeSampler::SetMetricData(std::optional<MetricData> metric_data) { |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 27 | metric_data_ = std::move(metric_data); |
| 28 | } |
| 29 | |
| 30 | int FakeSampler::GetNumCollectCalls() const { |
| 31 | return num_calls_; |
| 32 | } |
Ahmed Nasr | 27cd9f4e | 2021-11-12 22:49:40 | [diff] [blame] | 33 | |
Ahmed Nasr | d9af70c | 2022-09-02 17:45:10 | [diff] [blame] | 34 | FakeDelayedSampler::FakeDelayedSampler() = default; |
| 35 | |
| 36 | FakeDelayedSampler::~FakeDelayedSampler() = default; |
| 37 | |
| 38 | void FakeDelayedSampler::MaybeCollect(OptionalMetricCallback cb) { |
| 39 | ++num_calls_; |
| 40 | cb_ = std::move(cb); |
| 41 | } |
| 42 | |
| 43 | void FakeDelayedSampler::RunCallback() { |
Leonid Baraz | 48447f14 | 2023-07-31 19:46:21 | [diff] [blame] | 44 | CHECK(cb_); |
Ahmed Nasr | d9af70c | 2022-09-02 17:45:10 | [diff] [blame] | 45 | std::move(cb_).Run(metric_data_); |
| 46 | } |
| 47 | |
Ahmed Nasr | b94c2d3 | 2022-11-07 22:18:16 | [diff] [blame] | 48 | } // namespace reporting::test |