• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_METRICS_STRUCTURED_TEST_TEST_STRUCTURED_METRICS_PROVIDER_H_
6 #define COMPONENTS_METRICS_STRUCTURED_TEST_TEST_STRUCTURED_METRICS_PROVIDER_H_
7 
8 #include "base/files/scoped_temp_dir.h"
9 #include "components/metrics/metrics_provider.h"
10 #include "components/metrics/structured/event.h"
11 #include "components/metrics/structured/recorder.h"
12 #include "components/metrics/structured/structured_metrics_provider.h"
13 #include "components/metrics/structured/structured_metrics_recorder.h"
14 
15 namespace metrics::structured {
16 
17 class EventsProto;
18 
19 // TestStructuredMetricsProvider is a wrapper of StructuredMetricsProvider to
20 // be used for testing.
21 class TestStructuredMetricsProvider : public Recorder::RecorderImpl {
22  public:
23   TestStructuredMetricsProvider();
24   explicit TestStructuredMetricsProvider(
25       std::unique_ptr<StructuredMetricsRecorder> recorder);
26   ~TestStructuredMetricsProvider() override;
27   TestStructuredMetricsProvider(const TestStructuredMetricsProvider&) = delete;
28   TestStructuredMetricsProvider& operator=(
29       const TestStructuredMetricsProvider&) = delete;
30 
31   const EventsProto& ReadEvents() const;
32 
33   // Returns pointer to the first event with the hash |project_name_hash| and
34   // |event_name_hash|. If no event is found, returns absl::nullopt.
35   absl::optional<const StructuredEventProto*> FindEvent(
36       uint64_t project_name_hash,
37       uint64_t event_name_hash);
38 
39   // Returns a vector of pointers to the events with the hash
40   // |project_name_hash| and |event_name_hash|.
41   std::vector<const StructuredEventProto*> FindEvents(
42       uint64_t project_name_hash,
43       uint64_t event_name_hash);
44 
45   void EnableRecording();
46   void DisableRecording();
47 
48   void AddProfilePath(const base::FilePath& user_path);
49 
50   // Waits until the recorder is fully initialized.
51   void WaitUntilReady();
52 
53   // Sets a callback that will be called after the event is flushed to
54   // persistence.
55   void SetOnEventsRecordClosure(
56       base::RepeatingCallback<void(const Event& event)> event_record_callback);
57 
58  private:
59   // Recorder::RecorderImpl:
60   void OnProfileAdded(const base::FilePath& profile_path) override;
61   void OnEventRecord(const Event& event) override;
62   void OnReportingStateChanged(bool enabled) override;
63 
64   std::unique_ptr<StructuredMetricsRecorder> structured_metrics_recorder_;
65 
66   std::unique_ptr<StructuredMetricsProvider> structured_metrics_provider_;
67 
68   base::ScopedTempDir temp_dir_;
69 
70   base::RepeatingCallback<void(const Event& event)> event_record_callback_;
71 };
72 
73 }  // namespace metrics::structured
74 
75 #endif  // COMPONENTS_METRICS_STRUCTURED_TEST_TEST_STRUCTURED_METRICS_PROVIDER_H_
76