fake_sampler.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #ifndef COMPONENTS_REPORTING_METRICS_FAKE_SAMPLER_H_
  5. #define COMPONENTS_REPORTING_METRICS_FAKE_SAMPLER_H_
  6. #include "components/reporting/metrics/sampler.h"
  7. #include "components/reporting/proto/synced/metric_data.pb.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. namespace reporting {
  10. namespace test {
  11. class FakeSampler : public Sampler {
  12. public:
  13. FakeSampler();
  14. FakeSampler(const FakeSampler& other) = delete;
  15. FakeSampler& operator=(const FakeSampler& other) = delete;
  16. ~FakeSampler() override;
  17. void MaybeCollect(OptionalMetricCallback cb) override;
  18. void SetMetricData(absl::optional<MetricData> metric_data);
  19. int GetNumCollectCalls() const;
  20. private:
  21. absl::optional<MetricData> metric_data_;
  22. int num_calls_ = 0;
  23. };
  24. class FakeMetricEventObserver : public MetricEventObserver {
  25. public:
  26. FakeMetricEventObserver();
  27. FakeMetricEventObserver(const FakeMetricEventObserver& other) = delete;
  28. FakeMetricEventObserver& operator=(const FakeMetricEventObserver& other) =
  29. delete;
  30. ~FakeMetricEventObserver() override;
  31. void SetOnEventObservedCallback(MetricRepeatingCallback cb) override;
  32. void SetReportingEnabled(bool is_enabled) override;
  33. void RunCallback(MetricData metric_data);
  34. bool GetReportingEnabled() const;
  35. private:
  36. bool is_reporting_enabled_ = false;
  37. MetricRepeatingCallback cb_;
  38. };
  39. } // namespace test
  40. } // namespace reporting
  41. #endif // COMPONENTS_REPORTING_METRICS_FAKE_SAMPLER_H_