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_builders.h"
16 #include "services/metrics/public/cpp/ukm_source.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 // Merge the data from |in| to |out|.
24 void MergeEntry(const mojom::UkmEntry* in, mojom::UkmEntry* out) {
25 if (out->event_hash) {
26 EXPECT_EQ(out->source_id, in->source_id);
27 EXPECT_EQ(out->event_hash, in->event_hash);
29 out->event_hash = in->event_hash;
30 out->source_id = in->source_id;
32 for (const auto& metric : in->metrics) {
33 out->metrics.emplace(metric);
39 TestUkmRecorder::TestUkmRecorder() {
40 EnableRecording(/*extensions=*/true);
41 StoreWhitelistedEntries();
42 DisableSamplingForTesting();
45 TestUkmRecorder::~TestUkmRecorder() {
48 bool TestUkmRecorder::ShouldRestrictToWhitelistedSourceIds() const {
49 // In tests, we want to record all source ids (not just those that are
54 bool TestUkmRecorder::ShouldRestrictToWhitelistedEntries() const {
55 // In tests, we want to record all entries (not just those that are
60 void TestUkmRecorder::AddEntry(mojom::UkmEntryPtr entry) {
61 const bool should_run_callback =
62 on_add_entry_ && entry && entry_hash_to_wait_for_ == entry->event_hash;
63 UkmRecorderImpl::AddEntry(std::move(entry));
64 if (should_run_callback)
65 std::move(on_add_entry_).Run();
68 const UkmSource* TestUkmRecorder::GetSourceForSourceId(
69 SourceId source_id) const {
70 const UkmSource* source = nullptr;
71 for (const auto& kv : sources()) {
72 if (kv.second->id() == source_id) {
73 DCHECK_EQ(nullptr, source);
74 source = kv.second.get();
80 const ukm::mojom::UkmEntry* TestUkmRecorder::GetDocumentCreatedEntryForSourceId(
81 ukm::SourceId source_id) const {
82 auto entries = GetEntriesByName(ukm::builders::DocumentCreated::kEntryName);
83 for (auto* entry : entries) {
84 if (entry->source_id == source_id)
90 void TestUkmRecorder::SetOnAddEntryCallback(base::StringPiece entry_name,
91 base::OnceClosure on_add_entry) {
92 on_add_entry_ = std::move(on_add_entry);
93 entry_hash_to_wait_for_ = base::HashMetricName(entry_name);
96 std::vector<const mojom::UkmEntry*> TestUkmRecorder::GetEntriesByName(
97 base::StringPiece entry_name) const {
98 uint64_t hash = base::HashMetricName(entry_name);
99 std::vector<const mojom::UkmEntry*> result;
100 for (const auto& it : entries()) {
101 if (it->event_hash == hash)
102 result.push_back(it.get());
107 std::map<ukm::SourceId, mojom::UkmEntryPtr>
108 TestUkmRecorder::GetMergedEntriesByName(base::StringPiece entry_name) const {
109 uint64_t hash = base::HashMetricName(entry_name);
110 std::map<ukm::SourceId, mojom::UkmEntryPtr> result;
111 for (const auto& it : entries()) {
112 if (it->event_hash != hash)
114 mojom::UkmEntryPtr& entry_ptr = result[it->source_id];
116 entry_ptr = mojom::UkmEntry::New();
117 MergeEntry(it.get(), entry_ptr.get());
122 void TestUkmRecorder::ExpectEntrySourceHasUrl(const mojom::UkmEntry* entry,
123 const GURL& url) const {
124 const UkmSource* src = GetSourceForSourceId(entry->source_id);
125 if (src == nullptr) {
126 FAIL() << "Entry source id has no associated Source.";
129 EXPECT_EQ(src->url(), url);
133 bool TestUkmRecorder::EntryHasMetric(const mojom::UkmEntry* entry,
134 base::StringPiece metric_name) {
135 return GetEntryMetric(entry, metric_name) != nullptr;
139 const int64_t* TestUkmRecorder::GetEntryMetric(const mojom::UkmEntry* entry,
140 base::StringPiece metric_name) {
141 uint64_t hash = base::HashMetricName(metric_name);
142 const auto it = entry->metrics.find(hash);
143 if (it != entry->metrics.end())
149 void TestUkmRecorder::ExpectEntryMetric(const mojom::UkmEntry* entry,
150 base::StringPiece metric_name,
151 int64_t expected_value) {
152 const int64_t* metric = GetEntryMetric(entry, metric_name);
153 if (metric == nullptr) {
154 FAIL() << "Failed to find metric for event: " << metric_name;
157 EXPECT_EQ(expected_value, *metric) << " for metric:" << metric_name;
160 TestAutoSetUkmRecorder::TestAutoSetUkmRecorder() : self_ptr_factory_(this) {
161 DelegatingUkmRecorder::Get()->AddDelegate(self_ptr_factory_.GetWeakPtr());
164 TestAutoSetUkmRecorder::~TestAutoSetUkmRecorder() {
165 DelegatingUkmRecorder::Get()->RemoveDelegate(this);