OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/ukm/test_ukm_service.h" | 5 #include "components/ukm/test_ukm_service.h" |
6 | 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/metrics/metrics_hashes.h" |
| 9 #include "components/ukm/ukm_entry.h" |
7 #include "components/ukm/ukm_source.h" | 10 #include "components/ukm/ukm_source.h" |
8 | 11 |
9 namespace ukm { | 12 namespace ukm { |
10 | 13 |
11 UkmServiceTestingHarness::UkmServiceTestingHarness() { | 14 UkmServiceTestingHarness::UkmServiceTestingHarness() { |
12 UkmService::RegisterPrefs(test_prefs_.registry()); | 15 UkmService::RegisterPrefs(test_prefs_.registry()); |
13 test_prefs_.ClearPref(prefs::kUkmClientId); | 16 test_prefs_.ClearPref(prefs::kUkmClientId); |
14 test_prefs_.ClearPref(prefs::kUkmSessionId); | 17 test_prefs_.ClearPref(prefs::kUkmSessionId); |
15 test_prefs_.ClearPref(prefs::kUkmPersistedLogs); | 18 test_prefs_.ClearPref(prefs::kUkmPersistedLogs); |
16 | 19 |
17 test_ukm_service_ = base::MakeUnique<TestUkmService>(&test_prefs_); | 20 test_ukm_service_ = base::MakeUnique<TestUkmService>(&test_prefs_); |
18 } | 21 } |
19 | 22 |
20 UkmServiceTestingHarness::~UkmServiceTestingHarness() = default; | 23 UkmServiceTestingHarness::~UkmServiceTestingHarness() = default; |
21 | 24 |
22 TestUkmService::TestUkmService(PrefService* prefs_service) | 25 TestUkmService::TestUkmService(PrefService* prefs_service) |
23 : UkmService(prefs_service, &test_metrics_service_client_) { | 26 : UkmService(prefs_service, &test_metrics_service_client_) { |
24 EnableRecording(); | 27 EnableRecording(); |
25 DisableReporting(); | 28 DisableReporting(); |
26 } | 29 } |
27 | 30 |
28 TestUkmService::~TestUkmService() = default; | 31 TestUkmService::~TestUkmService() = default; |
29 | 32 |
30 const std::map<int32_t, std::unique_ptr<UkmSource>>& | 33 const std::map<int32_t, std::unique_ptr<UkmSource>>& |
31 TestUkmService::GetSources() const { | 34 TestUkmService::GetSources() const { |
32 return sources_for_testing(); | 35 return sources_for_testing(); |
33 } | 36 } |
34 | 37 |
35 const UkmSource* TestUkmService::GetSourceForUrl(const char* url) const { | 38 const UkmSource* TestUkmService::GetSourceForUrl(const char* url) const { |
| 39 const UkmSource* source = nullptr; |
36 for (const auto& kv : sources_for_testing()) { | 40 for (const auto& kv : sources_for_testing()) { |
37 if (kv.second->url() == url) | 41 if (kv.second->url() == url) { |
38 return kv.second.get(); | 42 DCHECK_EQ(nullptr, source); |
| 43 source = kv.second.get(); |
| 44 } |
39 } | 45 } |
40 return nullptr; | 46 return source; |
| 47 } |
| 48 |
| 49 const UkmSource* TestUkmService::GetSourceForSourceId(int32_t source_id) const { |
| 50 const UkmSource* source = nullptr; |
| 51 for (const auto& kv : sources_for_testing()) { |
| 52 if (kv.second->id() == source_id) { |
| 53 DCHECK_EQ(nullptr, source); |
| 54 source = kv.second.get(); |
| 55 } |
| 56 } |
| 57 return source; |
41 } | 58 } |
42 | 59 |
43 const UkmEntry* TestUkmService::GetEntry(size_t entry_num) const { | 60 const UkmEntry* TestUkmService::GetEntry(size_t entry_num) const { |
44 return entries_for_testing()[entry_num].get(); | 61 return entries_for_testing()[entry_num].get(); |
45 } | 62 } |
46 | 63 |
| 64 const UkmEntry* TestUkmService::GetEntryForEntryName( |
| 65 const char* entry_name) const { |
| 66 for (const auto& it : entries_for_testing()) { |
| 67 if (it->event_hash() == base::HashMetricName(entry_name)) |
| 68 return it.get(); |
| 69 } |
| 70 return nullptr; |
| 71 } |
| 72 |
47 } // namespace ukm | 73 } // namespace ukm |
OLD | NEW |