1 // Copyright 2017 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.
5 #include "components/ukm/test_ukm_recorder.h"
10 #include "base/logging.h"
11 #include "base/metrics/metrics_hashes.h"
12 #include "base/task/post_task.h"
13 #include "base/threading/sequenced_task_runner_handle.h"
14 #include "services/metrics/public/cpp/delegating_ukm_recorder.h"
15 #include "services/metrics/public/cpp/ukm_source.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 // Merge the data from |in| to |out|.
23 void MergeEntry(const mojom::UkmEntry* in, mojom::UkmEntry* out) {
24 if (out->event_hash) {
25 EXPECT_EQ(out->source_id, in->source_id);
26 EXPECT_EQ(out->event_hash, in->event_hash);
28 out->event_hash = in->event_hash;
29 out->source_id = in->source_id;
31 for (const auto& metric : in->metrics) {
32 out->metrics.emplace(metric);
38 TestUkmRecorder::TestUkmRecorder() {
39 EnableRecording(/*extensions=*/true);
40 StoreWhitelistedEntries();
41 DisableSamplingForTesting();
44 TestUkmRecorder::~TestUkmRecorder() {
47 bool TestUkmRecorder::ShouldRestrictToWhitelistedSourceIds() const {
48 // In tests, we want to record all source ids (not just those that are
53 bool TestUkmRecorder::ShouldRestrictToWhitelistedEntries() const {
54 // In tests, we want to record all entries (not just those that are
59 void TestUkmRecorder::AddEntry(mojom::UkmEntryPtr entry) {
60 const bool should_run_callback =
61 on_add_entry_ && entry && entry_hash_to_wait_for_ == entry->event_hash;
62 UkmRecorderImpl::AddEntry(std::move(entry));
63 if (should_run_callback)
64 std::move(on_add_entry_).Run();
67 const UkmSource* TestUkmRecorder::GetSourceForSourceId(
68 SourceId source_id) const {
69 const UkmSource* source = nullptr;
70 for (const auto& kv : sources()) {
71 if (kv.second->id() == source_id) {
72 DCHECK_EQ(nullptr, source);
73 source = kv.second.get();
79 void TestUkmRecorder::SetOnAddEntryCallback(base::StringPiece entry_name,
80 base::OnceClosure on_add_entry) {
81 on_add_entry_ = std::move(on_add_entry);
82 entry_hash_to_wait_for_ = base::HashMetricName(entry_name);
85 std::vector<const mojom::UkmEntry*> TestUkmRecorder::GetEntriesByName(
86 base::StringPiece entry_name) const {
87 uint64_t hash = base::HashMetricName(entry_name);
88 std::vector<const mojom::UkmEntry*> result;
89 for (const auto& it : entries()) {
90 if (it->event_hash == hash)
91 result.push_back(it.get());
96 std::map<ukm::SourceId, mojom::UkmEntryPtr>
97 TestUkmRecorder::GetMergedEntriesByName(base::StringPiece entry_name) const {
98 uint64_t hash = base::HashMetricName(entry_name);
99 std::map<ukm::SourceId, mojom::UkmEntryPtr> result;
100 for (const auto& it : entries()) {
101 if (it->event_hash != hash)
103 mojom::UkmEntryPtr& entry_ptr = result[it->source_id];
105 entry_ptr = mojom::UkmEntry::New();
106 MergeEntry(it.get(), entry_ptr.get());
111 void TestUkmRecorder::ExpectEntrySourceHasUrl(const mojom::UkmEntry* entry,
112 const GURL& url) const {
113 const UkmSource* src = GetSourceForSourceId(entry->source_id);
114 if (src == nullptr) {
115 FAIL() << "Entry source id has no associated Source.";
118 EXPECT_EQ(src->url(), url);
122 bool TestUkmRecorder::EntryHasMetric(const mojom::UkmEntry* entry,
123 base::StringPiece metric_name) {
124 return GetEntryMetric(entry, metric_name) != nullptr;
128 const int64_t* TestUkmRecorder::GetEntryMetric(const mojom::UkmEntry* entry,
129 base::StringPiece metric_name) {
130 uint64_t hash = base::HashMetricName(metric_name);
131 const auto it = entry->metrics.find(hash);
132 if (it != entry->metrics.end())
138 void TestUkmRecorder::ExpectEntryMetric(const mojom::UkmEntry* entry,
139 base::StringPiece metric_name,
140 int64_t expected_value) {
141 const int64_t* metric = GetEntryMetric(entry, metric_name);
142 if (metric == nullptr) {
143 FAIL() << "Failed to find metric for event: " << metric_name;
146 EXPECT_EQ(expected_value, *metric) << " for metric:" << metric_name;
149 TestAutoSetUkmRecorder::TestAutoSetUkmRecorder() : self_ptr_factory_(this) {
150 DelegatingUkmRecorder::Get()->AddDelegate(self_ptr_factory_.GetWeakPtr());
153 TestAutoSetUkmRecorder::~TestAutoSetUkmRecorder() {
154 DelegatingUkmRecorder::Get()->RemoveDelegate(this);