Upload upstream chromium 71.0.3578.0
[platform/framework/web/chromium-efl.git] / components / ukm / test_ukm_recorder.cc
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.
4
5 #include "components/ukm/test_ukm_recorder.h"
6
7 #include <algorithm>
8 #include <iterator>
9
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"
17
18 namespace ukm {
19
20 namespace {
21
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);
27   } else {
28     out->event_hash = in->event_hash;
29     out->source_id = in->source_id;
30   }
31   for (const auto& metric : in->metrics) {
32     out->metrics.emplace(metric);
33   }
34 }
35
36 }  // namespace
37
38 TestUkmRecorder::TestUkmRecorder() {
39   EnableRecording(/*extensions=*/true);
40   StoreWhitelistedEntries();
41   DisableSamplingForTesting();
42 }
43
44 TestUkmRecorder::~TestUkmRecorder() {
45 };
46
47 bool TestUkmRecorder::ShouldRestrictToWhitelistedSourceIds() const {
48   // In tests, we want to record all source ids (not just those that are
49   // whitelisted).
50   return false;
51 }
52
53 bool TestUkmRecorder::ShouldRestrictToWhitelistedEntries() const {
54   // In tests, we want to record all entries (not just those that are
55   // whitelisted).
56   return false;
57 }
58
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();
65 }
66
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();
74     }
75   }
76   return source;
77 }
78
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);
83 }
84
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());
92   }
93   return result;
94 }
95
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)
102       continue;
103     mojom::UkmEntryPtr& entry_ptr = result[it->source_id];
104     if (!entry_ptr)
105       entry_ptr = mojom::UkmEntry::New();
106     MergeEntry(it.get(), entry_ptr.get());
107   }
108   return result;
109 }
110
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.";
116     return;
117   }
118   EXPECT_EQ(src->url(), url);
119 }
120
121 // static
122 bool TestUkmRecorder::EntryHasMetric(const mojom::UkmEntry* entry,
123                                      base::StringPiece metric_name) {
124   return GetEntryMetric(entry, metric_name) != nullptr;
125 }
126
127 // static
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())
133     return &it->second;
134   return nullptr;
135 }
136
137 // static
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;
144     return;
145   }
146   EXPECT_EQ(expected_value, *metric) << " for metric:" << metric_name;
147 }
148
149 TestAutoSetUkmRecorder::TestAutoSetUkmRecorder() : self_ptr_factory_(this) {
150   DelegatingUkmRecorder::Get()->AddDelegate(self_ptr_factory_.GetWeakPtr());
151 }
152
153 TestAutoSetUkmRecorder::~TestAutoSetUkmRecorder() {
154   DelegatingUkmRecorder::Get()->RemoveDelegate(this);
155 };
156
157 }  // namespace ukm