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 #include "components/metrics/structured/test/test_structured_metrics_provider.h" 6 7 #include "base/files/file_path.h" 8 #include "base/memory/ptr_util.h" 9 #include "base/run_loop.h" 10 #include "base/test/bind.h" 11 #include "components/metrics/structured/test/test_event_storage.h" 12 #include "components/metrics/structured/test/test_key_data_provider.h" 13 14 namespace metrics::structured { 15 TestStructuredMetricsProvider()16TestStructuredMetricsProvider::TestStructuredMetricsProvider() { 17 if (temp_dir_.CreateUniqueTempDir()) { 18 structured_metrics_recorder_ = std::make_unique<StructuredMetricsRecorder>( 19 std::make_unique<TestKeyDataProvider>( 20 temp_dir_.GetPath() 21 .Append(FILE_PATH_LITERAL("structured_metrics")) 22 .Append(FILE_PATH_LITERAL("device_keys"))), 23 std::make_unique<TestEventStorage>()); 24 Recorder::GetInstance()->AddObserver(this); 25 } 26 } 27 TestStructuredMetricsProvider(std::unique_ptr<StructuredMetricsRecorder> recorder)28TestStructuredMetricsProvider::TestStructuredMetricsProvider( 29 std::unique_ptr<StructuredMetricsRecorder> recorder) 30 : structured_metrics_recorder_(std::move(recorder)) { 31 Recorder::GetInstance()->AddObserver(this); 32 } 33 ~TestStructuredMetricsProvider()34TestStructuredMetricsProvider::~TestStructuredMetricsProvider() { 35 Recorder::GetInstance()->RemoveObserver(this); 36 } 37 ReadEvents() const38const EventsProto& TestStructuredMetricsProvider::ReadEvents() const { 39 return *static_cast<const TestEventStorage*>( 40 structured_metrics_recorder_->event_storage()) 41 ->events(); 42 } 43 44 std::optional<const StructuredEventProto*> FindEvent(uint64_t project_name_hash,uint64_t event_name_hash)45TestStructuredMetricsProvider::FindEvent(uint64_t project_name_hash, 46 uint64_t event_name_hash) { 47 if (!structured_metrics_recorder_->CanProvideMetrics()) { 48 return std::nullopt; 49 } 50 51 const EventsProto& events = TestStructuredMetricsProvider::ReadEvents(); 52 53 for (const auto& event : events.events()) { 54 if (event.project_name_hash() == project_name_hash && 55 event.event_name_hash() == event_name_hash) { 56 return &event; 57 } 58 } 59 return std::nullopt; 60 } 61 62 std::vector<const StructuredEventProto*> FindEvents(uint64_t project_name_hash,uint64_t event_name_hash)63TestStructuredMetricsProvider::FindEvents(uint64_t project_name_hash, 64 uint64_t event_name_hash) { 65 std::vector<const StructuredEventProto*> events_vector; 66 if (!structured_metrics_recorder_->CanProvideMetrics()) { 67 return events_vector; 68 } 69 70 const EventsProto& events = TestStructuredMetricsProvider::ReadEvents(); 71 for (const auto& event : events.events()) { 72 if (event.project_name_hash() == project_name_hash && 73 event.event_name_hash() == event_name_hash) { 74 events_vector.push_back(&event); 75 } 76 } 77 return events_vector; 78 } 79 EnableRecording()80void TestStructuredMetricsProvider::EnableRecording() { 81 structured_metrics_recorder_->EnableRecording(); 82 } 83 DisableRecording()84void TestStructuredMetricsProvider::DisableRecording() { 85 structured_metrics_recorder_->DisableRecording(); 86 } 87 WaitUntilReady()88void TestStructuredMetricsProvider::WaitUntilReady() { 89 base::RunLoop run_loop; 90 structured_metrics_recorder_->SetOnReadyToRecord( 91 base::BindLambdaForTesting([&run_loop]() { run_loop.Quit(); })); 92 run_loop.Run(); 93 } 94 SetOnEventsRecordClosure(base::RepeatingCallback<void (const Event & event)> event_record_callback)95void TestStructuredMetricsProvider::SetOnEventsRecordClosure( 96 base::RepeatingCallback<void(const Event& event)> event_record_callback) { 97 event_record_callback_ = std::move(event_record_callback); 98 } 99 OnEventRecord(const Event & event)100void TestStructuredMetricsProvider::OnEventRecord(const Event& event) { 101 structured_metrics_recorder_->OnEventRecord(event); 102 if (!event_record_callback_) { 103 return; 104 } 105 106 event_record_callback_.Run(event); 107 } 108 109 } // namespace metrics::structured 110