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 | |
| 17 | void FakeSampler::Collect(MetricCallback cb) { |
| 18 | num_calls_++; |
| 19 | std::move(cb).Run(std::move(metric_data_)); |
| 20 | } |
| 21 | |
| 22 | void FakeSampler::SetMetricData(MetricData metric_data) { |
| 23 | metric_data_ = std::move(metric_data); |
| 24 | } |
| 25 | |
| 26 | int FakeSampler::GetNumCollectCalls() const { |
| 27 | return num_calls_; |
| 28 | } |
Ahmed Nasr | 27cd9f4e | 2021-11-12 22:49:40 | [diff] [blame^] | 29 | |
| 30 | FakeMetricEventObserver::FakeMetricEventObserver() = default; |
| 31 | |
| 32 | FakeMetricEventObserver::~FakeMetricEventObserver() = default; |
| 33 | |
| 34 | void FakeMetricEventObserver::SetOnEventObservedCallback( |
| 35 | MetricRepeatingCallback cb) { |
| 36 | EXPECT_FALSE(cb_); |
| 37 | cb_ = std::move(cb); |
| 38 | } |
| 39 | |
| 40 | void FakeMetricEventObserver::SetReportingEnabled(bool is_enabled) { |
| 41 | is_reporting_enabled_ = is_enabled; |
| 42 | } |
| 43 | |
| 44 | void FakeMetricEventObserver::RunCallback(MetricData metric_data) { |
| 45 | base::RunLoop run_loop; |
| 46 | cb_.Run(std::move(metric_data)); |
| 47 | base::SequencedTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 48 | run_loop.QuitClosure()); |
| 49 | run_loop.Run(); |
| 50 | } |
| 51 | |
| 52 | bool FakeMetricEventObserver::GetReportingEnabled() const { |
| 53 | return is_reporting_enabled_; |
| 54 | } |
Ahmed Nasr | ab0e582 | 2021-11-10 22:29:05 | [diff] [blame] | 55 | } // namespace test |
| 56 | } // namespace reporting |