[M73 Dev][EFL] Disable VizDisplayCompositor for EFL port
[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_builders.h"
16 #include "services/metrics/public/cpp/ukm_source.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace ukm {
20
21 namespace {
22
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);
28   } else {
29     out->event_hash = in->event_hash;
30     out->source_id = in->source_id;
31   }
32   for (const auto& metric : in->metrics) {
33     out->metrics.emplace(metric);
34   }
35 }
36
37 }  // namespace
38
39 TestUkmRecorder::TestUkmRecorder() {
40   EnableRecording(/*extensions=*/true);
41   StoreWhitelistedEntries();
42   DisableSamplingForTesting();
43 }
44
45 TestUkmRecorder::~TestUkmRecorder() {
46 };
47
48 bool TestUkmRecorder::ShouldRestrictToWhitelistedSourceIds() const {
49   // In tests, we want to record all source ids (not just those that are
50   // whitelisted).
51   return false;
52 }
53
54 bool TestUkmRecorder::ShouldRestrictToWhitelistedEntries() const {
55   // In tests, we want to record all entries (not just those that are
56   // whitelisted).
57   return false;
58 }
59
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();
66 }
67
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();
75     }
76   }
77   return source;
78 }
79
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)
85       return entry;
86   }
87   return nullptr;
88 }
89
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);
94 }
95
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());
103   }
104   return result;
105 }
106
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)
113       continue;
114     mojom::UkmEntryPtr& entry_ptr = result[it->source_id];
115     if (!entry_ptr)
116       entry_ptr = mojom::UkmEntry::New();
117     MergeEntry(it.get(), entry_ptr.get());
118   }
119   return result;
120 }
121
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.";
127     return;
128   }
129   EXPECT_EQ(src->url(), url);
130 }
131
132 // static
133 bool TestUkmRecorder::EntryHasMetric(const mojom::UkmEntry* entry,
134                                      base::StringPiece metric_name) {
135   return GetEntryMetric(entry, metric_name) != nullptr;
136 }
137
138 // static
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())
144     return &it->second;
145   return nullptr;
146 }
147
148 // static
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;
155     return;
156   }
157   EXPECT_EQ(expected_value, *metric) << " for metric:" << metric_name;
158 }
159
160 TestAutoSetUkmRecorder::TestAutoSetUkmRecorder() : self_ptr_factory_(this) {
161   DelegatingUkmRecorder::Get()->AddDelegate(self_ptr_factory_.GetWeakPtr());
162 }
163
164 TestAutoSetUkmRecorder::~TestAutoSetUkmRecorder() {
165   DelegatingUkmRecorder::Get()->RemoveDelegate(this);
166 };
167
168 }  // namespace ukm