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 #ifndef COMPONENTS_UKM_TEST_UKM_RECORDER_H_
6 #define COMPONENTS_UKM_TEST_UKM_RECORDER_H_
15 #include "base/compiler_specific.h"
16 #include "base/macros.h"
17 #include "base/memory/weak_ptr.h"
18 #include "components/ukm/ukm_recorder_impl.h"
19 #include "services/metrics/public/cpp/ukm_recorder.h"
20 #include "services/metrics/public/mojom/ukm_interface.mojom.h"
25 // Wraps an UkmRecorder with additional accessors used for testing.
26 class TestUkmRecorder : public UkmRecorderImpl {
29 ~TestUkmRecorder() override;
31 bool ShouldRestrictToWhitelistedSourceIds() const override;
32 bool ShouldRestrictToWhitelistedEntries() const override;
34 size_t sources_count() const { return sources().size(); }
36 size_t entries_count() const { return entries().size(); }
38 using UkmRecorderImpl::UpdateSourceURL;
40 // Gets all recorded UkmSource data.
41 const std::map<ukm::SourceId, std::unique_ptr<UkmSource>>& GetSources()
46 // Gets UkmSource data for a single SourceId.
47 const UkmSource* GetSourceForSourceId(ukm::SourceId source_id) const;
49 // Gets all of the entries recorded for entry name.
50 std::vector<const mojom::UkmEntry*> GetEntriesByName(
51 base::StringPiece entry_name) const;
53 // Gets the data for all entries with given entry name, merged to one entry
54 // for each source id. Intended for singular="true" metrics.
55 std::map<ukm::SourceId, mojom::UkmEntryPtr> GetMergedEntriesByName(
56 base::StringPiece entry_name) const;
58 // Checks if an entry is associated with a url.
59 void ExpectEntrySourceHasUrl(const mojom::UkmEntry* entry,
60 const GURL& url) const;
62 // Expects the value of a metric from an entry.
63 static void ExpectEntryMetric(const mojom::UkmEntry* entry,
64 base::StringPiece metric_name,
65 int64_t expected_value);
67 // Checks if an entry contains a specific metric.
68 static bool EntryHasMetric(const mojom::UkmEntry* entry,
69 base::StringPiece metric_name);
71 // Gets the value of a metric from an entry. Returns nullptr if the metric is
73 static const int64_t* GetEntryMetric(const mojom::UkmEntry* entry,
74 base::StringPiece metric_name);
77 DISALLOW_COPY_AND_ASSIGN(TestUkmRecorder);
80 // Similar to a TestUkmRecorder, but also sets itself as the global UkmRecorder
81 // on construction, and unsets itself on destruction.
82 class TestAutoSetUkmRecorder : public TestUkmRecorder {
84 TestAutoSetUkmRecorder();
85 ~TestAutoSetUkmRecorder() override;
88 base::WeakPtrFactory<TestAutoSetUkmRecorder> self_ptr_factory_;
93 #endif // COMPONENTS_UKM_TEST_UKM_RECORDER_H_