xref: /aosp_15_r20/external/cronet/components/metrics/structured/test/test_structured_metrics_recorder.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_RECORDER_H_
6 #define COMPONENTS_METRICS_STRUCTURED_TEST_TEST_STRUCTURED_METRICS_RECORDER_H_
7 
8 #include <vector>
9 
10 #include "components/metrics/structured/event.h"
11 #include "components/metrics/structured/structured_metrics_client.h"
12 
13 namespace metrics::structured {
14 
15 class TestStructuredMetricsRecorder
16     : StructuredMetricsClient::RecordingDelegate {
17  public:
18   TestStructuredMetricsRecorder();
19   ~TestStructuredMetricsRecorder() override;
20 
21   TestStructuredMetricsRecorder(const TestStructuredMetricsRecorder& recorder) =
22       delete;
23   TestStructuredMetricsRecorder& operator=(
24       const TestStructuredMetricsRecorder& recorder) = delete;
25 
26   // RecordingDelegate:
27   void RecordEvent(Event&& event) override;
28   bool IsReadyToRecord() const override;
29 
30   // Initializes the test recorder. Must be called to work properly.
31   void Initialize();
32 
33   // Unsets the global pointer.
34   void Destroy();
35 
36   // Returns the number of events captured.
37   const std::vector<Event>& GetEvents();
38 
39   // Returns the test recorder if Initialize() has been called. Returns
40   // nullptr otherwise.
41   static TestStructuredMetricsRecorder* Get();
42 
43  private:
44   std::vector<Event> events_;
45 };
46 
47 }  // namespace metrics::structured
48 
49 #endif  // COMPONENTS_METRICS_STRUCTURED_TEST_TEST_STRUCTURED_METRICS_RECORDER_H_
50