Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 1 | // Copyright 2021 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/reporting/metrics/fake_sampler.h" |
| 6 | |
Ahmed Nasr | 27cd9f4e | 2021-11-12 22:49:40 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
| 9 | #include "base/location.h" |
| 10 | #include "base/run_loop.h" |
| 11 | #include "base/threading/sequenced_task_runner_handle.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 14 | namespace reporting { |
| 15 | namespace test { |
| 16 | |
Ahmed Nasr | c4e332b | 2022-04-29 00:51:54 | [diff] [blame^] | 17 | FakeSampler::FakeSampler() = default; |
| 18 | |
| 19 | FakeSampler::~FakeSampler() = default; |
| 20 | |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 21 | void FakeSampler::Collect(MetricCallback cb) { |
Ahmed Nasr | c4e332b | 2022-04-29 00:51:54 | [diff] [blame^] | 22 | ASSERT_TRUE(metric_data_.has_value()); |
| 23 | num_calls_++; |
| 24 | std::move(cb).Run(metric_data_.value()); |
| 25 | } |
| 26 | |
| 27 | void FakeSampler::MaybeCollect(OptionalMetricCallback cb) { |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 28 | num_calls_++; |
Ahmed Nasr | a055e52 | 2021-12-20 21:09:41 | [diff] [blame] | 29 | std::move(cb).Run(metric_data_); |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 30 | } |
| 31 | |
Ahmed Nasr | c4e332b | 2022-04-29 00:51:54 | [diff] [blame^] | 32 | void FakeSampler::SetMetricData(absl::optional<MetricData> metric_data) { |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 33 | metric_data_ = std::move(metric_data); |
| 34 | } |
| 35 | |
| 36 | int FakeSampler::GetNumCollectCalls() const { |
| 37 | return num_calls_; |
| 38 | } |
Ahmed Nasr | 27cd9f4e | 2021-11-12 22:49:40 | [diff] [blame] | 39 | |
| 40 | FakeMetricEventObserver::FakeMetricEventObserver() = default; |
| 41 | |
| 42 | FakeMetricEventObserver::~FakeMetricEventObserver() = default; |
| 43 | |
| 44 | void FakeMetricEventObserver::SetOnEventObservedCallback( |
| 45 | MetricRepeatingCallback cb) { |
| 46 | EXPECT_FALSE(cb_); |
| 47 | cb_ = std::move(cb); |
| 48 | } |
| 49 | |
| 50 | void FakeMetricEventObserver::SetReportingEnabled(bool is_enabled) { |
| 51 | is_reporting_enabled_ = is_enabled; |
| 52 | } |
| 53 | |
| 54 | void FakeMetricEventObserver::RunCallback(MetricData metric_data) { |
| 55 | base::RunLoop run_loop; |
| 56 | cb_.Run(std::move(metric_data)); |
| 57 | base::SequencedTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 58 | run_loop.QuitClosure()); |
| 59 | run_loop.Run(); |
| 60 | } |
| 61 | |
| 62 | bool FakeMetricEventObserver::GetReportingEnabled() const { |
| 63 | return is_reporting_enabled_; |
| 64 | } |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 65 | } // namespace test |
| 66 | } // namespace reporting |