blob: d4f2ef9fd2ebd562c7882f558cfba6f919a63a76 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2021 The Chromium Authors
Ahmed Nasrab0e5822021-11-10 22:29:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ahmed Nasr9e4777052022-11-02 20:38:395#include "components/reporting/metrics/fakes/fake_sampler.h"
Ahmed Nasrab0e5822021-11-10 22:29:056
Arthur Sonzognic571efb2024-01-26 20:26:187#include <optional>
Ahmed Nasr27cd9f4e2021-11-12 22:49:408#include <utility>
9
Ahmed Nasrd9af70c2022-09-02 17:45:1010#include "base/check.h"
Ahmed Nasrb94c2d32022-11-07 22:18:1611#include "components/reporting/metrics/sampler.h"
12#include "components/reporting/proto/synced/metric_data.pb.h"
Ahmed Nasr27cd9f4e2021-11-12 22:49:4013#include "testing/gtest/include/gtest/gtest.h"
14
Ahmed Nasrb94c2d32022-11-07 22:18:1615namespace reporting::test {
Ahmed Nasrab0e5822021-11-10 22:29:0516
Ahmed Nasrc4e332b2022-04-29 00:51:5417FakeSampler::FakeSampler() = default;
18
19FakeSampler::~FakeSampler() = default;
20
Ahmed Nasrc4e332b2022-04-29 00:51:5421void FakeSampler::MaybeCollect(OptionalMetricCallback cb) {
Ahmed Nasrd9af70c2022-09-02 17:45:1022 ++num_calls_;
Ahmed Nasra055e522021-12-20 21:09:4123 std::move(cb).Run(metric_data_);
Ahmed Nasrab0e5822021-11-10 22:29:0524}
25
Arthur Sonzognic571efb2024-01-26 20:26:1826void FakeSampler::SetMetricData(std::optional<MetricData> metric_data) {
Ahmed Nasrab0e5822021-11-10 22:29:0527 metric_data_ = std::move(metric_data);
28}
29
30int FakeSampler::GetNumCollectCalls() const {
31 return num_calls_;
32}
Ahmed Nasr27cd9f4e2021-11-12 22:49:4033
Ahmed Nasrd9af70c2022-09-02 17:45:1034FakeDelayedSampler::FakeDelayedSampler() = default;
35
36FakeDelayedSampler::~FakeDelayedSampler() = default;
37
38void FakeDelayedSampler::MaybeCollect(OptionalMetricCallback cb) {
39 ++num_calls_;
40 cb_ = std::move(cb);
41}
42
43void FakeDelayedSampler::RunCallback() {
Leonid Baraz48447f142023-07-31 19:46:2144 CHECK(cb_);
Ahmed Nasrd9af70c2022-09-02 17:45:1045 std::move(cb_).Run(metric_data_);
46}
47
Ahmed Nasrb94c2d32022-11-07 22:18:1648} // namespace reporting::test